Exemplo n.º 1
0
 public If(Expr condition, Stmt then, Stmt el)
 {
     this.condition = condition;
     this.then      = then;
     this.el        = el;
 }
Exemplo n.º 2
0
 public While(Expr condition, Stmt body)
 {
     this.condition = condition;
     this.body      = body;
 }
Exemplo n.º 3
0
 private void Resolve(Stmt stmt)
 {
     stmt.Accept(this);
 }
Exemplo n.º 4
0
 public void Execute(Stmt statement)
 {
     statement.Accept(this);
 }
Exemplo n.º 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);
        }