示例#1
0
 internal While(Expr condition, Stmt body)
 {
     this.condition = condition;
     this.body      = body;
 }
示例#2
0
 private void execute(Stmt stmt)
 {
     stmt.accept(this);
 }
示例#3
0
 internal If(Expr condition, Stmt thenBranch, Stmt elseBranch)
 {
     this.condition  = condition;
     this.thenBranch = thenBranch;
     this.elseBranch = elseBranch;
 }
示例#4
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 condition = null;

            if (!check(TokenType.SEMICOLON))
            {
                condition = 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 (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);
        }