コード例 #1
0
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expect '(' after 'for'.");

            Stmt initializer;

            if (Match(SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(VAR))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!Check(SEMICOLON))
            {
                condition = Expression();
            }
            Consume(SEMICOLON, "Expect ';' after loop condition.");

            Expr increment = null;

            if (!Check(RIGHT_PAREN))
            {
                increment = Expression();
            }
            Consume(RIGHT_PAREN, "Expect ')' after for clauses.");

            var body = Statement();

            if (increment != null)
            {
                body = new Stmt.BlockStmt(new List <Stmt> {
                    body, new Stmt.ExpressionStmt(increment)
                });
            }

            condition ??= new Expr.LiteralExpr(true);
            body = new Stmt.WhileStmt(condition, body);

            if (initializer != null)
            {
                body = new Stmt.BlockStmt(new List <Stmt> {
                    initializer, body
                });
            }

            return(body);
        }
コード例 #2
0
ファイル: AstPrinter.cs プロジェクト: mhorskaya/cslox
 public string VisitWhileStmt(Stmt.WhileStmt stmt)
 {
     return(Parenthesize2("while", stmt.Condition, stmt.Body));
 }
コード例 #3
0
ファイル: Resolver.cs プロジェクト: mhorskaya/cslox
 public object VisitWhileStmt(Stmt.WhileStmt stmt)
 {
     Resolve(stmt.Condition);
     Resolve(stmt.Body);
     return(null);
 }