コード例 #1
0
ファイル: Context.Lookup.cs プロジェクト: vplemyannik/lens
        /// <summary>
        /// Resolves a lambda return type when its argument types have been inferred from usage.
        /// </summary>
        public Type ResolveLambda(LambdaNode lambda, Type[] argTypes)
        {
            lambda.SetInferredArgumentTypes(argTypes);
            var delegateType = lambda.Resolve(this);

            return(ReflectionHelper.WrapDelegate(delegateType).ReturnType);
        }
コード例 #2
0
        /// <summary>
        /// Binds a LambdaToken to metadata.
        /// </summary>
        /// <param name="lambdaToken">Token to bind.</param>
        /// <param name="state">Object to hold the state of binding.</param>
        /// <returns>A metadata bound any or all node.</returns>
        internal LambdaNode BindLambdaToken(LambdaToken lambdaToken, BindingState state)
        {
            ExceptionUtils.CheckArgumentNotNull(lambdaToken, "LambdaToken");
            ExceptionUtils.CheckArgumentNotNull(state, "state");

            // Start by binding the parent token
            CollectionNode parent        = this.BindParentToken(lambdaToken.Parent);
            RangeVariable  rangeVariable = null;

            // Add the lambda variable to the stack
            if (lambdaToken.Parameter != null)
            {
                rangeVariable = NodeFactory.CreateParameterNode(lambdaToken.Parameter, parent);
                state.RangeVariables.Push(rangeVariable);
            }

            // Bind the expression
            SingleValueNode expression = this.BindExpressionToken(lambdaToken.Expression);

            // Create the node
            LambdaNode lambdaNode = NodeFactory.CreateLambdaNode(state, parent, expression, rangeVariable, lambdaToken.Kind);

            // Remove the lambda variable as it is now out of scope
            if (rangeVariable != null)
            {
                state.RangeVariables.Pop();
            }

            return(lambdaNode);
        }
コード例 #3
0
        static VBParameter ExtractLambdaParameter(NameNode name)
        {
            if (name == null)
            {
                return(null);
            }
            if (!(name.Parent is DeclaratorNode) || !(name.Parent.Parent is ParameterNode))
            {
                return(null);
            }

            LambdaNode lambdaNode = name.Parent.Parent.Parent as LambdaNode;

            if (lambdaNode == null)
            {
                return(null);
            }
            LambdaExpression lambdaExpression = name.Tree.SourceFile.Binder.CompileExpression(lambdaNode) as LambdaExpression;

            if (lambdaExpression == null)
            {
                return(null);
            }
            return(lambdaExpression.Parameters
                   .FirstOrDefault(p => p.Name.EqualsNoCase(((IdentifierNode)name).Name)));
        }
コード例 #4
0
        public void LambdaExpressionBodyIsExpected()
        {
            IExpressionNode actual = TestObject.Build(TestLambdaExpression);

            LambdaNode   actualNode = actual.VerifyIsActually <LambdaNode>();
            ConstantNode bodyNode   = actualNode.Body.VerifyIsActually <ConstantNode>();

            Assert.AreSame(TestConstantZero, bodyNode.SourceExpression);
        }
コード例 #5
0
        public void LambdaExpressionIsExpected()
        {
            Expression testExpression = TestLambdaExpression;

            IExpressionNode actual = TestObject.Build(testExpression);

            LambdaNode actualNode = actual.VerifyIsActually <LambdaNode>();

            Assert.AreSame(testExpression, actualNode.SourceExpression);
        }
コード例 #6
0
ファイル: LambdaNodeTest.cs プロジェクト: whaycox/Creamery
        public void Init()
        {
            _testExpression = Expression.Lambda(TestBodyExpression, TestParameterExpression);

            MockNodeFactory
            .Setup(factory => factory.Build(TestBodyExpression))
            .Returns(MockBodyNode.Object);

            TestObject = new LambdaNode(
                MockNodeFactory.Object,
                TestExpression);
        }
コード例 #7
0
            private IEnumerable <string> VisitLambda(LambdaNode nodeIn)
            {
                var oldBase = BaseString;

                BaseString = Combine(nodeIn.Source);
                var returnVar = nodeIn.Body.Accept(this);

                if (!returnVar.Any() && BaseString != oldBase)
                {
                    returnVar = new[] { BaseString };
                }
                BaseString = oldBase;
                return(returnVar);
            }
コード例 #8
0
        public override void VisitLambda(LambdaNode lambdaNode)
        {
            if (SelectedProperty != null)
            {
                throw new InvalidOperationException("Cannot visit twice with the same visitor");
            }

            LambdaParameter = lambdaNode
                              .SourceExpression
                              .Parameters
                              .Single();
            lambdaNode
            .Body
            .AcceptVisitor(this);
        }
コード例 #9
0
        public void VisitLambda(LambdaNode node)
        {
            UpdateLine(node);
            NewProcedure(node.Name, false, node.Varargs, node.Position.Line, false);
            UpdateLine(node);

            node.Parameters.ForEach(p => p.Accept(this));
            node.Statements.ForEach(s => s.Accept(this));

            if (node.Statements.Count == 0 || !(node.Statements.Last() is ReturnNode))
            {
                asm.Return();
            }

            Closure(FinishProcedure());
        }
コード例 #10
0
        private void SetupForExpression(Expression <Func <TestEntity, int> > testExpression)
        {
            TestExpression          = testExpression;
            TestParameterExpression = TestExpression
                                      .Parameters
                                      .Single();
            MockNodeFactory
            .Setup(factory => factory.Build(TestExpression.Body))
            .Returns(MockLambdaBodyNode.Object);
            TestLambdaNode = new LambdaNode(MockNodeFactory.Object, TestExpression);

            TestMemberExpression = (MemberExpression)TestExpression.Body;
            MockNodeFactory
            .Setup(factory => factory.Build(TestMemberExpression.Expression))
            .Returns(MockMemberExpressionNode.Object);
            TestMemberAccessNode = new MemberAccessNode(MockNodeFactory.Object, TestMemberExpression);
        }
コード例 #11
0
            private QueryNode VisitLambda(LambdaNode nodeIn)
            {
                var sourceNode = (CollectionNavigationNode)nodeIn.Source;

                IsFilterable &= Filterable(sourceNode.NavigationProperty);
                if (IsFilterable)
                {
                    bool isPushed = PushPropertySettings(sourceNode.NavigationProperty);
                    nodeIn.Body.Accept(this);

                    if (isPushed)
                    {
                        _settings.Pop();
                    }
                }

                return(nodeIn);
            }
コード例 #12
0
        public AstNode VisitLambda(LambdaNode n)
        {
            Append("((");
            if (n.Parameters.Count > 0)
            {
                Visit(n.Parameters[0]);
                foreach (var p in n.Parameters.Skip(1))
                {
                    Append(", ");
                    Visit(p);
                }
            }

            Append(") => ");
            if (n.Statements == null || n.Statements.Count == 0)
            {
                Append("{}");
            }
            else if (n.Statements.Count == 1)
            {
                Visit(n.Statements[0]);
            }
            else
            {
                Append("{");
                IncreaseIndent();

                foreach (var statement in n.Statements)
                {
                    AppendLineAndIndent();
                    Visit(statement);
                    Append(";");
                }

                DecreaseIndent();
                AppendLineAndIndent();
                Append("}");
            }
            Append(")");

            return(n);
        }
コード例 #13
0
        public void Visit(LambdaNode lambdaNode)
        {
            _table.Enter(lambdaNode.Scope);
            _environment = new Environment(_environment);
            foreach (var arg in lambdaNode.Arguments)
            {
                _environment.Define(arg.Name);
            }

            var inLambda = _inLambda;

            _inLambda = true;
            var enclosing = _currentMethod;

            _currentMethod         = MethodType.Lambda;
            lambdaNode.Body.Parent = lambdaNode;
            lambdaNode.Body.Accept(this);
            _inLambda      = inLambda;
            _currentMethod = enclosing;
            _environment   = _environment.Enclosing;
            _table.Exit();
        }
コード例 #14
0
        public void Accept(LambdaNode node)
        {
            var lambda = new HassiumMethod(module);

            methodStack.Push(lambda);
            lambda.Parent = classStack.Peek();

            table.EnterScope();

            foreach (var param in node.Parameters.Arguments)
            {
                string name = ((IdentifierNode)param).Identifier;
                lambda.Parameters.Add(new FunctionParameter(FunctionParameterType.Normal, name), table.HandleSymbol(name));
            }

            node.Body.VisitChildren(this);

            table.LeaveScope();
            methodStack.Pop();

            emit(node.SourceLocation, InstructionType.PushObject, lambda);
            emit(node.SourceLocation, InstructionType.BuildClosure);
        }
コード例 #15
0
ファイル: LensParser.cs プロジェクト: menozz/lens
        /// <summary>
        /// lambda_line_expr                            = [ fun_args ] "->" line_expr
        /// </summary>
        private LambdaNode parseLambdaLineExpr()
        {
            var node = new LambdaNode();
            node.Arguments = attempt(() => parseFunArgs()) ?? new List<FunctionArgument>();

            if (!check(LexemType.Arrow))
                return null;

            node.Body.Add(ensure(parseLineStmt, ParserMessages.FunctionBodyExpected));
            return node;
        }
コード例 #16
0
 public LambdaNodeTests()
 {
     subject = new LambdaNode(SourcePosition.NIL);
 }
コード例 #17
0
        private LambdaNode ParseLambda()
        {
            Expect(TokenType.PROC);
            Next();

            Expect(TokenType.LPAREN);
            Next();

            var lambda = new LambdaNode(Position(-2));

            while (More() && !Accept(TokenType.RPAREN))
            {
                Expect(TokenType.FROZEN, TokenType.IDENT, TokenType.VARARGS);
                if (Accept(TokenType.FROZEN, TokenType.IDENT))
                {
                    bool frozen = false;

                    while (Accept(TokenType.FROZEN))
                    {
                        frozen = true;
                        Next();
                    }

                    lambda.Parameters.Add(new ParameterNode(Position(), ParseIdent(), frozen));
                }
                else if (Accept(TokenType.VARARGS))
                {
                    lambda.Varargs = true;
                    Next();
                    break;
                }
                else
                {
                    Unexpected();
                }

                if (Accept(TokenType.COMMA))
                {
                    ExpectNot(TokenType.RPAREN);
                    Next();
                }
                else
                {
                    Expect(TokenType.RPAREN);
                }
            }

            Expect(TokenType.RPAREN);
            Next();

            Expect(TokenType.FAT_ARROW, TokenType.LBRACE);
            if (Accept(TokenType.FAT_ARROW))
            {
                Next();

                lambda.Statements.Add(new ReturnNode(Position(), ParseExpression()));
            }
            else if (Accept(TokenType.LBRACE))
            {
                Next();

                while (More() && !Accept(TokenType.RBRACE))
                {
                    lambda.Statements.Add(ParseStatement());
                }

                Expect(TokenType.RBRACE);
                Next();
            }
            else
            {
                Unexpected();
            }

            return(lambda);
        }
コード例 #18
0
 public virtual AstNode VisitLambda(LambdaNode n)
 {
     Visit(n.Parameters);
     Visit(n.Statements);
     return(n);
 }
コード例 #19
0
ファイル: Context.Lookup.cs プロジェクト: menozz/lens
 /// <summary>
 /// Resolves a lambda return type when its argument types have been inferred from usage.
 /// </summary>
 public Type ResolveLambda(LambdaNode lambda, Type[] argTypes)
 {
     lambda.SetInferredArgumentTypes(argTypes);
     var delegateType = lambda.Resolve(this);
     return ReflectionHelper.WrapDelegate(delegateType).ReturnType;
 }
コード例 #20
0
 public virtual void VisitLambda(LambdaNode lambdaNode) => throw new NotImplementedException();
コード例 #21
0
 internal Closure(Scope parentScope, LambdaNode targetNode)
 {
     ParentScope = parentScope;
     Lambda      = targetNode;
 }
コード例 #22
0
ファイル: TestASTVisitor.cs プロジェクト: sci4me/Neo-old
 public void VisitLambda(LambdaNode node)
 {
     VisitLambdaHandler(node);
 }
コード例 #23
0
 public XzaarExpression Visit(LambdaNode lambda)
 {
     return(null);
 }
コード例 #24
0
ファイル: ODataConverter.cs プロジェクト: patrickCode/C-Sharp
        private string ResolveLamdaNode(LambdaNode node)
        {
            BinaryOperatorNode expression         = node.Body as BinaryOperatorNode;
            string             alias              = "";
            string             propertyName       = "";
            string             parentPropertyName = "";
            string             rootPropertyName   = "";

            //Property at root
            if (expression.Left is NonentityRangeVariableReferenceNode)
            {
                var leftNode = expression.Left as NonentityRangeVariableReferenceNode;
                alias = leftNode.Name;

                if (node.Source is CollectionPropertyAccessNode)
                {
                    propertyName = (node.Source as CollectionPropertyAccessNode).Property.Name;
                }
            }
            else if (expression.Left is SingleValuePropertyAccessNode)
            {
                var leftNode = expression.Left as SingleValuePropertyAccessNode;
                if (leftNode.Source is NonentityRangeVariableReferenceNode)
                {
                    alias = (leftNode.Source as NonentityRangeVariableReferenceNode).Name;
                }
                else if (leftNode.Source is EntityRangeVariableReferenceNode)
                {
                    alias = (leftNode.Source as EntityRangeVariableReferenceNode).Name;
                }
                propertyName = leftNode.Property.Name;

                if (node.Source is CollectionPropertyAccessNode)
                {
                    parentPropertyName = (node.Source as CollectionPropertyAccessNode).Property.Name;
                    var sourceNode = node.Source as CollectionPropertyAccessNode;
                    if (sourceNode.Source is SingleValuePropertyAccessNode)
                    {
                        var rootNode = sourceNode.Source as SingleValuePropertyAccessNode;
                        rootPropertyName = rootNode.Property.Name;
                    }
                }
                else if (node.Source is CollectionNavigationNode)
                {
                    var sourceNode = node.Source as CollectionNavigationNode;
                    parentPropertyName = sourceNode.NavigationProperty.Name;
                    if (sourceNode.Source is SingleNavigationNode)
                    {
                        var rootNode = sourceNode.Source as SingleNavigationNode;
                        rootPropertyName = rootNode.NavigationProperty.Name;
                    }
                }
            }

            var rightExpression = (expression.Right as ConstantNode).LiteralText;

            var mappedPropertyName = _fieldMapper.Map(propertyName, parentPropertyName, rootPropertyName);
            var lamdaProperty      = node.Kind == QueryNodeKind.Any ? "any" : "all";
            var expressionFormat   = $"{mappedPropertyName}/{lamdaProperty}({alias}: {alias} eq {rightExpression})";

            return(expressionFormat);
        }