1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
36 #include "Pooling.hpp"
37 #include "Options.hpp"
45 virtual void Output(struct CYOutput &out) const = 0;
63 CYOutput(std::ostream &out, CYOptions &options) :
73 void Check(char value);
76 CYOutput &operator <<(char rhs);
77 CYOutput &operator <<(const char *rhs);
79 _finline CYOutput &operator <<(const CYThing *rhs) {
85 _finline CYOutput &operator <<(const CYThing &rhs) {
91 struct CYPropertyName {
92 virtual void PropertyName(CYOutput &out) const = 0;
94 virtual ~CYPropertyName() {
109 CYNoBrace = (1 << 0),
110 CYNoFunction = (1 << 1),
113 CYNoRightHand = (1 << 4),
114 CYNoDangle = (1 << 5),
115 CYNoInteger = (1 << 6),
116 CYNoBF = (CYNoBrace | CYNoFunction),
119 _finline CYFlags operator ~(CYFlags rhs) {
120 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
123 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
124 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
127 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
128 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
131 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
132 return lhs = lhs | rhs;
135 _finline CYFlags CYLeft(CYFlags flags) {
136 return flags & ~(CYNoDangle | CYNoInteger);
139 _finline CYFlags CYRight(CYFlags flags) {
140 return flags & ~CYNoBF;
143 _finline CYFlags CYCenter(CYFlags flags) {
144 return CYLeft(CYRight(flags));
150 virtual ~CYStatement() {
153 void Single(CYOutput &out, CYFlags flags) const;
154 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
156 virtual CYStatement *Replace(CYContext &context) = 0;
159 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
162 struct CYStatements {
172 operator CYStatement *() const {
176 CYStatements &operator ->*(CYStatement *next) {
178 if (first_ == NULL) {
181 } else for (;; last_ = last_->next_)
182 if (last_->next_ == NULL) {
192 virtual ~CYClassName() {
195 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
196 virtual void ClassName(CYOutput &out, bool object) const = 0;
206 CYWord(const char *word) :
211 void Set(const char *value) {
215 virtual const char *Word() const;
216 virtual void Output(CYOutput &out) const;
218 virtual CYExpression *ClassName(CYContext &context, bool object);
219 virtual void ClassName(CYOutput &out, bool object) const;
220 virtual void PropertyName(CYOutput &out) const;
223 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
225 return lhs << rhs.Word();
228 struct CYIdentifier :
229 CYNext<CYIdentifier>,
232 CYIdentifier *replace_;
236 CYIdentifier(const char *word) :
244 virtual const char *Word() const;
245 CYIdentifier *Replace(CYContext &context);
253 CYComment(const char *value) :
258 virtual CYStatement *Replace(CYContext &context);
259 virtual void Output(CYOutput &out, CYFlags flags) const;
266 CYStatement *statement_;
268 CYLabel(CYIdentifier *name, CYStatement *statement) :
270 statement_(statement)
274 virtual CYStatement *Replace(CYContext &context);
275 virtual void Output(CYOutput &out, CYFlags flags) const;
278 struct CYCStringLess :
279 std::binary_function<const char *, const char *, bool>
281 _finline bool operator ()(const char *lhs, const char *rhs) const {
282 return strcmp(lhs, rhs) < 0;
286 struct CYIdentifierValueLess :
287 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
289 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
290 return CYCStringLess()(lhs->Word(), rhs->Word());
294 enum CYIdentifierFlags {
295 CYIdentifierArgument,
296 CYIdentifierVariable,
302 typedef std::set<const char *, CYCStringLess> CYCStringSet;
303 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
304 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
306 struct CYIdentifierUsage {
307 CYIdentifier *identifier_;
311 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
317 CYStatement *&statements_;
321 CYIdentifierAddressFlagsMap internal_;
322 CYIdentifierValueSet identifiers_;
324 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
329 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
330 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
331 void Merge(CYContext &context, CYIdentifier *identifier);
332 void Scope(CYContext &context, CYStatement *&statements);
338 CYStatement *statements_;
340 CYProgram(CYStatement *statements) :
341 statements_(statements)
345 virtual void Replace(CYContext &context);
346 virtual void Output(CYOutput &out) const;
358 CYIdentifierUsageVector rename_;
360 CYNonLocal *nonlocal_;
361 CYNonLocal *nextlocal_;
364 CYContext(CYOptions &options) :
374 virtual ~CYContext() {
377 template <typename Type_>
378 void ReplaceAll(Type_ *&values) {
379 Type_ **last(&values);
380 CYForEach (next, values) {
381 Replace(*last = next);
383 last = &(*last)->next_;
387 template <typename Type_>
388 void Replace(Type_ *&value) {
389 for (;;) if (value == NULL)
392 Type_ *replace(value->Replace(*this));
393 if (replace != value)
399 void NonLocal(CYStatement *&statements);
400 CYIdentifier *Unique();
404 CYIdentifier *identifier_;
411 CYIdentifier *Target(CYContext &context) {
412 if (identifier_ == NULL)
413 identifier_ = context.Unique();
421 CYIdentifier *identifier_;
428 CYIdentifier *Identifier(CYContext &context) {
430 return next_->Identifier(context);
431 if (identifier_ == NULL)
432 identifier_ = context.Unique();
441 CYStatement *statements_;
443 CYBlock(CYStatement *statements) :
444 statements_(statements)
448 operator CYStatement *() const {
452 void AddPrev(CYStatement *statement) {
453 CYSetLast(statement) = statements_;
454 statements_ = statement;
457 virtual CYStatement *Replace(CYContext &context);
459 virtual void Output(CYOutput &out) const;
460 virtual void Output(CYOutput &out, CYFlags flags) const;
468 public std::streambuf
471 CYBuffer(const char *start, const char *end) {
472 setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
477 CYStream(const char *start, const char *end) :
478 std::istream(&buffer_),
484 struct CYForInitialiser {
485 virtual ~CYForInitialiser() {
488 virtual CYExpression *Replace(CYContext &context) = 0;
489 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
492 struct CYForInInitialiser {
493 virtual ~CYForInInitialiser() {
496 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
497 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
499 virtual CYExpression *Replace(CYContext &context) = 0;
500 virtual CYAssignment *Assignment(CYContext &context) = 0;
502 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
508 struct CYExpression :
509 CYNext<CYExpression>,
515 virtual int Precedence() const = 0;
517 virtual bool RightHand() const {
521 virtual void ForIn(CYOutput &out, CYFlags flags) const;
522 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
524 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
526 virtual void Output(CYOutput &out) const;
527 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
528 void Output(CYOutput &out, int precedence, CYFlags flags) const;
530 virtual CYExpression *ClassName(CYContext &context, bool object);
531 virtual void ClassName(CYOutput &out, bool object) const;
533 virtual CYExpression *Replace(CYContext &context) = 0;
534 virtual CYAssignment *Assignment(CYContext &context);
536 virtual CYExpression *Primitive(CYContext &context) {
540 virtual CYNumber *Number(CYContext &context) {
544 virtual CYString *String(CYContext &context) {
548 virtual const char *Word() const {
553 #define CYAlphabetic(value) \
554 virtual bool Alphabetic() const { \
558 #define CYPrecedence(value) \
559 static const int Precedence_ = value; \
560 virtual int Precedence() const { \
561 return Precedence_; \
564 #define CYRightHand(value) \
565 virtual bool RightHand() const { \
572 CYExpression *expressions_;
574 CYCompound(CYExpression *expressions = NULL) :
575 expressions_(expressions)
579 void AddPrev(CYExpression *expression) {
580 CYSetLast(expression) = expressions_;
581 expressions_ = expression;
586 virtual CYExpression *Replace(CYContext &context);
587 void Output(CYOutput &out, CYFlags flags) const;
589 virtual CYExpression *Primitive(CYContext &context);
592 struct CYDeclaration;
594 struct CYFunctionParameter :
595 CYNext<CYFunctionParameter>,
598 CYForInInitialiser *initialiser_;
600 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
601 CYNext<CYFunctionParameter>(next),
602 initialiser_(initialiser)
606 void Replace(CYContext &context, CYBlock &code);
607 void Output(CYOutput &out) const;
610 struct CYComprehension :
611 CYNext<CYComprehension>,
614 CYComprehension(CYComprehension *next = NULL) :
615 CYNext<CYComprehension>(next)
619 CYComprehension *Modify(CYComprehension *next) {
624 virtual const char *Name() const = 0;
626 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
627 CYFunctionParameter *Parameters(CYContext &context) const;
628 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
629 virtual void Output(CYOutput &out) const = 0;
632 struct CYForInComprehension :
638 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
639 CYComprehension(next),
645 virtual const char *Name() const {
646 return name_->Word();
649 virtual CYFunctionParameter *Parameter(CYContext &context) const;
650 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
651 virtual void Output(CYOutput &out) const;
654 struct CYForOfComprehension :
660 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
661 CYComprehension(next),
667 virtual const char *Name() const {
668 return name_->Word();
671 virtual CYFunctionParameter *Parameter(CYContext &context) const;
672 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
673 virtual void Output(CYOutput &out) const;
676 struct CYIfComprehension :
681 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
682 CYComprehension(next),
687 virtual const char *Name() const {
691 virtual CYFunctionParameter *Parameter(CYContext &context) const;
692 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
693 virtual void Output(CYOutput &out) const;
696 struct CYArrayComprehension :
699 CYExpression *expression_;
700 CYComprehension *comprehensions_;
702 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
703 expression_(expression),
704 comprehensions_(comprehensions)
710 virtual CYExpression *Replace(CYContext &context);
711 virtual void Output(CYOutput &out, CYFlags flags) const;
724 virtual CYExpression *Replace(CYContext &context);
738 CYRange(uint64_t lo, uint64_t hi) :
743 bool operator [](uint8_t value) const {
744 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
747 void operator()(uint8_t value) {
750 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
754 extern CYRange DigitRange_;
755 extern CYRange WordStartRange_;
756 extern CYRange WordEndRange_;
771 CYString(const char *value) :
777 CYString(const char *value, size_t size) :
783 CYString(const CYWord *word) :
784 value_(word->Word()),
785 size_(strlen(value_))
789 const char *Value() const {
793 virtual const char *Word() const;
795 virtual CYNumber *Number(CYContext &context);
796 virtual CYString *String(CYContext &context);
798 CYString *Concat(CYContext &out, CYString *rhs) const;
799 virtual void Output(CYOutput &out, CYFlags flags) const;
800 virtual void PropertyName(CYOutput &out) const;
809 CYNumber(double value) :
814 double Value() const {
818 virtual CYNumber *Number(CYContext &context);
819 virtual CYString *String(CYContext &context);
821 virtual void Output(CYOutput &out, CYFlags flags) const;
822 virtual void PropertyName(CYOutput &out) const;
830 CYRegEx(const char *value) :
835 const char *Value() const {
839 virtual void Output(CYOutput &out, CYFlags flags) const;
851 virtual CYNumber *Number(CYContext &context);
852 virtual CYString *String(CYContext &context);
854 virtual void Output(CYOutput &out, CYFlags flags) const;
866 virtual CYExpression *Replace(CYContext &context);
867 virtual void Output(CYOutput &out, CYFlags flags) const;
873 virtual bool Value() const = 0;
874 virtual void Output(CYOutput &out, CYFlags flags) const;
886 virtual bool Value() const {
890 virtual CYNumber *Number(CYContext &context);
891 virtual CYString *String(CYContext &context);
903 virtual bool Value() const {
907 virtual CYNumber *Number(CYContext &context);
908 virtual CYString *String(CYContext &context);
916 CYVariable(CYIdentifier *name) :
921 CYVariable(const char *name) :
922 name_(new($pool) CYIdentifier(name))
929 virtual CYExpression *Replace(CYContext &context);
930 virtual void Output(CYOutput &out, CYFlags flags) const;
938 CYPrefix(CYExpression *rhs) :
943 virtual bool Alphabetic() const = 0;
944 virtual const char *Operator() const = 0;
948 virtual CYExpression *Replace(CYContext &context);
949 virtual void Output(CYOutput &out, CYFlags flags) const;
958 CYInfix(CYExpression *lhs, CYExpression *rhs) :
964 void SetLeft(CYExpression *lhs) {
968 virtual bool Alphabetic() const = 0;
969 virtual const char *Operator() const = 0;
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
980 CYPostfix(CYExpression *lhs) :
985 virtual const char *Operator() const = 0;
989 virtual CYExpression *Replace(CYContext &context);
990 virtual void Output(CYOutput &out, CYFlags flags) const;
993 struct CYAssignment :
999 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1005 void SetLeft(CYExpression *lhs) {
1009 virtual const char *Operator() const = 0;
1013 virtual CYExpression *Replace(CYContext &context);
1014 virtual void Output(CYOutput &out, CYFlags flags) const;
1022 CYExpression *value_;
1024 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1025 CYNext<CYArgument>(next),
1031 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1032 CYNext<CYArgument>(next),
1038 CYArgument *Replace(CYContext &context);
1039 void Output(CYOutput &out) const;
1055 CYExpression *case_;
1056 CYStatement *statements_;
1058 CYClause(CYExpression *_case, CYStatement *statements) :
1060 statements_(statements)
1064 void Replace(CYContext &context);
1065 virtual void Output(CYOutput &out) const;
1072 CYExpression *value_;
1074 CYElement(CYExpression *value, CYElement *next) :
1075 CYNext<CYElement>(next),
1080 void Replace(CYContext &context);
1081 void Output(CYOutput &out) const;
1087 CYElement *elements_;
1089 CYArray(CYElement *elements = NULL) :
1094 virtual CYExpression *Replace(CYContext &context);
1095 virtual void Output(CYOutput &out, CYFlags flags) const;
1102 CYPropertyName *name_;
1103 CYExpression *value_;
1105 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1106 CYNext<CYProperty>(next),
1112 void Replace(CYContext &context);
1113 virtual void Output(CYOutput &out) const;
1116 struct CYDeclaration :
1119 CYIdentifier *identifier_;
1120 CYExpression *initialiser_;
1122 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1123 identifier_(identifier),
1124 initialiser_(initialiser)
1128 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1129 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1131 virtual CYExpression *Replace(CYContext &context);
1133 virtual CYAssignment *Assignment(CYContext &context);
1134 CYVariable *Variable(CYContext &context);
1136 virtual void Output(CYOutput &out, CYFlags flags) const;
1139 struct CYDeclarations :
1140 CYNext<CYDeclarations>,
1143 CYDeclaration *declaration_;
1145 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1146 CYNext<CYDeclarations>(next),
1147 declaration_(declaration)
1151 void Replace(CYContext &context);
1153 CYCompound *Compound(CYContext &context);
1154 CYProperty *Property(CYContext &context);
1155 CYArgument *Argument(CYContext &context);
1156 CYFunctionParameter *Parameter(CYContext &context);
1158 virtual void Output(CYOutput &out) const;
1159 virtual void Output(CYOutput &out, CYFlags flags) const;
1162 struct CYForDeclarations :
1165 CYDeclarations *declarations_;
1167 CYForDeclarations(CYDeclarations *declarations) :
1168 declarations_(declarations)
1172 virtual CYCompound *Replace(CYContext &context);
1173 virtual void Output(CYOutput &out, CYFlags flags) const;
1179 CYDeclarations *declarations_;
1181 CYVar(CYDeclarations *declarations) :
1182 declarations_(declarations)
1186 virtual CYStatement *Replace(CYContext &context);
1187 virtual void Output(CYOutput &out, CYFlags flags) const;
1190 struct CYLetStatement :
1193 CYDeclarations *declarations_;
1196 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1197 declarations_(declarations),
1202 virtual CYStatement *Replace(CYContext &context);
1203 virtual void Output(CYOutput &out, CYFlags flags) const;
1209 CYForInitialiser *initialiser_;
1210 CYExpression *test_;
1211 CYExpression *increment_;
1214 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1215 initialiser_(initialiser),
1217 increment_(increment),
1222 virtual CYStatement *Replace(CYContext &context);
1223 virtual void Output(CYOutput &out, CYFlags flags) const;
1229 CYForInInitialiser *initialiser_;
1233 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1234 initialiser_(initialiser),
1240 virtual CYStatement *Replace(CYContext &context);
1241 virtual void Output(CYOutput &out, CYFlags flags) const;
1247 CYForInInitialiser *initialiser_;
1251 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1252 initialiser_(initialiser),
1258 virtual CYStatement *Replace(CYContext &context);
1259 virtual void Output(CYOutput &out, CYFlags flags) const;
1265 CYProperty *properties_;
1267 CYObject(CYProperty *properties = NULL) :
1268 properties_(properties)
1272 virtual CYExpression *Replace(CYContext &context);
1273 void Output(CYOutput &out, CYFlags flags) const;
1279 CYExpression *object_;
1280 CYExpression *property_;
1282 CYMember(CYExpression *object, CYExpression *property) :
1288 void SetLeft(CYExpression *object) {
1293 struct CYDirectMember :
1296 CYDirectMember(CYExpression *object, CYExpression *property) :
1297 CYMember(object, property)
1304 virtual CYExpression *Replace(CYContext &context);
1305 virtual void Output(CYOutput &out, CYFlags flags) const;
1308 struct CYIndirectMember :
1311 CYIndirectMember(CYExpression *object, CYExpression *property) :
1312 CYMember(object, property)
1319 virtual CYExpression *Replace(CYContext &context);
1320 virtual void Output(CYOutput &out, CYFlags flags) const;
1329 CYExpression *constructor_;
1330 CYArgument *arguments_;
1332 New(CYExpression *constructor, CYArgument *arguments) :
1333 constructor_(constructor),
1334 arguments_(arguments)
1338 virtual int Precedence() const {
1339 return arguments_ == NULL ? 2 : 1;
1344 virtual CYExpression *Replace(CYContext &context);
1345 virtual void Output(CYOutput &out, CYFlags flags) const;
1347 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1355 CYExpression *function_;
1356 CYArgument *arguments_;
1358 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1359 function_(function),
1360 arguments_(arguments)
1367 virtual CYExpression *Replace(CYContext &context);
1368 virtual void Output(CYOutput &out, CYFlags flags) const;
1370 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1375 struct CYRubyBlock :
1378 CYExpression *call_;
1381 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1390 virtual CYExpression *Replace(CYContext &context);
1391 virtual void Output(CYOutput &out, CYFlags flags) const;
1397 CYExpression *test_;
1399 CYStatement *false_;
1401 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1408 virtual CYStatement *Replace(CYContext &context);
1409 virtual void Output(CYOutput &out, CYFlags flags) const;
1415 CYExpression *test_;
1418 CYDoWhile(CYExpression *test, CYStatement *code) :
1424 virtual CYStatement *Replace(CYContext &context);
1425 virtual void Output(CYOutput &out, CYFlags flags) const;
1431 CYExpression *test_;
1434 CYWhile(CYExpression *test, CYStatement *code) :
1440 virtual CYStatement *Replace(CYContext &context);
1441 virtual void Output(CYOutput &out, CYFlags flags) const;
1444 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1446 CYIdentifier *name_;
1447 CYFunctionParameter *parameters_;
1450 CYNonLocal *nonlocal_;
1453 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1455 parameters_(parameters),
1461 virtual ~CYFunction() {
1464 void Inject(CYContext &context);
1465 virtual void Replace_(CYContext &context, bool outer);
1466 virtual void Output(CYOutput &out, CYFlags flags) const;
1469 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1470 struct CYFunctionExpression :
1474 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1475 CYFunction(name, parameters, statements)
1482 virtual CYExpression *Replace(CYContext &context);
1483 virtual void Output(CYOutput &out, CYFlags flags) const;
1486 // XXX: this should derive from CYAnonymousFunction
1491 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1492 CYFunction(NULL, parameters, statements)
1499 virtual CYExpression *Replace(CYContext &context);
1500 virtual void Output(CYOutput &out, CYFlags flags) const;
1503 // XXX: this should derive from CYAnonymousFunctionExpression
1505 CYFunctionExpression
1507 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1508 CYFunctionExpression(NULL, parameters, statements)
1512 virtual CYExpression *Replace(CYContext &context);
1513 virtual void Output(CYOutput &out, CYFlags flags) const;
1516 // XXX: this should derive from CYNamedFunction
1517 struct CYFunctionStatement :
1521 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1522 CYFunction(name, parameters, statements)
1526 virtual CYStatement *Replace(CYContext &context);
1527 virtual void Output(CYOutput &out, CYFlags flags) const;
1533 CYExpression *expression_;
1535 CYExpress(CYExpression *expression) :
1536 expression_(expression)
1538 if (expression == NULL)
1542 virtual CYStatement *Replace(CYContext &context);
1543 virtual void Output(CYOutput &out, CYFlags flags) const;
1549 CYIdentifier *label_;
1551 CYContinue(CYIdentifier *label) :
1556 virtual CYStatement *Replace(CYContext &context);
1557 virtual void Output(CYOutput &out, CYFlags flags) const;
1563 CYIdentifier *label_;
1565 CYBreak(CYIdentifier *label) :
1570 virtual CYStatement *Replace(CYContext &context);
1571 virtual void Output(CYOutput &out, CYFlags flags) const;
1577 CYExpression *value_;
1579 CYReturn(CYExpression *value) :
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1591 virtual CYStatement *Replace(CYContext &context);
1592 virtual void Output(CYOutput &out, CYFlags flags) const;
1600 CYFinally(CYStatement *statements) :
1605 void Replace(CYContext &context);
1606 virtual void Output(CYOutput &out) const;
1609 struct CYTypeSpecifier :
1612 virtual CYExpression *Replace(CYContext &context) = 0;
1621 virtual CYExpression *Replace(CYContext &context);
1622 virtual void Output(CYOutput &out) const;
1625 struct CYTypeVariable :
1628 CYIdentifier *name_;
1630 CYTypeVariable(CYIdentifier *name) :
1635 CYTypeVariable(const char *name) :
1636 name_(new($pool) CYIdentifier(name))
1640 virtual CYExpression *Replace(CYContext &context);
1641 virtual void Output(CYOutput &out) const;
1644 struct CYTypeUnsigned :
1647 CYTypeSpecifier *specifier_;
1649 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1650 specifier_(specifier)
1654 virtual CYExpression *Replace(CYContext &context);
1655 virtual void Output(CYOutput &out) const;
1658 struct CYTypeSigned :
1661 CYTypeSpecifier *specifier_;
1663 CYTypeSigned(CYTypeSpecifier *specifier) :
1664 specifier_(specifier)
1668 virtual CYExpression *Replace(CYContext &context);
1669 virtual void Output(CYOutput &out) const;
1675 CYTypeSpecifier *specifier_;
1677 CYTypeLong(CYTypeSpecifier *specifier) :
1678 specifier_(specifier)
1682 virtual CYExpression *Replace(CYContext &context);
1683 virtual void Output(CYOutput &out) const;
1686 struct CYTypeShort :
1689 CYTypeSpecifier *specifier_;
1691 CYTypeShort(CYTypeSpecifier *specifier) :
1692 specifier_(specifier)
1696 virtual CYExpression *Replace(CYContext &context);
1697 virtual void Output(CYOutput &out) const;
1700 struct CYTypeModifier :
1701 CYNext<CYTypeModifier>
1703 CYTypeModifier(CYTypeModifier *next) :
1704 CYNext<CYTypeModifier>(next)
1708 virtual int Precedence() const = 0;
1710 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1711 CYExpression *Replace(CYContext &context, CYExpression *type);
1713 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1714 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1717 struct CYTypeArrayOf :
1720 CYExpression *size_;
1722 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1723 CYTypeModifier(next),
1730 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1731 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1734 struct CYTypeConstant :
1737 CYTypeConstant(CYTypeModifier *next = NULL) :
1738 CYTypeModifier(next)
1744 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1745 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1748 struct CYTypePointerTo :
1751 CYTypePointerTo(CYTypeModifier *next = NULL) :
1752 CYTypeModifier(next)
1758 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1759 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1762 struct CYTypeVolatile :
1765 CYTypeVolatile(CYTypeModifier *next = NULL) :
1766 CYTypeModifier(next)
1772 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1773 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1776 struct CYTypedIdentifier :
1777 CYNext<CYTypedIdentifier>,
1780 CYIdentifier *identifier_;
1781 CYTypeSpecifier *specifier_;
1782 CYTypeModifier *modifier_;
1784 CYTypedIdentifier(CYIdentifier *identifier = NULL) :
1785 identifier_(identifier),
1791 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1793 specifier_(specifier),
1798 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1799 CYSetLast(modifier_) = modifier;
1803 virtual CYExpression *Replace(CYContext &context);
1804 virtual void Output(CYOutput &out) const;
1807 struct CYEncodedType :
1810 CYTypedIdentifier *typed_;
1812 CYEncodedType(CYTypedIdentifier *typed) :
1819 virtual CYExpression *Replace(CYContext &context);
1820 virtual void Output(CYOutput &out, CYFlags flags) const;
1823 struct CYTypedParameter :
1824 CYNext<CYTypedParameter>,
1827 CYTypedIdentifier *typed_;
1829 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1830 CYNext<CYTypedParameter>(next),
1835 CYArgument *Argument(CYContext &context);
1836 CYFunctionParameter *Parameters(CYContext &context);
1837 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1839 virtual void Output(CYOutput &out) const;
1845 CYTypedIdentifier *typed_;
1846 CYTypedParameter *parameters_;
1847 CYStatement *statements_;
1849 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1851 parameters_(parameters),
1852 statements_(statements)
1858 virtual CYExpression *Replace(CYContext &context);
1859 virtual void Output(CYOutput &out, CYFlags flags) const;
1862 struct CYTypeDefinition :
1865 CYTypedIdentifier *typed_;
1867 CYTypeDefinition(CYTypedIdentifier *typed) :
1872 virtual CYStatement *Replace(CYContext &context);
1873 virtual void Output(CYOutput &out, CYFlags flags) const;
1876 struct CYTypeBlockWith :
1879 CYTypedParameter *parameters_;
1881 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1882 CYTypeModifier(next),
1883 parameters_(parameters)
1889 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1890 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1893 struct CYTypeFunctionWith :
1896 CYTypedParameter *parameters_;
1898 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1899 CYTypeModifier(next),
1900 parameters_(parameters)
1906 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1907 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1916 CYIdentifier *name_;
1919 Catch(CYIdentifier *name, CYStatement *statements) :
1925 void Replace(CYContext &context);
1926 virtual void Output(CYOutput &out) const;
1934 CYFinally *finally_;
1936 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1943 virtual CYStatement *Replace(CYContext &context);
1944 virtual void Output(CYOutput &out, CYFlags flags) const;
1950 CYExpression *value_;
1952 Throw(CYExpression *value = NULL) :
1957 virtual CYStatement *Replace(CYContext &context);
1958 virtual void Output(CYOutput &out, CYFlags flags) const;
1966 CYExpression *scope_;
1969 CYWith(CYExpression *scope, CYStatement *code) :
1975 virtual CYStatement *Replace(CYContext &context);
1976 virtual void Output(CYOutput &out, CYFlags flags) const;
1982 CYExpression *value_;
1985 CYSwitch(CYExpression *value, CYClause *clauses) :
1991 virtual CYStatement *Replace(CYContext &context);
1992 virtual void Output(CYOutput &out, CYFlags flags) const;
2002 virtual CYStatement *Replace(CYContext &context);
2003 virtual void Output(CYOutput &out, CYFlags flags) const;
2006 struct CYCondition :
2009 CYExpression *test_;
2010 CYExpression *true_;
2011 CYExpression *false_;
2013 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2022 virtual CYExpression *Replace(CYContext &context);
2023 virtual void Output(CYOutput &out, CYFlags flags) const;
2026 struct CYAddressOf :
2029 CYAddressOf(CYExpression *rhs) :
2034 virtual const char *Operator() const {
2040 virtual CYExpression *Replace(CYContext &context);
2046 CYIndirect(CYExpression *rhs) :
2051 virtual const char *Operator() const {
2057 virtual CYExpression *Replace(CYContext &context);
2061 virtual CYExpression *Replace(CYContext &context);
2063 #define CYPostfix_(op, name, args...) \
2064 struct CY ## name : \
2067 CY ## name(CYExpression *lhs) : \
2072 virtual const char *Operator() const { \
2077 #define CYPrefix_(alphabetic, op, name, args...) \
2078 struct CY ## name : \
2081 CY ## name(CYExpression *rhs) : \
2086 CYAlphabetic(alphabetic) \
2088 virtual const char *Operator() const { \
2093 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2094 struct CY ## name : \
2097 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2102 CYAlphabetic(alphabetic) \
2103 CYPrecedence(precedence) \
2105 virtual const char *Operator() const { \
2110 #define CYAssignment_(op, name, args...) \
2111 struct CY ## name ## Assign : \
2114 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2115 CYAssignment(lhs, rhs) \
2119 virtual const char *Operator() const { \
2124 CYPostfix_("++", PostIncrement)
2125 CYPostfix_("--", PostDecrement)
2127 CYPrefix_(true, "delete", Delete)
2128 CYPrefix_(true, "void", Void)
2129 CYPrefix_(true, "typeof", TypeOf)
2130 CYPrefix_(false, "++", PreIncrement)
2131 CYPrefix_(false, "--", PreDecrement)
2132 CYPrefix_(false, "+", Affirm)
2133 CYPrefix_(false, "-", Negate)
2134 CYPrefix_(false, "~", BitwiseNot)
2135 CYPrefix_(false, "!", LogicalNot)
2137 CYInfix_(false, 5, "*", Multiply, CYReplace)
2138 CYInfix_(false, 5, "/", Divide)
2139 CYInfix_(false, 5, "%", Modulus)
2140 CYInfix_(false, 6, "+", Add, CYReplace)
2141 CYInfix_(false, 6, "-", Subtract)
2142 CYInfix_(false, 7, "<<", ShiftLeft)
2143 CYInfix_(false, 7, ">>", ShiftRightSigned)
2144 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2145 CYInfix_(false, 8, "<", Less)
2146 CYInfix_(false, 8, ">", Greater)
2147 CYInfix_(false, 8, "<=", LessOrEqual)
2148 CYInfix_(false, 8, ">=", GreaterOrEqual)
2149 CYInfix_(true, 8, "instanceof", InstanceOf)
2150 CYInfix_(true, 8, "in", In)
2151 CYInfix_(false, 9, "==", Equal)
2152 CYInfix_(false, 9, "!=", NotEqual)
2153 CYInfix_(false, 9, "===", Identical)
2154 CYInfix_(false, 9, "!==", NotIdentical)
2155 CYInfix_(false, 10, "&", BitwiseAnd)
2156 CYInfix_(false, 11, "^", BitwiseXOr)
2157 CYInfix_(false, 12, "|", BitwiseOr)
2158 CYInfix_(false, 13, "&&", LogicalAnd)
2159 CYInfix_(false, 14, "||", LogicalOr)
2161 CYAssignment_("=", )
2162 CYAssignment_("*=", Multiply)
2163 CYAssignment_("/=", Divide)
2164 CYAssignment_("%=", Modulus)
2165 CYAssignment_("+=", Add)
2166 CYAssignment_("-=", Subtract)
2167 CYAssignment_("<<=", ShiftLeft)
2168 CYAssignment_(">>=", ShiftRightSigned)
2169 CYAssignment_(">>>=", ShiftRightUnsigned)
2170 CYAssignment_("&=", BitwiseAnd)
2171 CYAssignment_("^=", BitwiseXOr)
2172 CYAssignment_("|=", BitwiseOr)
2174 #endif/*CYCRIPT_PARSER_HPP*/