Esempio n. 1
0
        public LambdaNode(IEnumerable<NodeBase> children)
            : base(NodeType.Lambda, children)
        {
            if (Children.Count != 2)
                throw new PomonaExpressionSyntaxException("Error parsing lambda expression");

            if (Children[0].NodeType != NodeType.Symbol)
                throw new PomonaExpressionSyntaxException("Left side of lambda expression needs to be a symbol.");

            argument = Children[0] as SymbolNode;
            body = Children[1];
        }
        public LambdaExpression ToLambdaExpression(Type thisType, NodeBase node)
        {
            var param = Expression.Parameter(thisType, "_this");

            return(ToLambdaExpression(param, param.WrapAsEnumerable(), null, node));
        }
 public Expression ParseExpression(NodeBase node)
 {
     return(ParseExpression(node, null, null));
 }
        private Expression ParseExpressionUnsafe(NodeBase node, Expression memberExpression, Type expectedType)
        {
            if (memberExpression == null)
            {
                memberExpression = this.thisParam;
            }

            if (node.NodeType == NodeType.ArrayLiteral)
            {
                return(ParseArrayLiteral((ArrayNode)node, expectedType != null ? expectedType.GetElementType() : null));
            }

            if (node.NodeType == NodeType.MethodCall)
            {
                return(ParseMethodCallNode((MethodCallNode)node, memberExpression));
            }

            if (node.NodeType == NodeType.IndexerAccess)
            {
                return(ParseIndexerAccessNode((IndexerAccessNode)node, memberExpression));
            }

            if (node.NodeType == NodeType.Symbol)
            {
                var dictionaryInterface = memberExpression.Type
                                          .GetInterfacesOfGeneric(typeof(IDictionary <,>))
                                          .FirstOrDefault();

                if (node.Children.Count == 0 && dictionaryInterface != null)
                {
                    return(ResolveDictionaryAccess((SymbolNode)node, memberExpression, dictionaryInterface));
                }

                return(ResolveSymbolNode((SymbolNode)node, memberExpression, expectedType));
            }

            var binaryOperatorNode = node as BinaryOperatorNode;

            if (binaryOperatorNode != null)
            {
                return(ParseBinaryOperator(binaryOperatorNode));
            }

            if (node.NodeType == NodeType.GuidLiteral)
            {
                var guidNode = (GuidNode)node;
                return(Expression.Constant(guidNode.Value));
            }

            if (node.NodeType == NodeType.DateTimeLiteral)
            {
                var dateTimeNode = (DateTimeNode)node;
                return(Expression.Constant(dateTimeNode.Value));
            }

            if (node.NodeType == NodeType.DateTimeOffsetLiteral)
            {
                var dateTimeOffsetNode = (DateTimeOffsetNode)node;
                return(Expression.Constant(dateTimeOffsetNode.Value));
            }

            if (node.NodeType == NodeType.StringLiteral)
            {
                var stringNode = (StringNode)node;

                if (expectedType != null && !expectedType.IsAssignableFrom(typeof(string)))
                {
                    if (expectedType.IsEnum)
                    {
                        return(Expression.Constant(Enum.Parse(expectedType, stringNode.Value, true)));
                    }
                    throw CreateParseException(stringNode,
                                               "Don't know what to do with string node when expected type is " +
                                               expectedType.FullName);
                }
                return(Expression.Constant(stringNode.Value));
            }

            if (node.NodeType == NodeType.NumberLiteral)
            {
                var intNode = (NumberNode)node;
                return(Expression.Constant(intNode.Parse()));
            }

            if (node.NodeType == NodeType.TypeNameLiteral)
            {
                var typeNameNode = (TypeNameNode)node;
                return(Expression.Constant(ResolveType(typeNameNode), typeof(Type)));
            }

            if (node.NodeType == NodeType.Lambda)
            {
                var lambdaNode = (LambdaNode)node;
                return(ParseLambda(lambdaNode, expectedType));
            }

            if (node.NodeType == NodeType.Not)
            {
                var notNode = (NotNode)node;
                return(Expression.Not(ParseExpression(notNode.Children[0])));
            }

            throw new NotImplementedException();
        }