Пример #1
0
        // This returns a block with an initializer statement and a while statement, a
        // dedicated Stmt.For class is not needed.
        Stmt ParseForStatement()
        {
            consume(TokenType.LEFT_PAREN, "Expect '(' before for statement condition.");
            Stmt initializer = null;

            if (match(TokenType.VAR))
            {
                initializer = ParseVarDeclaration();
            }
            else if (!match(TokenType.SEMICOLON))
            {
                initializer = ParseExpressionStatement();
            }
            Expr condition = null;

            if (!match(TokenType.SEMICOLON))
            {
                condition = ParseExpression();
                consume(TokenType.SEMICOLON, "Expect ';' after for statement condition.");
            }
            Expr increment = null;

            if (!match(TokenType.RIGHT_PAREN))
            {
                increment = ParseExpression();
                consume(TokenType.RIGHT_PAREN, "Expect ')' after for statement condition.");
            }

            Stmt body = ParseStatement();

            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);
        }
Пример #2
0
 public string visitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value == null ? "nil" : expr.value.ToString());
 }
Пример #3
0
 public object visitLiteralExpr(Expr.Literal expr)
 {
     return(null);
 }
Пример #4
0
 public object visitLiteralExpr(Expr.Literal expr)
 {
     return(expr.value);
 }