protected override Expression VisitConstructorExpression(ConstructorExpression expression)
        {
            var oldConstructorArguments = expression.ConstructorArguments.ToList().AsReadOnly();
            var newConstructorArguments = VisitExpressionList(oldConstructorArguments);

            var oldBindings = expression.Bindings.Select(b => b.Value).ToList().AsReadOnly();
            var newBindings = VisitExpressionList(oldBindings);

            var oldNativeBindings = expression.NativeBindings.Select(b => b.Value).ToList().AsReadOnly();
            var newNativeBindings = VisitExpressionList(oldNativeBindings);

            var notChanged =
                ReferenceEquals(oldConstructorArguments, newConstructorArguments) &&
                ReferenceEquals(oldBindings, newBindings) &&
                ReferenceEquals(oldNativeBindings, newNativeBindings);

            if (notChanged)
            {
                return(expression);
            }

            var bindings = expression.Bindings
                           .Zip(newBindings)
                           .ToDictionary(item => item.First.Key, item => item.Second);
            var nativeBingings = expression.NativeBindings
                                 .Zip(newNativeBindings)
                                 .ToDictionary(item => item.First.Key, item => item.Second);

            return(new ConstructorExpression(expression.Type, bindings, nativeBingings, expression.Constructor, newConstructorArguments));
        }
Esempio n. 2
0
        public void parsesConstructor2AttributesComma()
        {
            String                statement = "Company(id=1, name=\"IBM\")";
            OTestParser           parser    = new OTestParser(statement);
            ConstructorExpression c         = parser.parse_constructor_expression();

            Assert.IsNotNull(c);
            ArgumentList l = c.getArguments();

            Assert.IsNotNull(l);
            Assert.AreEqual(2, l.Count);
            Argument a = l[0];

            Assert.IsNotNull(a);
            Assert.AreEqual("id", a.GetName());
            IExpression e = a.getExpression();

            Assert.IsNotNull(e);
            Assert.IsTrue(e is IntegerLiteral);
            a = l[1];
            Assert.IsNotNull(a);
            Assert.AreEqual("name", a.GetName());
            e = a.getExpression();
            Assert.IsNotNull(e);
            Assert.IsTrue(e is TextLiteral);
        }
Esempio n. 3
0
        public void testConstructor2AttributesAnd()
        {
            String                statement = "Company with 1 as id and \"IBM\" as name";
            ETestParser           parser    = new ETestParser(statement, false);
            ConstructorExpression c         = parser.parse_constructor_expression();

            Assert.IsNotNull(c);
            ArgumentList l = c.getArguments();

            Assert.IsNotNull(l);
            Assert.AreEqual(2, l.Count);
            Argument a = l[0];

            Assert.IsNotNull(a);
            Assert.AreEqual("id", a.GetName());
            IExpression e = a.getExpression();

            Assert.IsNotNull(e);
            Assert.IsTrue(e is IntegerLiteral);
            a = l[1];
            Assert.IsNotNull(a);
            Assert.AreEqual("name", a.GetName());
            e = a.getExpression();
            Assert.IsNotNull(e);
            Assert.IsTrue(e is TextLiteral);
        }
Esempio n. 4
0
        public void testConstructorFrom1Attribute()
        {
            String                statement = "Company from entity with 1 as id";
            ETestParser           parser    = new ETestParser(statement, false);
            ConstructorExpression c         = parser.parse_constructor_expression();

            Assert.IsNotNull(c);
        }
Esempio n. 5
0
        public void parsesConstructorFrom1Attribute()
        {
            String                statement = "Company(entity,id=1)";
            OTestParser           parser    = new OTestParser(statement);
            ConstructorExpression c         = parser.parse_constructor_expression();

            Assert.IsNotNull(c);
        }
 protected override Expression VisitConstructorExpression(ConstructorExpression expression)
 {
     foreach (var binding in expression.Bindings)
     {
         Visit(binding.Value);
     }
     foreach (var argument in expression.ConstructorArguments)
     {
         Visit(argument);
     }
     return(base.VisitConstructorExpression(expression));
 }
Esempio n. 7
0
        protected override Expression VisitConstructorExpression(ConstructorExpression expression)
        {
            var newExpression = expression.Constructor == null
        ? Expression.New(expression.Type) // Value type with default ctor (expression.Constructor is null in that case)
        : Expression.New(expression.Constructor, expression.ConstructorArguments.Select(Visit));

            var realBindings = expression.NativeBindings;

            return(expression.NativeBindings.Count == 0
        ? newExpression
        : (Expression)Expression.MemberInit(newExpression, expression
                                            .NativeBindings
                                            .Where(item => Translator.FilterBindings(item.Key, item.Key.Name, item.Value.Type))
                                            .Select(item => Expression.Bind(item.Key, Visit(item.Value))).Cast <MemberBinding>()));
        }
Esempio n. 8
0
        protected override Expression VisitConstructorExpression(ConstructorExpression expression)
        {
            var  arguments      = new List <Expression>();
            var  bindings       = new Dictionary <MemberInfo, Expression>();
            var  nativeBindings = new Dictionary <MemberInfo, Expression>();
            bool recreate       = false;

            foreach (var argument in expression.ConstructorArguments)
            {
                var result = Visit(argument);
                if (result != argument)
                {
                    recreate = true;
                }
                arguments.Add(result);
            }
            foreach (var binding in expression.Bindings)
            {
                var result = Visit(binding.Value);
                if (result != binding.Value)
                {
                    recreate = true;
                }
                bindings.Add(binding.Key, result);
            }
            foreach (var nativeBinding in expression.NativeBindings)
            {
                var result = Visit(nativeBinding.Value);
                if (result != nativeBinding.Value)
                {
                    recreate = true;
                }
                nativeBindings.Add(nativeBinding.Key, result);
            }
            if (!recreate)
            {
                return(expression);
            }
            return(new ConstructorExpression(
                       expression.Type,
                       bindings,
                       nativeBindings,
                       expression.Constructor,
                       arguments));
        }
Esempio n. 9
0
            public object VisitConstructorExpression(ConstructorExpression expr, object context)
            {
                StringBuilder.Append("new ");
                StringBuilder.Append(expr.TypeName.Content);
                StringBuilder.Append("(");
                int i = 0;

                foreach (Expression argument in expr.Arguments)
                {
                    if (i > 0)
                    {
                        StringBuilder.Append(", ");
                    }
                    argument.AcceptVisitor(this, context);
                    i++;
                }
                StringBuilder.Append(")");
                return(null);
            }
Esempio n. 10
0
        public IValue interpret(Context context, String errorName)
        {
            IExpression exp = this.getExpression(context);

            if (exp == null)
            {
                ArgumentList args = new ArgumentList();
                args.Add(new Argument(new UnresolvedParameter("name"), new TextLiteral(this.GetType().Name)));
                args.Add(new Argument(new UnresolvedParameter("text"), new TextLiteral(this.Message)));
                exp = new ConstructorExpression(new CategoryType("Error"), null, args);
            }
            if (context.getRegisteredValue <INamed>(errorName) == null)
            {
                context.registerValue(new ErrorVariable(errorName));
            }
            IValue error = exp.interpret(context);

            context.setValue(errorName, error);
            return(error);
        }
Esempio n. 11
0
        private IValue populateError(ExecutionError e, Context context)
        {
            IExpression exp = e.getExpression(context);

            if (exp == null)
            {
                ArgumentList args = new ArgumentList();
                args.Add(new Argument(new UnresolvedParameter("name"), new TextLiteral(e.GetType().Name)));
                args.Add(new Argument(new UnresolvedParameter("text"), new TextLiteral(e.Message)));
                ConstructorExpression ctor = new ConstructorExpression(new CategoryType("Error"), null, args);
                exp = ctor;
            }
            if (context.getRegisteredValue <INamed>(errorName) == null)
            {
                context.registerValue(new ErrorVariable(errorName));
            }
            IValue value = exp.interpret(context);

            context.setValue(errorName, value);
            return(value);
        }
Esempio n. 12
0
 protected virtual Expression VisitConstructorExpression(ConstructorExpression expression)
 {
     return(expression);
 }