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