Example #1
0
        // TODO: see if this is really what we want.
        private Type ConstantType(int i)
        {
            object o = _constants.nth(i);
            Type   t = o.GetType();

            if (t.IsPublic)
            {
                // Java: can't emit derived fn types due to visibility
                if (typeof(LazySeq).IsAssignableFrom(t))
                {
                    return(typeof(ISeq));
                }
                else if (typeof(RestFn).IsAssignableFrom(t))
                {
                    return(typeof(RestFn));
                }
                else if (typeof(AFn).IsAssignableFrom(t))
                {
                    return(typeof(AFn));
                }
                else if (t == typeof(Var))
                {
                    return(t);
                }
                else if (t == typeof(String))
                {
                    return(t);
                }
            }
            return(typeof(object));
            // This ends up being too specific.
            // TODO: However, if we were to see the value returned by RT.readFromString(), we could make it work.
            //return t;
        }
Example #2
0
        private Type ConstantType(int i)
        {
            object o = _constants.nth(i);
            Type   t = o == null ? null : o.GetType();

            if (t != null && t.IsPublic)
            {
                // Java: can't emit derived fn types due to visibility
                if (typeof(LazySeq).IsAssignableFrom(t))
                {
                    return(typeof(ISeq));
                }
                else if (t == typeof(Keyword))
                {
                    return(t);
                }
                else if (typeof(RestFn).IsAssignableFrom(t))
                {
                    return(typeof(RestFn));
                }
                else if (typeof(AFn).IsAssignableFrom(t))
                {
                    return(typeof(AFn));
                }
                else if (t == typeof(Var))
                {
                    return(t);
                }
                else if (t == typeof(String))
                {
                    return(t);
                }
            }
            return(typeof(object));
        }
        public void CreateOnMultipleItemsWorks()
        {
            PersistentVector v = PersistentVector.create(2, 3, 4);

            Expect(v.count()).To.Equal(3);
            Expect(v.nth(0)).To.Equal(2);
            Expect(v.nth(1)).To.Equal(3);
            Expect(v.nth(2)).To.Equal(4);
        }
        public void CreateOnISeqHasItems()
        {
            ISeq             r = LongRange.create(2, 5);
            PersistentVector v = PersistentVector.create(r);

            Expect((long)v.nth(0)).To.Equal(2);
            Expect((long)v.nth(1)).To.Equal(3);
            Expect((long)v.nth(2)).To.Equal(4);
        }
        public void CreateOnISeqHasItems()
        {
            ISeq             r = LongRange.create(2, 5);
            PersistentVector v = PersistentVector.create(r);

            Expect(v.nth(0), EqualTo(2));
            Expect(v.nth(1), EqualTo(3));
            Expect(v.nth(2), EqualTo(4));
        }
        public void CreateOnMultipleItemsWorks()
        {
            PersistentVector v = PersistentVector.create(2, 3, 4);

            Expect(v.count(), EqualTo(3));
            Expect(v.nth(0), EqualTo(2));
            Expect(v.nth(1), EqualTo(3));
            Expect(v.nth(2), EqualTo(4));
        }
        public void CreateOnISeqHasItems()
        {
            Range            r = new Range(2, 5);
            PersistentVector v = PersistentVector.create((ISeq)r);

            Expect(v.nth(0), EqualTo(2));
            Expect(v.nth(1), EqualTo(3));
            Expect(v.nth(2), EqualTo(4));
        }
        public void AssocNReplacesInRangeForSmall()
        {
            ISeq              r  = LongRange.create(2, 5);
            PersistentVector  v1 = PersistentVector.create(r);
            IPersistentVector v2 = v1.assocN(1, 10L);

            Expect((long)v1.nth(0)).To.Equal(2);
            Expect((long)v1.nth(1)).To.Equal(3);
            Expect((long)v1.nth(2)).To.Equal(4);
            Expect((long)v1.count()).To.Equal(3);
            Expect((long)v2.nth(0)).To.Equal(2);
            Expect((long)v2.nth(1)).To.Equal(10);
            Expect((long)v2.nth(2)).To.Equal(4);
            Expect((long)v2.count()).To.Equal(3);
        }
        public void AssocNReplacesInRangeForSmall()
        {
            Range             r  = new Range(2, 5);
            PersistentVector  v1 = PersistentVector.create((ISeq)r);
            IPersistentVector v2 = v1.assocN(1, 10);

            Expect(v1.nth(0), EqualTo(2));
            Expect(v1.nth(1), EqualTo(3));
            Expect(v1.nth(2), EqualTo(4));
            Expect(v1.count(), EqualTo(3));
            Expect(v2.nth(0), EqualTo(2));
            Expect(v2.nth(1), EqualTo(10));
            Expect(v2.nth(2), EqualTo(4));
            Expect(v2.count(), EqualTo(3));
        }
        public void AssocNAddsAtEndForSmall()
        {
            ISeq              r  = LongRange.create(2, 5);
            PersistentVector  v1 = PersistentVector.create(r);
            IPersistentVector v2 = v1.assocN(3, 10);

            Expect(v1.nth(0), EqualTo(2));
            Expect(v1.nth(1), EqualTo(3));
            Expect(v1.nth(2), EqualTo(4));
            Expect(v1.count(), EqualTo(3));
            Expect(v2.nth(0), EqualTo(2));
            Expect(v2.nth(1), EqualTo(3));
            Expect(v2.nth(2), EqualTo(4));
            Expect(v2.nth(3), EqualTo(10));
            Expect(v2.count(), EqualTo(4));
        }
        public void CreateOnISeqWithManyItemsWorks()
        {
            // Want to bust out of the first tail, so need to insert more than 32 elements.
            Range            r = new Range(2, 1000);
            PersistentVector v = PersistentVector.create((ISeq)r);

            Expect(v.count(), EqualTo(r.count()));
            for (int i = 0; i < v.count(); ++i)
            {
                Expect(v.nth(i), EqualTo(i + 2));
            }
        }
        public void CreateOnISeqWithManyItemsWorks()
        {
            // Want to bust out of the first tail, so need to insert more than 32 elements.
            ISeq r = LongRange.create(2, 1000);

            PersistentVector v = PersistentVector.create(r);

            Expect(v.count()).To.Equal(r.count());
            for (int i = 0; i < v.count(); ++i)
            {
                Expect((long)v.nth(i)).To.Equal(i + 2L);
            }
        }
        public void CreateOnISeqWithManyManyItemsWorks()
        {
            // Want to bust out of the first tail, so need to insert more than 32 elements.
            // Let's get out of the second level, too.

            ISeq             r = LongRange.create(2, 100000);
            PersistentVector v = PersistentVector.create(r);

            Expect(v.count(), EqualTo(r.count()));
            for (int i = 0; i < v.count(); ++i)
            {
                Expect(v.nth(i), EqualTo(i + 2));
            }
        }
        public void AssocNChangesForBig()
        {
            Range             r  = new Range(2, 100000);
            PersistentVector  v1 = PersistentVector.create((ISeq)r);
            IPersistentVector v2 = v1;

            for (int i = 0; i < 110000; i++)
            {
                v2 = v2.assocN(i, i + 20);
            }

            for (int i = 0; i < v1.count(); ++i)
            {
                Expect(v1.nth(i), EqualTo(i + 2));
            }

            for (int i = 0; i < v2.count(); ++i)
            {
                Expect(v2.nth(i), EqualTo(i + 20));
            }
        }
        public void AssocNChangesForBig()
        {
            ISeq              r  = LongRange.create(2, 100000);
            PersistentVector  v1 = PersistentVector.create(r);
            IPersistentVector v2 = v1;

            for (int i = 0; i < 110000; i++)
            {
                v2 = v2.assocN(i, i + 20L);
            }

            for (int i = 0; i < v1.count(); ++i)
            {
                Expect((long)v1.nth(i)).To.Equal(i + 2L);
            }

            for (int i = 0; i < v2.count(); ++i)
            {
                object o = v2.nth(i);
                Expect(o).To.Be.An.Instance.Of <long>();
                Expect((long)o).To.Equal(i + 20L);
            }
        }
        public void NthOutOfRangeHighFails()
        {
            PersistentVector v = PersistentVector.create(1, 2, 3);

            v.nth(4);
        }
Example #17
0
            //(case* expr shift mask  default map<minhash, [test then]> table-type test-type skip-check?)
            //prepared by case macro and presumed correct
            //case macro binds actual expr in let so expr is always a local,
            //no need to worry about multiple evaluation
            public Expr Parse(ParserContext pcon, object frm)
            {
                ISeq form = (ISeq)frm;

                if (pcon.Rhc == RHC.Eval)
                {
                    return(Compiler.Analyze(pcon, RT.list(RT.list(Compiler.FnSym, PersistentVector.EMPTY, form)), "case__" + RT.nextID()));
                }

                PersistentVector args = PersistentVector.create(form.next());

                object         exprForm    = args.nth(0);
                int            shift       = Util.ConvertToInt(args.nth(1));
                int            mask        = Util.ConvertToInt(args.nth(2));
                object         defaultForm = args.nth(3);
                IPersistentMap caseMap     = (IPersistentMap)args.nth(4);
                Keyword        switchType  = (Keyword)args.nth(5);
                Keyword        testType    = (Keyword)args.nth(6);
                IPersistentSet skipCheck   = RT.count(args) < 8 ? null : (IPersistentSet)args.nth(7);

                ISeq             keys     = RT.keys(caseMap);
                int              low      = Util.ConvertToInt(RT.first(keys));
                int              high     = Util.ConvertToInt(RT.nth(keys, RT.count(keys) - 1));
                LocalBindingExpr testexpr = (LocalBindingExpr)Compiler.Analyze(pcon.SetRhc(RHC.Expression), exprForm);


                SortedDictionary <int, Expr> tests = new SortedDictionary <int, Expr>();
                Dictionary <int, Expr>       thens = new Dictionary <int, Expr>();

                //testexpr.shouldClear = false;
                //PathNode branch = new PathNode(PATHTYPE.BRANCH, (PathNode) CLEAR_PATH.get());

                foreach (IMapEntry me in caseMap)
                {
                    int    minhash  = Util.ConvertToInt(me.key());
                    object pair     = me.val(); // [test-val then-expr]
                    object first    = RT.first(pair);
                    Expr   testExpr = testType == _intKey
                        ? NumberExpr.Parse(Util.ConvertToInt(first))
                        : (first == null ? Compiler.NilExprInstance : new ConstantExpr(first));

                    tests[minhash] = testExpr;
                    Expr thenExpr;
                    //try
                    //{
                    //    Var.pushThreadBindings(
                    //        RT.map(CLEAR_PATH, new PathNode(PATHTYPE.PATH,branch)));
                    thenExpr = Compiler.Analyze(pcon, RT.second(pair));
                    //}
                    //finally
                    //{
                    //    Var.popThreadBindings();
                    //}
                    thens[minhash] = thenExpr;
                }

                Expr defaultExpr;

                //try
                //{
                //    Var.pushThreadBindings(
                //        RT.map(CLEAR_PATH, new PathNode(PATHTYPE.PATH,branch)));
                defaultExpr = Compiler.Analyze(pcon, defaultForm);
                //}
                //finally
                //{
                //    Var.popThreadBindings();
                //}
                return(new CaseExpr(
                           (IPersistentMap)Compiler.SourceSpanVar.deref(),
                           testexpr,
                           shift,
                           mask,
                           low,
                           high,
                           defaultExpr,
                           tests,
                           thens,
                           switchType,
                           testType,
                           skipCheck));
            }
Example #18
0
 public void NthOutOfRangeLowFails()
 {
     PersistentVector v   = PersistentVector.create(1, 2, 3);
     object           obj = v.nth(-4);
 }