Added MacOS, gcc, and clang support. Preliminary crappy autoconf scripts of course.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
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)
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.
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/.
20 #include "FixedArray.h"
26 Parser::Parser(CompileContext &cc, const char *stream, size_t length)
28 pool_(ZONE()->pool()),
30 scanner_(cc, stream, length),
34 unbound_ = new std::vector<NameProxy *>();
38 Parser::match(TokenKind kind)
40 if (scanner_.peek() != kind)
47 Parser::expect(TokenKind kind)
49 TokenKind got = scanner_.scan();
52 cc_.reportError(scanner_.begin(), Message_WrongToken, TokenNames[kind], TokenNames[got]);
57 Parser::enterScope(Scope::Kind kind)
59 scope_ = new (pool_) Scope(pool_, kind, scope_);
60 if (!scope_->initialize())
66 Parser::bind(Name *name)
68 BoundName *bound = scope_->lookup(name);
69 NameProxy *proxy = new (pool_) NameProxy(scanner_.begin(), name, bound);
71 unbound_->push_back(proxy);
76 Parser::matchName(Name **name, TokenKind kind)
78 assert(kind == TOK_NAME || kind == TOK_LABEL);
82 *name = strings_.add(scanner_.literal());
89 if (!expect(TOK_NAME))
91 return strings_.add(scanner_.literal());
95 Parser::redeclarationError(BoundName *other)
97 cc_.reportError(scanner_.begin(), Message_RedeclaredName, other->pos().line, other->pos().col);
103 TokenKind kind = scanner_.peek();
108 Name *name = strings_.add(scanner_.literal());
112 case TOK_FLOAT_LITERAL:
115 double value = Scanner::ParseDouble(scanner_.literal());
116 return new (pool_) FloatLiteral(scanner_.begin(), value);
119 case TOK_INTEGER_LITERAL:
122 int value = atoi(scanner_.literal());
123 return new (pool_) IntegerLiteral(scanner_.begin(), value);
126 case TOK_STRING_LITERAL:
130 ByteArray *array = ByteArray::New(ZONE(), scanner_.literal_length());
133 memcpy(array->raw(), scanner_.literal(), scanner_.literal_length());
134 return new (pool_) StringLiteral(scanner_.begin(), array);
139 if (kind != TOK_ERROR)
140 cc_.reportError(scanner_.begin(), Message_ExpectedExpression, TokenNames[kind]);
149 switch (scanner_.peek()) {
153 Expression *expr = expression();
156 if (!expect(TOK_RPAREN))
167 Parser::call(Expression *callee)
169 ExpressionList *arguments = new ExpressionList();
171 SourcePosition pos = scanner_.begin();
174 if (!match(TOK_RPAREN)) {
176 Expression *expr = expression();
180 arguments->push_back(expr);
182 if (!match(TOK_COMMA))
186 if (!expect(TOK_RPAREN))
190 return new (pool_) CallExpression(pos, callee, arguments);
194 Parser::index(Expression *left)
196 expect(TOK_LBRACKET);
198 SourcePosition pos = scanner_.begin();
199 Expression *expr = expression();
203 if (!expect(TOK_RBRACKET))
206 return new (pool_) IndexExpression(pos, left, expr);
212 Expression *expr = prefix();
217 switch (scanner_.peek()) {
239 TokenKind token = scanner_.peek();
241 SourcePosition pos = scanner_.begin();
247 Expression *expr = expression();
251 return new (pool_) IncDecExpression(pos, token, expr, false);
258 Expression *expr = primary();
260 token = scanner_.peek();
261 if (token == TOK_INCREMENT || token == TOK_DECREMENT) {
263 return new (pool_) IncDecExpression(pos, token, expr, true);
270 Parser::multiplication()
272 Expression *left = unary();
274 if (!match(TOK_SLASH) && !match(TOK_STAR))
276 SourcePosition pos = scanner_.begin();
277 TokenKind kind = scanner_.last();
278 Expression *right = unary();
281 left = new (pool_) BinaryExpression(pos, kind, left, right);
289 Expression *left = multiplication();
291 if (!match(TOK_PLUS) && !match(TOK_MINUS))
293 SourcePosition pos = scanner_.begin();
294 TokenKind kind = scanner_.last();
295 Expression *right = multiplication();
298 left = new (pool_) BinaryExpression(pos, kind, left, right);
306 Expression *left = addition();
308 if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
310 SourcePosition pos = scanner_.begin();
311 TokenKind kind = scanner_.last();
312 Expression *right = addition();
315 left = new (pool_) BinaryExpression(pos, kind, left, right);
323 Expression *left = shift();
325 if (!match(TOK_BITAND))
327 SourcePosition pos = scanner_.begin();
328 Expression *right = shift();
331 left = new (pool_) BinaryExpression(pos, TOK_BITAND, left, right);
339 Expression *left = bitand_();
341 if (!match(TOK_BITXOR))
343 SourcePosition pos = scanner_.begin();
344 Expression *right = shift();
347 left = new (pool_) BinaryExpression(pos, TOK_BITXOR, left, right);
355 Expression *left = bitxor();
357 if (!match(TOK_BITOR))
359 SourcePosition pos = scanner_.begin();
360 Expression *right = shift();
363 left = new (pool_) BinaryExpression(pos, TOK_BITOR, left, right);
371 Expression *left = bitor_();
373 TokenKind kind = scanner_.peek();
374 if (kind < TOK_LT || kind > TOK_GE)
377 SourcePosition pos = scanner_.begin();
378 Expression *right = shift();
381 left = new (pool_) BinaryExpression(pos, kind, left, right);
389 Expression *left = relational();
391 if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
393 TokenKind kind = scanner_.last();
394 SourcePosition pos = scanner_.begin();
395 Expression *right = relational();
398 left = new (pool_) BinaryExpression(pos, kind, left, right);
406 Expression *left = equals();
408 TokenKind token = scanner_.peek();
409 if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
412 SourcePosition pos = scanner_.begin();
413 Expression *expr = equals();
417 left = new (pool_) Assignment(pos, token, left, expr);
431 SourcePosition pos = scanner_.begin();
434 if (!expect(TOK_LPAREN))
437 Expression *condition = expression();
441 if (!expect(TOK_RPAREN))
444 Statement *body = localStatement();
448 return new (pool_) WhileStatement(pos, TOK_WHILE, condition, body);
454 SourcePosition pos = scanner_.begin();
457 Statement *body = block();
461 if (!expect(TOK_WHILE))
464 if (!expect(TOK_LPAREN))
466 Expression *condition = expression();
469 if (!expect(TOK_RPAREN))
472 return new (pool_) WhileStatement(pos, TOK_DO, condition, body);
476 Parser::forStatement()
478 SourcePosition pos = scanner_.begin();
480 if (!expect(TOK_LPAREN))
483 Statement *decl = NULL;
484 if (!match(TOK_SEMICOLON)) {
485 if (scanner_.peek() == TOK_NEW) {
486 if (!enterScope(Scope::BLOCK))
488 if ((decl = variableDeclaration()) == NULL)
491 if ((decl = expressionStatement()) == NULL)
494 if (!expect(TOK_SEMICOLON))
498 Expression *condition = NULL;
499 if (!match(TOK_SEMICOLON)) {
500 if ((condition = expression()) == NULL)
502 if (!expect(TOK_SEMICOLON))
506 Statement *update = NULL;
507 if (!match(TOK_RPAREN)) {
508 if ((update = expressionStatement()) == NULL)
510 if (!expect(TOK_RPAREN))
514 Statement *body = localStatement();
518 Scope *blockScope = NULL;
519 if (decl && decl->isVariableDeclaration()) {
520 blockScope = scope_->unlinkIfEmpty();
521 scope_ = scope_->enclosing();
523 return new (pool_) ForStatement(pos, blockScope, decl, condition, update, body);
526 VariableDeclaration *
527 Parser::variableDeclaration()
529 if (!expect(TOK_NEW))
533 if (matchName(&tag, TOK_LABEL) && !tag)
536 Name *name = expectName();
540 if (BoundName *other = scope_->localLookup(name)) {
541 redeclarationError(other);
545 Variable *var = scope_->declareVariable(name, scanner_.begin());
549 ExpressionList *postDimensions = NULL;
550 if (scanner_.peek() == TOK_LBRACKET) {
551 postDimensions = new ExpressionList();
552 while (match(TOK_LBRACKET)) {
553 Expression *dim = expression();
557 if (!expect(TOK_RBRACKET))
560 postDimensions->push_back(dim);
564 Expression *expr = NULL;
565 if (match(TOK_ASSIGN)) {
566 if ((expr = expression()) == NULL)
570 return new (pool_) VariableDeclaration(var->pos(), var, tag, postDimensions, expr);
574 Parser::returnStatement()
576 SourcePosition pos = scanner_.begin();
577 if (!expect(TOK_RETURN))
580 Expression *expr = expression();
584 return new (pool_) ReturnStatement(pos, expr);
588 Parser::expressionStatement()
590 Expression *left = assignment();
594 return new (pool_) ExpressionStatement(left);
600 if (!expect(TOK_LBRACE))
603 SourcePosition pos = scanner_.begin();
604 if (!enterScope(Scope::BLOCK))
607 StatementList *statements = new (pool_) StatementList();
608 while (!match(TOK_RBRACE)) {
609 Statement *statement = localStatement();
610 if (statement == NULL)
612 statements->append(pool_, statement);
615 Scope *blockScope = scope_->unlinkIfEmpty();
616 BlockStatement *block = new (pool_) BlockStatement(pos, blockScope, statements);
618 scope_ = scope_->enclosing();
627 SourcePosition pos = scanner_.begin();
628 if (!expect(TOK_LPAREN))
631 Expression *cond = expression();
635 if (!expect(TOK_RPAREN))
638 Statement *ifTrue = localStatement();
642 IfStatement *outer = new (pool_) IfStatement(pos, cond, ifTrue);
644 IfStatement *last = outer;
645 while (match(TOK_ELSE)) {
646 if (!match(TOK_IF)) {
647 Statement *ifFalse = localStatement();
651 last->setIfFalse(ifFalse);
658 SourcePosition pos = scanner_.begin();
659 if (!expect(TOK_LPAREN))
662 Expression *otherCond = expression();
666 if (!expect(TOK_RPAREN))
669 Statement *otherIfTrue = localStatement();
673 IfStatement *inner = new (pool_) IfStatement(pos, otherCond, otherIfTrue);
674 last->setIfFalse(inner);
682 Parser::localStatement()
684 Statement *statement = NULL;
686 // Statements which must be followed by a semicolon will break out of the
687 // switch. If they may end in BlockStatements, they'll immediately return.
689 switch (scanner_.peek()) {
691 if ((statement = variableDeclaration()) == NULL)
696 return forStatement();
703 statement = new (pool_) BreakStatement(scanner_.begin());
708 statement = new (pool_) ContinueStatement(scanner_.begin());
712 if ((statement = do_()) == NULL)
717 if ((statement = returnStatement()) == NULL)
722 if ((statement = enum_()) == NULL)
737 if ((statement = expressionStatement()) == NULL)
741 if (!expect(TOK_SEMICOLON))
752 SourcePosition pos = scanner_.begin();
755 if (match(TOK_NAME)) {
756 if ((name = strings_.add(scanner_.literal())) == NULL)
759 if (BoundName *other = scope_->localLookup(name)) {
760 redeclarationError(other);
764 if (!scope_->declareEnum(name, scanner_.begin()))
768 EnumStatement::EntryList *entries = new EnumStatement::EntryList();
770 if (!expect(TOK_LBRACE))
774 if (scanner_.peek() == TOK_RBRACE)
777 Name *name = expectName();
781 if (BoundName *other = scope_->localLookup(name)) {
782 redeclarationError(other);
786 NamedConstant *constant = scope_->declareConstant(name, scanner_.begin());
790 Expression *expr = NULL;
791 if (match(TOK_ASSIGN)) {
792 if ((expr = expression()) == NULL)
796 entries->push_back(EnumStatement::Entry(constant, expr));
797 } while (match(TOK_COMMA));
798 if (!expect(TOK_RBRACE))
801 return new (pool_) EnumStatement(pos, name, entries);
807 Name *name = expectName();
811 SourcePosition pos = scanner_.begin();
813 // The function name goes into the outer (global) scope.
814 assert(!scope_->enclosing());
816 if (BoundName *other = scope_->localLookup(name)) {
817 redeclarationError(other);
820 NamedFunction *fun = scope_->declareFunction(name, scanner_.begin());
824 Scope *scope = enterScope(Scope::FUNCTION);
828 ParameterList *parameters = new ParameterList();
830 if (!expect(TOK_LPAREN))
832 if (!match(TOK_RPAREN)) {
834 bool ref = match(TOK_AMPERSAND);
836 Name *name = expectName();
840 if (BoundName *other = scope_->localLookup(name)) {
841 redeclarationError(other);
844 Variable *arg = scope_->declareArgument(name, scanner_.begin());
848 Parameter *parameter = new (pool_) Parameter(arg, NULL, ref);
849 parameters->push_back(parameter);
851 if (!match(TOK_COMMA))
854 if (!expect(TOK_RPAREN))
858 Statement *body = block();
862 scope_ = scope_->enclosing();
863 return new (pool_) FunctionStatement(pos, scope, fun, body, parameters);
869 StatementList *statements = new (pool_) StatementList();
871 if (!enterScope(Scope::GLOBAL))
875 Statement *statement = NULL;
877 switch (scanner_.peek()) {
885 if ((statement = function()) == NULL)
890 if ((statement = enum_()) == NULL)
892 // :TODO: require semicolon
896 fprintf(stderr, "expected function or global variable\n");
903 statements->append(pool_, statement);
906 return new (pool_) ParseTree(scope_, statements, unbound_);
909 class AstPrinter : public AstVisitor
916 for (size_t i = 0; i < level_; i++)
933 void visit(NameProxy *name) {
935 fprintf(fp_, "[ NameProxy (%s)%s\n", name->name(),
936 (name->binding() ? "" : " unbound"));
938 void visit(CallExpression *node) {
940 fprintf(fp_, "[ CallExpression\n");
942 node->callee()->accept(this);
943 for (size_t i = 0; i < node->arguments()->size(); i++)
944 node->arguments()->at(i)->accept(this);
947 void visit(FunctionStatement *node) {
949 fprintf(fp_, "[ FunctionStatement (%s)\n", node->fun()->name());
951 node->body()->accept(this);
954 void visit(ExpressionStatement *node) {
956 fprintf(fp_, "[ ExpressionStatement\n");
958 node->expression()->accept(this);
961 void visit(Assignment *node) {
963 fprintf(fp_, "[ Assignment\n");
965 node->lvalue()->accept(this);
966 node->expression()->accept(this);
969 void visit(BinaryExpression *node) {
971 fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
973 node->left()->accept(this);
974 node->right()->accept(this);
977 void visit(ReturnStatement *node) {
979 fprintf(fp_, "[ ReturnStatement\n");
981 node->expression()->accept(this);
984 void visit(ForStatement *node) {
986 fprintf(fp_, "[ ForStatement\n");
988 if (node->initialization())
989 node->initialization()->accept(this);
990 if (node->condition())
991 node->condition()->accept(this);
993 node->update()->accept(this);
994 node->body()->accept(this);
997 void visit(BlockStatement *node) {
999 fprintf(fp_, "[ BlockStatement\n");
1001 for (size_t i = 0; i < node->statements()->length(); i++)
1002 node->statements()->at(i)->accept(this);
1005 void visit(VariableDeclaration *node) {
1007 fprintf(fp_, "[ VariableDeclaration (%s)\n", node->variable()->name());
1009 if (node->initialization())
1010 node->initialization()->accept(this);
1013 void visit(IntegerLiteral *node) {
1015 fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
1017 void visit(FloatLiteral *node) {
1019 fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
1021 void visit(IfStatement *node) {
1023 fprintf(fp_, "[ IfStatement\n");
1025 node->ifTrue()->accept(this);
1026 if (node->ifFalse())
1027 node->ifFalse()->accept(this);
1030 void visit(IndexExpression *node) {
1032 fprintf(fp_, "[ IndexExpression\n");
1034 node->left()->accept(this);
1035 node->right()->accept(this);
1038 void visit(EnumStatement *node) {
1040 fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name() : "<anonymous>");
1042 for (size_t i = 0; i < node->entries()->size(); i++) {
1044 fprintf(fp_, "%s =\n", node->entries()->at(i).name->name());
1045 if (node->entries()->at(i).expr) {
1047 node->entries()->at(i).expr->accept(this);
1053 void visit(WhileStatement *node) {
1055 fprintf(fp_, "[ WhileStatement (%s)\n",
1056 (node->token() == TOK_DO) ? "do" : "while");
1058 if (node->token() == TOK_DO) {
1059 node->condition()->accept(this);
1060 node->body()->accept(this);
1062 node->condition()->accept(this);
1063 node->body()->accept(this);
1067 void visit(BreakStatement *node) {
1069 fprintf(fp_, "[ BreakStatement\n");
1071 void visit(ContinueStatement *node) {
1073 fprintf(fp_, "[ ContinueStatement\n");
1075 void visit(StringLiteral *node) {
1077 fprintf(fp_, "[ StringLiteral\n");
1079 void visit(IncDecExpression *node) {
1081 fprintf(fp_, "[ IncDecExpression (postfix=%d)\n", node->postfix());
1083 node->expression()->accept(this);
1089 ParseTree::dump(FILE *fp)
1091 AstPrinter printer(fp);
1093 for (size_t i = 0; i < statements_->length(); i++)
1094 statements_->at(i)->accept(&printer);