1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
21 #include "FixedArray.h"
28 Parser::Parser(Zone *zone, CompileContext &cc, const char *stream, size_t length)
32 scanner_(cc, stream, length),
36 unbound_ = new (pool_) PoolList<NameProxy *>();
40 Parser::match(TokenKind kind)
42 if (scanner_.peek() != kind)
49 Parser::expect(TokenKind kind)
51 TokenKind got = scanner_.scan();
54 cc_.reportError(scanner_.begin(), Message_WrongToken, TokenNames[kind], TokenNames[got]);
59 Parser::enterScope(Scope::Kind kind)
61 scope_ = new (pool_) Scope(pool_, kind, scope_);
62 if (!scope_->initialize())
68 Parser::bind(Handle<String> name)
70 BoundName *bound = scope_->lookup(name);
71 NameProxy *proxy = new (pool_) NameProxy(scanner_.begin(), name, bound);
72 if (!bound && !unbound_->append(proxy))
78 Parser::matchName(String **name, TokenKind kind)
80 assert(kind == TOK_NAME || kind == TOK_LABEL);
84 *name = zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
91 if (!expect(TOK_NAME))
93 return zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
97 Parser::redeclarationError(BoundName *other)
99 cc_.reportError(scanner_.begin(), Message_RedeclaredName, other->pos().line, other->pos().col);
105 TokenKind kind = scanner_.peek();
109 Local<String> name(zone_, expectName());
115 case TOK_FLOAT_LITERAL:
118 double value = Scanner::ParseDouble(scanner_.literal());
119 return new (pool_) FloatLiteral(scanner_.begin(), value);
122 case TOK_INTEGER_LITERAL:
125 int value = atoi(scanner_.literal());
126 return new (pool_) IntegerLiteral(scanner_.begin(), value);
129 case TOK_STRING_LITERAL:
132 Local<ByteArray> array(zone_, ByteArray::New(zone_, scanner_.literal_length() + 1));
135 memcpy(array->raw(), scanner_.literal(), scanner_.literal_length() + 1);
136 return new (pool_) StringLiteral(scanner_.begin(), array);
141 if (kind != TOK_ERROR)
142 cc_.reportError(scanner_.begin(), Message_ExpectedExpression, TokenNames[kind]);
151 switch (scanner_.peek()) {
155 Expression *expr = expression();
158 if (!expect(TOK_RPAREN))
169 Parser::call(Expression *callee)
171 ExpressionList *arguments = new (pool_) ExpressionList();
173 SourcePosition pos = scanner_.begin();
176 if (!match(TOK_RPAREN)) {
178 Expression *expr = expression();
182 if (!arguments->append(expr))
185 if (!match(TOK_COMMA))
189 if (!expect(TOK_RPAREN))
193 return new (pool_) CallExpression(pos, callee, arguments);
197 Parser::index(Expression *left)
199 expect(TOK_LBRACKET);
201 SourcePosition pos = scanner_.begin();
202 Expression *expr = expression();
206 if (!expect(TOK_RBRACKET))
209 return new (pool_) IndexExpression(pos, left, expr);
215 Expression *expr = prefix();
220 switch (scanner_.peek()) {
242 TokenKind token = scanner_.peek();
244 SourcePosition pos = scanner_.begin();
250 Expression *expr = expression();
254 return new (pool_) IncDecExpression(pos, token, expr, false);
261 Expression *expr = primary();
263 token = scanner_.peek();
264 if (token == TOK_INCREMENT || token == TOK_DECREMENT) {
266 return new (pool_) IncDecExpression(pos, token, expr, true);
273 Parser::multiplication()
275 Expression *left = unary();
277 if (!match(TOK_SLASH) && !match(TOK_STAR))
279 SourcePosition pos = scanner_.begin();
280 TokenKind kind = scanner_.last();
281 Expression *right = unary();
284 left = new (pool_) BinaryExpression(pos, kind, left, right);
292 Expression *left = multiplication();
294 if (!match(TOK_PLUS) && !match(TOK_MINUS))
296 SourcePosition pos = scanner_.begin();
297 TokenKind kind = scanner_.last();
298 Expression *right = multiplication();
301 left = new (pool_) BinaryExpression(pos, kind, left, right);
309 Expression *left = addition();
311 if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
313 SourcePosition pos = scanner_.begin();
314 TokenKind kind = scanner_.last();
315 Expression *right = addition();
318 left = new (pool_) BinaryExpression(pos, kind, left, right);
326 Expression *left = shift();
328 if (!match(TOK_BITAND))
330 SourcePosition pos = scanner_.begin();
331 Expression *right = shift();
334 left = new (pool_) BinaryExpression(pos, TOK_BITAND, left, right);
342 Expression *left = bitand_();
344 if (!match(TOK_BITXOR))
346 SourcePosition pos = scanner_.begin();
347 Expression *right = shift();
350 left = new (pool_) BinaryExpression(pos, TOK_BITXOR, left, right);
358 Expression *left = bitxor();
360 if (!match(TOK_BITOR))
362 SourcePosition pos = scanner_.begin();
363 Expression *right = shift();
366 left = new (pool_) BinaryExpression(pos, TOK_BITOR, left, right);
374 Expression *left = bitor_();
376 TokenKind kind = scanner_.peek();
377 if (kind < TOK_LT || kind > TOK_GE)
380 SourcePosition pos = scanner_.begin();
381 Expression *right = shift();
384 left = new (pool_) BinaryExpression(pos, kind, left, right);
392 Expression *left = relational();
394 if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
396 TokenKind kind = scanner_.last();
397 SourcePosition pos = scanner_.begin();
398 Expression *right = relational();
401 left = new (pool_) BinaryExpression(pos, kind, left, right);
409 Expression *left = equals();
411 TokenKind token = scanner_.peek();
412 if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
415 SourcePosition pos = scanner_.begin();
416 Expression *expr = equals();
420 left = new (pool_) Assignment(pos, token, left, expr);
434 SourcePosition pos = scanner_.begin();
437 if (!expect(TOK_LPAREN))
440 Expression *condition = expression();
444 if (!expect(TOK_RPAREN))
447 // Inside a loop, we force the creation of a scope object. Normally, we
448 // will cull scopes if they have no variables. However inside a loop,
449 // function calls that take references may compute a LIFO object, and to
450 // avoid temporarily leaking many such objects, we ensure that a scope
451 // is present so a LIFO slot can be allocated.
452 if (!enterScope(Scope::BLOCK))
456 if (scanner_.peek() == TOK_LBRACE)
457 body = block(scope_);
459 body = localStatement();
463 scope_ = scope_->enclosing();
464 return new (pool_) WhileStatement(pos, TOK_WHILE, condition, body);
470 SourcePosition pos = scanner_.begin();
473 // See the comment in ::while_() as to why we force a scope.
474 if (!enterScope(Scope::BLOCK))
478 if (scanner_.peek() == TOK_LBRACE)
479 body = block(scope_);
481 body = localStatement();
485 scope_ = scope_->enclosing();
487 if (!expect(TOK_WHILE))
490 if (!expect(TOK_LPAREN))
492 Expression *condition = expression();
495 if (!expect(TOK_RPAREN))
498 return new (pool_) WhileStatement(pos, TOK_DO, condition, body);
502 Parser::forStatement()
504 SourcePosition pos = scanner_.begin();
506 if (!expect(TOK_LPAREN))
509 Statement *decl = NULL;
510 if (!match(TOK_SEMICOLON)) {
511 if (scanner_.peek() == TOK_NEW) {
512 if (!enterScope(Scope::BLOCK))
514 if ((decl = variableDeclaration()) == NULL)
517 if ((decl = expressionStatement()) == NULL)
520 if (!expect(TOK_SEMICOLON))
524 Expression *condition = NULL;
525 if (!match(TOK_SEMICOLON)) {
526 if ((condition = expression()) == NULL)
528 if (!expect(TOK_SEMICOLON))
532 Statement *update = NULL;
533 if (!match(TOK_RPAREN)) {
534 if ((update = expressionStatement()) == NULL)
536 if (!expect(TOK_RPAREN))
540 // See the comment in ::while_() as to why we force a scope.
541 if (!enterScope(Scope::BLOCK))
545 if (scanner_.peek() == TOK_LBRACE)
546 body = block(scope_);
548 body = localStatement();
552 scope_ = scope_->enclosing();
554 Scope *declScope = NULL;
555 if (decl && decl->isVariableDeclaration()) {
556 declScope = scope_->unlinkIfEmpty();
557 scope_ = scope_->enclosing();
559 return new (pool_) ForStatement(pos, declScope, decl, condition, update, body);
562 VariableDeclaration *
563 Parser::variableDeclaration()
565 if (!expect(TOK_NEW))
568 Local<String> tag(zone_);
569 if (matchName(tag.ref(), TOK_LABEL) && !tag)
572 Local<String> name(zone_, expectName());
576 if (BoundName *other = scope_->localLookup(name)) {
577 redeclarationError(other);
581 Variable *var = scope_->declareVariable(name, scanner_.begin());
585 ExpressionList *postDimensions = NULL;
586 if (scanner_.peek() == TOK_LBRACKET) {
587 postDimensions = new (pool_) ExpressionList();
588 while (match(TOK_LBRACKET)) {
589 Expression *dim = expression();
593 if (!expect(TOK_RBRACKET))
596 if (!postDimensions->append(dim))
601 Expression *expr = NULL;
602 if (match(TOK_ASSIGN)) {
603 if ((expr = expression()) == NULL)
607 return new (pool_) VariableDeclaration(var->pos(), var, tag, postDimensions, expr);
611 Parser::returnStatement()
613 SourcePosition pos = scanner_.begin();
614 if (!expect(TOK_RETURN))
617 Expression *expr = expression();
621 return new (pool_) ReturnStatement(pos, expr);
625 Parser::expressionStatement()
627 Expression *left = assignment();
631 return new (pool_) ExpressionStatement(left);
635 Parser::block(Scope *scope)
637 if (!expect(TOK_LBRACE))
640 SourcePosition pos = scanner_.begin();
641 if (!scope && !enterScope(Scope::BLOCK))
644 StatementList *statements = new (pool_) StatementList();
645 while (!match(TOK_RBRACE)) {
646 Statement *statement = localStatement();
647 if (statement == NULL)
649 if (!statements->append(statement))
654 scope = scope_->unlinkIfEmpty();
655 scope_ = scope_->enclosing();
657 BlockStatement *block = new (pool_) BlockStatement(pos, scope, statements);
666 SourcePosition pos = scanner_.begin();
667 if (!expect(TOK_LPAREN))
670 Expression *cond = expression();
674 if (!expect(TOK_RPAREN))
677 Statement *ifTrue = localStatement();
681 IfStatement *outer = new (pool_) IfStatement(pos, cond, ifTrue);
683 IfStatement *last = outer;
684 while (match(TOK_ELSE)) {
685 if (!match(TOK_IF)) {
686 Statement *ifFalse = localStatement();
690 last->setIfFalse(ifFalse);
697 SourcePosition pos = scanner_.begin();
698 if (!expect(TOK_LPAREN))
701 Expression *otherCond = expression();
705 if (!expect(TOK_RPAREN))
708 Statement *otherIfTrue = localStatement();
712 IfStatement *inner = new (pool_) IfStatement(pos, otherCond, otherIfTrue);
713 last->setIfFalse(inner);
721 Parser::localStatement()
723 Statement *statement = NULL;
725 // Statements which must be followed by a semicolon will break out of the
726 // switch. If they may end in BlockStatements, they'll immediately return.
728 switch (scanner_.peek()) {
730 if ((statement = variableDeclaration()) == NULL)
735 return forStatement();
742 statement = new (pool_) BreakStatement(scanner_.begin());
747 statement = new (pool_) ContinueStatement(scanner_.begin());
751 if ((statement = do_()) == NULL)
756 if ((statement = returnStatement()) == NULL)
761 if ((statement = enum_()) == NULL)
776 if ((statement = expressionStatement()) == NULL)
780 if (!expect(TOK_SEMICOLON))
791 SourcePosition pos = scanner_.begin();
793 Local<String> name(zone_);
794 if (matchName(name.ref())) {
798 if (BoundName *other = scope_->localLookup(name)) {
799 redeclarationError(other);
803 if (!scope_->declareEnum(name, scanner_.begin()))
807 EnumStatement::EntryList *entries = new (pool_) EnumStatement::EntryList();
809 if (!expect(TOK_LBRACE))
813 if (scanner_.peek() == TOK_RBRACE)
816 Local<String> name(zone_, expectName());
820 if (BoundName *other = scope_->localLookup(name)) {
821 redeclarationError(other);
825 NamedConstant *constant = scope_->declareConstant(name, scanner_.begin());
829 Expression *expr = NULL;
830 if (match(TOK_ASSIGN)) {
831 if ((expr = expression()) == NULL)
835 if (!entries->append(EnumStatement::Entry(constant, expr)))
837 } while (match(TOK_COMMA));
838 if (!expect(TOK_RBRACE))
841 return new (pool_) EnumStatement(pos, name, entries);
847 Local<String> name(zone_, expectName());
851 SourcePosition pos = scanner_.begin();
853 // The function name goes into the outer (global) scope.
854 assert(!scope_->enclosing());
856 if (BoundName *other = scope_->localLookup(name)) {
857 redeclarationError(other);
860 NamedFunction *fun = scope_->declareFunction(name, scanner_.begin());
864 Scope *scope = enterScope(Scope::FUNCTION);
868 ParameterList *parameters = new (pool_) ParameterList();
870 if (!expect(TOK_LPAREN))
872 if (!match(TOK_RPAREN)) {
874 bool ref = match(TOK_AMPERSAND);
876 Local<String> name(zone_, expectName());
880 if (BoundName *other = scope_->localLookup(name)) {
881 redeclarationError(other);
884 Variable *arg = scope_->declareArgument(name, scanner_.begin());
888 Local<String> tag(zone_);
889 Parameter *parameter = new (pool_) Parameter(arg, tag, ref);
890 if (!parameters->append(parameter))
893 if (!match(TOK_COMMA))
896 if (!expect(TOK_RPAREN))
900 Statement *body = block();
904 scope_ = scope_->enclosing();
905 return new (pool_) FunctionStatement(pos, scope, fun, body, parameters);
911 StatementList *statements = new (pool_) StatementList();
913 if (!enterScope(Scope::GLOBAL))
917 Statement *statement = NULL;
919 switch (scanner_.peek()) {
927 if ((statement = function()) == NULL)
932 if ((statement = enum_()) == NULL)
934 // :TODO: require semicolon
938 fprintf(stderr, "expected function or global variable\n");
945 if (!statements->append(statement))
949 return new (pool_) ParseTree(scope_, statements, unbound_);
952 class AstPrinter : public AstVisitor
959 for (size_t i = 0; i < level_; i++)
976 void visit(NameProxy *name) {
978 fprintf(fp_, "[ NameProxy (%s)%s\n", name->name()->chars(),
979 (name->binding() ? "" : " unbound"));
981 void visit(CallExpression *node) {
983 fprintf(fp_, "[ CallExpression\n");
985 node->callee()->accept(this);
986 for (size_t i = 0; i < node->arguments()->length(); i++)
987 node->arguments()->at(i)->accept(this);
990 void visit(FunctionStatement *node) {
992 fprintf(fp_, "[ FunctionStatement (%s)\n", node->fun()->name()->chars());
994 node->body()->accept(this);
997 void visit(ExpressionStatement *node) {
999 fprintf(fp_, "[ ExpressionStatement\n");
1001 node->expression()->accept(this);
1004 void visit(Assignment *node) {
1006 fprintf(fp_, "[ Assignment\n");
1008 node->lvalue()->accept(this);
1009 node->expression()->accept(this);
1012 void visit(BinaryExpression *node) {
1014 fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
1016 node->left()->accept(this);
1017 node->right()->accept(this);
1020 void visit(ReturnStatement *node) {
1022 fprintf(fp_, "[ ReturnStatement\n");
1024 node->expression()->accept(this);
1027 void visit(ForStatement *node) {
1029 fprintf(fp_, "[ ForStatement\n");
1031 if (node->initialization())
1032 node->initialization()->accept(this);
1033 if (node->condition())
1034 node->condition()->accept(this);
1036 node->update()->accept(this);
1037 node->body()->accept(this);
1040 void visit(BlockStatement *node) {
1042 fprintf(fp_, "[ BlockStatement\n");
1044 for (size_t i = 0; i < node->statements()->length(); i++)
1045 node->statements()->at(i)->accept(this);
1048 void visit(VariableDeclaration *node) {
1050 fprintf(fp_, "[ VariableDeclaration (%s)\n", node->variable()->name()->chars());
1052 if (node->initialization())
1053 node->initialization()->accept(this);
1056 void visit(IntegerLiteral *node) {
1058 fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
1060 void visit(FloatLiteral *node) {
1062 fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
1064 void visit(IfStatement *node) {
1066 fprintf(fp_, "[ IfStatement\n");
1068 node->ifTrue()->accept(this);
1069 if (node->ifFalse())
1070 node->ifFalse()->accept(this);
1073 void visit(IndexExpression *node) {
1075 fprintf(fp_, "[ IndexExpression\n");
1077 node->left()->accept(this);
1078 node->right()->accept(this);
1081 void visit(EnumStatement *node) {
1083 fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name()->chars() : "<anonymous>");
1085 for (size_t i = 0; i < node->entries()->length(); i++) {
1087 fprintf(fp_, "%s =\n", node->entries()->at(i).name->name()->chars());
1088 if (node->entries()->at(i).expr) {
1090 node->entries()->at(i).expr->accept(this);
1096 void visit(WhileStatement *node) {
1098 fprintf(fp_, "[ WhileStatement (%s)\n",
1099 (node->token() == TOK_DO) ? "do" : "while");
1101 if (node->token() == TOK_DO) {
1102 node->condition()->accept(this);
1103 node->body()->accept(this);
1105 node->condition()->accept(this);
1106 node->body()->accept(this);
1110 void visit(BreakStatement *node) {
1112 fprintf(fp_, "[ BreakStatement\n");
1114 void visit(ContinueStatement *node) {
1116 fprintf(fp_, "[ ContinueStatement\n");
1118 void visit(StringLiteral *node) {
1120 fprintf(fp_, "[ StringLiteral\n");
1122 void visit(IncDecExpression *node) {
1124 fprintf(fp_, "[ IncDecExpression (postfix=%d)\n", node->postfix());
1126 node->expression()->accept(this);
1132 ParseTree::dump(FILE *fp)
1134 AstPrinter printer(fp);
1136 for (size_t i = 0; i < statements_->length(); i++)
1137 statements_->at(i)->accept(&printer);