Exemplo n.º 1
0
 public Void visitLiteralExpr(Expr.Literal expr)
 {
     return(null);
 }
Exemplo n.º 2
0
 public object visitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value);
 }
Exemplo n.º 3
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);
        }