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