internal static PythonAst ConvertToPythonAst(CodeContext codeContext, AST source ) { Statement stmt; PythonCompilerOptions options = new PythonCompilerOptions(ModuleOptions.ExecOrEvalCode); SourceUnit unit = new SourceUnit(codeContext.LanguageContext, NullTextContentProvider.Null, "", SourceCodeKind.AutoDetect); CompilerContext compilerContext = new CompilerContext(unit, options, ErrorSink.Default); bool printExpression = false; if (source is Expression) { Expression exp = (Expression)source; stmt = new ReturnStatement(expr.Revert(exp.body)); } else if (source is Module) { Module module = (Module)source; stmt = _ast.stmt.RevertStmts(module.body); } else if (source is Interactive) { Interactive interactive = (Interactive)source; stmt = _ast.stmt.RevertStmts(interactive.body); printExpression = true; } else throw PythonOps.TypeError("unsupported type of AST: {0}",(source.GetType())); return new PythonAst(stmt, false, ModuleOptions.ExecOrEvalCode, printExpression, compilerContext, new int[] {} ); }
public override bool Walk(ReturnStatement node) { CommonWalk(node); return true; }
private Statement ParseReturnStmt() { if (CurrentFunction == null) { ReportSyntaxError(IronPython.Resources.MisplacedReturn); } NextToken(); Expression expr = null; SourceLocation start = GetStart(); if (!NeverTestToken(PeekToken())) { expr = ParseTestListAsExpr(true); } ReturnStatement ret = new ReturnStatement(expr); ret.SetLoc(start, GetEnd()); return ret; }
// ReturnStatement public bool Walk(ReturnStatement node) { return Process(node); }
private Statement ParseReturnStmt() { if (CurrentFunction == null) { ReportSyntaxError("'return' outside function"); } NextToken(); Expression expr = null; Location start = GetStart(); if (!NeverTestToken(PeekToken())) { expr = ParseTestListAsExpr(true); } ReturnStatement ret = new ReturnStatement(expr); ret.SetLoc(GetExternal(), start, GetEnd()); return ret; }
private PythonAst ParseFileWorker(bool makeModule, bool returnValue) { _globalParent = new PythonAst(makeModule, _languageFeatures, false, _context); StartParsing(); List<Statement> l = new List<Statement>(); // // A future statement must appear near the top of the module. // The only lines that can appear before a future statement are: // - the module docstring (if any), // - comments, // - blank lines, and // - other future statements. // MaybeEatNewLine(); if (PeekToken(TokenKind.Constant)) { Statement s = ParseStmt(); l.Add(s); _fromFutureAllowed = false; ExpressionStatement es = s as ExpressionStatement; if (es != null) { ConstantExpression ce = es.Expression as ConstantExpression; if (ce != null && ce.Value is string) { // doc string _fromFutureAllowed = true; } } } MaybeEatNewLine(); // from __future__ if (_fromFutureAllowed) { while (PeekToken(Tokens.KeywordFromToken)) { Statement s = ParseStmt(); l.Add(s); FromImportStatement fis = s as FromImportStatement; if (fis != null && !fis.IsFromFuture) { // end of from __future__ break; } } } // the end of from __future__ sequence _fromFutureAllowed = false; while (true) { if (MaybeEat(TokenKind.EndOfFile)) break; if (MaybeEatNewLine()) continue; Statement s = ParseStmt(); l.Add(s); } Statement[] stmts = l.ToArray(); if (returnValue && stmts.Length > 0) { ExpressionStatement exprStmt = stmts[stmts.Length - 1] as ExpressionStatement; if (exprStmt != null) { var retStmt = new ReturnStatement(exprStmt.Expression); stmts[stmts.Length - 1] = retStmt; retStmt.SetLoc(_globalParent, exprStmt.Expression.IndexSpan); } } SuiteStatement ret = new SuiteStatement(stmts); ret.SetLoc(_globalParent, 0, GetEnd()); return FinishParsing(ret); }
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; }
public Return(ReturnStatement statement) : this() { // statement.Expression is never null //or is it? if (statement.Expression == null) _value = null; else _value = Convert(statement.Expression); }
public static string Format(ReturnStatement node) { return("return " + Format(node.Expression)); }
public override void PostWalk(ReturnStatement node) { }
public override bool Walk(ReturnStatement node) { Emit(node); return false; }
public override bool Walk(ReturnStatement node) { FoundReturnStatement = true; return false; }
public virtual void PostWalk(ReturnStatement node) { }
// ReturnStatement public virtual bool Walk(ReturnStatement node) { return true; }
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; }
public override void PostWalk(ReturnStatement node) { var tmp = tree.Pop(); var parent = tree.Peek() as FunctionDefinition; tree.Push(tmp); string ind = parent == null || !parent.IsLambda ? String.Empty : Indent(); string terminator = parent == null || !parent.IsLambda ? String.Empty : ";"; if (node.Expression != null) { Content("{0}return {1}{2}", ind, Content(), terminator); } else { Content("return"); } CommonPostWalk(node); }
public override bool Walk(ReturnStatement node) { found = true; return true; }
public override void PostWalk(ReturnStatement node) { }
public PythonAst ParseTopExpression() { try { // TODO: move from source unit .TrimStart(' ', '\t') ReturnStatement ret = new ReturnStatement(ParseTestListAsExpression()); ret.SetLoc(SourceSpan.None); return new PythonAst(ret, false, _languageFeatures, false); } catch (BadSourceException bse) { throw BadSourceError(bse); } }
internal override AstExpression Revert() { Statement newBody; AstExpression exp = expr.Revert(body); if (!_containsYield) newBody = new ReturnStatement(exp); else newBody = new ExpressionStatement(exp); Parameter[] para = args.Revert(); FunctionDefinition fd = new FunctionDefinition(null, para, newBody); fd.IsGenerator = _containsYield; _containsYield = false; return new LambdaExpression(fd); }
public PythonAst ParseTopExpression() { try { // TODO: move from source unit .TrimStart(' ', '\t') _globalParent = new PythonAst(false, _languageFeatures, false, _context); ReturnStatement ret = new ReturnStatement(ParseTestListAsExpression()); ret.SetLoc(_globalParent, 0, 0); return FinishParsing(ret); } catch (BadSourceException bse) { throw BadSourceError(bse); } }
public void PostWalk(ReturnStatement node) { PostProcess(node); }
private Statement ParseReturnStmt() { if (CurrentFunction == null) { ReportSyntaxError(IronPython.Resources.MisplacedReturn); } NextToken(); Expression expr = null; var start = GetStart(); if (!NeverTestToken(PeekToken())) { expr = ParseTestListAsExpr(); } if (expr != null) { _returnWithValue = true; if (_isGenerator) { ReportSyntaxError("'return' with argument inside generator"); } } ReturnStatement ret = new ReturnStatement(expr); ret.SetLoc(_globalParent, start, GetEnd()); return ret; }
public void Visit(PyAst.ReturnStatement node) { AppendLineWithIndentation("return" + (node.Expression is null ? "" : " " + Visit(node.Expression))); }