Exemplo n.º 1
0
 public object Visit(LiteralExpr expr)
 {
     return(expr.Value);
 }
Exemplo n.º 2
0
        Stmt ForStatement()
        {
            Consume(TokenType.LeftParen, "Expected opening '('.");

            Stmt initializer = null;

            if (MatchNext(TokenType.Var))
            {
                initializer = VarDeclaration();
            }
            else if (MatchNext(TokenType.SemiColon))
            {
                initializer = null;
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr conditional = null;

            if (!MatchNext(TokenType.SemiColon))
            {
                conditional = Expression();
                Consume(TokenType.SemiColon, "Expecting ';' in for loop");
            }


            Expr incrementer = null;

            if (!MatchNext(TokenType.RightParen))
            {
                incrementer = Expression();
                Consume(TokenType.RightParen, "Expected closing ')' in for loop");
            }

            var body = Statement();

            if (incrementer != null)
            {
                body = new BlockStmt(new List <Stmt> {
                    body,
                    new ExpressionStmt(incrementer)
                });
            }

            if (conditional == null)
            {
                conditional = new LiteralExpr(true);
            }
            body = new WhileStmt(conditional, body);

            if (initializer != null)
            {
                body = new BlockStmt(new List <Stmt> {
                    initializer,
                    body
                });
            }

            return(body);
        }