public string Visit(PyAst.LambdaExpression node)
        {
            var args          = string.Join(", ", node.Function.Parameters.Select(p => Visit(p)));
            var convertedExpr = Visit(((PyAst.ReturnStatement)node.Function.Body).Expression);

            return($"lambda { args }: {convertedExpr}");
        }
Exemple #2
0
        public Node VisitExpression(IronPython.Compiler.Ast.LambdaExpression exp)
        {
            var changed = false;
            var newCode = exp;

            foreach (var edit in _edits)
            {
                if (edit.CanApply(exp))
                {
                    changed = true;
                    if (edit is Update)
                    {
                        newCode = (IronPython.Compiler.Ast.LambdaExpression)edit.ModifiedNode.InnerNode;
                    }
                    else if (edit is Insert)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
            }
            if (changed)
            {
                return(newCode);
            }

            return(new IronPython.Compiler.Ast.LambdaExpression(newCode.Function));
        }
Exemple #3
0
        private Expression ParseLambdaHelperEnd(FunctionDefinition func, Expression expr) {
            // Pep 342 in Python 2.5 allows Yield Expressions, which can occur inside a Lambda body. 
            // In this case, the lambda is a generator and will yield it's final result instead of just return it.
            Statement body;
            if (func.IsGenerator) {
                YieldExpression y = new YieldExpression(expr);
                y.SetLoc(_globalParent, expr.IndexSpan);
                body = new ExpressionStatement(y);
            } else {
                body = new ReturnStatement(expr);
            }
            body.SetLoc(_globalParent, expr.StartIndex, expr.EndIndex);

            FunctionDefinition func2 = PopFunction();
            System.Diagnostics.Debug.Assert(func == func2);

            func.Body = body;
            func.EndIndex = GetEnd();

            LambdaExpression ret = new LambdaExpression(func);
            func.SetLoc(_globalParent, func.IndexSpan);
            ret.SetLoc(_globalParent, func.IndexSpan);
            return ret;
        }
Exemple #4
0
 internal Lambda(LambdaExpression lambda)
     : this() {
     FunctionDef def = (FunctionDef)Convert(lambda.Function);
     _args = def.args;
     Debug.Assert(def.body.Count == 1, "LambdaExpression body should be one Return statement.");
     _body = ((Return)def.body[0]).value;
 }
 public override void PostWalk(LambdaExpression node)
 {
     CommonPostWalk(node);
 }
 public override bool Walk(LambdaExpression node)
 {
     CommonWalk(node);
     return true;
 }
 // LambdaExpression
 public bool Walk(LambdaExpression node)
 {
     return Process(node);
 }
 public void PostWalk(LambdaExpression node)
 {
     PostProcess(node);
 }
Exemple #9
0
            internal Lambda(LambdaExpression lambda)
                : this()
            {
                FunctionDef def = (FunctionDef)Convert(lambda.Function);
                _args = def.args;
                Debug.Assert(def.body.Count == 1, "LambdaExpression body should be one statement.");
                stmt statement = (stmt)def.body[0];
                if (statement is Return)
                    _body = ((Return)statement).value;
                else if (statement is Expr) {
                    // What should be sufficient is:
                    // _body = ((Expr)statement).value;
                    // but, AST comes with trees containing twice YieldExpression.
                    // For:
                    //   lamba x: (yield x)
                    // it comes back with:
                    //
                    //IronPython.Compiler.Ast.LambdaExpression
                    //IronPython.Compiler.Ast.FunctionDefinition<lambda$334>generator
                    //  IronPython.Compiler.Ast.Parameter  x
                    //  IronPython.Compiler.Ast.ExpressionStatement
                    //    IronPython.Compiler.Ast.YieldExpression     <<<<<<<<
                    //      IronPython.Compiler.Ast.YieldExpression   <<<<<<<< why twice?
                    //        IronPython.Compiler.Ast.NameExpression x

                    _body = ((Yield)((Expr)statement).value).value;
                }  else
                    throw PythonOps.TypeError("Unexpected statement type: {0}, expected Return or Expr", statement.GetType());
            }
Exemple #10
0
 private static ISet<Namespace> MakeLambdaFunction(LambdaExpression node, ExpressionEvaluator ee)
 {
     return ee.GlobalScope.NodeVariables[node.Function];
 }
Exemple #11
0
 // LambdaExpr
 public override bool Walk(LambdaExpression node) { return false; }
		public override bool Walk(LambdaExpression node)
		{
			return base.Walk(node);

		}
 public virtual void PostWalk(LambdaExpression node)
 {
 }
 // LambdaExpression
 public virtual bool Walk(LambdaExpression node)
 {
     return true;
 }
Exemple #15
0
        private Expression FinishOldLambdef()
        {
            Location start = GetStart();
            Expression[] parameters, defaults;
            FunctionAttributes flags;
            ParseVarArgsList(out parameters, out defaults, out flags, TokenKind.Colon);
            Location mid = GetEnd();

            Expression expr = ParseOldTest();
            Statement body = new ReturnStatement(expr);
            body.SetLoc(GetExternal(), expr.Start, expr.End);
            FunctionDefinition func = new FunctionDefinition(SymbolTable.StringToId("<lambda$" + (oldLambdaCount++) + ">"), parameters, defaults, flags, body, context.SourceFile);
            func.SetLoc(GetExternal(), start, GetEnd());
            func.Header = mid;
            LambdaExpression ret = new LambdaExpression(func);
            ret.SetLoc(GetExternal(), start, GetEnd());
            return ret;
        }