Esempio n. 1
0
        private Stmt forStatement()
        {
            consume(LEFT_PAREN, "Expect '(' after 'for'.");

            Stmt initializer;

            if (match(SEMICOLON))
            {
                initializer = null;
            }
            else if (match(VAR))
            {
                initializer = varDeclaration();
            }
            else
            {
                initializer = expressionStatement();
            }

            Expr condition = null;

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

            Expr increment = null;

            if (!check(RIGHT_PAREN))
            {
                increment = expression();
            }
            consume(RIGHT_PAREN, "Expect '(' after for clauses.");

            var body = statement();

            if (increment != null)
            {
                body = new Stmt.Block(new[] { 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[] { initializer, body });
            }

            return(body);
        }
Esempio n. 2
0
 public object visitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value);
 }
Esempio n. 3
0
 public object visitLiteralExpr(Expr.Literal expr) => null;