src/Parser.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 01:30:33 2012 -0700 (2012-07-25)
changeset 92 e3974e7360e9
parent 86 32ad0523b309
child 117 2041ab5ec56c
permissions -rw-r--r--
Add GCC/Linux support.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <string.h>
    20 #include "Zone.h"
    21 #include "Parser.h"
    22 #include "FixedArray.h"
    23 #include "Heap.h"
    24 #include "Strings.h"
    25 #include "Heap-inl.h"
    26 
    27 using namespace ke;
    28 
    29 Parser::Parser(Zone *zone, CompileContext &cc, const char *stream, size_t length)
    30   : zone_(zone),
    31     cc_(cc),
    32     pool_(zone_->pool()),
    33     scanner_(cc, stream, length),
    34     scope_(NULL),
    35     unbound_(NULL)
    36 {
    37     unbound_ = new (pool_) PoolList<NameProxy *>();
    38 }
    39 
    40 bool
    41 Parser::match(TokenKind kind)
    42 {
    43     if (scanner_.peek() != kind)
    44         return false;
    45     scanner_.scan();
    46     return true;
    47 }
    48 
    49 bool
    50 Parser::matchSemicolon()
    51 {
    52     return expect(TOK_SEMICOLON);
    53 }
    54 
    55 bool
    56 Parser::expect(TokenKind kind)
    57 {
    58     TokenKind got = scanner_.scan();
    59     if (got == kind)
    60         return true;
    61     cc_.reportError(scanner_.begin(), Message_WrongToken, TokenNames[kind], TokenNames[got]);
    62     return false;
    63 }
    64 
    65 Scope *
    66 Parser::enterScope(Scope::Kind kind)
    67 {
    68     scope_ = new (pool_) Scope(pool_, kind, scope_);
    69     if (!scope_->initialize())
    70         return NULL;
    71     return scope_;
    72 }
    73 
    74 NameProxy *
    75 Parser::bind(Handle<String> name)
    76 {
    77     BoundName *bound = scope_->lookup(name);
    78     NameProxy *proxy = new (pool_) NameProxy(scanner_.begin(), name, bound);
    79     if (!bound && !unbound_->append(proxy))
    80         return NULL;
    81     return proxy;
    82 }
    83 
    84 bool
    85 Parser::matchName(String **name, TokenKind kind)
    86 {
    87     assert(kind == TOK_NAME || kind == TOK_LABEL);
    88     *name = NULL;
    89     if (!match(kind))
    90         return false;
    91     *name = zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
    92     return true;
    93 }
    94 
    95 String *
    96 Parser::expectName()
    97 {
    98     if (!expect(TOK_NAME))
    99         return NULL;
   100     return zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
   101 }
   102 
   103 void
   104 Parser::redeclarationError(BoundName *other)
   105 {
   106     cc_.reportError(scanner_.begin(), Message_RedeclaredName, other->name()->chars(),
   107                     other->pos().line, other->pos().col);
   108 }
   109 
   110 Expression *
   111 Parser::primitive()
   112 {
   113     TokenKind kind = scanner_.peek();
   114     switch (kind) {
   115       case TOK_NAME:
   116       {
   117         Local<String> name(zone_, expectName());
   118         if (!name)
   119             return NULL;
   120         return bind(name);
   121       }
   122 
   123       case TOK_FLOAT_LITERAL:
   124       {
   125         scanner_.scan();
   126         double value = Scanner::ParseDouble(scanner_.literal());
   127         return new (pool_) FloatLiteral(scanner_.begin(), value);
   128       }
   129 
   130       case TOK_INTEGER_LITERAL:
   131       {
   132         scanner_.scan();
   133         int value = atoi(scanner_.literal());
   134         return new (pool_) IntegerLiteral(scanner_.begin(), value);
   135       }
   136 
   137       case TOK_STRING_LITERAL:
   138       {
   139         scanner_.scan();
   140         Local<ByteArray> array(zone_, ByteArray::New(zone_, scanner_.literal_length() + 1));
   141         if (!array)
   142             return NULL;
   143         memcpy(array->raw(), scanner_.literal(), scanner_.literal_length() + 1);
   144         return new (pool_) StringLiteral(scanner_.begin(), array);
   145       }
   146 
   147       default:
   148       {
   149         if (kind != TOK_ERROR)
   150             cc_.reportError(scanner_.begin(), Message_ExpectedExpression, TokenNames[kind]);
   151         return NULL;
   152       }
   153     }
   154 }
   155 
   156 Expression *
   157 Parser::prefix()
   158 {
   159     switch (scanner_.peek()) {
   160       case TOK_LPAREN:
   161       {
   162         scanner_.scan();
   163         Expression *expr = expression();
   164         if (!expr)
   165             return NULL;
   166         if (!expect(TOK_RPAREN))
   167             return NULL;
   168         return expr;
   169       }
   170 
   171       default:
   172         return primitive();
   173     }
   174 }
   175 
   176 Expression *
   177 Parser::call(Expression *callee)
   178 {
   179     ExpressionList *arguments = new (pool_) ExpressionList();
   180 
   181     SourcePosition pos = scanner_.begin();
   182     expect(TOK_LPAREN);
   183     
   184     if (!match(TOK_RPAREN)) {
   185         while (true) {
   186             Expression *expr = expression();
   187             if (!expr)
   188                 return NULL;
   189 
   190             if (!arguments->append(expr))
   191                 return NULL;
   192 
   193             if (!match(TOK_COMMA))
   194                 break;
   195         }
   196 
   197         if (!expect(TOK_RPAREN))
   198             return NULL;
   199     }
   200 
   201     return new (pool_) CallExpression(pos, callee, arguments);
   202 }
   203 
   204 Expression *
   205 Parser::index(Expression *left)
   206 {
   207     expect(TOK_LBRACKET);
   208     
   209     SourcePosition pos = scanner_.begin();
   210     Expression *expr = expression();
   211     if (!expr)
   212         return NULL;
   213 
   214     if (!expect(TOK_RBRACKET))
   215         return NULL;
   216 
   217     return new (pool_) IndexExpression(pos, left, expr);
   218 }
   219 
   220 Expression *
   221 Parser::primary()
   222 {
   223     Expression *expr = prefix();
   224     if (!expr)
   225         return NULL;
   226 
   227     for (;;) {
   228         switch (scanner_.peek()) {
   229           case TOK_LPAREN:
   230             expr = call(expr);
   231             if (!expr)
   232                 return NULL;
   233             break;
   234 
   235           case TOK_LBRACKET:
   236             expr = index(expr);
   237             if (!expr)
   238                 return NULL;
   239             break;
   240 
   241           default:
   242             return expr;
   243         }
   244     }
   245 }
   246 
   247 Expression *
   248 Parser::unary()
   249 {
   250     TokenKind token = scanner_.peek();
   251 
   252     SourcePosition pos = scanner_.begin();
   253     switch (token) {
   254       case TOK_INCREMENT:
   255       case TOK_DECREMENT:
   256       {
   257         scanner_.scan();
   258         Expression *expr = expression();
   259         if (!expr)
   260             return NULL;
   261         return new (pool_) IncDecExpression(pos, token, expr, false);
   262       }
   263 
   264       case TOK_MINUS:
   265       case TOK_NOT:
   266       case TOK_TILDE:
   267       {
   268         scanner_.scan();
   269         Expression *expr = expression();
   270         if (!expr)
   271             return NULL;
   272         return new (pool_) UnaryExpression(pos, token, expr);
   273       }
   274 
   275       default:
   276         break;
   277     }
   278 
   279     Expression *expr = primary();
   280 
   281     token = scanner_.peek();
   282     if (token == TOK_INCREMENT || token == TOK_DECREMENT) {
   283         scanner_.scan();
   284         return new (pool_) IncDecExpression(pos, token, expr, true);
   285     }
   286 
   287     return expr;
   288 }
   289 
   290 Expression *
   291 Parser::multiplication()
   292 {
   293     Expression *left = unary();
   294     do {
   295         if (!match(TOK_SLASH) && !match(TOK_STAR) && !match(TOK_PERCENT))
   296             break;
   297         SourcePosition pos = scanner_.begin();
   298         TokenKind kind = scanner_.last();
   299         Expression *right = unary();
   300         if (!right)
   301             return NULL;
   302         left = new (pool_) BinaryExpression(pos, kind, left, right);
   303     } while (left);
   304     return left;
   305 }
   306 
   307 Expression *
   308 Parser::addition()
   309 {
   310     Expression *left = multiplication();
   311     while (left) {
   312         if (!match(TOK_PLUS) && !match(TOK_MINUS))
   313             break;
   314         SourcePosition pos = scanner_.begin();
   315         TokenKind kind = scanner_.last();
   316         Expression *right = multiplication();
   317         if (!right)
   318             return NULL;
   319         left = new (pool_) BinaryExpression(pos, kind, left, right);
   320     }
   321     return left;
   322 }
   323 
   324 Expression *
   325 Parser::shift()
   326 {
   327     Expression *left = addition();
   328     while (left) {
   329         if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
   330             break;
   331         SourcePosition pos = scanner_.begin();
   332         TokenKind kind = scanner_.last();
   333         Expression *right = addition();
   334         if (!right)
   335             return NULL;
   336         left = new (pool_) BinaryExpression(pos, kind, left, right);
   337     }
   338     return left;
   339 }
   340 
   341 Expression *
   342 Parser::bitand_()
   343 {
   344     Expression *left = shift();
   345     while (left) {
   346         if (!match(TOK_BITAND))
   347             break;
   348         SourcePosition pos = scanner_.begin();
   349         Expression *right = shift();
   350         if (!right)
   351             return NULL;
   352         left = new (pool_) BinaryExpression(pos, TOK_BITAND, left, right);
   353     }
   354     return left;
   355 }
   356 
   357 Expression *
   358 Parser::bitxor()
   359 {
   360     Expression *left = bitand_();
   361     while (left) {
   362         if (!match(TOK_BITXOR))
   363             break;
   364         SourcePosition pos = scanner_.begin();
   365         Expression *right = shift();
   366         if (!right)
   367             return NULL;
   368         left = new (pool_) BinaryExpression(pos, TOK_BITXOR, left, right);
   369     }
   370     return left;
   371 }
   372 
   373 Expression *
   374 Parser::bitor_()
   375 {
   376     Expression *left = bitxor();
   377     while (left) {
   378         if (!match(TOK_BITOR))
   379             break;
   380         SourcePosition pos = scanner_.begin();
   381         Expression *right = bitxor();
   382         if (!right)
   383             return NULL;
   384         left = new (pool_) BinaryExpression(pos, TOK_BITOR, left, right);
   385     }
   386     return left;
   387 }
   388 
   389 Expression *
   390 Parser::relational()
   391 {
   392     Expression *left = bitor_();
   393     while (left) {
   394         TokenKind kind = scanner_.peek();
   395         if (kind < TOK_LT || kind > TOK_GE)
   396             break;
   397         scanner_.scan();
   398         SourcePosition pos = scanner_.begin();
   399         Expression *right = shift();
   400         if (!right)
   401             return NULL;
   402         left = new (pool_) BinaryExpression(pos, kind, left, right);
   403     }
   404     return left;
   405 }
   406 
   407 Expression *
   408 Parser::equals()
   409 {
   410     Expression *left = relational();
   411     while (left) {
   412         if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
   413             break;
   414         TokenKind kind = scanner_.last();
   415         SourcePosition pos = scanner_.begin();
   416         Expression *right = relational();
   417         if (!right)
   418             return NULL;
   419         left = new (pool_) BinaryExpression(pos, kind, left, right);
   420     }
   421     return left;
   422 }
   423 
   424 Expression *
   425 Parser::and_()
   426 {
   427     Expression *left = equals();
   428     while (left) {
   429         if (!match(TOK_AND))
   430             break;
   431         SourcePosition pos = scanner_.begin();
   432         Expression *right = equals();
   433         if (!right)
   434             return NULL;
   435         left = new (pool_) BinaryExpression(pos, TOK_AND, left, right);
   436     }
   437     return left;
   438 }
   439 
   440 Expression *
   441 Parser::or_()
   442 {
   443     Expression *left = and_();
   444     while (left) {
   445         if (!match(TOK_OR))
   446             break;
   447         SourcePosition pos = scanner_.begin();
   448         Expression *right = and_();
   449         if (!right)
   450             return NULL;
   451         left = new (pool_) BinaryExpression(pos, TOK_OR, left, right);
   452     }
   453     return left;
   454 }
   455 
   456 Expression *
   457 Parser::ternary()
   458 {
   459     Expression *cond = or_();
   460     if (!cond)
   461         return NULL;
   462     
   463     if (!match(TOK_QMARK))
   464         return cond;
   465 
   466     SourcePosition pos = scanner_.begin();
   467     Expression *left = expression();
   468     if (!left)
   469         return NULL;
   470 
   471     if (!expect(TOK_COLON))
   472         return NULL;
   473 
   474     Expression *right = expression();
   475     if (!right)
   476         return NULL;
   477 
   478     return new (pool_) TernaryExpression(pos, cond, left, right);
   479 }
   480 
   481 Expression *
   482 Parser::assignment()
   483 {
   484     Expression *left = ternary();
   485     while (left) {
   486         TokenKind token = scanner_.peek();
   487         if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
   488             break;
   489         scanner_.scan();
   490         SourcePosition pos = scanner_.begin();
   491         Expression *expr = ternary();
   492         if (!expr)
   493             return NULL;
   494 
   495         left = new (pool_) Assignment(pos, token, left, expr);
   496     }
   497     return left;
   498 }
   499 
   500 Expression *
   501 Parser::expression()
   502 {
   503     return assignment();
   504 }
   505 
   506 Statement *
   507 Parser::while_()
   508 {
   509     SourcePosition pos = scanner_.begin();
   510     scanner_.scan();
   511 
   512     if (!expect(TOK_LPAREN))
   513         return NULL;
   514 
   515     Expression *condition = expression();
   516     if (!condition)
   517         return NULL;
   518 
   519     if (!expect(TOK_RPAREN))
   520         return NULL;
   521 
   522     // Inside a loop, we force the creation of a scope object. Normally, we
   523     // will cull scopes if they have no variables. However inside a loop,
   524     // function calls that take references may compute a LIFO object, and to
   525     // avoid temporarily leaking many such objects, we ensure that a scope
   526     // is present so a LIFO slot can be allocated.
   527     if (!enterScope(Scope::BLOCK))
   528         return NULL;
   529 
   530     Statement *body;
   531     if (scanner_.peek() == TOK_LBRACE)
   532         body = block(scope_);
   533     else
   534         body = localStatement();
   535     if (!body)
   536         return NULL;
   537 
   538     scope_ = scope_->enclosing();
   539     return new (pool_) WhileStatement(pos, TOK_WHILE, condition, body);
   540 }
   541 
   542 Statement *
   543 Parser::do_()
   544 {
   545     SourcePosition pos = scanner_.begin();
   546     scanner_.scan();
   547 
   548     // See the comment in ::while_() as to why we force a scope.
   549     if (!enterScope(Scope::BLOCK))
   550         return NULL;
   551 
   552     Statement *body;
   553     if (scanner_.peek() == TOK_LBRACE)
   554         body = block(scope_);
   555     else
   556         body = localStatement();
   557     if (!body)
   558         return NULL;
   559 
   560     scope_ = scope_->enclosing();
   561 
   562     if (!expect(TOK_WHILE))
   563         return NULL;
   564 
   565     if (!expect(TOK_LPAREN))
   566         return NULL;
   567     Expression *condition = expression();
   568     if (!condition)
   569         return NULL;
   570     if (!expect(TOK_RPAREN))
   571         return NULL;
   572 
   573     return new (pool_) WhileStatement(pos, TOK_DO, condition, body);
   574 }
   575 
   576 Statement *
   577 Parser::forStatement()
   578 {
   579     SourcePosition pos = scanner_.begin();
   580     scanner_.scan();
   581     if (!expect(TOK_LPAREN))
   582         return NULL;
   583 
   584     Statement *decl = NULL;
   585     if (!match(TOK_SEMICOLON)) {
   586         if (scanner_.peek() == TOK_NEW) {
   587             if (!enterScope(Scope::BLOCK))
   588                 return NULL;
   589             if ((decl = variableDeclaration()) == NULL)
   590                 return NULL;
   591         } else {
   592             if ((decl = expressionStatement()) == NULL)
   593                 return NULL;
   594         }
   595         if (!expect(TOK_SEMICOLON))
   596             return NULL;
   597     }
   598 
   599     Expression *condition = NULL;
   600     if (!match(TOK_SEMICOLON)) {
   601         if ((condition = expression()) == NULL)
   602             return NULL;
   603         if (!expect(TOK_SEMICOLON))
   604             return NULL;
   605     }
   606 
   607     Statement *update = NULL;
   608     if (!match(TOK_RPAREN)) {
   609         if ((update = expressionStatement()) == NULL)
   610             return NULL;
   611         if (!expect(TOK_RPAREN))
   612             return NULL;
   613     }
   614 
   615     // See the comment in ::while_() as to why we force a scope.
   616     if (!enterScope(Scope::BLOCK))
   617         return NULL;
   618 
   619     Statement *body;
   620     if (scanner_.peek() == TOK_LBRACE)
   621         body = block(scope_);
   622     else
   623         body = localStatement();
   624     if (!body)
   625         return NULL;
   626 
   627     scope_ = scope_->enclosing();
   628 
   629     Scope *declScope = NULL;
   630     if (decl && decl->isVariableDeclaration()) {
   631         declScope = scope_->unlinkIfEmpty();
   632         scope_ = scope_->enclosing();
   633     }
   634     return new (pool_) ForStatement(pos, declScope, decl, condition, update, body);
   635 }
   636 
   637 VariableDeclaration *
   638 Parser::variableDeclaration()
   639 {
   640     if (!expect(TOK_NEW))
   641         return NULL;
   642 
   643     Local<String> tag(zone_);
   644     if (matchName(tag.ref(), TOK_LABEL) && !tag)
   645         return NULL;
   646 
   647     Local<String> name(zone_, expectName());
   648     if (!name)
   649         return NULL;
   650 
   651     if (BoundName *other = scope_->localLookup(name)) {
   652         redeclarationError(other);
   653         return NULL;
   654     }
   655 
   656     Variable *var = scope_->declareVariable(name, scanner_.begin());
   657     if (!var)
   658         return NULL;
   659 
   660     ExpressionList *postDimensions = NULL;
   661     if (scanner_.peek() == TOK_LBRACKET) {
   662         postDimensions = new (pool_) ExpressionList();
   663         while (match(TOK_LBRACKET)) {
   664             Expression *dim = expression();
   665             if (!dim)
   666                 return NULL;
   667 
   668             if (!expect(TOK_RBRACKET))
   669                 return NULL;
   670 
   671             if (!postDimensions->append(dim))
   672                 return NULL;
   673         }
   674     }
   675     
   676     Expression *expr = NULL;
   677     if (match(TOK_ASSIGN)) {
   678         if ((expr = expression()) == NULL)
   679             return NULL;
   680     }
   681 
   682     return new (pool_) VariableDeclaration(var->pos(), var, tag, postDimensions, expr);
   683 }
   684 
   685 Statement *
   686 Parser::returnStatement()
   687 {
   688     SourcePosition pos = scanner_.begin();
   689     if (!expect(TOK_RETURN))
   690         return NULL;
   691 
   692     Expression *expr = NULL;
   693     if (scanner_.peek() != TOK_SEMICOLON) {
   694         if ((expr = expression()) == NULL)
   695             return NULL;
   696 
   697         // We only care about non-void returns when determining whether a
   698         // tagless function is non-void.
   699         encounteredReturn_ = true;
   700     }
   701 
   702     return new (pool_) ReturnStatement(pos, expr);
   703 }
   704 
   705 Statement *
   706 Parser::expressionStatement()
   707 {
   708     Expression *left = assignment();
   709     if (!left)
   710         return NULL;
   711 
   712     return new (pool_) ExpressionStatement(left);
   713 }
   714 
   715 Statement *
   716 Parser::block(Scope *scope)
   717 {
   718     if (!expect(TOK_LBRACE))
   719         return NULL;
   720 
   721     SourcePosition pos = scanner_.begin();
   722     if (!scope && !enterScope(Scope::BLOCK))
   723         return NULL;
   724 
   725     StatementList *statements = new (pool_) StatementList();
   726     while (!match(TOK_RBRACE)) {
   727         Statement *statement = localStatement();
   728         if (statement == NULL)
   729             return NULL;
   730         if (!statements->append(statement))
   731             return NULL;
   732     }
   733 
   734     if (!scope) {
   735     	scope = scope_->unlinkIfEmpty();
   736         scope_ = scope_->enclosing();
   737     }
   738     BlockStatement *block = new (pool_) BlockStatement(pos, scope, statements);
   739     return block;
   740 }
   741 
   742 Statement *
   743 Parser::if_()
   744 {
   745     expect(TOK_IF);
   746     
   747     SourcePosition pos = scanner_.begin();
   748     if (!expect(TOK_LPAREN))
   749         return NULL;
   750 
   751     Expression *cond = expression();
   752     if (!cond)
   753         return NULL;
   754 
   755     if (!expect(TOK_RPAREN))
   756         return NULL;
   757 
   758     Statement *ifTrue = localStatement();
   759     if (!ifTrue)
   760         return NULL;
   761 
   762     IfStatement *outer = new (pool_) IfStatement(pos, cond, ifTrue);
   763 
   764     IfStatement *last = outer;
   765     while (match(TOK_ELSE)) {
   766         if (!match(TOK_IF)) {
   767             Statement *ifFalse = localStatement();
   768             if (!ifFalse)
   769                 return NULL;
   770 
   771             last->setIfFalse(ifFalse);
   772             break;
   773         }
   774 
   775         SourcePosition pos = scanner_.begin();
   776         if (!expect(TOK_LPAREN))
   777             return NULL;
   778 
   779         Expression *otherCond = expression();
   780         if (!otherCond)
   781             return NULL;
   782 
   783         if (!expect(TOK_RPAREN))
   784             return NULL;
   785 
   786         Statement *otherIfTrue = localStatement();
   787         if (!otherIfTrue)
   788             return NULL;
   789 
   790         IfStatement *inner = new (pool_) IfStatement(pos, otherCond, otherIfTrue);
   791         last->setIfFalse(inner);
   792         last = inner;
   793     }
   794 
   795     return outer;
   796 }
   797 
   798 Statement *
   799 Parser::localStatement()
   800 {
   801     Statement *statement = NULL;
   802 
   803     // Statements which must be followed by a semicolon will break out of the
   804     // switch. If they may end in BlockStatements, they'll immediately return.
   805 
   806     switch (scanner_.peek()) {
   807         case TOK_NEW:
   808           if ((statement = variableDeclaration()) == NULL)
   809               return NULL;
   810           break;
   811 
   812         case TOK_FOR:
   813           return forStatement();
   814 
   815         case TOK_WHILE:
   816           return while_();
   817 
   818         case TOK_BREAK:
   819           scanner_.scan();
   820           statement = new (pool_) BreakStatement(scanner_.begin());
   821           break;
   822 
   823         case TOK_CONTINUE:
   824           scanner_.scan();
   825           statement = new (pool_) ContinueStatement(scanner_.begin());
   826           break;
   827 
   828         case TOK_DO:
   829           if ((statement = do_()) == NULL)
   830               return NULL;
   831           break;
   832 
   833         case TOK_RETURN:
   834           if ((statement = returnStatement()) == NULL)
   835               return NULL;
   836           break;
   837 
   838         case TOK_ENUM:
   839           if ((statement = enum_()) == NULL)
   840               return NULL;
   841           break;
   842 
   843         case TOK_IF:
   844           return if_();
   845 
   846         case TOK_LBRACE:
   847           return block();
   848 
   849 	default:
   850 	  break;
   851     }
   852 
   853     if (!statement) {
   854         if ((statement = expressionStatement()) == NULL)
   855             return NULL;
   856     }
   857 
   858     if (!expect(TOK_SEMICOLON))
   859         return NULL;
   860 
   861     return statement;
   862 }
   863 
   864 Statement *
   865 Parser::enum_()
   866 {
   867     expect(TOK_ENUM);
   868 
   869     SourcePosition pos = scanner_.begin();
   870 
   871     Local<String> name(zone_);
   872     if (matchName(name.ref())) {
   873         if (!name)
   874             return NULL;
   875 
   876         if (BoundName *other = scope_->localLookup(name)) {
   877             redeclarationError(other);
   878             return NULL;
   879         }
   880 
   881         if (!scope_->declareEnum(name, scanner_.begin()))
   882             return NULL;
   883     }
   884 
   885     EnumStatement::EntryList *entries = new (pool_) EnumStatement::EntryList();
   886 
   887     if (!expect(TOK_LBRACE))
   888         return NULL;
   889 
   890     do {
   891         if (scanner_.peek() == TOK_RBRACE)
   892             break;
   893 
   894         Local<String> name(zone_, expectName());
   895         if (!name)
   896             return NULL;
   897 
   898         if (BoundName *other = scope_->localLookup(name)) {
   899             redeclarationError(other);
   900             return NULL;
   901         }
   902 
   903         NamedConstant *constant = scope_->declareConstant(name, scanner_.begin());
   904         if (!constant)
   905             return NULL;
   906 
   907         Expression *expr = NULL;
   908         if (match(TOK_ASSIGN)) {
   909             if ((expr = expression()) == NULL)
   910                 return NULL;
   911         }
   912 
   913         if (!entries->append(EnumStatement::Entry(constant, expr)))
   914             return NULL;
   915     } while (match(TOK_COMMA));
   916     if (!expect(TOK_RBRACE))
   917         return NULL;
   918 
   919     return new (pool_) EnumStatement(pos, name, entries);
   920 }
   921 
   922 Statement *
   923 Parser::function(TokenKind token)
   924 {
   925     Local<String> name(zone_, expectName());
   926     if (!name)
   927         return NULL;
   928 
   929     SourcePosition pos = scanner_.begin();
   930 
   931     // The function name goes into the outer (global) scope.
   932     assert(!scope_->enclosing());
   933 
   934     NamedFunction *fun;
   935     if (BoundName *other = scope_->localLookup(name)) {
   936         // We allow a redeclaration if the new type is a public, and the old
   937         // type was a forward. However, the forward must come first.
   938         if (other->kind() != BoundName::FUNCTION) {
   939             redeclarationError(other);
   940             return NULL;
   941         }
   942 
   943         fun = other->toNamedFunction();
   944         if (!fun->isForward() || token != TOK_PUBLIC) {
   945             redeclarationError(other);
   946             return NULL;
   947         }
   948         
   949         // We've got the same name, but the original is a forward, so let it
   950         // slide and re-use the original declaration.
   951         fun->setImplemented(scanner_.begin());
   952     } else {
   953         fun = scope_->declareFunction(name, scanner_.begin(), token);
   954         if (!fun)
   955             return NULL;
   956     }
   957 
   958     Scope *scope = enterScope(Scope::FUNCTION);
   959     if (!scope)
   960         return NULL;
   961 
   962     ParameterList *parameters = new (pool_) ParameterList();
   963 
   964     if (!expect(TOK_LPAREN))
   965         return NULL;
   966     if (!match(TOK_RPAREN)) {
   967         while (true) {
   968             bool isConst = match(TOK_CONST);
   969             bool ref = match(TOK_AMPERSAND);
   970 
   971             SourcePosition begin = scanner_.begin();
   972             Local<String> tag(zone_);
   973             if (matchName(tag.ref(), TOK_LABEL) && !tag)
   974                 return NULL;
   975 
   976             if (match(TOK_ELLIPSES)) {
   977                 if (isConst || ref) {
   978                     cc_.reportError(begin, Message_InvalidVarArgs);
   979                     return NULL;
   980                 }
   981 
   982                 if (token != TOK_NATIVE) {
   983                     cc_.reportError(begin, Message_VarArgsOutsideNative);
   984                     return NULL;
   985                 }
   986 
   987                 fun->markNativeVariadic();
   988                 break;
   989             }
   990 
   991             Local<String> name(zone_, expectName());
   992             if (!name)
   993                 return NULL;
   994 
   995             if (BoundName *other = scope_->localLookup(name)) {
   996                 redeclarationError(other);
   997                 return NULL;
   998             }
   999             Variable *arg = scope_->declareArgument(name, scanner_.begin());
  1000             if (!arg)
  1001                 return NULL;
  1002 
  1003             // Look for dimensions.
  1004             ExpressionList *postDimensions = NULL;
  1005             while (match(TOK_LBRACKET)) {
  1006                 if (!postDimensions)
  1007                     postDimensions = new (pool_) ExpressionList();
  1008 
  1009                 Expression *dim = NULL;
  1010                 if (!match(TOK_RBRACKET)) {
  1011                     if ((dim = expression()) == NULL)
  1012                         return NULL;
  1013                     if (!expect(TOK_RBRACKET))
  1014                         return NULL;
  1015                 }
  1016 
  1017                 if (!postDimensions->append(dim))
  1018                     return NULL;
  1019             }
  1020 
  1021             Parameter *parameter = new (pool_) Parameter(arg, tag, ref, isConst, postDimensions);
  1022             if (!parameters->append(parameter))
  1023                 return NULL;
  1024 
  1025             if (!match(TOK_COMMA))
  1026                 break;
  1027         }
  1028         if (!expect(TOK_RPAREN))
  1029             return NULL;
  1030     }
  1031 
  1032     encounteredReturn_ = false;
  1033 
  1034     Statement *body = NULL;
  1035     if (token != TOK_FORWARD && token != TOK_NATIVE) {
  1036         body = block();
  1037         if (!body)
  1038             return NULL;
  1039     }
  1040 
  1041     scope_ = scope_->enclosing();
  1042     return new (pool_) FunctionStatement(pos, scope, fun, body, parameters, encounteredReturn_);
  1043 }
  1044 
  1045 ParseTree *
  1046 Parser::parse()
  1047 {
  1048     StatementList *statements = new (pool_) StatementList();
  1049 
  1050     if (!enterScope(Scope::GLOBAL))
  1051         return NULL;
  1052 
  1053     for (;;) {
  1054         Statement *statement = NULL;
  1055 
  1056         switch (scanner_.peek()) {
  1057           case TOK_ERROR:
  1058             return NULL;
  1059 
  1060           case TOK_EOF:
  1061             break;
  1062 
  1063           case TOK_NAME:
  1064             if ((statement = function(TOK_NAME)) == NULL)
  1065                 return NULL;
  1066             break;
  1067 
  1068           case TOK_FORWARD:
  1069           case TOK_PUBLIC:
  1070           case TOK_NATIVE:
  1071           {
  1072             TokenKind kind = scanner_.scan();
  1073             if ((statement = function(kind)) == NULL)
  1074                 return NULL;
  1075             if ((kind == TOK_FORWARD || kind == TOK_NATIVE) && !matchSemicolon())
  1076                 return NULL;
  1077             break;
  1078           }
  1079 
  1080           case TOK_NEW:
  1081           {
  1082             if ((statement = variableDeclaration()) == NULL)
  1083                return NULL;
  1084             if (!matchSemicolon())
  1085                 return NULL;
  1086             break;
  1087           }
  1088 
  1089           case TOK_ENUM:
  1090             if ((statement = enum_()) == NULL)
  1091                 return NULL;
  1092             if (!matchSemicolon())
  1093                 return NULL;
  1094             break;
  1095 
  1096           default:
  1097             cc_.reportError(scanner_.begin(), Message_ExpectedGlobal);
  1098             return NULL;
  1099         }
  1100 
  1101         if (!statement)
  1102             break;
  1103 
  1104         if (!statements->append(statement))
  1105             return NULL;
  1106     }
  1107 
  1108     return new (pool_) ParseTree(scope_, statements, unbound_);
  1109 }
  1110 
  1111 class AstPrinter : public AstVisitor
  1112 {
  1113     FILE *fp_;
  1114     size_t level_;
  1115 
  1116   private:
  1117     void prefix() {
  1118         for (size_t i = 0; i < level_; i++)
  1119             fprintf(fp_, "  ");
  1120     }
  1121     void indent() {
  1122         level_++;
  1123     }
  1124     void unindent() {
  1125         level_--;
  1126     }
  1127 
  1128   public:
  1129     AstPrinter(FILE *fp)
  1130       : fp_(fp),
  1131         level_(0)
  1132     {
  1133     }
  1134 
  1135     void visit(NameProxy *name) {
  1136         prefix();
  1137         fprintf(fp_, "[ NameProxy (%s)%s\n", name->name()->chars(),
  1138                 (name->binding() ? "" : " unbound"));
  1139     }
  1140     void visit(CallExpression *node) {
  1141         prefix();
  1142         fprintf(fp_, "[ CallExpression\n");
  1143         indent();
  1144         node->callee()->accept(this);
  1145         for (size_t i = 0; i < node->arguments()->length(); i++)
  1146             node->arguments()->at(i)->accept(this);
  1147         unindent();
  1148     }
  1149     void visit(FunctionStatement *node) {
  1150         prefix();
  1151         fprintf(fp_, "[ FunctionStatement (%s)\n", node->fun()->name()->chars());
  1152         indent();
  1153         if (node->body())
  1154             node->body()->accept(this);
  1155         unindent();
  1156     }
  1157     void visit(ExpressionStatement *node) {
  1158         prefix();
  1159         fprintf(fp_, "[ ExpressionStatement\n");
  1160         indent();
  1161         node->expression()->accept(this);
  1162         unindent();
  1163     }
  1164     void visit(Assignment *node) {
  1165         prefix();
  1166         fprintf(fp_, "[ Assignment\n");
  1167         indent();
  1168         node->lvalue()->accept(this);
  1169         node->expression()->accept(this);
  1170         unindent();
  1171     }
  1172     void visit(TernaryExpression *node) {
  1173         prefix();
  1174         fprintf(fp_, "[ TernaryExpression\n");
  1175         indent();
  1176         node->condition()->accept(this);
  1177         node->left()->accept(this);
  1178         node->right()->accept(this);
  1179         unindent();
  1180     }
  1181     void visit(BinaryExpression *node) {
  1182         prefix();
  1183         fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
  1184         indent();
  1185         node->left()->accept(this);
  1186         node->right()->accept(this);
  1187         unindent();
  1188     }
  1189     void visit(UnaryExpression *node) {
  1190         prefix();
  1191         fprintf(fp_, "[ UnaryExpression (%s)\n", TokenNames[node->token()]);
  1192         indent();
  1193         node->expression()->accept(this);
  1194         unindent();
  1195     }
  1196     void visit(ReturnStatement *node) {
  1197         prefix();
  1198         fprintf(fp_, "[ ReturnStatement\n");
  1199         indent();
  1200         if (node->expression())
  1201             node->expression()->accept(this);
  1202         unindent();
  1203     }
  1204     void visit(ForStatement *node) {
  1205         prefix();
  1206         fprintf(fp_, "[ ForStatement\n");
  1207         indent();
  1208         if (node->initialization())
  1209             node->initialization()->accept(this);
  1210         if (node->condition())
  1211             node->condition()->accept(this);
  1212         if (node->update())
  1213             node->update()->accept(this);
  1214         node->body()->accept(this);
  1215         unindent();
  1216     }
  1217     void visit(BlockStatement *node) {
  1218         prefix();
  1219         fprintf(fp_, "[ BlockStatement\n");
  1220         indent();
  1221         for (size_t i = 0; i < node->statements()->length(); i++)
  1222             node->statements()->at(i)->accept(this);
  1223         unindent();
  1224     }
  1225     void visit(VariableDeclaration *node) {
  1226         prefix();
  1227         fprintf(fp_, "[ VariableDeclaration (%s)\n", node->variable()->name()->chars());
  1228         indent();
  1229         if (node->initialization())
  1230             node->initialization()->accept(this);
  1231         unindent();
  1232     }
  1233     void visit(IntegerLiteral *node) {
  1234         prefix();
  1235         fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
  1236     }
  1237     void visit(FloatLiteral *node) {
  1238         prefix();
  1239         fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
  1240     }
  1241     void visit(IfStatement *node) {
  1242         prefix();
  1243         fprintf(fp_, "[ IfStatement\n");
  1244         indent();
  1245         node->ifTrue()->accept(this);
  1246         if (node->ifFalse())
  1247             node->ifFalse()->accept(this);
  1248         unindent();
  1249     }
  1250     void visit(IndexExpression *node) {
  1251         prefix();
  1252         fprintf(fp_, "[ IndexExpression\n");
  1253         indent();
  1254         node->left()->accept(this);
  1255         node->right()->accept(this);
  1256         unindent();
  1257     }
  1258     void visit(EnumStatement *node) {
  1259         prefix();
  1260         fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name()->chars() : "<anonymous>");
  1261         indent();
  1262         for (size_t i = 0; i < node->entries()->length(); i++) {
  1263             prefix();
  1264             fprintf(fp_, "%s =\n", node->entries()->at(i).name->name()->chars());
  1265             if (node->entries()->at(i).expr) {
  1266                 indent();
  1267                 node->entries()->at(i).expr->accept(this);
  1268                 unindent();
  1269             }
  1270         }
  1271         unindent();
  1272     }
  1273     void visit(WhileStatement *node) {
  1274         prefix();
  1275         fprintf(fp_, "[ WhileStatement (%s)\n",
  1276                 (node->token() == TOK_DO) ? "do" : "while");
  1277         indent();
  1278         if (node->token() == TOK_DO) {
  1279             node->condition()->accept(this);
  1280             node->body()->accept(this);
  1281         } else {
  1282             node->condition()->accept(this);
  1283             node->body()->accept(this);
  1284         }
  1285         unindent();
  1286     }
  1287     void visit(BreakStatement *node) {
  1288         prefix();
  1289         fprintf(fp_, "[ BreakStatement\n");
  1290     }
  1291     void visit(ContinueStatement *node) {
  1292         prefix();
  1293         fprintf(fp_, "[ ContinueStatement\n");
  1294     }
  1295     void visit(StringLiteral *node) {
  1296         prefix();
  1297         fprintf(fp_, "[ StringLiteral\n");
  1298     }
  1299     void visit(IncDecExpression *node) {
  1300         prefix();
  1301         fprintf(fp_, "[ IncDecExpression (postfix=%d)\n", node->postfix());
  1302         indent();
  1303         node->expression()->accept(this);
  1304         unindent();
  1305     }
  1306 };
  1307 
  1308 void
  1309 ParseTree::dump(FILE *fp)
  1310 {
  1311     AstPrinter printer(fp);
  1312 
  1313     for (size_t i = 0; i < statements_->length(); i++)
  1314         statements_->at(i)->accept(&printer);
  1315 }