Example #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 Stmt.Block(new List <Stmt>(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 List <Stmt>(new[] { initializer, body }));
            }

            return(body);
        }
Example #2
0
 public string VisitLiteral(Expr.Literal expr)
 {
     if (expr.Value == null)
     {
         return("nil");
     }
     if (expr.Value is string)
     {
         return(string.Format("\"{0}\"", expr.Value));
     }
     return(expr.Value.ToString());
 }
Example #3
0
        // varDecl :: "var" IDENTIFIER ( "=" expression )? ";" ;
        private Stmt VarDeclaration()
        {
            var name = Consume(TokenType.Identifier, "Expect variable name");

            Expr init = new Expr.Literal(null);

            if (MatchAny(TokenType.Equal))
            {
                init = Expression();
            }

            Consume(TokenType.Semicolon, "Expect ';' after variable declaration");
            return(new Stmt.Var(name, init));
        }
Example #4
0
 public object VisitLiteralExpr(Expr.Literal expr)
 {
     return(expr.Value);
 }
Example #5
0
        // forStmt :: "for" "(" ( varDecl | exprStmt | "; ")
        //                      expression? ";"
        //                      expression? ")" statement ;
        private Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expect '(' after 'for'");

            // Get the initializer.
            Stmt init;

            if (MatchAny(TokenType.Semicolon))
            {
                init = null;
            }
            else if (MatchAny(TokenType.Var))
            {
                init = VarDeclaration();
            }
            else
            {
                init = ExpressionStatement();
            }

            // Get the condition.
            Expr condition = null;

            if (!Check(TokenType.Semicolon))
            {
                condition = Expression();
            }

            Consume(TokenType.Semicolon, "Expect ';' after loop condition");

            // Get the increment.
            Expr increment = null;

            if (!Check(TokenType.RightParen))
            {
                increment = Expression();
            }

            Consume(TokenType.RightParen, "Expect ')' after for clauses");

            var body = Statement();

            // Desugar by creating a while loop instead.
            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 (init != null)
            {
                body = new Stmt.Block(
                    new List <Stmt> {
                    init,
                    body
                });
            }

            return(body);
        }
Example #6
0
 object Expr.IVisitor <object> .VisitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value);
 }
Example #7
0
 public object VisitLiteralExpr(Expr.Literal expr)
 {
     return(null);
 }
Example #8
0
 public Unit VisitLiteral(Expr.Literal expr)
 {
     return(Unit.Default);
 }
Example #9
0
 public Void Visit(Expr.Literal expr)
 {
     return(Void.Instance);
 }
Example #10
0
 object Expr.IVisitor <object> .VisitLiteralExpr(Expr.Literal expr)
 {
     return(null);
 }