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