Exemple #1
0
 public object visitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value);
 }
Exemple #2
0
 public object VisitLiteralExpr(Expr.Literal expr)
 => expr.Value;
Exemple #3
0
 object Expr.IVisitor <object> .Visit(Expr.Literal _literal)
 {
     return(_literal.value);
 }
Exemple #4
0
 public string visitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value == null ? "nil" : expr.value.ToString());
 }
Exemple #5
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 Stmt.Block(new List <Stmt>
                {
                    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 List <Stmt> {
                    initializer, body
                });
            }

            return(body);
        }
        private Stmt ForStatement()
        {
            Consume(LEFT_PAREN, "Expect '(' after 'for'.");

            // For loop doesn't get its own statement.
            // Instead we desugar and reduce it to a while loop.

            Stmt initializer;

            if (Match(SEMICOLON))
            {
                initializer = null;
            }
            else if (Match(VAR))
            {
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!IsCurrentTokenType(SEMICOLON))
            {
                condition = Expression();
            }
            Consume(SEMICOLON, "Expect ';' after loop condition.");

            Expr increment = null;

            if (!IsCurrentTokenType(RIGHT_PAREN))
            {
                increment = Expression();
            }
            Consume(RIGHT_PAREN, "Expect ')' after for clauses.");

            // Parse the statement body of the for loop.
            Stmt body = Statement();

            if (increment != null)
            {
                // Add increment to be executed after the main body.
                body = new Stmt.Block(new List <Stmt>
                {
                    body,
                    new Stmt.Expression(increment)
                });
            }

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

            body = new Stmt.While(condition, body);
            if (initializer != null)
            {
                // Run the initializer once before the while loop.
                body = new Stmt.Block(new List <Stmt> {
                    initializer, body
                });
            }

            return(body);
        }
Exemple #7
0
 public object visitLiteralExpr(Expr.Literal literal)
 {
     return(null);
 }
Exemple #8
0
 public object VisitLiteralExpr(Expr.Literal expr)
 {
     return(null);
 }
Exemple #9
0
 public Void VisitLiteralExpr(Expr.Literal expr) => null;
Exemple #10
0
 public string VisitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value.ToString());
 }
Exemple #11
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 List <Stmt>()
                {
                    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 List <Stmt>()
                {
                    initializer, body
                });
            }

            return(body);
        }
Exemple #12
0
 public object Visit(Expr.Literal _literal)
 {
     return(null);
 }