jitcraft/Parser.cpp
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 22:31:14 2012 -0700 (2012-06-30)
changeset 14 0784ac5cc347
parent 10 0c3bcd50e361
child 21 af5d8e985502
permissions -rw-r--r--
Added support for non-array references.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include "Zone.h"
    19 #include "Parser.h"
    20 
    21 using namespace jc;
    22 
    23 Parser::Parser(const char *stream, size_t length)
    24   : pool_(ZONE()->pool()),
    25     strings_(pool_),
    26     scanner_(stream, length),
    27     scope_(NULL),
    28     unbound_(NULL)
    29 {
    30     unbound_ = new std::vector<NameProxy *>();
    31 }
    32 
    33 bool
    34 Parser::match(TokenKind kind)
    35 {
    36     if (scanner_.peek() != kind)
    37         return false;
    38     scanner_.scan();
    39     return true;
    40 }
    41 
    42 bool
    43 Parser::expect(TokenKind kind)
    44 {
    45     TokenKind got = scanner_.scan();
    46     if (got == kind)
    47         return true;
    48     fprintf(stderr, "error: expected %s, got %s\n", TokenNames[kind], TokenNames[got]);
    49     return false;
    50 }
    51 
    52 Scope *
    53 Parser::enterScope(Scope::Kind kind)
    54 {
    55     scope_ = new (pool_) Scope(pool_, kind, scope_);
    56     if (!scope_->initialize())
    57         return NULL;
    58     return scope_;
    59 }
    60 
    61 NameProxy *
    62 Parser::bind(Name *name)
    63 {
    64     BoundName *bound = scope_->lookup(name);
    65     NameProxy *proxy = new (pool_) NameProxy(name, bound);
    66     if (!bound)
    67         unbound_->push_back(proxy);
    68     return proxy;
    69 }
    70 
    71 bool
    72 Parser::matchName(Name **name, TokenKind kind)
    73 {
    74     assert(kind == TOK_NAME || kind == TOK_LABEL);
    75     *name = NULL;
    76     if (!match(kind))
    77         return false;
    78     *name = strings_.add(scanner_.literal());
    79     return true;
    80 }
    81 
    82 Name *
    83 Parser::expectName()
    84 {
    85     if (!expect(TOK_NAME))
    86         return NULL;
    87     return strings_.add(scanner_.literal());
    88 }
    89 
    90 Expression *
    91 Parser::primitive()
    92 {
    93     switch (scanner_.peek()) {
    94       case TOK_NAME:
    95       {
    96         scanner_.scan();
    97         Name *name = strings_.add(scanner_.literal());
    98         return bind(name);
    99       }
   100 
   101       case TOK_FLOAT_LITERAL:
   102       {
   103         scanner_.scan();
   104         double value = Scanner::ParseDouble(scanner_.literal());
   105         return new (pool_) FloatLiteral(value);
   106       }
   107 
   108       case TOK_INTEGER_LITERAL:
   109       {
   110         scanner_.scan();
   111         int value = atoi(scanner_.literal());
   112         return new (pool_) IntegerLiteral(value);
   113       }
   114 
   115       default:
   116       {
   117         fprintf(stderr, "error: expected expression!\n");
   118         return NULL;
   119       }
   120     }
   121 }
   122 
   123 Expression *
   124 Parser::prefix()
   125 {
   126     switch (scanner_.peek()) {
   127       case TOK_LPAREN:
   128       {
   129         scanner_.scan();
   130         Expression *expr = expression();
   131         if (!expr)
   132             return NULL;
   133         if (!expect(TOK_RPAREN))
   134             return NULL;
   135         return expr;
   136       }
   137 
   138       default:
   139         return primitive();
   140     }
   141 }
   142 
   143 Expression *
   144 Parser::call(Expression *callee)
   145 {
   146     ExpressionList *arguments = new ExpressionList();
   147 
   148     expect(TOK_LPAREN);
   149     
   150     if (!match(TOK_RPAREN)) {
   151         while (true) {
   152             Expression *expr = expression();
   153             if (!expr)
   154                 return NULL;
   155 
   156             arguments->push_back(expr);
   157 
   158             if (!match(TOK_COMMA))
   159                 break;
   160         }
   161 
   162         if (!expect(TOK_RPAREN))
   163             return NULL;
   164     }
   165 
   166     return new (pool_) CallExpression(callee, arguments);
   167 }
   168 
   169 Expression *
   170 Parser::index(Expression *left)
   171 {
   172     expect(TOK_LBRACKET);
   173     
   174     Expression *expr = expression();
   175     if (!expr)
   176         return NULL;
   177 
   178     if (!expect(TOK_RBRACKET))
   179         return NULL;
   180 
   181     return new (pool_) IndexExpression(left, expr);
   182 }
   183 
   184 Expression *
   185 Parser::primary()
   186 {
   187     Expression *expr = prefix();
   188     if (!expr)
   189         return NULL;
   190 
   191     for (;;) {
   192         switch (scanner_.peek()) {
   193           case TOK_LPAREN:
   194             expr = call(expr);
   195             if (!expr)
   196                 return NULL;
   197             break;
   198 
   199           case TOK_LBRACKET:
   200             expr = index(expr);
   201             if (!expr)
   202                 return NULL;
   203             break;
   204 
   205           default:
   206             return expr;
   207         }
   208     }
   209 }
   210 
   211 Expression *
   212 Parser::unary()
   213 {
   214     return primary();
   215 }
   216 
   217 Expression *
   218 Parser::multiplication()
   219 {
   220     Expression *left = unary();
   221     do {
   222         if (!match(TOK_SLASH) && !match(TOK_STAR))
   223             break;
   224         TokenKind kind = scanner_.last();
   225         Expression *right = unary();
   226         if (!right)
   227             return NULL;
   228         left = new (pool_) BinaryExpression(kind, left, right);
   229     } while (left);
   230     return left;
   231 }
   232 
   233 Expression *
   234 Parser::addition()
   235 {
   236     Expression *left = multiplication();
   237     while (left) {
   238         if (!match(TOK_PLUS) && !match(TOK_MINUS))
   239             break;
   240         TokenKind kind = scanner_.last();
   241         Expression *right = multiplication();
   242         if (!right)
   243             return NULL;
   244         left = new (pool_) BinaryExpression(kind, left, right);
   245     }
   246     return left;
   247 }
   248 
   249 Expression *
   250 Parser::shift()
   251 {
   252     Expression *left = addition();
   253     while (left) {
   254         if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
   255             break;
   256         TokenKind kind = scanner_.last();
   257         Expression *right = addition();
   258         if (!right)
   259             return NULL;
   260         left = new (pool_) BinaryExpression(kind, left, right);
   261     }
   262     return left;
   263 }
   264 
   265 Expression *
   266 Parser::bitand()
   267 {
   268     Expression *left = shift();
   269     while (left) {
   270         if (!match(TOK_BITAND))
   271             break;
   272         Expression *right = shift();
   273         if (!right)
   274             return NULL;
   275         left = new (pool_) BinaryExpression(TOK_BITAND, left, right);
   276     }
   277     return left;
   278 }
   279 
   280 Expression *
   281 Parser::bitxor()
   282 {
   283     Expression *left = bitand();
   284     while (left) {
   285         if (!match(TOK_BITXOR))
   286             break;
   287         Expression *right = shift();
   288         if (!right)
   289             return NULL;
   290         left = new (pool_) BinaryExpression(TOK_BITXOR, left, right);
   291     }
   292     return left;
   293 }
   294 
   295 Expression *
   296 Parser::bitor()
   297 {
   298     Expression *left = bitxor();
   299     while (left) {
   300         if (!match(TOK_BITOR))
   301             break;
   302         Expression *right = shift();
   303         if (!right)
   304             return NULL;
   305         left = new (pool_) BinaryExpression(TOK_BITOR, left, right);
   306     }
   307     return left;
   308 }
   309 
   310 Expression *
   311 Parser::relational()
   312 {
   313     Expression *left = bitor();
   314     while (left) {
   315         TokenKind kind = scanner_.peek();
   316         if (kind < TOK_LT || kind > TOK_GE)
   317             break;
   318         scanner_.scan();
   319         Expression *right = shift();
   320         if (!right)
   321             return NULL;
   322         left = new (pool_) BinaryExpression(kind, left, right);
   323     }
   324     return left;
   325 }
   326 
   327 Expression *
   328 Parser::equals()
   329 {
   330     Expression *left = relational();
   331     while (left) {
   332         if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
   333             break;
   334         TokenKind kind = scanner_.last();
   335         Expression *right = relational();
   336         if (!right)
   337             return NULL;
   338         left = new (pool_) BinaryExpression(kind, left, right);
   339     }
   340     return left;
   341 }
   342 
   343 Expression *
   344 Parser::expression()
   345 {
   346     return equals();
   347 }
   348 
   349 Statement *
   350 Parser::while_()
   351 {
   352     scanner_.scan();
   353 
   354     if (!expect(TOK_LPAREN))
   355         return NULL;
   356     Expression *condition = expression();
   357     if (!condition)
   358         return NULL;
   359     if (!expect(TOK_RPAREN))
   360         return NULL;
   361 
   362     Statement *body = localStatement();
   363     if (!body)
   364         return NULL;
   365 
   366     return new (pool_) WhileStatement(TOK_WHILE, condition, body);
   367 }
   368 
   369 Statement *
   370 Parser::do_()
   371 {
   372     scanner_.scan();
   373 
   374     Statement *body = block();
   375     if (!body)
   376         return NULL;
   377 
   378     if (!expect(TOK_WHILE))
   379         return NULL;
   380 
   381     if (!expect(TOK_LPAREN))
   382         return NULL;
   383     Expression *condition = expression();
   384     if (!condition)
   385         return NULL;
   386     if (!expect(TOK_RPAREN))
   387         return NULL;
   388 
   389     return new (pool_) WhileStatement(TOK_DO, condition, body);
   390 }
   391 
   392 Statement *
   393 Parser::forStatement()
   394 {
   395     scanner_.scan();
   396     if (!expect(TOK_LPAREN))
   397         return NULL;
   398 
   399     VariableDeclaration *decl = NULL;
   400     if (!match(TOK_SEMICOLON)) {
   401         if (!enterScope(Scope::BLOCK))
   402             return NULL;
   403         if ((decl = variableDeclaration()) == NULL)
   404             return NULL;
   405         if (!expect(TOK_SEMICOLON))
   406             return NULL;
   407     }
   408 
   409     Expression *condition = NULL;
   410     if (!match(TOK_SEMICOLON)) {
   411         if ((condition = expression()) == NULL)
   412             return NULL;
   413         if (!expect(TOK_SEMICOLON))
   414             return NULL;
   415     }
   416 
   417     Statement *update = NULL;
   418     if (!match(TOK_RPAREN)) {
   419         if ((update = expressionStatement()) == NULL)
   420             return NULL;
   421         if (!expect(TOK_RPAREN))
   422             return NULL;
   423     }
   424 
   425     Statement *body = localStatement();
   426     if (!body)
   427         return NULL;
   428 
   429     Scope *blockScope = NULL;
   430     if (decl) {
   431         blockScope = scope_->unlinkIfEmpty();
   432         scope_ = scope_->enclosing();
   433     }
   434     return new (pool_) ForStatement(blockScope, decl, condition, update, body);
   435 }
   436 
   437 VariableDeclaration *
   438 Parser::variableDeclaration()
   439 {
   440     if (!expect(TOK_NEW))
   441         return NULL;
   442 
   443     Name *tag;
   444     if (matchName(&tag, TOK_LABEL) && !tag)
   445         return NULL;
   446 
   447     Name *name = expectName();
   448     if (!name)
   449         return NULL;
   450 
   451     ExpressionList *postDimensions = NULL;
   452     if (scanner_.peek() == TOK_LBRACKET) {
   453         postDimensions = new ExpressionList();
   454         while (match(TOK_LBRACKET)) {
   455             Expression *dim = expression();
   456             if (!dim)
   457                 return NULL;
   458 
   459             if (!expect(TOK_RBRACKET))
   460                 return NULL;
   461 
   462             postDimensions->push_back(dim);
   463         }
   464     }
   465     
   466     Expression *expr = NULL;
   467     if (match(TOK_ASSIGN)) {
   468         if ((expr = expression()) == NULL)
   469             return NULL;
   470     }
   471     
   472     if (scope_->localLookup(name)) {
   473         redeclarationError();
   474         return NULL;
   475     }
   476 
   477     Variable *var = scope_->declareVariable(name);
   478     VariableDeclaration *decl = new (pool_) VariableDeclaration(var, tag, postDimensions, expr);
   479 
   480     return decl;
   481 }
   482 
   483 Statement *
   484 Parser::returnStatement()
   485 {
   486     if (!expect(TOK_RETURN))
   487         return NULL;
   488 
   489     Expression *expr = expression();
   490     if (!expr)
   491         return NULL;
   492 
   493     return new (pool_) ReturnStatement(expr);
   494 }
   495 
   496 Statement *
   497 Parser::expressionStatement()
   498 {
   499     Expression *left = primary();
   500     if (!left) {
   501         fprintf(stderr, "expected expression or statement\n");
   502         return NULL;
   503     }
   504 
   505     if (match(TOK_ASSIGN)) {
   506         Expression *expr = expression();
   507         if (!expr)
   508             return NULL;
   509 
   510         left = new (pool_) Assignment(left, expr);
   511     }
   512 
   513     return new (pool_) ExpressionStatement(left);
   514 }
   515 
   516 Statement *
   517 Parser::block()
   518 {
   519     if (!expect(TOK_LBRACE))
   520         return NULL;
   521 
   522     if (!enterScope(Scope::BLOCK))
   523         return NULL;
   524 
   525     StatementList *statements = new (pool_) StatementList();
   526     while (!match(TOK_RBRACE)) {
   527         Statement *statement = localStatement();
   528         if (statement == NULL)
   529             return NULL;
   530         statements->append(pool_, statement);
   531     }
   532 
   533     Scope *blockScope = scope_->unlinkIfEmpty();
   534     BlockStatement *block = new (pool_) BlockStatement(blockScope, statements);
   535 
   536     scope_ = scope_->enclosing();
   537     return block;
   538 }
   539 
   540 Statement *
   541 Parser::if_()
   542 {
   543     expect(TOK_IF);
   544 
   545     if (!expect(TOK_LPAREN))
   546         return NULL;
   547 
   548     Expression *cond = expression();
   549     if (!cond)
   550         return NULL;
   551 
   552     if (!expect(TOK_RPAREN))
   553         return NULL;
   554 
   555     Statement *ifTrue = localStatement();
   556     if (!ifTrue)
   557         return NULL;
   558 
   559     IfStatement *outer = new (pool_) IfStatement(cond, ifTrue);
   560 
   561     IfStatement *last = outer;
   562     while (match(TOK_ELSE)) {
   563         if (!match(TOK_IF)) {
   564             Statement *ifFalse = localStatement();
   565             if (!ifFalse)
   566                 return NULL;
   567 
   568             last->setIfFalse(ifFalse);
   569             break;
   570         }
   571 
   572         if (!expect(TOK_IF))
   573             return false;
   574 
   575         if (!expect(TOK_LPAREN))
   576             return NULL;
   577 
   578         Expression *otherCond = expression();
   579         if (!otherCond)
   580             return NULL;
   581 
   582         if (!expect(TOK_RPAREN))
   583             return NULL;
   584 
   585         Statement *otherIfTrue = localStatement();
   586         if (!otherIfTrue)
   587             return NULL;
   588 
   589         IfStatement *inner = new (pool_) IfStatement(otherCond, otherIfTrue);
   590         last->setIfFalse(inner);
   591         last = inner;
   592     }
   593 
   594     return outer;
   595 }
   596 
   597 Statement *
   598 Parser::localStatement()
   599 {
   600     Statement *statement = NULL;
   601 
   602     // Statements which must be followed by a semicolon will break out of the
   603     // switch. If they may end in BlockStatements, they'll immediately return.
   604 
   605     switch (scanner_.peek()) {
   606         case TOK_NEW:
   607           statement = variableDeclaration();
   608           break;
   609 
   610         case TOK_FOR:
   611           return forStatement();
   612 
   613         case TOK_WHILE:
   614           return while_();
   615 
   616         case TOK_BREAK:
   617           scanner_.scan();
   618           statement = new (pool_) BreakStatement();
   619           break;
   620 
   621         case TOK_CONTINUE:
   622           scanner_.scan();
   623           statement = new (pool_) ContinueStatement();
   624           break;
   625 
   626         case TOK_DO:
   627           if ((statement = do_()) == NULL)
   628               return NULL;
   629           break;
   630 
   631         case TOK_RETURN:
   632           statement = returnStatement();
   633           break;
   634 
   635         case TOK_ENUM:
   636           statement = enum_();
   637           break;
   638 
   639         case TOK_IF:
   640           return if_();
   641 
   642         case TOK_LBRACE:
   643           return block();
   644     }
   645 
   646     if (!statement) {
   647         if ((statement = expressionStatement()) == NULL)
   648             return NULL;
   649     }
   650 
   651     if (!expect(TOK_SEMICOLON))
   652         return NULL;
   653 
   654     return statement;
   655 }
   656 
   657 Statement *
   658 Parser::enum_()
   659 {
   660     expect(TOK_ENUM);
   661 
   662     Name *name = NULL;
   663     if (match(TOK_NAME)) {
   664         if ((name = strings_.add(scanner_.literal())) == NULL)
   665             return NULL;
   666 
   667         if (scope_->localLookup(name)) {
   668             redeclarationError();
   669             return NULL;
   670         }
   671 
   672         NamedEnum *e = scope_->declareEnum(name);
   673         if (!e)
   674             return NULL;
   675     }
   676 
   677     EnumStatement::EntryList *entries = new EnumStatement::EntryList();
   678 
   679     if (!expect(TOK_LBRACE))
   680         return NULL;
   681 
   682     do {
   683         if (scanner_.peek() == TOK_RBRACE)
   684             break;
   685 
   686         Name *name = expectName();
   687         if (!name)
   688             return NULL;
   689 
   690         if (scope_->localLookup(name)) {
   691             redeclarationError();
   692             return NULL;
   693         }
   694 
   695         NamedConstant *constant = scope_->declareConstant(name);
   696         if (!constant)
   697             return NULL;
   698 
   699         Expression *expr = NULL;
   700         if (match(TOK_ASSIGN)) {
   701             if ((expr = expression()) == NULL)
   702                 return NULL;
   703         }
   704 
   705         entries->push_back(EnumStatement::Entry(constant, expr));
   706     } while (match(TOK_COMMA));
   707     if (!expect(TOK_RBRACE))
   708         return NULL;
   709 
   710     return new (pool_) EnumStatement(name, entries);
   711 }
   712 
   713 Statement *
   714 Parser::function()
   715 {
   716     Name *name = expectName();
   717     if (!name)
   718         return NULL;
   719 
   720     // The function name goes into the outer (global) scope.
   721     assert(!scope_->enclosing());
   722 
   723     if (scope_->localLookup(name)) {
   724         redeclarationError();
   725         return NULL;
   726     }
   727     NamedFunction *fun = scope_->declareFunction(name);
   728     if (!fun)
   729         return NULL;
   730 
   731     Scope *scope = enterScope(Scope::FUNCTION);
   732     if (!scope)
   733         return NULL;
   734 
   735     ParameterList *parameters = new ParameterList();
   736 
   737     if (!expect(TOK_LPAREN))
   738         return NULL;
   739     if (!match(TOK_RPAREN)) {
   740         while (true) {
   741             bool ref = match(TOK_AMPERSAND);
   742 
   743             Name *name = expectName();
   744             if (!name)
   745                 return NULL;
   746 
   747             if (scope_->localLookup(name)) {
   748                 redeclarationError();
   749                 return NULL;
   750             }
   751             Variable *arg = scope_->declareArgument(name);
   752             if (!arg)
   753                 return NULL;
   754 
   755             Parameter *parameter = new (pool_) Parameter(arg, NULL, ref);
   756             parameters->push_back(parameter);
   757 
   758             if (!match(TOK_COMMA))
   759                 break;
   760         }
   761         if (!expect(TOK_RPAREN))
   762             return NULL;
   763     }
   764 
   765     Statement *body = block();
   766     if (!body)
   767         return NULL;
   768 
   769     scope_ = scope_->enclosing();
   770     return new (pool_) FunctionStatement(scope, fun, body, parameters);
   771 }
   772 
   773 ParseTree *
   774 Parser::parse()
   775 {
   776     StatementList *statements = new (pool_) StatementList();
   777 
   778     if (!enterScope(Scope::GLOBAL))
   779         return NULL;
   780 
   781     for (;;) {
   782         Statement *statement = NULL;
   783 
   784         switch (scanner_.peek()) {
   785           case TOK_ERROR:
   786             return NULL;
   787 
   788           case TOK_EOF:
   789             break;
   790 
   791           case TOK_NAME:
   792             if ((statement = function()) == NULL)
   793                 return NULL;
   794             break;
   795 
   796           case TOK_ENUM:
   797             if ((statement = enum_()) == NULL)
   798                 return NULL;
   799             // :TODO: require semicolon
   800             break;
   801 
   802           default:
   803             fprintf(stderr, "expected function or global variable\n");
   804             return NULL;
   805         }
   806 
   807         if (!statement)
   808             break;
   809 
   810         statements->append(pool_, statement);
   811     }
   812 
   813     return new (pool_) ParseTree(scope_, statements, unbound_);
   814 }
   815 
   816 class AstPrinter : public AstVisitor
   817 {
   818     FILE *fp_;
   819     size_t level_;
   820 
   821   private:
   822     void prefix() {
   823         for (size_t i = 0; i < level_; i++)
   824             fprintf(fp_, "  ");
   825     }
   826     void indent() {
   827         level_++;
   828     }
   829     void unindent() {
   830         level_--;
   831     }
   832 
   833   public:
   834     AstPrinter(FILE *fp)
   835       : fp_(fp),
   836         level_(0)
   837     {
   838     }
   839 
   840     void visit(NameProxy *name) {
   841         prefix();
   842         fprintf(fp_, "[ NameProxy (%s)%s\n", name->name(),
   843                 (name->binding() ? "" : " unbound"));
   844     }
   845     void visit(CallExpression *node) {
   846         prefix();
   847         fprintf(fp_, "[ CallExpression\n");
   848         indent();
   849         node->callee()->accept(this);
   850         for (size_t i = 0; i < node->arguments()->size(); i++)
   851             node->arguments()->at(i)->accept(this);
   852         unindent();
   853     }
   854     void visit(FunctionStatement *node) {
   855         prefix();
   856         fprintf(fp_, "[ FunctionStatement (%s)\n", node->fun()->name());
   857         indent();
   858         node->body()->accept(this);
   859         unindent();
   860     }
   861     void visit(ExpressionStatement *node) {
   862         prefix();
   863         fprintf(fp_, "[ ExpressionStatement\n");
   864         indent();
   865         node->expression()->accept(this);
   866         unindent();
   867     }
   868     void visit(Assignment *node) {
   869         prefix();
   870         fprintf(fp_, "[ Assignment\n");
   871         indent();
   872         node->lvalue()->accept(this);
   873         node->expression()->accept(this);
   874         unindent();
   875     }
   876     void visit(BinaryExpression *node) {
   877         prefix();
   878         fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
   879         indent();
   880         node->left()->accept(this);
   881         node->right()->accept(this);
   882         unindent();
   883     }
   884     void visit(ReturnStatement *node) {
   885         prefix();
   886         fprintf(fp_, "[ ReturnStatement\n");
   887         indent();
   888         node->expression()->accept(this);
   889         unindent();
   890     }
   891     void visit(ForStatement *node) {
   892         prefix();
   893         fprintf(fp_, "[ ForStatement\n");
   894         indent();
   895         if (node->declaration())
   896             node->declaration()->accept(this);
   897         if (node->condition())
   898             node->condition()->accept(this);
   899         if (node->update())
   900             node->update()->accept(this);
   901         node->body()->accept(this);
   902         unindent();
   903     }
   904     void visit(BlockStatement *node) {
   905         prefix();
   906         fprintf(fp_, "[ BlockStatement\n");
   907         indent();
   908         for (size_t i = 0; i < node->statements()->length(); i++)
   909             node->statements()->at(i)->accept(this);
   910         unindent();
   911     }
   912     void visit(VariableDeclaration *node) {
   913         prefix();
   914         fprintf(fp_, "[ VariableDeclaration (%s)\n", node->variable()->name());
   915         indent();
   916         if (node->initialization())
   917             node->initialization()->accept(this);
   918         unindent();
   919     }
   920     void visit(IntegerLiteral *node) {
   921         prefix();
   922         fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
   923     }
   924     void visit(FloatLiteral *node) {
   925         prefix();
   926         fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
   927     }
   928     void visit(IfStatement *node) {
   929         prefix();
   930         fprintf(fp_, "[ IfStatement\n");
   931         indent();
   932         node->ifTrue()->accept(this);
   933         if (node->ifFalse())
   934             node->ifFalse()->accept(this);
   935         unindent();
   936     }
   937     void visit(IndexExpression *node) {
   938         prefix();
   939         fprintf(fp_, "[ IndexExpression\n");
   940         indent();
   941         node->left()->accept(this);
   942         node->right()->accept(this);
   943         unindent();
   944     }
   945     void visit(EnumStatement *node) {
   946         prefix();
   947         fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name() : "<anonymous>");
   948         indent();
   949         for (size_t i = 0; i < node->entries()->size(); i++) {
   950             prefix();
   951             fprintf(fp_, "%s =\n", node->entries()->at(i).name->name());
   952             if (node->entries()->at(i).expr) {
   953                 indent();
   954                 node->entries()->at(i).expr->accept(this);
   955                 unindent();
   956             }
   957         }
   958         unindent();
   959     }
   960     void visit(WhileStatement *node) {
   961         prefix();
   962         fprintf(fp_, "[ WhileStatement (%s)\n",
   963                 (node->token() == TOK_DO) ? "do" : "while");
   964         indent();
   965         if (node->token() == TOK_DO) {
   966             node->condition()->accept(this);
   967             node->body()->accept(this);
   968         } else {
   969             node->condition()->accept(this);
   970             node->body()->accept(this);
   971         }
   972         unindent();
   973     }
   974     void visit(BreakStatement *node) {
   975         prefix();
   976         fprintf(fp_, "[ BreakStatement\n");
   977     }
   978     void visit(ContinueStatement *node) {
   979         prefix();
   980         fprintf(fp_, "[ ContinueStatement\n");
   981     }
   982 };
   983 
   984 void
   985 ParseTree::dump(FILE *fp)
   986 {
   987     AstPrinter printer(fp);
   988 
   989     for (size_t i = 0; i < statements_->length(); i++)
   990         statements_->at(i)->accept(&printer);
   991 }