Ejemplo n.º 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 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 Statements.Block(new List <Stmt>()
                {
                    body, new Statements.Expression(increment)
                });
            }

            if (condition == null)
            {
                condition = new Expressions.Literal(true);
            }

            body = new Statements.While(condition, body);

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

            return(body);
        }
Ejemplo n.º 2
0
 object Expressions.IVisitor <object> .VisitLiteralExpr(Expressions.Literal expr)
 {
     return(null);
 }