diff -r 069d207a8f2a -r 73781c5a59d7 jitcraft/Parser.cpp --- a/jitcraft/Parser.cpp Thu Jun 28 00:26:55 2012 -0700 +++ b/jitcraft/Parser.cpp Thu Jun 28 00:54:45 2012 -0700 @@ -347,6 +347,49 @@ } Statement * +Parser::while_() +{ + scanner_.scan(); + + if (!expect(TOK_LPAREN)) + return NULL; + Expression *condition = expression(); + if (!condition) + return NULL; + if (!expect(TOK_RPAREN)) + return NULL; + + Statement *body = localStatement(); + if (!body) + return NULL; + + return new (pool_) WhileStatement(TOK_WHILE, condition, body); +} + +Statement * +Parser::do_() +{ + scanner_.scan(); + + Statement *body = block(); + if (!body) + return NULL; + + if (!expect(TOK_WHILE)) + return NULL; + + if (!expect(TOK_LPAREN)) + return NULL; + Expression *condition = expression(); + if (!condition) + return NULL; + if (!expect(TOK_RPAREN)) + return NULL; + + return new (pool_) WhileStatement(TOK_DO, condition, body); +} + +Statement * Parser::forStatement() { scanner_.scan(); @@ -567,6 +610,14 @@ case TOK_FOR: return forStatement(); + case TOK_WHILE: + return while_(); + + case TOK_DO: + if ((statement = do_()) == NULL) + return NULL; + break; + case TOK_RETURN: statement = returnStatement(); break; @@ -888,6 +939,20 @@ } unindent(); } + void visit(WhileStatement *node) { + prefix(); + fprintf(fp_, "[ WhileStatement (%s)\n", + (node->token() == TOK_DO) ? "do" : "while"); + indent(); + if (node->token() == TOK_DO) { + node->condition()->accept(this); + node->body()->accept(this); + } else { + node->condition()->accept(this); + node->body()->accept(this); + } + unindent(); + } }; void