예제 #1
0
        private Stmt forStatement()
        {
            consume(TokenTypes.LEFT_PAREN, "Expect '(' after 'for'.");

            Stmt initializer;

            if (match(TokenTypes.SEMICOLON))
            {
                initializer = null;
            }
            else if (match(TokenTypes.INT, TokenTypes.FLOAT, TokenTypes.VAR))
            {
                initializer = varDeclaration();
            }
            else
            {
                initializer = expressionStatement();
            }

            Expr condition = null;

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

            Expr increment = null;

            if (!check(TokenTypes.RIGHT_PAREN))
            {
                increment = expression();
            }
            consume(TokenTypes.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 (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
        // *** END EXPRESSION ***

        // *** STATEMENT ***
        // === Statement Visit Methods ===
        public object visitBlockStmt(Stmt.Block stmt)
        {
            executeBlock(stmt.statements, new Environment(environment));
            return(null);
        }