jitcraft/Parser.cpp
changeset 22 765ab1b672a3
parent 21 af5d8e985502
child 23 8a31742781c1
     1.1 --- a/jitcraft/Parser.cpp	Sun Jul 01 14:52:57 2012 -0700
     1.2 +++ b/jitcraft/Parser.cpp	Sun Jul 01 16:36:39 2012 -0700
     1.3 @@ -356,9 +356,27 @@
     1.4  }
     1.5  
     1.6  Expression *
     1.7 +Parser::assignment()
     1.8 +{
     1.9 +    Expression *left = equals();
    1.10 +    while (left) {
    1.11 +        TokenKind token = scanner_.peek();
    1.12 +        if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
    1.13 +            break;
    1.14 +        scanner_.scan();
    1.15 +        Expression *expr = equals();
    1.16 +        if (!expr)
    1.17 +            return NULL;
    1.18 +
    1.19 +        left = new (pool_) Assignment(token, left, expr);
    1.20 +    }
    1.21 +    return left;
    1.22 +}
    1.23 +
    1.24 +Expression *
    1.25  Parser::expression()
    1.26  {
    1.27 -    return equals();
    1.28 +    return assignment();
    1.29  }
    1.30  
    1.31  Statement *
    1.32 @@ -411,12 +429,17 @@
    1.33      if (!expect(TOK_LPAREN))
    1.34          return NULL;
    1.35  
    1.36 -    VariableDeclaration *decl = NULL;
    1.37 +    Statement *decl = NULL;
    1.38      if (!match(TOK_SEMICOLON)) {
    1.39 -        if (!enterScope(Scope::BLOCK))
    1.40 -            return NULL;
    1.41 -        if ((decl = variableDeclaration()) == NULL)
    1.42 -            return NULL;
    1.43 +        if (scanner_.peek() == TOK_NEW) {
    1.44 +            if (!enterScope(Scope::BLOCK))
    1.45 +                return NULL;
    1.46 +            if ((decl = variableDeclaration()) == NULL)
    1.47 +                return NULL;
    1.48 +        } else {
    1.49 +            if ((decl = expressionStatement()) == NULL)
    1.50 +                return NULL;
    1.51 +        }
    1.52          if (!expect(TOK_SEMICOLON))
    1.53              return NULL;
    1.54      }
    1.55 @@ -442,7 +465,7 @@
    1.56          return NULL;
    1.57  
    1.58      Scope *blockScope = NULL;
    1.59 -    if (decl) {
    1.60 +    if (decl && decl->isVariableDeclaration()) {
    1.61          blockScope = scope_->unlinkIfEmpty();
    1.62          scope_ = scope_->enclosing();
    1.63      }
    1.64 @@ -511,18 +534,10 @@
    1.65  Statement *
    1.66  Parser::expressionStatement()
    1.67  {
    1.68 -    Expression *left = primary();
    1.69 +    Expression *left = assignment();
    1.70      if (!left)
    1.71          return NULL;
    1.72  
    1.73 -    if (match(TOK_ASSIGN)) {
    1.74 -        Expression *expr = expression();
    1.75 -        if (!expr)
    1.76 -            return NULL;
    1.77 -
    1.78 -        left = new (pool_) Assignment(left, expr);
    1.79 -    }
    1.80 -
    1.81      return new (pool_) ExpressionStatement(left);
    1.82  }
    1.83  
    1.84 @@ -908,8 +923,8 @@
    1.85          prefix();
    1.86          fprintf(fp_, "[ ForStatement\n");
    1.87          indent();
    1.88 -        if (node->declaration())
    1.89 -            node->declaration()->accept(this);
    1.90 +        if (node->initialization())
    1.91 +            node->initialization()->accept(this);
    1.92          if (node->condition())
    1.93              node->condition()->accept(this);
    1.94          if (node->update())