예제 #1
0
 public If(Expr condition, Stmt then, Stmt el)
 {
     this.condition = condition;
     this.then      = then;
     this.el        = el;
 }
예제 #2
0
 public While(Expr condition, Stmt body)
 {
     this.condition = condition;
     this.body      = body;
 }
예제 #3
0
 private void Resolve(Stmt stmt)
 {
     stmt.Accept(this);
 }
예제 #4
0
 public void Execute(Stmt statement)
 {
     statement.Accept(this);
 }
예제 #5
0
        private Stmt ForStatement()
        {
            Consume(TokenType.LEFT_PAREN, "Expect '(' after for statement.");

            Stmt initializer;

            if (Match(TokenType.SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(TokenType.LET))
            {
                initializer = LetDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr cond = null;

            if (!Check(TokenType.SEMICOLON))
            {
                cond = Expression();
            }
            Consume(TokenType.SEMICOLON, "Expect ';' after condition in for statement");

            Expr increment = null;

            if (!Check(TokenType.RIGHT_PAREN))
            {
                increment = Expression();
            }

            Consume(TokenType.RIGHT_PAREN, "Expect ')' after for expressions.");

            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);
        }