Added initial String: support.
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(const char *stream, size_t length)
27 : pool_(ZONE()->pool()),
29 scanner_(stream, length),
33 unbound_ = new std::vector<NameProxy *>();
37 Parser::match(TokenKind kind)
39 if (scanner_.peek() != kind)
46 Parser::expect(TokenKind kind)
48 TokenKind got = scanner_.scan();
51 fprintf(stderr, "error: expected %s, got %s\n", TokenNames[kind], TokenNames[got]);
56 Parser::enterScope(Scope::Kind kind)
58 scope_ = new (pool_) Scope(pool_, kind, scope_);
59 if (!scope_->initialize())
65 Parser::bind(Name *name)
67 BoundName *bound = scope_->lookup(name);
68 NameProxy *proxy = new (pool_) NameProxy(name, bound);
70 unbound_->push_back(proxy);
75 Parser::matchName(Name **name, TokenKind kind)
77 assert(kind == TOK_NAME || kind == TOK_LABEL);
81 *name = strings_.add(scanner_.literal());
88 if (!expect(TOK_NAME))
90 return strings_.add(scanner_.literal());
96 switch (scanner_.peek()) {
100 Name *name = strings_.add(scanner_.literal());
104 case TOK_FLOAT_LITERAL:
107 double value = Scanner::ParseDouble(scanner_.literal());
108 return new (pool_) FloatLiteral(value);
111 case TOK_INTEGER_LITERAL:
114 int value = atoi(scanner_.literal());
115 return new (pool_) IntegerLiteral(value);
118 case TOK_STRING_LITERAL:
122 ByteArray *array = ByteArray::New(ZONE(), scanner_.literal_length());
125 memcpy(array->raw(), scanner_.literal(), scanner_.literal_length());
127 return new (pool_) StringLiteral(array);
132 fprintf(stderr, "error: expected expression!\n");
141 switch (scanner_.peek()) {
145 Expression *expr = expression();
148 if (!expect(TOK_RPAREN))
159 Parser::call(Expression *callee)
161 ExpressionList *arguments = new ExpressionList();
165 if (!match(TOK_RPAREN)) {
167 Expression *expr = expression();
171 arguments->push_back(expr);
173 if (!match(TOK_COMMA))
177 if (!expect(TOK_RPAREN))
181 return new (pool_) CallExpression(callee, arguments);
185 Parser::index(Expression *left)
187 expect(TOK_LBRACKET);
189 Expression *expr = expression();
193 if (!expect(TOK_RBRACKET))
196 return new (pool_) IndexExpression(left, expr);
202 Expression *expr = prefix();
207 switch (scanner_.peek()) {
233 Parser::multiplication()
235 Expression *left = unary();
237 if (!match(TOK_SLASH) && !match(TOK_STAR))
239 TokenKind kind = scanner_.last();
240 Expression *right = unary();
243 left = new (pool_) BinaryExpression(kind, left, right);
251 Expression *left = multiplication();
253 if (!match(TOK_PLUS) && !match(TOK_MINUS))
255 TokenKind kind = scanner_.last();
256 Expression *right = multiplication();
259 left = new (pool_) BinaryExpression(kind, left, right);
267 Expression *left = addition();
269 if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
271 TokenKind kind = scanner_.last();
272 Expression *right = addition();
275 left = new (pool_) BinaryExpression(kind, left, right);
283 Expression *left = shift();
285 if (!match(TOK_BITAND))
287 Expression *right = shift();
290 left = new (pool_) BinaryExpression(TOK_BITAND, left, right);
298 Expression *left = bitand();
300 if (!match(TOK_BITXOR))
302 Expression *right = shift();
305 left = new (pool_) BinaryExpression(TOK_BITXOR, left, right);
313 Expression *left = bitxor();
315 if (!match(TOK_BITOR))
317 Expression *right = shift();
320 left = new (pool_) BinaryExpression(TOK_BITOR, left, right);
328 Expression *left = bitor();
330 TokenKind kind = scanner_.peek();
331 if (kind < TOK_LT || kind > TOK_GE)
334 Expression *right = shift();
337 left = new (pool_) BinaryExpression(kind, left, right);
345 Expression *left = relational();
347 if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
349 TokenKind kind = scanner_.last();
350 Expression *right = relational();
353 left = new (pool_) BinaryExpression(kind, left, right);
369 if (!expect(TOK_LPAREN))
371 Expression *condition = expression();
374 if (!expect(TOK_RPAREN))
377 Statement *body = localStatement();
381 return new (pool_) WhileStatement(TOK_WHILE, condition, body);
389 Statement *body = block();
393 if (!expect(TOK_WHILE))
396 if (!expect(TOK_LPAREN))
398 Expression *condition = expression();
401 if (!expect(TOK_RPAREN))
404 return new (pool_) WhileStatement(TOK_DO, condition, body);
408 Parser::forStatement()
411 if (!expect(TOK_LPAREN))
414 VariableDeclaration *decl = NULL;
415 if (!match(TOK_SEMICOLON)) {
416 if (!enterScope(Scope::BLOCK))
418 if ((decl = variableDeclaration()) == NULL)
420 if (!expect(TOK_SEMICOLON))
424 Expression *condition = NULL;
425 if (!match(TOK_SEMICOLON)) {
426 if ((condition = expression()) == NULL)
428 if (!expect(TOK_SEMICOLON))
432 Statement *update = NULL;
433 if (!match(TOK_RPAREN)) {
434 if ((update = expressionStatement()) == NULL)
436 if (!expect(TOK_RPAREN))
440 Statement *body = localStatement();
444 Scope *blockScope = NULL;
446 blockScope = scope_->unlinkIfEmpty();
447 scope_ = scope_->enclosing();
449 return new (pool_) ForStatement(blockScope, decl, condition, update, body);
452 VariableDeclaration *
453 Parser::variableDeclaration()
455 if (!expect(TOK_NEW))
459 if (matchName(&tag, TOK_LABEL) && !tag)
462 Name *name = expectName();
466 ExpressionList *postDimensions = NULL;
467 if (scanner_.peek() == TOK_LBRACKET) {
468 postDimensions = new ExpressionList();
469 while (match(TOK_LBRACKET)) {
470 Expression *dim = expression();
474 if (!expect(TOK_RBRACKET))
477 postDimensions->push_back(dim);
481 Expression *expr = NULL;
482 if (match(TOK_ASSIGN)) {
483 if ((expr = expression()) == NULL)
487 if (scope_->localLookup(name)) {
488 redeclarationError();
492 Variable *var = scope_->declareVariable(name);
493 VariableDeclaration *decl = new (pool_) VariableDeclaration(var, tag, postDimensions, expr);
499 Parser::returnStatement()
501 if (!expect(TOK_RETURN))
504 Expression *expr = expression();
508 return new (pool_) ReturnStatement(expr);
512 Parser::expressionStatement()
514 Expression *left = primary();
518 if (match(TOK_ASSIGN)) {
519 Expression *expr = expression();
523 left = new (pool_) Assignment(left, expr);
526 return new (pool_) ExpressionStatement(left);
532 if (!expect(TOK_LBRACE))
535 if (!enterScope(Scope::BLOCK))
538 StatementList *statements = new (pool_) StatementList();
539 while (!match(TOK_RBRACE)) {
540 Statement *statement = localStatement();
541 if (statement == NULL)
543 statements->append(pool_, statement);
546 Scope *blockScope = scope_->unlinkIfEmpty();
547 BlockStatement *block = new (pool_) BlockStatement(blockScope, statements);
549 scope_ = scope_->enclosing();
558 if (!expect(TOK_LPAREN))
561 Expression *cond = expression();
565 if (!expect(TOK_RPAREN))
568 Statement *ifTrue = localStatement();
572 IfStatement *outer = new (pool_) IfStatement(cond, ifTrue);
574 IfStatement *last = outer;
575 while (match(TOK_ELSE)) {
576 if (!match(TOK_IF)) {
577 Statement *ifFalse = localStatement();
581 last->setIfFalse(ifFalse);
588 if (!expect(TOK_LPAREN))
591 Expression *otherCond = expression();
595 if (!expect(TOK_RPAREN))
598 Statement *otherIfTrue = localStatement();
602 IfStatement *inner = new (pool_) IfStatement(otherCond, otherIfTrue);
603 last->setIfFalse(inner);
611 Parser::localStatement()
613 Statement *statement = NULL;
615 // Statements which must be followed by a semicolon will break out of the
616 // switch. If they may end in BlockStatements, they'll immediately return.
618 switch (scanner_.peek()) {
620 if ((statement = variableDeclaration()) == NULL)
625 return forStatement();
632 statement = new (pool_) BreakStatement();
637 statement = new (pool_) ContinueStatement();
641 if ((statement = do_()) == NULL)
646 if ((statement = returnStatement()) == NULL)
651 if ((statement = enum_()) == NULL)
663 if ((statement = expressionStatement()) == NULL)
667 if (!expect(TOK_SEMICOLON))
679 if (match(TOK_NAME)) {
680 if ((name = strings_.add(scanner_.literal())) == NULL)
683 if (scope_->localLookup(name)) {
684 redeclarationError();
688 NamedEnum *e = scope_->declareEnum(name);
693 EnumStatement::EntryList *entries = new EnumStatement::EntryList();
695 if (!expect(TOK_LBRACE))
699 if (scanner_.peek() == TOK_RBRACE)
702 Name *name = expectName();
706 if (scope_->localLookup(name)) {
707 redeclarationError();
711 NamedConstant *constant = scope_->declareConstant(name);
715 Expression *expr = NULL;
716 if (match(TOK_ASSIGN)) {
717 if ((expr = expression()) == NULL)
721 entries->push_back(EnumStatement::Entry(constant, expr));
722 } while (match(TOK_COMMA));
723 if (!expect(TOK_RBRACE))
726 return new (pool_) EnumStatement(name, entries);
732 Name *name = expectName();
736 // The function name goes into the outer (global) scope.
737 assert(!scope_->enclosing());
739 if (scope_->localLookup(name)) {
740 redeclarationError();
743 NamedFunction *fun = scope_->declareFunction(name);
747 Scope *scope = enterScope(Scope::FUNCTION);
751 ParameterList *parameters = new ParameterList();
753 if (!expect(TOK_LPAREN))
755 if (!match(TOK_RPAREN)) {
757 bool ref = match(TOK_AMPERSAND);
759 Name *name = expectName();
763 if (scope_->localLookup(name)) {
764 redeclarationError();
767 Variable *arg = scope_->declareArgument(name);
771 Parameter *parameter = new (pool_) Parameter(arg, NULL, ref);
772 parameters->push_back(parameter);
774 if (!match(TOK_COMMA))
777 if (!expect(TOK_RPAREN))
781 Statement *body = block();
785 scope_ = scope_->enclosing();
786 return new (pool_) FunctionStatement(scope, fun, body, parameters);
792 StatementList *statements = new (pool_) StatementList();
794 if (!enterScope(Scope::GLOBAL))
798 Statement *statement = NULL;
800 switch (scanner_.peek()) {
808 if ((statement = function()) == NULL)
813 if ((statement = enum_()) == NULL)
815 // :TODO: require semicolon
819 fprintf(stderr, "expected function or global variable\n");
826 statements->append(pool_, statement);
829 return new (pool_) ParseTree(scope_, statements, unbound_);
832 class AstPrinter : public AstVisitor
839 for (size_t i = 0; i < level_; i++)
856 void visit(NameProxy *name) {
858 fprintf(fp_, "[ NameProxy (%s)%s\n", name->name(),
859 (name->binding() ? "" : " unbound"));
861 void visit(CallExpression *node) {
863 fprintf(fp_, "[ CallExpression\n");
865 node->callee()->accept(this);
866 for (size_t i = 0; i < node->arguments()->size(); i++)
867 node->arguments()->at(i)->accept(this);
870 void visit(FunctionStatement *node) {
872 fprintf(fp_, "[ FunctionStatement (%s)\n", node->fun()->name());
874 node->body()->accept(this);
877 void visit(ExpressionStatement *node) {
879 fprintf(fp_, "[ ExpressionStatement\n");
881 node->expression()->accept(this);
884 void visit(Assignment *node) {
886 fprintf(fp_, "[ Assignment\n");
888 node->lvalue()->accept(this);
889 node->expression()->accept(this);
892 void visit(BinaryExpression *node) {
894 fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
896 node->left()->accept(this);
897 node->right()->accept(this);
900 void visit(ReturnStatement *node) {
902 fprintf(fp_, "[ ReturnStatement\n");
904 node->expression()->accept(this);
907 void visit(ForStatement *node) {
909 fprintf(fp_, "[ ForStatement\n");
911 if (node->declaration())
912 node->declaration()->accept(this);
913 if (node->condition())
914 node->condition()->accept(this);
916 node->update()->accept(this);
917 node->body()->accept(this);
920 void visit(BlockStatement *node) {
922 fprintf(fp_, "[ BlockStatement\n");
924 for (size_t i = 0; i < node->statements()->length(); i++)
925 node->statements()->at(i)->accept(this);
928 void visit(VariableDeclaration *node) {
930 fprintf(fp_, "[ VariableDeclaration (%s)\n", node->variable()->name());
932 if (node->initialization())
933 node->initialization()->accept(this);
936 void visit(IntegerLiteral *node) {
938 fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
940 void visit(FloatLiteral *node) {
942 fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
944 void visit(IfStatement *node) {
946 fprintf(fp_, "[ IfStatement\n");
948 node->ifTrue()->accept(this);
950 node->ifFalse()->accept(this);
953 void visit(IndexExpression *node) {
955 fprintf(fp_, "[ IndexExpression\n");
957 node->left()->accept(this);
958 node->right()->accept(this);
961 void visit(EnumStatement *node) {
963 fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name() : "<anonymous>");
965 for (size_t i = 0; i < node->entries()->size(); i++) {
967 fprintf(fp_, "%s =\n", node->entries()->at(i).name->name());
968 if (node->entries()->at(i).expr) {
970 node->entries()->at(i).expr->accept(this);
976 void visit(WhileStatement *node) {
978 fprintf(fp_, "[ WhileStatement (%s)\n",
979 (node->token() == TOK_DO) ? "do" : "while");
981 if (node->token() == TOK_DO) {
982 node->condition()->accept(this);
983 node->body()->accept(this);
985 node->condition()->accept(this);
986 node->body()->accept(this);
990 void visit(BreakStatement *node) {
992 fprintf(fp_, "[ BreakStatement\n");
994 void visit(ContinueStatement *node) {
996 fprintf(fp_, "[ ContinueStatement\n");
998 void visit(StringLiteral *node) {
1000 fprintf(fp_, "[ StringLiteral\n");
1005 ParseTree::dump(FILE *fp)
1007 AstPrinter printer(fp);
1009 for (size_t i = 0; i < statements_->length(); i++)
1010 statements_->at(i)->accept(&printer);