Fix build bustage.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
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)
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.
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/.
21 #include "../FixedArray.h"
23 #include "../Strings.h"
24 #include "../TypeManager.h"
26 #include "../Heap-inl.h"
30 Parser::Parser(Zone *zone, CompileContext &cc, const char *stream, size_t length)
34 scanner_(cc, stream, length),
37 unbound_ = new (pool_) PoolList<NameProxy *>();
38 imports_ = new (pool_) PoolList<ImportStatement *>();
42 Parser::peek(TokenKind kind)
44 return scanner_.peek() == kind;
48 Parser::match(TokenKind kind)
57 Parser::matchSemicolon()
59 return expect(TOK_SEMICOLON);
63 Parser::expect(TokenKind kind)
65 TokenKind got = scanner_.scan();
68 cc_.reportError(scanner_.begin(), Message_WrongToken, TokenNames[kind], TokenNames[got]);
73 Parser::matchName(String **name, TokenKind kind)
75 assert(kind == TOK_NAME || kind == TOK_LABEL);
79 *name = zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
86 if (!expect(TOK_NAME))
88 return zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
94 TokenKind kind = scanner_.peek();
96 case TOK_FLOAT_LITERAL:
99 double value = Scanner::ParseDouble(scanner_.literal());
100 return new (pool_) FloatLiteral(scanner_.begin(), value);
103 case TOK_HEX_LITERAL:
106 int value = strtol(scanner_.literal(), NULL, 16);
107 return new (pool_) IntegerLiteral(scanner_.begin(), value);
110 case TOK_INTEGER_LITERAL:
113 int value = atoi(scanner_.literal());
114 return new (pool_) IntegerLiteral(scanner_.begin(), value);
120 return new (pool_) BooleanLiteral(scanner_.begin(), kind);
122 case TOK_STRING_LITERAL:
125 Local<String> string(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
126 return new (pool_) StringLiteral(scanner_.begin(), string);
131 if (kind != TOK_ERROR)
132 cc_.reportError(scanner_.begin(), Message_ExpectedExpression, TokenNames[kind]);
139 Parser::fixedArrayLiteral()
141 SourcePosition pos = scanner_.begin();
143 ExpressionList *list = new (pool_) ExpressionList();
145 // { } is an illegal array literal.
146 if (list->length() > 0 && match(TOK_LBRACE))
149 Expression *expr = NULL;
150 if (peek(TOK_LBRACE))
151 expr = fixedArrayLiteral();
157 if (!list->append(expr))
160 if (!match(TOK_COMMA)) {
161 if (!expect(TOK_RBRACE))
167 return new (pool_) ArrayLiteral(pos, TOK_LBRACE, list);
173 switch (scanner_.peek()) {
177 Expression *expr = expression();
180 if (!expect(TOK_RPAREN))
187 Local<String> name(zone_, expectName());
190 return new (pool_) NameProxy(scanner_.begin(), name);
198 // Parses only old-style declarations.
200 Parser::typeAndName(Expression **type, String **namep)
202 if (match(TOK_LABEL)) {
203 // Old-style declaration:
205 Local<String> name(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
209 *type = new (pool_) NameProxy(scanner_.begin(), name);
210 *namep = expectName();
215 *namep = expectName();
220 // Parses both old and new style declarations.
222 Parser::fullTypeAndName(Expression **type, String **namep, DeclKind kind)
225 // NewDecl ::= Type Ident
226 // OldDecl ::= Label? (Ident | "...")
228 // Label ::= Ident ":"
230 if (match(TOK_LABEL)) {
231 // We something of the form:
234 Local<String> name(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
238 *type = new (pool_) NameProxy(scanner_.begin(), name);
239 if (kind == DeclArg && match(TOK_ELLIPSES)) {
242 if ((*namep = expectName()) == NULL)
248 Local<String> name(zone_, expectName());
252 if (peek(TOK_NAME) || (kind == DeclArg && peek(TOK_ELLIPSES))) {
253 // We got something of the form:
255 *type = new (pool_) NameProxy(scanner_.begin(), name);
256 if (peek(TOK_NAME)) {
257 if ((*namep = expectName()) == NULL)
265 // We got something of the form:
273 Parser::call(Expression *callee)
275 ExpressionList *arguments = new (pool_) ExpressionList();
277 SourcePosition pos = scanner_.begin();
280 if (!match(TOK_RPAREN)) {
282 Expression *expr = expression();
286 if (!arguments->append(expr))
289 if (!match(TOK_COMMA))
293 if (!expect(TOK_RPAREN))
297 return new (pool_) CallExpression(pos, callee, arguments);
301 Parser::index(Expression *left)
303 expect(TOK_LBRACKET);
305 SourcePosition pos = scanner_.begin();
306 Expression *expr = expression();
310 if (!expect(TOK_RBRACKET))
313 return new (pool_) IndexExpression(pos, left, expr);
317 Parser::field(Expression *left)
321 SourcePosition pos = scanner_.begin();
322 Local<String> name(zone_, expectName());
326 return new (pool_) FieldExpression(pos, left, name);
332 Expression *expr = prefix();
337 switch (scanner_.peek()) {
339 if ((expr = call(expr)) == NULL)
344 if ((expr = index(expr)) == NULL)
349 if ((expr = field(expr)) == NULL)
362 TokenKind token = scanner_.peek();
364 SourcePosition pos = scanner_.begin();
370 Expression *expr = unary();
373 return new (pool_) IncDecExpression(pos, token, expr, false);
381 Expression *expr = unary();
384 return new (pool_) UnaryExpression(pos, token, expr);
390 if (!expect(TOK_LPAREN))
392 Expression *expr = unary();
395 if (!expect(TOK_RPAREN))
397 return new (pool_) UnaryExpression(pos, token, expr);
403 Local<String> tag(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
407 NameProxy *proxy = new (pool_) NameProxy(scanner_.begin(), tag);
409 Expression *expr = unary();
413 return new (pool_) UnaryExpression(pos, TOK_LABEL, expr, proxy);
420 Expression *expr = primary();
422 token = scanner_.peek();
423 if (token == TOK_INCREMENT || token == TOK_DECREMENT) {
425 return new (pool_) IncDecExpression(pos, token, expr, true);
432 Parser::multiplication()
434 Expression *left = unary();
436 if (!match(TOK_SLASH) && !match(TOK_STAR) && !match(TOK_PERCENT))
438 SourcePosition pos = scanner_.begin();
439 TokenKind kind = scanner_.last();
440 Expression *right = unary();
443 left = new (pool_) BinaryExpression(pos, kind, left, right);
451 Expression *left = multiplication();
453 if (!match(TOK_PLUS) && !match(TOK_MINUS))
455 SourcePosition pos = scanner_.begin();
456 TokenKind kind = scanner_.last();
457 Expression *right = multiplication();
460 left = new (pool_) BinaryExpression(pos, kind, left, right);
468 Expression *left = addition();
470 if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
472 SourcePosition pos = scanner_.begin();
473 TokenKind kind = scanner_.last();
474 Expression *right = addition();
477 left = new (pool_) BinaryExpression(pos, kind, left, right);
485 Expression *left = shift();
487 if (!match(TOK_BITAND))
489 SourcePosition pos = scanner_.begin();
490 Expression *right = shift();
493 left = new (pool_) BinaryExpression(pos, TOK_BITAND, left, right);
501 Expression *left = bitand_();
503 if (!match(TOK_BITXOR))
505 SourcePosition pos = scanner_.begin();
506 Expression *right = shift();
509 left = new (pool_) BinaryExpression(pos, TOK_BITXOR, left, right);
517 Expression *left = bitxor();
519 if (!match(TOK_BITOR))
521 SourcePosition pos = scanner_.begin();
522 Expression *right = bitxor();
525 left = new (pool_) BinaryExpression(pos, TOK_BITOR, left, right);
533 Expression *left = bitor_();
535 TokenKind kind = scanner_.peek();
536 if (kind < TOK_LT || kind > TOK_GE)
539 SourcePosition pos = scanner_.begin();
540 Expression *right = shift();
543 left = new (pool_) BinaryExpression(pos, kind, left, right);
551 Expression *left = relational();
553 if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
555 TokenKind kind = scanner_.last();
556 SourcePosition pos = scanner_.begin();
557 Expression *right = relational();
560 left = new (pool_) BinaryExpression(pos, kind, left, right);
568 Expression *left = equals();
572 SourcePosition pos = scanner_.begin();
573 Expression *right = equals();
576 left = new (pool_) BinaryExpression(pos, TOK_AND, left, right);
584 Expression *left = and_();
588 SourcePosition pos = scanner_.begin();
589 Expression *right = and_();
592 left = new (pool_) BinaryExpression(pos, TOK_OR, left, right);
600 Expression *cond = or_();
604 if (!match(TOK_QMARK))
608 SourcePosition pos = scanner_.begin();
609 AutoAllowTags<false> disableTags(scanner_);
611 if ((left = expression()) == NULL)
615 if (!expect(TOK_COLON))
618 Expression *right = expression();
622 return new (pool_) TernaryExpression(pos, cond, left, right);
628 Expression *left = ternary();
630 TokenKind token = scanner_.peek();
631 if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
634 SourcePosition pos = scanner_.begin();
635 Expression *expr = ternary();
639 left = new (pool_) Assignment(pos, token, left, expr);
653 SourcePosition pos = scanner_.begin();
656 if (!expect(TOK_LPAREN))
659 Expression *condition = expression();
663 if (!expect(TOK_RPAREN))
667 if (scanner_.peek() == TOK_LBRACE)
670 body = localStatement();
674 return new (pool_) WhileStatement(pos, TOK_WHILE, condition, body);
680 SourcePosition pos = scanner_.begin();
684 if (scanner_.peek() == TOK_LBRACE)
687 body = localStatement();
691 if (!expect(TOK_WHILE))
694 if (!expect(TOK_LPAREN))
696 Expression *condition = expression();
699 if (!expect(TOK_RPAREN))
702 return new (pool_) WhileStatement(pos, TOK_DO, condition, body);
710 SourcePosition pos = scanner_.begin();
712 if (!expect(TOK_LPAREN))
714 Expression *expr = expression();
717 if (!expect(TOK_RPAREN))
720 if (!expect(TOK_LBRACE))
723 SourcePosition defaultPos;
724 PoolList<Case *> *cases = new (pool_) PoolList<Case *>();
726 Statement *defaultCase = NULL;
728 while (!peek(TOK_RBRACE)) {
729 Expression *expr = NULL;
730 ExpressionList *others = NULL;
731 if (match(TOK_DEFAULT)) {
733 cc_.reportError(scanner_.begin(), Message_OneDefaultPerSwitch);
737 defaultPos = scanner_.begin();
740 cc_.reportError(defaultPos, Message_DefaultMustBeLastCase);
744 if (!expect(TOK_CASE))
747 // A limitation in the grammar is that |case <NAME>:| will be
748 // detected as a label.
749 AutoAllowTags<false> disableTags(scanner_);
751 if ((expr = expression()) == NULL)
754 if (peek(TOK_COMMA)) {
755 others = new (pool_) ExpressionList();
756 while (match(TOK_COMMA)) {
757 Expression *other = expression();
760 if (!others->append(other))
766 if (!expect(TOK_COLON))
769 Statement *statement = localStatement();
773 if (!peek(TOK_CASE) &&
774 !peek(TOK_DEFAULT) &&
777 cc_.reportError(scanner_.begin(), Message_SingleStatementPerCase);
782 Case *caze = new (pool_) Case(expr, others, statement);
783 if (!cases->append(caze))
786 defaultCase = statement;
790 if (!expect(TOK_RBRACE))
793 return new (pool_) SwitchStatement(pos, expr, cases, defaultCase);
797 Parser::forStatement()
799 SourcePosition pos = scanner_.begin();
801 if (!expect(TOK_LPAREN))
804 Statement *decl = NULL;
805 if (!match(TOK_SEMICOLON)) {
806 if (match(TOK_NEW)) {
807 if ((decl = vars(TypeQual_None)) == NULL)
810 if ((decl = expressionStatement()) == NULL)
813 if (!expect(TOK_SEMICOLON))
817 Expression *condition = NULL;
818 if (!match(TOK_SEMICOLON)) {
819 if ((condition = expression()) == NULL)
821 if (!expect(TOK_SEMICOLON))
825 Statement *update = NULL;
826 if (!match(TOK_RPAREN)) {
827 if ((update = expressionStatement()) == NULL)
829 if (!expect(TOK_RPAREN))
834 if (scanner_.peek() == TOK_LBRACE)
837 body = localStatement();
841 return new (pool_) ForStatement(pos, decl, condition, update, body);
847 ExpressionList *postDimensions = new (pool_) ExpressionList();
848 while (match(TOK_LBRACKET)) {
849 Expression *dim = NULL;
850 if (!match(TOK_RBRACKET)) {
851 if ((dim = expression()) == NULL)
853 if (!expect(TOK_RBRACKET))
857 if (!postDimensions->append(dim))
860 return postDimensions;
863 VariableDeclaration *
864 Parser::var(TypeQualifiers qual)
867 Local<String> name(zone_);
869 if (!typeAndName(&type, name.ref()))
872 ExpressionList *dims = NULL;
873 if (peek(TOK_LBRACKET)) {
874 if ((dims = dimensions()) == NULL)
878 SourcePosition pos = scanner_.begin();
880 Expression *expr = NULL;
881 if (match(TOK_ASSIGN)) {
882 switch (scanner_.peek()) {
883 // Note: we don't support [] literals yet.
886 if ((expr = fixedArrayLiteral()) == NULL)
891 if ((expr = expression()) == NULL)
897 return new (pool_) VariableDeclaration(pos, qual, name, type, dims, expr);
901 Parser::vars(TypeQualifiers qual)
903 VariableDeclaration *decl = var(qual);
907 if (!peek(TOK_COMMA))
910 StatementList *statements = new (pool_) StatementList();
911 if (!statements->append(decl))
914 while (match(TOK_COMMA)) {
915 Statement *decl = var(qual);
918 if (!statements->append(decl))
922 // Create a fake-o block with no scope to create a statement list.
923 return new (pool_) BlockStatement(decl->pos(), statements);
927 Parser::returnStatement()
929 SourcePosition pos = scanner_.begin();
930 if (!expect(TOK_RETURN))
933 Expression *expr = NULL;
934 if (scanner_.peek() != TOK_SEMICOLON) {
935 if ((expr = expression()) == NULL)
938 // We only care about non-void returns when determining whether a
939 // tagless function is non-void.
940 encounteredReturn_ = true;
943 return new (pool_) ReturnStatement(pos, expr);
947 Parser::expressionStatement()
949 Expression *left = assignment();
953 return new (pool_) ExpressionStatement(left);
959 if (!expect(TOK_LBRACE))
962 SourcePosition pos = scanner_.begin();
964 StatementList *statements = new (pool_) StatementList();
965 while (!match(TOK_RBRACE)) {
966 Statement *statement = localStatement();
967 if (statement == NULL)
969 if (!statements->append(statement))
973 BlockStatement *block = new (pool_) BlockStatement(pos, statements);
982 SourcePosition pos = scanner_.begin();
983 if (!expect(TOK_LPAREN))
986 Expression *cond = expression();
990 if (!expect(TOK_RPAREN))
993 Statement *ifTrue = localStatement();
997 IfStatement *outer = new (pool_) IfStatement(pos, cond, ifTrue);
999 IfStatement *last = outer;
1000 while (match(TOK_ELSE)) {
1001 if (!match(TOK_IF)) {
1002 Statement *ifFalse = localStatement();
1006 last->setIfFalse(ifFalse);
1010 SourcePosition pos = scanner_.begin();
1011 if (!expect(TOK_LPAREN))
1014 Expression *otherCond = expression();
1018 if (!expect(TOK_RPAREN))
1021 Statement *otherIfTrue = localStatement();
1025 IfStatement *inner = new (pool_) IfStatement(pos, otherCond, otherIfTrue);
1026 last->setIfFalse(inner);
1034 Parser::localStatement()
1036 Statement *statement = NULL;
1038 // Statements which must be followed by a semicolon will break out of the
1039 // switch. If they may end in BlockStatements, they'll immediately return.
1041 TokenKind kind = scanner_.peek();
1046 TypeQualifiers quals = (kind == TOK_CONST)
1050 if ((statement = vars(quals)) == NULL)
1056 return forStatement();
1063 statement = new (pool_) BreakStatement(scanner_.begin());
1068 statement = new (pool_) ContinueStatement(scanner_.begin());
1072 if ((statement = do_()) == NULL)
1077 if ((statement = returnStatement()) == NULL)
1082 if ((statement = enum_()) == NULL)
1087 if ((statement = struct_()) == NULL)
1105 if ((statement = expressionStatement()) == NULL)
1109 if (!expect(TOK_SEMICOLON))
1120 Local<String> name(zone_, expectName());
1124 SourcePosition pos = scanner_.begin();
1126 if (!expect(TOK_LBRACE))
1129 FieldList *fields = new (pool_) FieldList();
1131 while (!peek(TOK_RBRACE)) {
1133 Local<String> fieldName(zone_);
1135 if (!fullTypeAndName(&type, fieldName.ref(), DeclField))
1138 SourcePosition pos = scanner_.begin();
1140 if (!matchSemicolon())
1143 StructureStatement::Field *field =
1144 new (pool_) StructureStatement::Field(pos, type, fieldName);
1145 if (!field || !fields->append(field))
1149 if (!expect(TOK_RBRACE))
1152 if (fields->empty()) {
1153 cc_.reportError(pos, Message_StructCantBeEmpty);
1157 return new (pool_) StructureStatement(pos, name, fields);
1165 SourcePosition pos = scanner_.begin();
1167 Local<String> name(zone_);
1168 if (matchName(name.ref()) && !name)
1171 EnumStatement::EntryList *entries = new (pool_) EnumStatement::EntryList();
1173 if (!expect(TOK_LBRACE))
1177 if (scanner_.peek() == TOK_RBRACE)
1180 SourcePosition pos = scanner_.begin();
1181 Local<String> name(zone_, expectName());
1185 Expression *expr = NULL;
1186 if (match(TOK_ASSIGN)) {
1187 if ((expr = expression()) == NULL)
1191 if (!entries->append(EnumStatement::Entry(name, expr, pos)))
1193 } while (match(TOK_COMMA));
1194 if (!expect(TOK_RBRACE))
1197 return new (pool_) EnumStatement(pos, name, entries);
1201 static inline ScopedRoot<T> *
1202 NewScopedRoot(PoolAllocator &pool, Handle<T> obj)
1204 ScopedRoot<T> *root = pool.alloc<ScopedRoot<T> >();
1205 new (root) ScopedRoot<T>(obj);
1212 NameList *names = new (pool_) NameList();
1214 SourcePosition pos = scanner_.begin();
1216 Local<String> name(zone_, expectName());
1217 if (!name || !names->append(NewScopedRoot<String>(pool_, name)))
1220 TokenKind suffix = TOK_NONE;
1221 while (match(TOK_DOT)) {
1222 if (match(TOK_STAR)) {
1227 if ((name = expectName()) == NULL)
1230 if (!names->append(NewScopedRoot<String>(pool_, name)))
1234 return new (pool_) ImportStatement(pos, names, suffix);
1238 Parser::function(TokenKind token)
1240 Expression *returnType;
1241 Local<String> name(zone_);
1243 if (!fullTypeAndName(&returnType, name.ref(), DeclFun))
1246 SourcePosition pos = scanner_.begin();
1247 ParameterList *parameters = new (pool_) ParameterList();
1249 if (!expect(TOK_LPAREN))
1252 bool variadic = false;
1254 if (!match(TOK_RPAREN)) {
1256 TypeQualifiers quals = TypeQual_None;
1257 if (match(TOK_CONST))
1258 quals = TypeQualifiers(quals | TypeQual_Const);
1260 bool ref = match(TOK_AMPERSAND);
1262 SourcePosition begin = scanner_.begin();
1264 Local<String> name(zone_);
1265 if (!fullTypeAndName(&type, name.ref(), DeclArg))
1269 if ((quals & TypeQual_Const) || ref) {
1270 cc_.reportError(begin, Message_InvalidVarArgs);
1274 if (token != TOK_NATIVE) {
1275 cc_.reportError(begin, Message_VarArgsOutsideNative);
1283 ExpressionList *dims = NULL;
1284 if (peek(TOK_LBRACKET)) {
1285 if ((dims = dimensions()) == NULL)
1289 Expression *expr = NULL;
1290 if (match(TOK_ASSIGN)) {
1291 if ((expr = expression()) == NULL)
1295 Parameter *parameter = new (pool_) Parameter(name, type, ref, quals, dims, expr, begin);
1296 if (!parameters->append(parameter))
1299 if (!match(TOK_COMMA))
1302 if (!expect(TOK_RPAREN))
1306 encounteredReturn_ = false;
1308 Statement *body = NULL;
1309 if (token != TOK_FORWARD && token != TOK_NATIVE && token != TOK_TYPEDEF) {
1315 FunctionSignature signature(returnType, parameters, variadic);
1316 if (token == TOK_TYPEDEF)
1317 return new (pool_) FunctionTypeStatement(pos, name, signature);
1319 return new (pool_) FunctionStatement(pos, name, token, body, signature, encounteredReturn_);
1323 DeclareIntrinsicType(Zone *zone, Scope *scope, const char *name, Handle<Type> type)
1325 Local<String> tag(zone, zone->makeSymbol(name));
1330 return false; //return !!scope->declareType(tag, type);
1334 DeclareIntrinsicType(Zone *zone, Scope *scope, const char *name, PrimitiveType type)
1336 Local<Type> primitive(zone, zone->types()->getPrimitive(type));
1338 return DeclareIntrinsicType(zone, scope, name, primitive);
1344 StatementList *statements = new (pool_) StatementList();
1347 Statement *statement = NULL;
1349 TokenKind kind = scanner_.peek();
1358 if ((statement = function(TOK_NAME)) == NULL)
1365 if (statements->length())
1366 cc_.reportError(scanner_.begin(), Message_ImportsMustBeFirst);
1367 ImportStatement *stmt = import();
1370 if (!imports_->append(stmt))
1372 if (!matchSemicolon())
1384 if (kind != TOK_LABEL)
1386 if (kind == TOK_PUBLIC && peek(TOK_CONST)) {
1387 if ((statement = var(TypeQual_Const)) == NULL)
1389 if (!matchSemicolon())
1392 if ((statement = function(kind)) == NULL)
1394 if ((kind == TOK_FORWARD || kind == TOK_NATIVE || kind == TOK_TYPEDEF) && !matchSemicolon())
1403 TypeQualifiers quals = (kind == TOK_CONST) ? TypeQual_Const : TypeQual_None;
1404 // Note: at global scope, we don't accept comma-delimited
1405 // declarations yet, to see if we can get away with forcing
1406 // a nicer top-level style.
1408 if ((statement = var(quals)) == NULL)
1410 if (!matchSemicolon())
1416 if ((statement = enum_()) == NULL)
1418 if (!matchSemicolon())
1423 if ((statement = struct_()) == NULL)
1425 if (!matchSemicolon())
1430 cc_.reportError(scanner_.begin(), Message_ExpectedGlobal);
1437 if (!statements->append(statement))
1441 return new (pool_) ParseTree(statements, unbound_, imports_);
1444 class AstPrinter : public AstVisitor
1451 for (size_t i = 0; i < level_; i++)
1462 AstPrinter(FILE *fp)
1468 void dump(const FunctionSignature &sig) {
1470 fprintf(fp_, "()\n");
1472 void visitNameProxy(NameProxy *name) {
1474 fprintf(fp_, "[ NameProxy (%s)\n", name->name()->chars());
1476 void visitCallExpression(CallExpression *node) {
1478 fprintf(fp_, "[ CallExpression\n");
1480 node->callee()->accept(this);
1481 for (size_t i = 0; i < node->arguments()->length(); i++)
1482 node->arguments()->at(i)->accept(this);
1485 void visitFunctionTypeStatement(FunctionTypeStatement *node) {
1487 fprintf(fp_, "[ FunctionTypeStatement (%s)\n", node->name()->chars());
1489 dump(node->signature());
1492 void visitFunctionStatement(FunctionStatement *node) {
1494 fprintf(fp_, "[ FunctionStatement (%s)\n", node->name()->chars());
1496 dump(node->signature());
1498 node->body()->accept(this);
1501 void visitExpressionStatement(ExpressionStatement *node) {
1503 fprintf(fp_, "[ ExpressionStatement\n");
1505 node->expression()->accept(this);
1508 void visitAssignment(Assignment *node) {
1510 fprintf(fp_, "[ Assignment\n");
1512 node->lvalue()->accept(this);
1513 node->expression()->accept(this);
1516 void visitTernaryExpression(TernaryExpression *node) {
1518 fprintf(fp_, "[ TernaryExpression\n");
1520 node->condition()->accept(this);
1521 node->left()->accept(this);
1522 node->right()->accept(this);
1525 void visitBinaryExpression(BinaryExpression *node) {
1527 fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
1529 node->left()->accept(this);
1530 node->right()->accept(this);
1533 void visitUnaryExpression(UnaryExpression *node) {
1535 fprintf(fp_, "[ UnaryExpression (%s)\n", TokenNames[node->token()]);
1537 node->expression()->accept(this);
1540 void visitReturnStatement(ReturnStatement *node) {
1542 fprintf(fp_, "[ ReturnStatement\n");
1544 if (node->expression())
1545 node->expression()->accept(this);
1548 void visitForStatement(ForStatement *node) {
1550 fprintf(fp_, "[ ForStatement\n");
1552 if (node->initialization())
1553 node->initialization()->accept(this);
1554 if (node->condition())
1555 node->condition()->accept(this);
1557 node->update()->accept(this);
1558 node->body()->accept(this);
1561 void visitBlockStatement(BlockStatement *node) {
1563 fprintf(fp_, "[ BlockStatement\n");
1565 for (size_t i = 0; i < node->statements()->length(); i++)
1566 node->statements()->at(i)->accept(this);
1569 void visitVariableDeclaration(VariableDeclaration *node) {
1571 fprintf(fp_, "[ VariableDeclaration (%s)\n", node->name()->chars());
1573 if (node->initialization())
1574 node->initialization()->accept(this);
1577 void visitIntegerLiteral(IntegerLiteral *node) {
1579 fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
1581 void visitBooleanLiteral(BooleanLiteral *node) {
1583 fprintf(fp_, "[ BooleanLiteral (%s)\n",
1584 (node->token() == TOK_TRUE) ? "true" : "false");
1586 void visitFloatLiteral(FloatLiteral *node) {
1588 fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
1590 void visitIfStatement(IfStatement *node) {
1592 fprintf(fp_, "[ IfStatement\n");
1594 node->ifTrue()->accept(this);
1595 if (node->ifFalse())
1596 node->ifFalse()->accept(this);
1599 void visitIndexExpression(IndexExpression *node) {
1601 fprintf(fp_, "[ IndexExpression\n");
1603 node->left()->accept(this);
1604 node->right()->accept(this);
1607 void visitEnumStatement(EnumStatement *node) {
1609 fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name()->chars() : "<anonymous>");
1611 for (size_t i = 0; i < node->entries()->length(); i++) {
1613 fprintf(fp_, "%s =\n", node->entries()->at(i).name->chars());
1614 if (node->entries()->at(i).expr) {
1616 node->entries()->at(i).expr->accept(this);
1622 void visitWhileStatement(WhileStatement *node) {
1624 fprintf(fp_, "[ WhileStatement (%s)\n",
1625 (node->token() == TOK_DO) ? "do" : "while");
1627 if (node->token() == TOK_DO) {
1628 node->condition()->accept(this);
1629 node->body()->accept(this);
1631 node->condition()->accept(this);
1632 node->body()->accept(this);
1636 void visitBreakStatement(BreakStatement *node) {
1638 fprintf(fp_, "[ BreakStatement\n");
1640 void visitContinueStatement(ContinueStatement *node) {
1642 fprintf(fp_, "[ ContinueStatement\n");
1644 void visitStringLiteral(StringLiteral *node) {
1646 fprintf(fp_, "[ StringLiteral\n");
1648 void visitIncDecExpression(IncDecExpression *node) {
1650 fprintf(fp_, "[ IncDecExpression (postfix=%d)\n", node->postfix());
1652 node->expression()->accept(this);
1655 void visitStructureStatement(StructureStatement *node) {
1657 fprintf(fp_, "[ StructureStatement %s\n", node->name()->chars());
1659 void visitFieldExpression(FieldExpression *node) {
1661 fprintf(fp_, "[ FieldExpression %s\n", node->field()->chars());
1663 node->left()->accept(this);
1666 void visitSwitchStatement(SwitchStatement *node) {
1668 fprintf(fp_, "[ SwitchStatement\n");
1670 node->expression()->accept(this);
1671 for (size_t i = 0; i < node->cases()->length(); i++) {
1672 Case *c = node->cases()->at(i);
1673 c->expression()->accept(this);
1675 for (size_t j = 0; j < c->others()->length(); j++)
1676 c->others()->at(j)->accept(this);
1679 c->statement()->accept(this);
1682 if (node->defaultCase())
1683 node->defaultCase()->accept(this);
1686 void visitArrayLiteral(ArrayLiteral *node) {
1688 fprintf(fp_, "[ ArrayLiteral\n");
1690 for (size_t i = 0; i < node->expressions()->length(); i++) {
1691 Expression *expr = node->expressions()->at(i);
1697 void visitImportStatement(ImportStatement *node) {
1699 fprintf(fp_, "[ ImportStatement\n");
1704 ParseTree::dump(FILE *fp)
1706 AstPrinter printer(fp);
1708 for (size_t i = 0; i < statements_->length(); i++)
1709 statements_->at(i)->accept(&printer);