示例#1
0
 public Void Visit_WhileStmt(WhileStmt stmt)
 {
     Resolve(stmt.condition);
     Resolve(stmt.body);
     return(null);
 }
示例#2
0
文件: Parser.cs 项目: sirgru/CsLox
        private Stmt ForStatement()
        {
            Expect(TokenType.LeftParen, ErrorType.ExpectedOpenParen);

            Stmt initializer;

            if (CheckCurrentToken(TokenType.Semicolon))
            {
                initializer = null;
                Advance();
            }
            else if (CheckCurrentToken(TokenType.Var))
            {
                Advance();
                initializer = VarDeclaration();
            }
            else
            {
                initializer = ExpressionStatement();
            }

            Expr condition = null;

            if (!CheckCurrentToken(TokenType.Semicolon))
            {
                condition = Expression();
            }
            Expect(TokenType.Semicolon, ErrorType.ExpectedSemicolon);

            Expr increment = null;

            if (!CheckCurrentToken(TokenType.RightParen))
            {
                increment = Expression();
            }
            Expect(TokenType.RightParen, ErrorType.ExpectedClosedParen);

            Stmt body = Statement();

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

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

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

            return(body);
        }
示例#3
0
 public string Visit_WhileStmt(WhileStmt stmt)
 {
     return(Parenthesize("While", new ExpressionStmt(stmt.condition), stmt.body));
 }