jitcraft/Parser.cpp
changeset 9 73781c5a59d7
parent 8 069d207a8f2a
child 10 0c3bcd50e361
     1.1 --- a/jitcraft/Parser.cpp	Thu Jun 28 00:26:55 2012 -0700
     1.2 +++ b/jitcraft/Parser.cpp	Thu Jun 28 00:54:45 2012 -0700
     1.3 @@ -347,6 +347,49 @@
     1.4  }
     1.5  
     1.6  Statement *
     1.7 +Parser::while_()
     1.8 +{
     1.9 +    scanner_.scan();
    1.10 +
    1.11 +    if (!expect(TOK_LPAREN))
    1.12 +        return NULL;
    1.13 +    Expression *condition = expression();
    1.14 +    if (!condition)
    1.15 +        return NULL;
    1.16 +    if (!expect(TOK_RPAREN))
    1.17 +        return NULL;
    1.18 +
    1.19 +    Statement *body = localStatement();
    1.20 +    if (!body)
    1.21 +        return NULL;
    1.22 +
    1.23 +    return new (pool_) WhileStatement(TOK_WHILE, condition, body);
    1.24 +}
    1.25 +
    1.26 +Statement *
    1.27 +Parser::do_()
    1.28 +{
    1.29 +    scanner_.scan();
    1.30 +
    1.31 +    Statement *body = block();
    1.32 +    if (!body)
    1.33 +        return NULL;
    1.34 +
    1.35 +    if (!expect(TOK_WHILE))
    1.36 +        return NULL;
    1.37 +
    1.38 +    if (!expect(TOK_LPAREN))
    1.39 +        return NULL;
    1.40 +    Expression *condition = expression();
    1.41 +    if (!condition)
    1.42 +        return NULL;
    1.43 +    if (!expect(TOK_RPAREN))
    1.44 +        return NULL;
    1.45 +
    1.46 +    return new (pool_) WhileStatement(TOK_DO, condition, body);
    1.47 +}
    1.48 +
    1.49 +Statement *
    1.50  Parser::forStatement()
    1.51  {
    1.52      scanner_.scan();
    1.53 @@ -567,6 +610,14 @@
    1.54          case TOK_FOR:
    1.55            return forStatement();
    1.56  
    1.57 +        case TOK_WHILE:
    1.58 +          return while_();
    1.59 +
    1.60 +        case TOK_DO:
    1.61 +          if ((statement = do_()) == NULL)
    1.62 +              return NULL;
    1.63 +          break;
    1.64 +
    1.65          case TOK_RETURN:
    1.66            statement = returnStatement();
    1.67            break;
    1.68 @@ -888,6 +939,20 @@
    1.69          }
    1.70          unindent();
    1.71      }
    1.72 +    void visit(WhileStatement *node) {
    1.73 +        prefix();
    1.74 +        fprintf(fp_, "[ WhileStatement (%s)\n",
    1.75 +                (node->token() == TOK_DO) ? "do" : "while");
    1.76 +        indent();
    1.77 +        if (node->token() == TOK_DO) {
    1.78 +            node->condition()->accept(this);
    1.79 +            node->body()->accept(this);
    1.80 +        } else {
    1.81 +            node->condition()->accept(this);
    1.82 +            node->body()->accept(this);
    1.83 +        }
    1.84 +        unindent();
    1.85 +    }
    1.86  };
    1.87  
    1.88  void