static SourceLocation GetBodyEndLocation(Statement body)
		{
			if (body.Parent != null) {
				return body.End;
			}
			return SourceLocation.Invalid;
		}
예제 #2
0
        public PythonAst(Statement body, bool isModule, PythonLanguageFeatures languageFeatures, bool printExpressions) {
            ContractUtils.RequiresNotNull(body, "body");

            _body = body;
            _isModule = isModule;
            _printExpressions = printExpressions;
            _languageFeatures = languageFeatures;
        }
예제 #3
0
        public void Visit(PyAst.Statement node)
        {
            switch (node)
            {
            case PyAst.AssertStatement n: Visit(n); return;

            case PyAst.AssignmentStatement n: Visit(n); return;

            case PyAst.AugmentedAssignStatement n: Visit(n); return;

            case PyAst.BreakStatement n: Visit(n); return;

            case PyAst.ClassDefinition n: Visit(n); return;

            case PyAst.ContinueStatement n: Visit(n); return;

            case PyAst.DelStatement n: Visit(n); return;

            case PyAst.EmptyStatement n: Visit(n); return;

            case PyAst.ExecStatement n: Visit(n); return;

            case PyAst.ExpressionStatement n: Visit(n); return;

            case PyAst.ForStatement n: Visit(n); return;

            case PyAst.FromImportStatement n: Visit(n); return;

            case PyAst.FunctionDefinition n: Visit(n); return;

            case PyAst.GlobalStatement n: Visit(n); return;

            case PyAst.IfStatement n: Visit(n); return;

            case PyAst.ImportStatement n: Visit(n); return;

            case PyAst.PrintStatement n: Visit(n); return;

            //case PyAst.PythonAst n: Visit(n); return;
            case PyAst.RaiseStatement n: Visit(n); return;

            case PyAst.ReturnStatement n: Visit(n); return;

            case PyAst.SuiteStatement n: Visit(n); return;

            case PyAst.TryStatement n: Visit(n); return;

            case PyAst.WhileStatement n: Visit(n); return;

            case PyAst.WithStatement n: Visit(n); return;

            default:
                throw new NotImplementedException($"Printing of statement {node} not implemented");
            }
        }
예제 #4
0
        internal static IEnumerable<IScopeNode> EnumerateBody(Statement body)
        {
            SuiteStatement suite = body as SuiteStatement;
            if (suite != null) {
                foreach (Statement stmt in suite.Statements) {
                    ClassDefinition klass = stmt as ClassDefinition;
                    if (klass != null) {
                        yield return new ClassScopeNode(klass);
                    }

                    FunctionDefinition func = stmt as FunctionDefinition;
                    if (func != null) {
                        yield return new FunctionScopeNode(func);
                    }
                }
            }
        }
예제 #5
0
 public IfStatement(IfStatementTest[] tests, Statement else_) {
     _tests = tests;
     _else = else_;
 }
예제 #6
0
 public IfStatement(IfStatementTest[] tests, Statement else_)
 {
     this.tests = tests; this.elseStmt = else_;
 }
예제 #7
0
        private List<YieldTarget> yieldTargets; // = new List<YieldTarget>();

        #endregion Fields

        #region Constructors

        public WithStatement(Expression contextManager, Expression var, Statement body)
        {
            this.contextManager = contextManager;
            this.var = var;
            this.body = body;
        }
예제 #8
0
 public TryStatementHandler(Expression test, Expression target, Statement body)
 {
     this.test = test; this.target = target; this.body = body;
 }
예제 #9
0
 public TryStatement(Statement body, TryStatementHandler[] handlers, Statement elseSuite, Statement finallySuite)
 {
     this.body = body;
     this.handlers = handlers;
     this.elseStmt = elseSuite;
     this.finallyStmt = finallySuite;
 }
예제 #10
0
        private PythonAst FinishParsing(Statement ret) {
            var res = _globalParent;
            _globalParent = null;
            var lineLocs = _tokenizer.GetLineLocations();
            // update line mapping
            if (_sourceUnit.HasLineMapping) {
                List<int> newLineMapping = new List<int>();
                int last = 0;
                for (int i = 0; i < lineLocs.Length; i++) {
                    while (newLineMapping.Count < i) {
                        newLineMapping.Add(last);
                    }
                    last = lineLocs[i] + 1;
                    newLineMapping.Add(lineLocs[i]);
                }

                lineLocs = newLineMapping.ToArray();
            }
            res.ParsingFinished(lineLocs, ret, _languageFeatures);

            return res;
        }
예제 #11
0
 public FunctionDefinition(string name, Parameter[] parameters, Statement body, SourceUnit sourceUnit)
     : this(name, parameters, body)
 {
 }
예제 #12
0
 public WhileStatement(Expression test, Statement body, Statement else_)
 {
     _test = test;
     _body = body;
     _else = else_;
 }
예제 #13
0
 public IfStatement(IfStatementTest[] tests, Statement else_)
 {
     _tests = tests;
     _else  = else_;
 }
예제 #14
0
 public WithStatement(Expression contextManager, Expression var, Statement body)
 {
     _contextManager = contextManager;
     _var            = var;
     _body           = body;
 }
예제 #15
0
 internal static PythonList ConvertStatements(Statement stmt) {
     return ConvertStatements(stmt, false);
 }
예제 #16
0
            internal static stmt Convert(Statement stmt) {
                stmt ast;

                if (stmt is FunctionDefinition)
                    ast = new FunctionDef((FunctionDefinition)stmt);
                else if (stmt is ReturnStatement)
                    ast = new Return((ReturnStatement)stmt);
                else if (stmt is AssignmentStatement)
                    ast = new Assign((AssignmentStatement)stmt);
                else if (stmt is AugmentedAssignStatement)
                    ast = new AugAssign((AugmentedAssignStatement)stmt);
                else if (stmt is DelStatement)
                    ast = new Delete((DelStatement)stmt);
                else if (stmt is PrintStatement)
                    ast = new Print((PrintStatement)stmt);
                else if (stmt is ExpressionStatement)
                    ast = new Expr((ExpressionStatement)stmt);
                else if (stmt is ForStatement)
                    ast = new For((ForStatement)stmt);
                else if (stmt is WhileStatement)
                    ast = new While((WhileStatement)stmt);
                else if (stmt is IfStatement)
                    ast = new If((IfStatement)stmt);
                else if (stmt is WithStatement)
                    ast = new With((WithStatement)stmt);
                else if (stmt is RaiseStatement)
                    ast = new Raise((RaiseStatement)stmt);
                else if (stmt is TryStatement)
                    ast = Convert((TryStatement)stmt);
                else if (stmt is AssertStatement)
                    ast = new Assert((AssertStatement)stmt);
                else if (stmt is ImportStatement)
                    ast = new Import((ImportStatement)stmt);
                else if (stmt is FromImportStatement)
                    ast = new ImportFrom((FromImportStatement)stmt);
                else if (stmt is ExecStatement)
                    ast = new Exec((ExecStatement)stmt);
                else if (stmt is GlobalStatement)
                    ast = new Global((GlobalStatement)stmt);
                else if (stmt is ClassDefinition)
                    ast = new ClassDef((ClassDefinition)stmt);
                else if (stmt is BreakStatement)
                    ast = new Break();
                else if (stmt is ContinueStatement)
                    ast = new Continue();
                else if (stmt is EmptyStatement)
                    ast = new Pass();
                else
                    throw new ArgumentTypeException("Unexpected statement type: " + stmt.GetType());

                ast.GetSourceLocation(stmt);
                return ast;
            }
예제 #17
0
        internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.ParameterExpression enumerator,
                                                      Expression list, Expression left, MSAst.Expression body,
                                                      Statement else_, SourceSpan span, SourceLocation header,
                                                      MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel, bool isStatement)
        {
            // enumerator, isDisposable = Dynamic(GetEnumeratorBinder, list)
            MSAst.Expression init = Ast.Assign(
                enumerator,
                new PythonDynamicExpression1 <KeyValuePair <IEnumerator, IDisposable> >(
                    Binders.UnaryOperationBinder(
                        parent.GlobalParent.PyContext,
                        PythonOperationKind.GetEnumeratorForIteration
                        ),
                    parent.GlobalParent.CompilationMode,
                    AstUtils.Convert(list, typeof(object))
                    )
                );

            // while enumerator.MoveNext():
            //    left = enumerator.Current
            //    body
            // else:
            //    else
            MSAst.Expression ls = AstUtils.Loop(
                parent.GlobalParent.AddDebugInfo(
                    Ast.Call(
                        Ast.Property(
                            enumerator,
                            typeof(KeyValuePair <IEnumerator, IDisposable>).GetProperty("Key")
                            ),
                        typeof(IEnumerator).GetMethod("MoveNext")
                        ),
                    left.Span
                    ),
                null,
                Ast.Block(
                    left.TransformSet(
                        SourceSpan.None,
                        Ast.Call(
                            Ast.Property(
                                enumerator,
                                typeof(KeyValuePair <IEnumerator, IDisposable>).GetProperty("Key")
                                ),
                            typeof(IEnumerator).GetProperty("Current").GetGetMethod()
                            ),
                        PythonOperationKind.None
                        ),
                    body,
                    isStatement ? UpdateLineNumber(parent.GlobalParent.IndexToLocation(list.StartIndex).Line) : AstUtils.Empty(),
                    AstUtils.Empty()
                    ),
                else_,
                breakLabel,
                continueLabel
                );

            return(Ast.Block(
                       init,
                       Ast.TryFinally(
                           ls,
                           Ast.Call(AstMethods.ForLoopDispose, enumerator)
                           )
                       ));
        }
예제 #18
0
 private Statement FinishSmallStmt(Statement stmt) {
     NextToken();
     stmt.SetLoc(_globalParent, GetStart(), GetEnd());
     return stmt;
 }
예제 #19
0
 public TryStatementHandler(Expression test, Expression target, Statement body)
 {
     _test   = test;
     _target = target;
     _body   = body;
 }
예제 #20
0
 public SuiteStatement(Statement[] statements)
 {
     this.stmts = statements;
 }
예제 #21
0
 public TryStatement(Statement body, TryStatementHandler[] handlers, Statement else_, Statement finally_)
 {
     _body     = body;
     _handlers = handlers;
     _else     = else_;
     _finally  = finally_;
 }
예제 #22
0
            public static bool FindControlFlow(Statement statement, out bool foundLoopControl)
            {
                // No return in null statement
                if (statement == null) {
                    foundLoopControl = false;
                    return false;
                }

                // find it now.
                ControlFlowFinder rf = new ControlFlowFinder();
                statement.Walk(rf);
                foundLoopControl = rf.foundLoopControl;
                return rf.found;
            }
예제 #23
0
 public FunctionDefinition(SymbolId name, Parameter[] parameters, Statement body, SourceUnit sourceUnit) {
     _name = name;
     _parameters = parameters;
     _body = body;
     _sourceUnit = sourceUnit;
 }
예제 #24
0
 public WhileStatement(Expression test, Statement body, Statement else_)
 {
     this.test = test; this.body = body; this.elseStmt = else_;
 }
예제 #25
0
 public ClassDefinition(SymbolId name, Expression[] bases, Statement body) {
     _name = name;
     _bases = bases;
     _body = body;
 }
예제 #26
0
 public ForStatement(Expression lhs, Expression list, Statement body, Statement else_)
 {
     this.lhs = lhs; this.list = list;
     this.body = body; this.elseStmt = else_;
 }
예제 #27
0
파일: _ast.cs 프로젝트: TerabyteX/main
 internal static Statement RevertStmts(PythonList stmts)
 {
     if (stmts.Count == 1)
         return ((stmt)stmts[0]).Revert();
     Statement[] statements = new Statement[stmts.Count];
     for (int i = 0; i < stmts.Count; i++)
         statements[i] = ((stmt)stmts[i]).Revert();
     return new SuiteStatement(statements);
 }
예제 #28
0
 public IfStatementTest(Expression test, Statement body)
 {
     this.test = test; this.body = body;
 }
예제 #29
0
        private Module DoAnalyze(Modules modules, string name, Statement root)
        {
            GlobalSuite global = new GlobalSuite(root);
            module = new Module(modules, name, global, scopes);

            ModuleScope modsc;
            module.ModuleScope = modsc = new ModuleScope(module, null, global);

            PushScope(modsc);

            root.Walk(this);

            foreach (FieldAssignment fer in this.fields) {
                fer.Infer(module);
            }
            return module;
        }
예제 #30
0
 private static stmt ConvertToAST(Statement stmt) {
     ContractUtils.RequiresNotNull(stmt, "stmt");
     return AST.Convert(stmt);
 }
예제 #31
0
 public ForStatement(Expression left, Expression list, Statement body, Statement else_) {
     _left = left;
     _list = list;
     _body = body;
     _else = else_;
 }
예제 #32
0
            internal static PythonList ConvertStatements(Statement stmt, bool allowNull) {
                if (stmt == null)
                    if (allowNull)
                        return PythonOps.MakeEmptyList(0);
                    else
                        throw new ArgumentNullException("stmt");

                if (stmt is SuiteStatement) {
                    SuiteStatement suite = (SuiteStatement)stmt;
                    PythonList l = PythonOps.MakeEmptyList(suite.Statements.Count);
                    foreach (Statement s in suite.Statements)
                        l.Add(Convert(s));

                    return l;
                }

                return PythonOps.MakeListNoCopy(Convert(stmt));
            }
예제 #33
0
        internal static MSAst.Expression TransformForStatement(AstGenerator ag, MSAst.ParameterExpression enumerator,
                                                    Expression list, Expression left, MSAst.Expression body,
                                                    Statement else_, SourceSpan span, SourceLocation header,
                                                    MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel) {
            // enumerator = PythonOps.GetEnumeratorForIteration(list)
            MSAst.Expression init = Ast.Assign(
                    enumerator, 
                    ag.Operation(
                        typeof(IEnumerator),
                        PythonOperationKind.GetEnumeratorForIteration,
                        ag.TransformAsObject(list)
                    )
                );

            // while enumerator.MoveNext():
            //    left = enumerator.Current
            //    body
            // else:
            //    else
            MSAst.Expression ls = AstUtils.Loop(
                    ag.AddDebugInfo(Ast.Call(
                        enumerator,
                        typeof(IEnumerator).GetMethod("MoveNext")
                    ), left.Span),
                    null,
                    Ast.Block(
                        left.TransformSet(
                            ag,
                            SourceSpan.None,
                            Ast.Call(
                                enumerator,
                                typeof(IEnumerator).GetProperty("Current").GetGetMethod()
                            ),
                            PythonOperationKind.None
                        ),
                        body,
                        ag.UpdateLineNumber(list.Start.Line),
                        AstUtils.Empty()
                    ), 
                    ag.Transform(else_),
                    breakLabel, 
                    continueLabel
            );

            return Ast.Block(
                init,
                ls,
                AstUtils.Empty()
            );
        }
예제 #34
0
 private Statement ParseFinallySuite(Statement finallySuite) {
     MarkFunctionContainsFinally();
     bool inFinally = _inFinally, inFinallyLoop = _inFinallyLoop;
     try {
         _inFinally = true;
         _inFinallyLoop = false;
         finallySuite = ParseSuite();
     } finally {
         _inFinally = inFinally;
         _inFinallyLoop = inFinallyLoop;
     }
     return finallySuite;
 }
예제 #35
0
 public FunctionDefinition(SymbolId name, Expression[] parameters, Expression[] defaults, FunctionAttributes flags, Statement body, string sourceFile)
     : base(body)
 {
     this.name = name;
     this.parameters = parameters;
     this.defaults = defaults;
     this.flags = flags;
     this.decorators = null;
     this.filename = sourceFile;
 }
예제 #36
0
 private static Statement NestGenExpr(Statement current, Statement nested) {
     ForStatement fes = current as ForStatement;
     IfStatement ifs;
     if (fes != null) {
         fes.Body = nested;
     } else if ((ifs = current as IfStatement) != null) {
         ifs.Tests[0].Body = nested;
     }
     return nested;
 }
예제 #37
0
 public WhileStatement(Expression test, Statement body, Statement else_) {
     _test = test;
     _body = body;
     _else = else_;
 }
예제 #38
0
 public DropDownEntryInfo(Statement body)
 {
     Body = body;
 }
예제 #39
0
 public IfStatementTest(Expression test, Statement body)
 {
     _test = test;
     _body = body;
 }