Пример #1
0
        // forStmt :: "for" "(" ( varDecl | exprStmt | "; ")
        //                      expression? ";"
        //                      expression? ")" statement ;
        private Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expect '(' after 'for'");

            // Get the initializer.
            Stmt init;

            if (MatchAny(TokenType.Semicolon))
            {
                init = null;
            }
            else if (MatchAny(TokenType.Var))
            {
                init = VarDeclaration();
            }
            else
            {
                init = ExpressionStatement();
            }

            // Get the condition.
            Expr condition = null;

            if (!Check(TokenType.Semicolon))
            {
                condition = Expression();
            }

            Consume(TokenType.Semicolon, "Expect ';' after loop condition");

            // Get the increment.
            Expr increment = null;

            if (!Check(TokenType.RightParen))
            {
                increment = Expression();
            }

            Consume(TokenType.RightParen, "Expect ')' after for clauses");

            var body = Statement();

            // Desugar by creating a while loop instead.
            if (increment != null)
            {
                body = new Stmt.Block(
                    new List <Stmt> {
                    body,
                    new Stmt.Expression(increment)
                });
            }

            if (condition == null)
            {
                condition = new Expr.Literal(true);
            }

            body = new Stmt.While(condition, body);

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

            return(body);
        }
Пример #2
0
 public Unit VisitWhileStmt(Stmt.While stmt)
 {
     Resolve(stmt.Condition);
     Resolve(stmt.Body);
     return(Unit.Default);
 }
Пример #3
0
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expect '(' after 'while'");

            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.Block(new List <Stmt>
                {
                    body,
                    new Stmt.Expression(increment)
                });
            }

            if (condition == null)
            {
                condition = new Expr.Literal(true);
            }
            body = new Stmt.While(condition, body);

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

            return(body);
        }
Пример #4
0
 object Stmt.IVisitor <object> .VisitWhileStmt(Stmt.While stmt)
 {
     Resolve(stmt.condition);
     Resolve(stmt.body);
     return(null);
 }