Пример #1
0
        Expression CreateBinaryExpression(Node node)
        {
            Expression left = CreateExpressionCore(node.Children[0]);
            Expression right = CreateExpressionCore(node.Children[1]);

            if (left == null || right == null)
            {
                return null;
            }

            switch (node.Kind)
            {
                case NodeType.Addition:
                    return Expression.Add(left, right);
                case NodeType.Subtraction:
                    return Expression.Subtract(left, right);
                case NodeType.Multiplication:
                    return Expression.Multiply(left, right);
                case NodeType.Division:
                    return Expression.Divide(left, right);
                case NodeType.Power:
                    return Expression.Power(left, right);
            }
            return null;
        }
Пример #2
0
        public Expression<Func<double, double>> CreateFunction(Node root, CompileResult status)
        {
            Status = status;
            ParameterExpression parameter = Expression.Parameter(typeof(double), "x");
            Binder.RegisterParameter(parameter);
            Expression body = CreateExpressionCore(root);
            if (body == null)
            {
                return null;
            }

            var expressionTree = Expression.Lambda<Func<double, double>>(body, parameter);
            return expressionTree;
        }
Пример #3
0
        public Expression<Func<double>> CreateExpression(Node root, CompileResult status)
        {
            Status = status;
            Expression body = CreateExpressionCore(root);
            if (body == null)
            {
                return null;
            }

            // If expression does not return double, we'll get an error.
            if (body.Type != typeof(double))
            {
                return null;
            }

            var expressionTree = Expression.Lambda<Func<double>>(body);
            return expressionTree;
        }
Пример #4
0
        Expression CreateCallExpression(Node root)
        {
            string functionName = root.Token.Text;
            MethodInfo method = Binder.ResolveMethod(functionName);
            if (method == null)
            {
                Status.AddMethodNotFoundError(functionName);
                return null;
            }

            var arguments = root.Children;
            if (arguments.Count == 1)
            {
                var argument = CreateExpressionCore(arguments[0]);
                if (argument == null)
                {
                    return null;
                }

                return Expression.Call(method, argument);
            }

            Status.AddMethodNotFoundError(functionName);
            return null;
        }
Пример #5
0
        Expression CreatePropertyAccessExpression(Node root)
        {
            string propertyName = root.Children[1].Token.Text;
            var argument = CreateExpressionCore(root.Children[0]);
            if (argument == null)
            {
                return null;
            }

            Type type = argument.Type;
            var property = type.GetRuntimeProperty(propertyName);
            if (property == null)
            {
                Status.AddBindError(propertyName);
                return null;
            }

            var propertyExpression = Expression.Property(argument, property);
            return propertyExpression;
        }
Пример #6
0
        Expression CreateUnaryExpression(Node root)
        {
            Expression operand = CreateExpressionCore(root.Children[0]);
            if (operand == null)
            {
                return null;
            }

            return Expression.Negate(operand);
        }
Пример #7
0
 Expression CreateExpressionCore(Node root)
 {
     switch (root.Kind)
     {
         case NodeType.Negation:
             return CreateUnaryExpression(root);
         case NodeType.Addition:
         case NodeType.Subtraction:
         case NodeType.Multiplication:
         case NodeType.Division:
         case NodeType.Power:
             return CreateBinaryExpression(root);
         case NodeType.Variable:
             return CreateIdentifierExpression(root);
         case NodeType.Constant:
             return CreateLiteralExpression(Convert.ToDouble(root.Token.Text));
         case NodeType.FunctionCall:
             return CreateCallExpression(root);
         case NodeType.PropertyAccess:
             return CreatePropertyAccessExpression(root);
         default:
             return null;
     }
 }
Пример #8
0
        Expression CreateIdentifierExpression(Node root)
        {
            var text = root.Token.Text;

            var parameter = Binder.Resolve(text);
            if (parameter == null)
            {
                Status.AddUnknownIdentifierError(text);
            }

            return parameter;
        }
Пример #9
0
 private Node PropertyAccess(Node left, Node right)
 {
     return new Node(NodeType.PropertyAccess, left, right);
 }
Пример #10
0
        private Node ParseFunctionCall()
        {
            var result = new Node(NodeType.FunctionCall, Current);
            AdvanceToken();
            AdvanceToken(TokenType.OpenParen);
            AddArgument(result, ParseExpression());
            while (CurrentTokenKind == TokenType.Comma)
            {
                AdvanceToken();
                AddArgument(result, ParseExpression());
            }

            AdvanceToken(TokenType.CloseParen);
            return result;
        }
Пример #11
0
 private Node Negation(Node unaryOperand)
 {
     return new Node(NodeType.Negation, unaryOperand);
 }
Пример #12
0
 private Node BinaryOperator(NodeType operationType, Node leftOperand, Node rightOperand)
 {
     return new Node(operationType, leftOperand, rightOperand);
 }
Пример #13
0
 private void AddArgument(Node functionCall, Node argument)
 {
     functionCall.Children.Add(argument);
 }