// genexpr_for ::= "for" target_list "in" or_test [genexpr_iter] // genexpr_iter ::= (genexpr_for | genexpr_if) * // // "for" has NOT been eaten before entering this method private Expression ParseGeneratorExpression(Expression expr) { ForStatement root = ParseGenExprFor(); Statement current = root; for (; ; ) { if (PeekToken(Tokens.KeywordForToken)) { current = NestGenExpr(current, ParseGenExprFor()); } else if (PeekToken(Tokens.KeywordIfToken)) { current = NestGenExpr(current, ParseGenExprIf()); } else { // Generator Expressions have an implicit function definition and yield around their expression. // (x for i in R) // becomes: // def f(): // for i in R: yield (x) ExpressionStatement ys = new ExpressionStatement(new YieldExpression(expr)); ys.Expression.SetLoc(_globalParent, expr.IndexSpan); ys.SetLoc(_globalParent, expr.IndexSpan); NestGenExpr(current, ys); break; } } // We pass the outermost iterable in as a parameter because Python semantics // say that this one piece is computed at definition time rather than iteration time const string fname = "<genexpr>"; Parameter parameter = new Parameter("__gen_$_parm__", 0); FunctionDefinition func = new FunctionDefinition(fname, new Parameter[] { parameter }, root); func.IsGenerator = true; func.SetLoc(_globalParent, root.StartIndex, GetEnd()); func.HeaderIndex = root.EndIndex; // Transform the root "for" statement Expression outermost = root.List; NameExpression ne = new NameExpression("__gen_$_parm__"); ne.SetLoc(_globalParent, outermost.IndexSpan); root.List = ne; GeneratorExpression ret = new GeneratorExpression(func, outermost); ret.SetLoc(_globalParent, expr.StartIndex, GetEnd()); return ret; }
protected override MSAst.Expression VisitExtension(MSAst.Expression node) { if (node == _globalContext) { return(PythonAst._globalContext); } // we need to re-write nested scoeps ScopeStatement scope = node as ScopeStatement; if (scope != null) { return(base.VisitExtension(VisitScope(scope))); } LambdaExpression lambda = node as LambdaExpression; if (lambda != null) { return(base.VisitExtension(new LambdaExpression((FunctionDefinition)VisitScope(lambda.Function)))); } GeneratorExpression generator = node as GeneratorExpression; if (generator != null) { return(base.VisitExtension(new GeneratorExpression((FunctionDefinition)VisitScope(generator.Function), generator.Iterable))); } // update the global get/set/raw gets variables PythonGlobalVariableExpression global = node as PythonGlobalVariableExpression; if (global != null) { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, global.Variable.Name, global.Variable.Kind == VariableKind.Local )); } // set covers sets and deletes var setGlobal = node as PythonSetGlobalVariableExpression; if (setGlobal != null) { if (setGlobal.Value == PythonGlobalVariableExpression.Uninitialized) { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, setGlobal.Global.Variable.Name, setGlobal.Global.Variable.Kind == VariableKind.Local ).Delete()); } else { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, setGlobal.Global.Variable.Name, setGlobal.Global.Variable.Kind == VariableKind.Local ).Assign(Visit(setGlobal.Value))); } } var rawValue = node as PythonRawGlobalValueExpression; if (rawValue != null) { return(new LookupGlobalVariable( _curScope == null ? PythonAst._globalContext : _curScope.LocalContext, rawValue.Global.Variable.Name, rawValue.Global.Variable.Kind == VariableKind.Local )); } return(base.VisitExtension(node)); }
public override bool Walk(GeneratorExpression node) { node.Parent = _currentScope; return base.Walk(node); }
internal GeneratorExp(GeneratorExpression expr) : this() { ExtractListComprehensionIterators walker = new ExtractListComprehensionIterators(); expr.Function.Body.Walk(walker); ComprehensionIterator[] iters = walker.Iterators; Debug.Assert(iters.Length != 0, "A generator expression cannot have zero iterators."); iters[0] = new ComprehensionFor(((ComprehensionFor)iters[0]).Left, expr.Iterable); _elt = Convert(walker.Yield.Expression); _generators = Convert(iters); }
public override void PostWalk(GeneratorExpression node) { CommonPostWalk(node); }
// GeneratorExpression public override bool Walk(GeneratorExpression node) { node.Parent = _currentScope; return(base.Walk(node)); }
// GeneratorExpression public bool Walk(GeneratorExpression node) { return Process(node); }
public override bool Walk(GeneratorExpression node) { CommonWalk(node); return true; }
public void PostWalk(GeneratorExpression node) { PostProcess(node); }
public virtual void PostWalk(GeneratorExpression node) { }
// GeneratorExpression public virtual bool Walk(GeneratorExpression node) { return true; }
private Expression ParseGeneratorExpression(Expression test) { ForStatement root = ParseGenExprFor(); Statement current = root; for (; ; ) { if (PeekToken(Tokens.KeywordForToken)) { current = NestGenExpr(current, ParseGenExprFor()); } else if (PeekToken(Tokens.KeywordIfToken)) { current = NestGenExpr(current, ParseGenExprIf()); } else { YieldStatement ys = new YieldStatement(test, 0); ys.SetLoc(GetExternal(), test.Start, test.End); NestGenExpr(current, ys); break; } } SymbolId fname = SymbolTable.StringToId("__gen_" + System.Threading.Interlocked.Increment(ref genexp_counter)); NameExpression pname = new NameExpression(SymbolTable.GeneratorParmName); FunctionDefinition func = new FunctionDefinition(fname, new Expression[] { pname }, new Expression[] { }, FunctionAttributes.None, root, context.SourceFile); func.YieldCount = 1; func.SetLoc(GetExternal(), root.Start, GetEnd()); func.Header = root.End; // Transform the root "for" statement Expression outermost = root.List; root.List = pname; CallExpression gexp = FinishCallExpr(new NameExpression(fname), new Arg(outermost)); CallExpression iter = FinishCallExpr(new NameExpression(SymbolTable.Iter), new Arg(gexp)); GeneratorExpression ret = new GeneratorExpression(func, iter); ret.SetLoc(GetExternal(), root.Start, GetEnd()); return ret; }
public string Visit(PyAst.GeneratorExpression node) => throw CreateNotImplementedEx();