Пример #1
0
        private Stmt forStatement()
        {
            consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'");
            Stmt initializer;

            if (match(TokenType.SEMICOLON))
            {
                initializer = null;
            }
            else if (match(TokenType.VAR))
            {
                initializer = varDeclaration();
            }
            else
            {
                initializer = expressionStatement();
            }

            Expr cond = null;

            if (!check(TokenType.SEMICOLON))
            {
                cond = expression();
            }
            consume(TokenType.SEMICOLON, "Expect ';' after loop condition");

            Expr increment = null;

            if (!check(TokenType.RIGHT_PAREN))
            {
                increment = expression();
            }
            consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses");
            Stmt body = statement();

            if (increment != null)
            {
                body = new Stmt.Block(
                    new List <Stmt> {
                    body,
                    new Stmt.Expression(increment)
                }
                    );
            }
            if (cond != null)
            {
                cond = new Expr.Literal(true);
            }

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

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

            return(body);
        }
Пример #2
0
 public object visitBlockStmt(Stmt.Block stmt)
 {
     beginScope();
     resolve(stmt.Statements);
     endScope();
     return(null);
 }
Пример #3
0
 public object visitBlockStmt(Stmt.Block stmt)
 {
     executeBlock(stmt.Statements, new Envir(env));
     return(null);
 }