Implemented ! operator.
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::matchSemicolon()
51 return expect(TOK_SEMICOLON);
55 Parser::expect(TokenKind kind)
57 TokenKind got = scanner_.scan();
60 cc_.reportError(scanner_.begin(), Message_WrongToken, TokenNames[kind], TokenNames[got]);
65 Parser::enterScope(Scope::Kind kind)
67 scope_ = new (pool_) Scope(pool_, kind, scope_);
68 if (!scope_->initialize())
74 Parser::bind(Handle<String> name)
76 BoundName *bound = scope_->lookup(name);
77 NameProxy *proxy = new (pool_) NameProxy(scanner_.begin(), name, bound);
78 if (!bound && !unbound_->append(proxy))
84 Parser::matchName(String **name, TokenKind kind)
86 assert(kind == TOK_NAME || kind == TOK_LABEL);
90 *name = zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
97 if (!expect(TOK_NAME))
99 return zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
103 Parser::redeclarationError(BoundName *other)
105 cc_.reportError(scanner_.begin(), Message_RedeclaredName, other->name()->chars(),
106 other->pos().line, other->pos().col);
112 TokenKind kind = scanner_.peek();
116 Local<String> name(zone_, expectName());
122 case TOK_FLOAT_LITERAL:
125 double value = Scanner::ParseDouble(scanner_.literal());
126 return new (pool_) FloatLiteral(scanner_.begin(), value);
129 case TOK_INTEGER_LITERAL:
132 int value = atoi(scanner_.literal());
133 return new (pool_) IntegerLiteral(scanner_.begin(), value);
136 case TOK_STRING_LITERAL:
139 Local<ByteArray> array(zone_, ByteArray::New(zone_, scanner_.literal_length() + 1));
142 memcpy(array->raw(), scanner_.literal(), scanner_.literal_length() + 1);
143 return new (pool_) StringLiteral(scanner_.begin(), array);
148 if (kind != TOK_ERROR)
149 cc_.reportError(scanner_.begin(), Message_ExpectedExpression, TokenNames[kind]);
158 switch (scanner_.peek()) {
162 Expression *expr = expression();
165 if (!expect(TOK_RPAREN))
176 Parser::call(Expression *callee)
178 ExpressionList *arguments = new (pool_) ExpressionList();
180 SourcePosition pos = scanner_.begin();
183 if (!match(TOK_RPAREN)) {
185 Expression *expr = expression();
189 if (!arguments->append(expr))
192 if (!match(TOK_COMMA))
196 if (!expect(TOK_RPAREN))
200 return new (pool_) CallExpression(pos, callee, arguments);
204 Parser::index(Expression *left)
206 expect(TOK_LBRACKET);
208 SourcePosition pos = scanner_.begin();
209 Expression *expr = expression();
213 if (!expect(TOK_RBRACKET))
216 return new (pool_) IndexExpression(pos, left, expr);
222 Expression *expr = prefix();
227 switch (scanner_.peek()) {
249 TokenKind token = scanner_.peek();
251 SourcePosition pos = scanner_.begin();
257 Expression *expr = expression();
260 return new (pool_) IncDecExpression(pos, token, expr, false);
267 Expression *expr = expression();
270 return new (pool_) UnaryExpression(pos, token, expr);
277 Expression *expr = primary();
279 token = scanner_.peek();
280 if (token == TOK_INCREMENT || token == TOK_DECREMENT) {
282 return new (pool_) IncDecExpression(pos, token, expr, true);
289 Parser::multiplication()
291 Expression *left = unary();
293 if (!match(TOK_SLASH) && !match(TOK_STAR))
295 SourcePosition pos = scanner_.begin();
296 TokenKind kind = scanner_.last();
297 Expression *right = unary();
300 left = new (pool_) BinaryExpression(pos, kind, left, right);
308 Expression *left = multiplication();
310 if (!match(TOK_PLUS) && !match(TOK_MINUS))
312 SourcePosition pos = scanner_.begin();
313 TokenKind kind = scanner_.last();
314 Expression *right = multiplication();
317 left = new (pool_) BinaryExpression(pos, kind, left, right);
325 Expression *left = addition();
327 if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
329 SourcePosition pos = scanner_.begin();
330 TokenKind kind = scanner_.last();
331 Expression *right = addition();
334 left = new (pool_) BinaryExpression(pos, kind, left, right);
342 Expression *left = shift();
344 if (!match(TOK_BITAND))
346 SourcePosition pos = scanner_.begin();
347 Expression *right = shift();
350 left = new (pool_) BinaryExpression(pos, TOK_BITAND, left, right);
358 Expression *left = bitand_();
360 if (!match(TOK_BITXOR))
362 SourcePosition pos = scanner_.begin();
363 Expression *right = shift();
366 left = new (pool_) BinaryExpression(pos, TOK_BITXOR, left, right);
374 Expression *left = bitxor();
376 if (!match(TOK_BITOR))
378 SourcePosition pos = scanner_.begin();
379 Expression *right = bitxor();
382 left = new (pool_) BinaryExpression(pos, TOK_BITOR, left, right);
390 Expression *left = bitor_();
392 TokenKind kind = scanner_.peek();
393 if (kind < TOK_LT || kind > TOK_GE)
396 SourcePosition pos = scanner_.begin();
397 Expression *right = shift();
400 left = new (pool_) BinaryExpression(pos, kind, left, right);
408 Expression *left = relational();
410 if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
412 TokenKind kind = scanner_.last();
413 SourcePosition pos = scanner_.begin();
414 Expression *right = relational();
417 left = new (pool_) BinaryExpression(pos, kind, left, right);
425 Expression *left = equals();
429 SourcePosition pos = scanner_.begin();
430 Expression *right = equals();
433 left = new (pool_) BinaryExpression(pos, TOK_AND, left, right);
441 Expression *left = and_();
445 SourcePosition pos = scanner_.begin();
446 Expression *right = and_();
449 left = new (pool_) BinaryExpression(pos, TOK_OR, left, right);
457 Expression *left = or_();
459 TokenKind token = scanner_.peek();
460 if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
463 SourcePosition pos = scanner_.begin();
464 Expression *expr = or_();
468 left = new (pool_) Assignment(pos, token, left, expr);
482 SourcePosition pos = scanner_.begin();
485 if (!expect(TOK_LPAREN))
488 Expression *condition = expression();
492 if (!expect(TOK_RPAREN))
495 // Inside a loop, we force the creation of a scope object. Normally, we
496 // will cull scopes if they have no variables. However inside a loop,
497 // function calls that take references may compute a LIFO object, and to
498 // avoid temporarily leaking many such objects, we ensure that a scope
499 // is present so a LIFO slot can be allocated.
500 if (!enterScope(Scope::BLOCK))
504 if (scanner_.peek() == TOK_LBRACE)
505 body = block(scope_);
507 body = localStatement();
511 scope_ = scope_->enclosing();
512 return new (pool_) WhileStatement(pos, TOK_WHILE, condition, body);
518 SourcePosition pos = scanner_.begin();
521 // See the comment in ::while_() as to why we force a scope.
522 if (!enterScope(Scope::BLOCK))
526 if (scanner_.peek() == TOK_LBRACE)
527 body = block(scope_);
529 body = localStatement();
533 scope_ = scope_->enclosing();
535 if (!expect(TOK_WHILE))
538 if (!expect(TOK_LPAREN))
540 Expression *condition = expression();
543 if (!expect(TOK_RPAREN))
546 return new (pool_) WhileStatement(pos, TOK_DO, condition, body);
550 Parser::forStatement()
552 SourcePosition pos = scanner_.begin();
554 if (!expect(TOK_LPAREN))
557 Statement *decl = NULL;
558 if (!match(TOK_SEMICOLON)) {
559 if (scanner_.peek() == TOK_NEW) {
560 if (!enterScope(Scope::BLOCK))
562 if ((decl = variableDeclaration()) == NULL)
565 if ((decl = expressionStatement()) == NULL)
568 if (!expect(TOK_SEMICOLON))
572 Expression *condition = NULL;
573 if (!match(TOK_SEMICOLON)) {
574 if ((condition = expression()) == NULL)
576 if (!expect(TOK_SEMICOLON))
580 Statement *update = NULL;
581 if (!match(TOK_RPAREN)) {
582 if ((update = expressionStatement()) == NULL)
584 if (!expect(TOK_RPAREN))
588 // See the comment in ::while_() as to why we force a scope.
589 if (!enterScope(Scope::BLOCK))
593 if (scanner_.peek() == TOK_LBRACE)
594 body = block(scope_);
596 body = localStatement();
600 scope_ = scope_->enclosing();
602 Scope *declScope = NULL;
603 if (decl && decl->isVariableDeclaration()) {
604 declScope = scope_->unlinkIfEmpty();
605 scope_ = scope_->enclosing();
607 return new (pool_) ForStatement(pos, declScope, decl, condition, update, body);
610 VariableDeclaration *
611 Parser::variableDeclaration()
613 if (!expect(TOK_NEW))
616 Local<String> tag(zone_);
617 if (matchName(tag.ref(), TOK_LABEL) && !tag)
620 Local<String> name(zone_, expectName());
624 if (BoundName *other = scope_->localLookup(name)) {
625 redeclarationError(other);
629 Variable *var = scope_->declareVariable(name, scanner_.begin());
633 ExpressionList *postDimensions = NULL;
634 if (scanner_.peek() == TOK_LBRACKET) {
635 postDimensions = new (pool_) ExpressionList();
636 while (match(TOK_LBRACKET)) {
637 Expression *dim = expression();
641 if (!expect(TOK_RBRACKET))
644 if (!postDimensions->append(dim))
649 Expression *expr = NULL;
650 if (match(TOK_ASSIGN)) {
651 if ((expr = expression()) == NULL)
655 return new (pool_) VariableDeclaration(var->pos(), var, tag, postDimensions, expr);
659 Parser::returnStatement()
661 SourcePosition pos = scanner_.begin();
662 if (!expect(TOK_RETURN))
665 Expression *expr = NULL;
666 if (scanner_.peek() != TOK_SEMICOLON) {
667 if ((expr = expression()) == NULL)
670 // We only care about non-void returns when determining whether a
671 // tagless function is non-void.
672 encounteredReturn_ = true;
675 return new (pool_) ReturnStatement(pos, expr);
679 Parser::expressionStatement()
681 Expression *left = assignment();
685 return new (pool_) ExpressionStatement(left);
689 Parser::block(Scope *scope)
691 if (!expect(TOK_LBRACE))
694 SourcePosition pos = scanner_.begin();
695 if (!scope && !enterScope(Scope::BLOCK))
698 StatementList *statements = new (pool_) StatementList();
699 while (!match(TOK_RBRACE)) {
700 Statement *statement = localStatement();
701 if (statement == NULL)
703 if (!statements->append(statement))
708 scope = scope_->unlinkIfEmpty();
709 scope_ = scope_->enclosing();
711 BlockStatement *block = new (pool_) BlockStatement(pos, scope, statements);
720 SourcePosition pos = scanner_.begin();
721 if (!expect(TOK_LPAREN))
724 Expression *cond = expression();
728 if (!expect(TOK_RPAREN))
731 Statement *ifTrue = localStatement();
735 IfStatement *outer = new (pool_) IfStatement(pos, cond, ifTrue);
737 IfStatement *last = outer;
738 while (match(TOK_ELSE)) {
739 if (!match(TOK_IF)) {
740 Statement *ifFalse = localStatement();
744 last->setIfFalse(ifFalse);
748 SourcePosition pos = scanner_.begin();
749 if (!expect(TOK_LPAREN))
752 Expression *otherCond = expression();
756 if (!expect(TOK_RPAREN))
759 Statement *otherIfTrue = localStatement();
763 IfStatement *inner = new (pool_) IfStatement(pos, otherCond, otherIfTrue);
764 last->setIfFalse(inner);
772 Parser::localStatement()
774 Statement *statement = NULL;
776 // Statements which must be followed by a semicolon will break out of the
777 // switch. If they may end in BlockStatements, they'll immediately return.
779 switch (scanner_.peek()) {
781 if ((statement = variableDeclaration()) == NULL)
786 return forStatement();
793 statement = new (pool_) BreakStatement(scanner_.begin());
798 statement = new (pool_) ContinueStatement(scanner_.begin());
802 if ((statement = do_()) == NULL)
807 if ((statement = returnStatement()) == NULL)
812 if ((statement = enum_()) == NULL)
827 if ((statement = expressionStatement()) == NULL)
831 if (!expect(TOK_SEMICOLON))
842 SourcePosition pos = scanner_.begin();
844 Local<String> name(zone_);
845 if (matchName(name.ref())) {
849 if (BoundName *other = scope_->localLookup(name)) {
850 redeclarationError(other);
854 if (!scope_->declareEnum(name, scanner_.begin()))
858 EnumStatement::EntryList *entries = new (pool_) EnumStatement::EntryList();
860 if (!expect(TOK_LBRACE))
864 if (scanner_.peek() == TOK_RBRACE)
867 Local<String> name(zone_, expectName());
871 if (BoundName *other = scope_->localLookup(name)) {
872 redeclarationError(other);
876 NamedConstant *constant = scope_->declareConstant(name, scanner_.begin());
880 Expression *expr = NULL;
881 if (match(TOK_ASSIGN)) {
882 if ((expr = expression()) == NULL)
886 if (!entries->append(EnumStatement::Entry(constant, expr)))
888 } while (match(TOK_COMMA));
889 if (!expect(TOK_RBRACE))
892 return new (pool_) EnumStatement(pos, name, entries);
896 Parser::function(TokenKind token)
898 Local<String> name(zone_, expectName());
902 SourcePosition pos = scanner_.begin();
904 // The function name goes into the outer (global) scope.
905 assert(!scope_->enclosing());
908 if (BoundName *other = scope_->localLookup(name)) {
909 // We allow a redeclaration if the new type is a public, and the old
910 // type was a forward. However, the forward must come first.
911 if (other->kind() != BoundName::FUNCTION) {
912 redeclarationError(other);
916 fun = other->toNamedFunction();
917 if (!fun->isForward() || token != TOK_PUBLIC) {
918 redeclarationError(other);
922 // We've got the same name, but the original is a forward, so let it
923 // slide and re-use the original declaration.
924 fun->setImplemented(scanner_.begin());
926 fun = scope_->declareFunction(name, scanner_.begin(), token);
931 Scope *scope = enterScope(Scope::FUNCTION);
935 ParameterList *parameters = new (pool_) ParameterList();
937 if (!expect(TOK_LPAREN))
939 if (!match(TOK_RPAREN)) {
941 bool isConst = match(TOK_CONST);
942 bool ref = match(TOK_AMPERSAND);
944 SourcePosition begin = scanner_.begin();
945 Local<String> tag(zone_);
946 if (matchName(tag.ref(), TOK_LABEL) && !tag)
949 if (match(TOK_ELLIPSES)) {
950 if (isConst || ref) {
951 cc_.reportError(begin, Message_InvalidVarArgs);
955 if (token != TOK_NATIVE) {
956 cc_.reportError(begin, Message_VarArgsOutsideNative);
960 fun->markNativeVariadic();
964 Local<String> name(zone_, expectName());
968 if (BoundName *other = scope_->localLookup(name)) {
969 redeclarationError(other);
972 Variable *arg = scope_->declareArgument(name, scanner_.begin());
976 // Look for dimensions.
977 ExpressionList *postDimensions = NULL;
978 while (match(TOK_LBRACKET)) {
980 postDimensions = new (pool_) ExpressionList();
982 Expression *dim = NULL;
983 if (!match(TOK_RBRACKET)) {
984 if ((dim = expression()) == NULL)
986 if (!expect(TOK_RBRACKET))
990 if (!postDimensions->append(dim))
994 Parameter *parameter = new (pool_) Parameter(arg, tag, ref, isConst, postDimensions);
995 if (!parameters->append(parameter))
998 if (!match(TOK_COMMA))
1001 if (!expect(TOK_RPAREN))
1005 encounteredReturn_ = false;
1007 Statement *body = NULL;
1008 if (token != TOK_FORWARD && token != TOK_NATIVE) {
1014 scope_ = scope_->enclosing();
1015 return new (pool_) FunctionStatement(pos, scope, fun, body, parameters, encounteredReturn_);
1021 StatementList *statements = new (pool_) StatementList();
1023 if (!enterScope(Scope::GLOBAL))
1027 Statement *statement = NULL;
1029 switch (scanner_.peek()) {
1037 if ((statement = function(TOK_NAME)) == NULL)
1045 TokenKind kind = scanner_.scan();
1046 if ((statement = function(kind)) == NULL)
1048 if ((kind == TOK_FORWARD || kind == TOK_NATIVE) && !matchSemicolon())
1055 // if ((statement = variableDeclaration()) == NULL)
1061 if ((statement = enum_()) == NULL)
1063 if (!matchSemicolon())
1068 cc_.reportError(scanner_.begin(), Message_ExpectedGlobal);
1075 if (!statements->append(statement))
1079 return new (pool_) ParseTree(scope_, statements, unbound_);
1082 class AstPrinter : public AstVisitor
1089 for (size_t i = 0; i < level_; i++)
1100 AstPrinter(FILE *fp)
1106 void visit(NameProxy *name) {
1108 fprintf(fp_, "[ NameProxy (%s)%s\n", name->name()->chars(),
1109 (name->binding() ? "" : " unbound"));
1111 void visit(CallExpression *node) {
1113 fprintf(fp_, "[ CallExpression\n");
1115 node->callee()->accept(this);
1116 for (size_t i = 0; i < node->arguments()->length(); i++)
1117 node->arguments()->at(i)->accept(this);
1120 void visit(FunctionStatement *node) {
1122 fprintf(fp_, "[ FunctionStatement (%s)\n", node->fun()->name()->chars());
1125 node->body()->accept(this);
1128 void visit(ExpressionStatement *node) {
1130 fprintf(fp_, "[ ExpressionStatement\n");
1132 node->expression()->accept(this);
1135 void visit(Assignment *node) {
1137 fprintf(fp_, "[ Assignment\n");
1139 node->lvalue()->accept(this);
1140 node->expression()->accept(this);
1143 void visit(BinaryExpression *node) {
1145 fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
1147 node->left()->accept(this);
1148 node->right()->accept(this);
1151 void visit(UnaryExpression *node) {
1153 fprintf(fp_, "[ UnaryExpression (%s)\n", TokenNames[node->token()]);
1155 node->expression()->accept(this);
1158 void visit(ReturnStatement *node) {
1160 fprintf(fp_, "[ ReturnStatement\n");
1162 if (node->expression())
1163 node->expression()->accept(this);
1166 void visit(ForStatement *node) {
1168 fprintf(fp_, "[ ForStatement\n");
1170 if (node->initialization())
1171 node->initialization()->accept(this);
1172 if (node->condition())
1173 node->condition()->accept(this);
1175 node->update()->accept(this);
1176 node->body()->accept(this);
1179 void visit(BlockStatement *node) {
1181 fprintf(fp_, "[ BlockStatement\n");
1183 for (size_t i = 0; i < node->statements()->length(); i++)
1184 node->statements()->at(i)->accept(this);
1187 void visit(VariableDeclaration *node) {
1189 fprintf(fp_, "[ VariableDeclaration (%s)\n", node->variable()->name()->chars());
1191 if (node->initialization())
1192 node->initialization()->accept(this);
1195 void visit(IntegerLiteral *node) {
1197 fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
1199 void visit(FloatLiteral *node) {
1201 fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
1203 void visit(IfStatement *node) {
1205 fprintf(fp_, "[ IfStatement\n");
1207 node->ifTrue()->accept(this);
1208 if (node->ifFalse())
1209 node->ifFalse()->accept(this);
1212 void visit(IndexExpression *node) {
1214 fprintf(fp_, "[ IndexExpression\n");
1216 node->left()->accept(this);
1217 node->right()->accept(this);
1220 void visit(EnumStatement *node) {
1222 fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name()->chars() : "<anonymous>");
1224 for (size_t i = 0; i < node->entries()->length(); i++) {
1226 fprintf(fp_, "%s =\n", node->entries()->at(i).name->name()->chars());
1227 if (node->entries()->at(i).expr) {
1229 node->entries()->at(i).expr->accept(this);
1235 void visit(WhileStatement *node) {
1237 fprintf(fp_, "[ WhileStatement (%s)\n",
1238 (node->token() == TOK_DO) ? "do" : "while");
1240 if (node->token() == TOK_DO) {
1241 node->condition()->accept(this);
1242 node->body()->accept(this);
1244 node->condition()->accept(this);
1245 node->body()->accept(this);
1249 void visit(BreakStatement *node) {
1251 fprintf(fp_, "[ BreakStatement\n");
1253 void visit(ContinueStatement *node) {
1255 fprintf(fp_, "[ ContinueStatement\n");
1257 void visit(StringLiteral *node) {
1259 fprintf(fp_, "[ StringLiteral\n");
1261 void visit(IncDecExpression *node) {
1263 fprintf(fp_, "[ IncDecExpression (postfix=%d)\n", node->postfix());
1265 node->expression()->accept(this);
1271 ParseTree::dump(FILE *fp)
1273 AstPrinter printer(fp);
1275 for (size_t i = 0; i < statements_->length(); i++)
1276 statements_->at(i)->accept(&printer);