Esempio n. 1
0
        private Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expect '(' after 'for'");

            // initializer
            Stmt initializer = null;

            if (Match(TokenType.Semicolon))
            {
                initializer = null;
            }
            else if (Match(TokenType.Var))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            // condition
            Expr condition = null;

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

            // increment
            Expr increment = null;

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

            // body
            Stmt body = Statement();

            // desugar
            if (increment != null)
            {
                List <Stmt> statements = new List <Stmt>();
                statements.Add(body);
                statements.Add(new Stmt.Expression(increment));

                body = new Stmt.Block(statements);
            }

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

            if (initializer != null)
            {
                List <Stmt> statements = new List <Stmt>();
                statements.Add(initializer);
                statements.Add(body);

                body = new Stmt.Block(statements);
            }

            return(body);
        }
Esempio n. 2
0
 public void Visit(Stmt.While stmt)
 {
     Resolve(stmt.condition);
     Resolve(stmt.body);
 }