示例#1
0
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expected '(' 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, "Expected ';' after loop condition.");

            Expr increment = null;

            if (!Check(RIGHT_PAREN))
            {
                increment = Expression();
            }
            Consume(RIGHT_PAREN, "Expected ')' 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);
        }
示例#2
0
 public string VisitWhileStmt(Stmt.While stmt)
 {
     return(Parenthesize2("while", stmt.Condition, stmt.Body));
 }
示例#3
0
 public object VisitWhileStmt(Stmt.While stmt)
 {
     Resolve(stmt.Condition);
     Resolve(stmt.Body);
     return(null);
 }