Exemplo n.º 1
0
            public Expr Parse(ParserContext pcon, object frm)
            {
                // frm is:  (reify this-name? [interfaces] (method-name [args] body)* )
                ISeq      form            = (ISeq)frm;
                ObjMethod enclosingMethod = (ObjMethod)Compiler.MethodVar.deref();
                string    baseName        = enclosingMethod != null
                    ? (ObjExpr.TrimGenId(enclosingMethod.Objx.Name) + "$")
                    : (Compiler.munge(Compiler.CurrentNamespace.Name.Name) + "$");
                string simpleName = "reify__" + RT.nextID();
                string className  = baseName + simpleName;

                ISeq rform = RT.next(form);

                IPersistentVector interfaces = ((IPersistentVector)RT.first(rform)).cons(Symbol.intern("clojure.lang.IObj"));

                rform = RT.next(rform);

                ObjExpr ret  = Build(interfaces, null, null, className, Symbol.intern(className), null, rform, frm);
                IObj    iobj = frm as IObj;

                if (iobj != null && iobj.meta() != null)
                {
                    return(new MetaExpr(ret, MapExpr.Parse(pcon.EvalOrExpr(), iobj.meta())));
                }
                else
                {
                    return(ret);
                }
            }
Exemplo n.º 2
0
        public void withMeta_has_correct_meta()
        {
            InitMocks();
            IObj obj2 = _obj.withMeta(_meta);

            Expect(obj2.meta(), SameAs(_meta));
        }
Exemplo n.º 3
0
        public void withMeta_has_correct_meta()
        {
            InitMocks();
            IObj obj2 = _obj.withMeta(_meta);

            Expect(Object.ReferenceEquals(obj2.meta(), _meta));
        }
Exemplo n.º 4
0
        public void Meta_ctor_has_meta()
        {
            IPersistentMap meta = new DummyMeta();
            IObj           r    = ((IObj)_createFn(2L, 5L)).withMeta(meta);

            Expect(r.meta(), EqualTo(meta));
        }
Exemplo n.º 5
0
 public void Verify_Null_Meta()
 {
     if (_objWithNullMeta == null)
     {
         return;
     }
     Expect(_objWithNullMeta.meta()).To.Be.Null();
 }
Exemplo n.º 6
0
 public void withMeta_returns_self_if_no_change()
 {
     if (_testNoChange)
     {
         IObj obj2 = _obj.withMeta(_obj.meta());
         Expect(Object.ReferenceEquals(obj2, _obj));
     }
 }
Exemplo n.º 7
0
 public void withMeta_returns_self_if_no_change()
 {
     if (_testNoChange)
     {
         IObj obj2 = _obj.withMeta(_obj.meta());
         Expect(obj2, SameAs(_obj));
     }
 }
        public void EmptyPreservesMeta()
        {
            IPersistentMap meta = new DummyMeta();

            IPersistentCollection p = (IPersistentCollection) new PersistentList("abc").withMeta(meta);
            IObj obj = (IObj)p.empty();

            Expect(Object.ReferenceEquals(obj.meta(), meta));
        }
        public void RSeqWithMetaHasMeta()
        {
            IPersistentMap meta = new DummyMeta();

            CPV  v   = new CPV(new object[] { 4, 5, 6 });
            IObj s   = (IObj)v.rseq();
            IObj obj = s.withMeta(meta);

            Expect(obj.meta(), SameAs(meta));
        }
Exemplo n.º 10
0
        public void SeqWithMetaHasMeta()
        {
            IPersistentMap meta = new DummyMeta();

            CPV  v   = new CPV(new object[] { 4, 5, 6 });
            IObj s   = (IObj)v.seq();
            IObj obj = s.withMeta(meta);

            Expect(Object.ReferenceEquals(obj.meta(), meta));
        }
Exemplo n.º 11
0
        public void EmptyPreservesMeta()
        {
            MockRepository mocks = new MockRepository();
            IPersistentMap meta  = mocks.StrictMock <IPersistentMap>();

            mocks.ReplayAll();

            IPersistentCollection p = (IPersistentCollection) new PersistentList("abc").withMeta(meta);
            IObj obj = (IObj)p.empty();

            Expect(obj.meta(), SameAs(meta));
            mocks.VerifyAll();
        }
Exemplo n.º 12
0
        public void RSeqWithMetaHasMeta()
        {
            MockRepository mocks = new MockRepository();
            IPersistentMap meta  = mocks.StrictMock <IPersistentMap>();

            mocks.ReplayAll();

            CPV  v   = new CPV(new object[] { 4, 5, 6 });
            IObj s   = (IObj)v.rseq();
            IObj obj = s.withMeta(meta);

            Expect(obj.meta(), SameAs(meta));
            mocks.VerifyAll();
        }
Exemplo n.º 13
0
        public static Expr Parse(ParserContext pcon, IPersistentMap form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();
            bool          constant  = true;

            IPersistentVector keyvals = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                IMapEntry e = (IMapEntry)s.first();
                Expr      k = Compiler.Analyze(pconToUse, e.key());
                Expr      v = Compiler.Analyze(pconToUse, e.val());
                keyvals = (IPersistentVector)keyvals.cons(k);
                keyvals = (IPersistentVector)keyvals.cons(v);
                if (!(k is LiteralExpr && v is LiteralExpr))
                {
                    constant = false;
                }
            }
            Expr ret = new MapExpr(keyvals);

            IObj iobjForm = form as IObj;

            if (iobjForm != null && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
            else if (constant)
            {
                // This 'optimzation' works, mostly, unless you have nested map values.
                // The nested map values do not participate in the constants map, so you end up with the code to create the keys.
                // Result: huge duplication of keyword creation.  3X increase in init time to the REPL.
                //IPersistentMap m = PersistentHashMap.EMPTY;
                //for (int i = 0; i < keyvals.length(); i += 2)
                //    m = m.assoc(((LiteralExpr)keyvals.nth(i)).Val, ((LiteralExpr)keyvals.nth(i + 1)).Val);
                //return new ConstantExpr(m);
                return(ret);
            }
            else
            {
                return(ret);
            }
        }
Exemplo n.º 14
0
        public static Expr Parse(ParserContext pcon, IPersistentVector form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();
            bool          constant  = true;

            IPersistentVector args = PersistentVector.EMPTY;

            for (int i = 0; i < form.count(); i++)
            {
                Expr v = Compiler.Analyze(pconToUse, form.nth(i));
                args = (IPersistentVector)args.cons(v);
                if (!(v is LiteralExpr))
                {
                    constant = false;
                }
            }

            Expr ret      = new VectorExpr(args);
            IObj iobjForm = form as IObj;

            if (iobjForm != null && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
            else if (constant)
            {
                IPersistentVector rv = PersistentVector.EMPTY;
                for (int i = 0; i < args.count(); i++)
                {
                    LiteralExpr ve = (LiteralExpr)args.nth(i);
                    rv = (IPersistentVector)rv.cons(ve.Val);
                }
                return(new ConstantExpr(rv));
            }
            else
            {
                return(ret);
            }
        }
Exemplo n.º 15
0
        public static Expr Parse(ParserContext pcon, IPersistentSet form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();
            bool          constant  = true;

            IPersistentVector keys = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                object e    = s.first();
                Expr   expr = Compiler.Analyze(pconToUse, e);
                keys = (IPersistentVector)keys.cons(expr);
                if (!(expr is LiteralExpr))
                {
                    constant = false;
                }
            }
            Expr ret      = new SetExpr(keys);
            IObj iobjForm = form as IObj;

            if (iobjForm != null && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
            else if (constant)
            {
                IPersistentSet set = PersistentHashSet.EMPTY;
                for (int i = 0; i < keys.count(); i++)
                {
                    LiteralExpr ve = (LiteralExpr)keys.nth(i);
                    set = (IPersistentSet)set.cons(ve.Val);
                }
                return(new ConstantExpr(set));
            }
            else
            {
                return(ret);
            }
        }
Exemplo n.º 16
0
        public static Expr Parse(ParserContext pcon, IPersistentMap form)
        {
            ParserContext pconToUse = pcon.EvalOrExpr();

            bool           keysConstant          = true;
            bool           valsConstant          = true;
            bool           allConstantKeysUnique = true;
            IPersistentSet constantKeys          = PersistentHashSet.EMPTY;

            IPersistentVector keyvals = PersistentVector.EMPTY;

            for (ISeq s = RT.seq(form); s != null; s = s.next())
            {
                IMapEntry e = (IMapEntry)s.first();
                Expr      k = Compiler.Analyze(pconToUse, e.key());
                Expr      v = Compiler.Analyze(pconToUse, e.val());
                keyvals = (IPersistentVector)keyvals.cons(k);
                keyvals = (IPersistentVector)keyvals.cons(v);
                if (k is LiteralExpr)
                {
                    object kval = k.Eval();
                    if (constantKeys.contains(kval))
                    {
                        allConstantKeysUnique = false;
                    }
                    else
                    {
                        constantKeys = (IPersistentSet)constantKeys.cons(kval);
                    }
                }
                else
                {
                    keysConstant = false;
                }
                if (!(v is LiteralExpr))
                {
                    valsConstant = false;
                }
            }

            Expr ret = new MapExpr(keyvals);

            IObj iobjForm = form as IObj;

            if (iobjForm != null && iobjForm.meta() != null)
            {
                return(Compiler.OptionallyGenerateMetaInit(pcon, form, ret));
            }
            //else if (constant)
            //{
            // This 'optimzation' works, mostly, unless you have nested map values.
            // The nested map values do not participate in the constants map, so you end up with the code to create the keys.
            // Result: huge duplication of keyword creation.  3X increase in init time to the REPL.
            //    //IPersistentMap m = PersistentHashMap.EMPTY;
            //    //for (int i = 0; i < keyvals.length(); i += 2)
            //    //    m = m.assoc(((LiteralExpr)keyvals.nth(i)).Val, ((LiteralExpr)keyvals.nth(i + 1)).Val);
            //    //return new ConstantExpr(m);
            //    return ret;
            //}
            else if (keysConstant)
            {
                // TBD: Add more detail to exception thrown below.
                if (!allConstantKeysUnique)
                {
                    throw new ArgumentException("Duplicate constant keys in map");
                }
                if (valsConstant)
                {
                    // This 'optimzation' works, mostly, unless you have nested map values.
                    // The nested map values do not participate in the constants map, so you end up with the code to create the keys.
                    // Result: huge duplication of keyword creation.  3X increase in init time to the REPL.
                    //IPersistentMap m = PersistentHashMap.EMPTY;
                    //for (int i = 0; i < keyvals.length(); i += 2)
                    //    m = m.assoc(((LiteralExpr)keyvals.nth(i)).Val, ((LiteralExpr)keyvals.nth(i + 1)).Val);
                    //return new ConstantExpr(m);
                    return(ret);
                }
                else
                {
                    return(ret);
                }
            }
            else
            {
                return(ret);
            }
        }
Exemplo n.º 17
0
        public void Basic_ctor_has_no_meta()
        {
            IObj r = (IObj)_createFn(2L, 5L);

            Expect(r.meta(), Null);
        }