1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
36 #include "Location.hpp"
37 #include "Pooling.hpp"
38 #include "Options.hpp"
46 virtual void Output(struct CYOutput &out) const = 0;
64 CYOutput(std::ostream &out, CYOptions &options) :
74 void Check(char value);
77 CYOutput &operator <<(char rhs);
78 CYOutput &operator <<(const char *rhs);
80 _finline CYOutput &operator <<(const CYThing *rhs) {
86 _finline CYOutput &operator <<(const CYThing &rhs) {
92 struct CYPropertyName {
93 virtual void PropertyName(CYOutput &out) const = 0;
95 virtual ~CYPropertyName() {
110 CYNoBrace = (1 << 0),
111 CYNoFunction = (1 << 1),
114 CYNoRightHand = (1 << 4),
115 CYNoDangle = (1 << 5),
116 CYNoInteger = (1 << 6),
117 CYNoBF = (CYNoBrace | CYNoFunction),
120 _finline CYFlags operator ~(CYFlags rhs) {
121 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
124 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
125 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
128 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
129 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
132 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
133 return lhs = lhs | rhs;
136 _finline CYFlags CYLeft(CYFlags flags) {
137 return flags & ~(CYNoDangle | CYNoInteger);
140 _finline CYFlags CYRight(CYFlags flags) {
141 return flags & ~CYNoBF;
144 _finline CYFlags CYCenter(CYFlags flags) {
145 return CYLeft(CYRight(flags));
151 virtual ~CYStatement() {
154 void Single(CYOutput &out, CYFlags flags) const;
155 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
157 virtual CYStatement *Replace(CYContext &context) = 0;
160 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
163 struct CYStatements {
173 operator CYStatement *() const {
177 CYStatements &operator ->*(CYStatement *next) {
179 if (first_ == NULL) {
182 } else for (;; last_ = last_->next_)
183 if (last_->next_ == NULL) {
193 virtual ~CYClassName() {
196 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
197 virtual void ClassName(CYOutput &out, bool object) const = 0;
207 CYWord(const char *word) :
212 void Set(const char *value) {
216 virtual const char *Word() const;
217 virtual void Output(CYOutput &out) const;
219 virtual CYExpression *ClassName(CYContext &context, bool object);
220 virtual void ClassName(CYOutput &out, bool object) const;
221 virtual void PropertyName(CYOutput &out) const;
224 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
226 return lhs << rhs.Word();
229 struct CYIdentifier :
230 CYNext<CYIdentifier>,
233 CYIdentifier *replace_;
237 CYIdentifier(const char *word) :
245 virtual const char *Word() const;
246 CYIdentifier *Replace(CYContext &context);
254 CYComment(const char *value) :
259 virtual CYStatement *Replace(CYContext &context);
260 virtual void Output(CYOutput &out, CYFlags flags) const;
267 CYStatement *statement_;
269 CYLabel(CYIdentifier *name, CYStatement *statement) :
271 statement_(statement)
275 virtual CYStatement *Replace(CYContext &context);
276 virtual void Output(CYOutput &out, CYFlags flags) const;
279 struct CYCStringLess :
280 std::binary_function<const char *, const char *, bool>
282 _finline bool operator ()(const char *lhs, const char *rhs) const {
283 return strcmp(lhs, rhs) < 0;
287 struct CYIdentifierValueLess :
288 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
290 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
291 return CYCStringLess()(lhs->Word(), rhs->Word());
295 enum CYIdentifierFlags {
296 CYIdentifierArgument,
297 CYIdentifierVariable,
303 typedef std::set<const char *, CYCStringLess> CYCStringSet;
304 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
305 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
307 struct CYIdentifierUsage {
308 CYIdentifier *identifier_;
312 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
318 CYIdentifierAddressFlagsMap internal_;
319 CYIdentifierValueSet identifiers_;
321 CYScope(bool transparent, CYContext &context);
323 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
324 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
325 void Merge(CYContext &context, CYIdentifier *identifier);
326 void Close(CYContext &context, CYStatement *&statements);
332 CYStatement *statements_;
334 CYProgram(CYStatement *statements) :
335 statements_(statements)
339 virtual void Replace(CYContext &context);
340 virtual void Output(CYOutput &out) const;
352 CYIdentifierUsageVector rename_;
354 CYNonLocal *nonlocal_;
355 CYNonLocal *nextlocal_;
358 CYContext(CYOptions &options) :
368 virtual ~CYContext() {
371 template <typename Type_>
372 void ReplaceAll(Type_ *&values) {
373 Type_ **last(&values);
374 CYForEach (next, values) {
375 Replace(*last = next);
377 last = &(*last)->next_;
381 template <typename Type_>
382 void Replace(Type_ *&value) {
383 for (;;) if (value == NULL)
386 Type_ *replace(value->Replace(*this));
387 if (replace != value)
393 void NonLocal(CYStatement *&statements);
394 CYIdentifier *Unique();
398 CYIdentifier *identifier_;
405 CYIdentifier *Target(CYContext &context) {
406 if (identifier_ == NULL)
407 identifier_ = context.Unique();
415 CYIdentifier *identifier_;
422 CYIdentifier *Identifier(CYContext &context) {
424 return next_->Identifier(context);
425 if (identifier_ == NULL)
426 identifier_ = context.Unique();
435 CYStatement *statements_;
437 CYBlock(CYStatement *statements) :
438 statements_(statements)
442 operator CYStatement *() const {
446 void AddPrev(CYStatement *statement) {
447 CYSetLast(statement) = statements_;
448 statements_ = statement;
451 virtual CYStatement *Replace(CYContext &context);
453 virtual void Output(CYOutput &out) const;
454 virtual void Output(CYOutput &out, CYFlags flags) const;
457 struct CYForInitialiser {
458 virtual ~CYForInitialiser() {
461 virtual CYExpression *Replace(CYContext &context) = 0;
462 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
465 struct CYForInInitialiser {
466 virtual ~CYForInInitialiser() {
469 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
470 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
472 virtual CYExpression *Replace(CYContext &context) = 0;
473 virtual CYAssignment *Assignment(CYContext &context) = 0;
475 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
478 struct CYFunctionParameter;
483 struct CYExpression :
489 virtual int Precedence() const = 0;
491 virtual bool RightHand() const {
495 virtual void ForIn(CYOutput &out, CYFlags flags) const;
496 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
498 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
500 virtual void Output(CYOutput &out) const;
501 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
502 void Output(CYOutput &out, int precedence, CYFlags flags) const;
504 virtual CYExpression *ClassName(CYContext &context, bool object);
505 virtual void ClassName(CYOutput &out, bool object) const;
507 virtual CYExpression *Replace(CYContext &context) = 0;
508 virtual CYAssignment *Assignment(CYContext &context);
510 virtual CYExpression *Primitive(CYContext &context) {
514 virtual CYFunctionParameter *Parameter() const;
515 virtual CYFunctionParameter *Parameters() const;
517 virtual CYNumber *Number(CYContext &context) {
521 virtual CYString *String(CYContext &context) {
525 virtual const char *Word() const {
530 #define CYAlphabetic(value) \
531 virtual bool Alphabetic() const { \
535 #define CYPrecedence(value) \
536 static const int Precedence_ = value; \
537 virtual int Precedence() const { \
538 return Precedence_; \
541 #define CYRightHand(value) \
542 virtual bool RightHand() const { \
549 CYExpression *expression_;
552 CYCompound(CYExpression *expression, CYExpression *next = NULL) :
553 expression_(expression),
556 if (expression_ == NULL)
558 _assert(expression_ != NULL);
563 virtual CYExpression *Replace(CYContext &context);
564 void Output(CYOutput &out, CYFlags flags) const;
566 virtual CYExpression *Primitive(CYContext &context);
567 virtual CYFunctionParameter *Parameters() const;
570 struct CYDeclaration;
572 struct CYFunctionParameter :
573 CYNext<CYFunctionParameter>,
576 CYForInInitialiser *initialiser_;
578 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
579 CYNext<CYFunctionParameter>(next),
580 initialiser_(initialiser)
584 void Replace(CYContext &context, CYBlock &code);
585 void Output(CYOutput &out) const;
588 struct CYComprehension :
589 CYNext<CYComprehension>,
592 CYComprehension(CYComprehension *next = NULL) :
593 CYNext<CYComprehension>(next)
597 CYComprehension *Modify(CYComprehension *next) {
602 virtual const char *Name() const = 0;
604 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
605 CYFunctionParameter *Parameters(CYContext &context) const;
606 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
607 virtual void Output(CYOutput &out) const = 0;
610 struct CYForInComprehension :
616 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
617 CYComprehension(next),
623 virtual const char *Name() const {
624 return name_->Word();
627 virtual CYFunctionParameter *Parameter(CYContext &context) const;
628 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
629 virtual void Output(CYOutput &out) const;
632 struct CYForOfComprehension :
638 CYForOfComprehension(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 CYIfComprehension :
659 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
660 CYComprehension(next),
665 virtual const char *Name() const {
669 virtual CYFunctionParameter *Parameter(CYContext &context) const;
670 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
671 virtual void Output(CYOutput &out) const;
674 struct CYArrayComprehension :
677 CYExpression *expression_;
678 CYComprehension *comprehensions_;
680 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
681 expression_(expression),
682 comprehensions_(comprehensions)
688 virtual CYExpression *Replace(CYContext &context);
689 virtual void Output(CYOutput &out, CYFlags flags) const;
698 virtual CYExpression *Primitive(CYContext &context) {
706 virtual CYExpression *Replace(CYContext &context);
720 CYRange(uint64_t lo, uint64_t hi) :
725 bool operator [](uint8_t value) const {
726 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
729 void operator()(uint8_t value) {
732 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
736 extern CYRange DigitRange_;
737 extern CYRange WordStartRange_;
738 extern CYRange WordEndRange_;
753 CYString(const char *value) :
759 CYString(const char *value, size_t size) :
765 CYString(const CYWord *word) :
766 value_(word->Word()),
767 size_(strlen(value_))
771 const char *Value() const {
775 virtual const char *Word() const;
777 virtual CYNumber *Number(CYContext &context);
778 virtual CYString *String(CYContext &context);
780 CYString *Concat(CYContext &out, CYString *rhs) const;
781 virtual void Output(CYOutput &out, CYFlags flags) const;
782 virtual void PropertyName(CYOutput &out) const;
791 CYNumber(double value) :
796 double Value() const {
800 virtual CYNumber *Number(CYContext &context);
801 virtual CYString *String(CYContext &context);
803 virtual void Output(CYOutput &out, CYFlags flags) const;
804 virtual void PropertyName(CYOutput &out) const;
812 CYRegEx(const char *value) :
817 const char *Value() const {
821 virtual void Output(CYOutput &out, CYFlags flags) const;
833 virtual CYNumber *Number(CYContext &context);
834 virtual CYString *String(CYContext &context);
836 virtual void Output(CYOutput &out, CYFlags flags) const;
848 virtual CYExpression *Replace(CYContext &context);
849 virtual void Output(CYOutput &out, CYFlags flags) const;
855 virtual bool Value() const = 0;
856 virtual void Output(CYOutput &out, CYFlags flags) const;
868 virtual bool Value() const {
872 virtual CYNumber *Number(CYContext &context);
873 virtual CYString *String(CYContext &context);
885 virtual bool Value() const {
889 virtual CYNumber *Number(CYContext &context);
890 virtual CYString *String(CYContext &context);
898 CYVariable(CYIdentifier *name) :
903 CYVariable(const char *name) :
904 name_(new($pool) CYIdentifier(name))
911 virtual CYExpression *Replace(CYContext &context);
912 virtual void Output(CYOutput &out, CYFlags flags) const;
914 virtual CYFunctionParameter *Parameter() const;
922 CYPrefix(CYExpression *rhs) :
927 virtual bool Alphabetic() const = 0;
928 virtual const char *Operator() const = 0;
932 virtual CYExpression *Replace(CYContext &context);
933 virtual void Output(CYOutput &out, CYFlags flags) const;
942 CYInfix(CYExpression *lhs, CYExpression *rhs) :
948 void SetLeft(CYExpression *lhs) {
952 virtual bool Alphabetic() const = 0;
953 virtual const char *Operator() const = 0;
955 virtual CYExpression *Replace(CYContext &context);
956 virtual void Output(CYOutput &out, CYFlags flags) const;
964 CYPostfix(CYExpression *lhs) :
969 virtual const char *Operator() const = 0;
973 virtual CYExpression *Replace(CYContext &context);
974 virtual void Output(CYOutput &out, CYFlags flags) const;
977 struct CYAssignment :
983 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
989 void SetLeft(CYExpression *lhs) {
993 virtual const char *Operator() const = 0;
997 virtual CYExpression *Replace(CYContext &context);
998 virtual void Output(CYOutput &out, CYFlags flags) const;
1006 CYExpression *value_;
1008 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1009 CYNext<CYArgument>(next),
1015 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1016 CYNext<CYArgument>(next),
1022 CYArgument *Replace(CYContext &context);
1023 void Output(CYOutput &out) const;
1039 CYExpression *case_;
1040 CYStatement *statements_;
1042 CYClause(CYExpression *_case, CYStatement *statements) :
1044 statements_(statements)
1048 void Replace(CYContext &context);
1049 virtual void Output(CYOutput &out) const;
1056 CYExpression *value_;
1058 CYElement(CYExpression *value, CYElement *next) :
1059 CYNext<CYElement>(next),
1064 void Replace(CYContext &context);
1065 void Output(CYOutput &out) const;
1071 CYElement *elements_;
1073 CYArray(CYElement *elements = NULL) :
1078 virtual CYExpression *Replace(CYContext &context);
1079 virtual void Output(CYOutput &out, CYFlags flags) const;
1086 CYPropertyName *name_;
1087 CYExpression *value_;
1089 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1090 CYNext<CYProperty>(next),
1096 void Replace(CYContext &context);
1097 virtual void Output(CYOutput &out) const;
1100 struct CYDeclaration :
1103 CYIdentifier *identifier_;
1104 CYExpression *initialiser_;
1106 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1107 identifier_(identifier),
1108 initialiser_(initialiser)
1112 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1113 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1115 virtual CYExpression *Replace(CYContext &context);
1117 virtual CYAssignment *Assignment(CYContext &context);
1118 CYVariable *Variable(CYContext &context);
1120 virtual void Output(CYOutput &out, CYFlags flags) const;
1123 struct CYDeclarations :
1124 CYNext<CYDeclarations>,
1127 CYDeclaration *declaration_;
1129 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1130 CYNext<CYDeclarations>(next),
1131 declaration_(declaration)
1135 void Replace(CYContext &context);
1137 CYCompound *Compound(CYContext &context);
1138 CYProperty *Property(CYContext &context);
1139 CYArgument *Argument(CYContext &context);
1140 CYFunctionParameter *Parameter(CYContext &context);
1142 virtual void Output(CYOutput &out) const;
1143 virtual void Output(CYOutput &out, CYFlags flags) const;
1146 struct CYForDeclarations :
1149 CYDeclarations *declarations_;
1151 CYForDeclarations(CYDeclarations *declarations) :
1152 declarations_(declarations)
1156 virtual CYCompound *Replace(CYContext &context);
1157 virtual void Output(CYOutput &out, CYFlags flags) const;
1163 CYDeclarations *declarations_;
1165 CYVar(CYDeclarations *declarations) :
1166 declarations_(declarations)
1170 virtual CYStatement *Replace(CYContext &context);
1171 virtual void Output(CYOutput &out, CYFlags flags) const;
1174 struct CYLetStatement :
1177 CYDeclarations *declarations_;
1180 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1181 declarations_(declarations),
1186 virtual CYStatement *Replace(CYContext &context);
1187 virtual void Output(CYOutput &out, CYFlags flags) const;
1193 CYForInitialiser *initialiser_;
1194 CYExpression *test_;
1195 CYExpression *increment_;
1198 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1199 initialiser_(initialiser),
1201 increment_(increment),
1206 virtual CYStatement *Replace(CYContext &context);
1207 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 CYForInInitialiser *initialiser_;
1217 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1218 initialiser_(initialiser),
1224 virtual CYStatement *Replace(CYContext &context);
1225 virtual void Output(CYOutput &out, CYFlags flags) const;
1231 CYForInInitialiser *initialiser_;
1235 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1236 initialiser_(initialiser),
1242 virtual CYStatement *Replace(CYContext &context);
1243 virtual void Output(CYOutput &out, CYFlags flags) const;
1249 CYProperty *properties_;
1251 CYObject(CYProperty *properties = NULL) :
1252 properties_(properties)
1256 virtual CYExpression *Replace(CYContext &context);
1257 void Output(CYOutput &out, CYFlags flags) const;
1263 CYExpression *object_;
1264 CYExpression *property_;
1266 CYMember(CYExpression *object, CYExpression *property) :
1272 void SetLeft(CYExpression *object) {
1277 struct CYDirectMember :
1280 CYDirectMember(CYExpression *object, CYExpression *property) :
1281 CYMember(object, property)
1288 virtual CYExpression *Replace(CYContext &context);
1289 virtual void Output(CYOutput &out, CYFlags flags) const;
1292 struct CYIndirectMember :
1295 CYIndirectMember(CYExpression *object, CYExpression *property) :
1296 CYMember(object, property)
1303 virtual CYExpression *Replace(CYContext &context);
1304 virtual void Output(CYOutput &out, CYFlags flags) const;
1313 CYExpression *constructor_;
1314 CYArgument *arguments_;
1316 New(CYExpression *constructor, CYArgument *arguments) :
1317 constructor_(constructor),
1318 arguments_(arguments)
1322 virtual int Precedence() const {
1323 return arguments_ == NULL ? 2 : 1;
1328 virtual CYExpression *Replace(CYContext &context);
1329 virtual void Output(CYOutput &out, CYFlags flags) const;
1331 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1339 CYExpression *function_;
1340 CYArgument *arguments_;
1342 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1343 function_(function),
1344 arguments_(arguments)
1351 virtual CYExpression *Replace(CYContext &context);
1352 virtual void Output(CYOutput &out, CYFlags flags) const;
1354 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1359 struct CYRubyBlock :
1362 CYExpression *call_;
1365 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1374 virtual CYExpression *Replace(CYContext &context);
1375 virtual void Output(CYOutput &out, CYFlags flags) const;
1381 CYExpression *test_;
1383 CYStatement *false_;
1385 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1392 virtual CYStatement *Replace(CYContext &context);
1393 virtual void Output(CYOutput &out, CYFlags flags) const;
1399 CYExpression *test_;
1402 CYDoWhile(CYExpression *test, CYStatement *code) :
1408 virtual CYStatement *Replace(CYContext &context);
1409 virtual void Output(CYOutput &out, CYFlags flags) const;
1415 CYExpression *test_;
1418 CYWhile(CYExpression *test, CYStatement *code) :
1424 virtual CYStatement *Replace(CYContext &context);
1425 virtual void Output(CYOutput &out, CYFlags flags) const;
1428 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1430 CYIdentifier *name_;
1431 CYFunctionParameter *parameters_;
1434 CYNonLocal *nonlocal_;
1437 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1439 parameters_(parameters),
1445 virtual ~CYFunction() {
1448 void Inject(CYContext &context);
1449 virtual void Replace_(CYContext &context, bool outer);
1450 virtual void Output(CYOutput &out, CYFlags flags) const;
1453 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1454 struct CYFunctionExpression :
1458 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1459 CYFunction(name, parameters, statements)
1466 virtual CYExpression *Replace(CYContext &context);
1467 virtual void Output(CYOutput &out, CYFlags flags) const;
1470 // XXX: this should derive from CYAnonymousFunction
1475 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1476 CYFunction(NULL, parameters, statements)
1483 virtual CYExpression *Replace(CYContext &context);
1484 virtual void Output(CYOutput &out, CYFlags flags) const;
1487 // XXX: this should derive from CYAnonymousFunctionExpression
1489 CYFunctionExpression
1491 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1492 CYFunctionExpression(NULL, parameters, statements)
1496 virtual CYExpression *Replace(CYContext &context);
1497 virtual void Output(CYOutput &out, CYFlags flags) const;
1500 // XXX: this should derive from CYNamedFunction
1501 struct CYFunctionStatement :
1505 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1506 CYFunction(name, parameters, statements)
1510 virtual CYStatement *Replace(CYContext &context);
1511 virtual void Output(CYOutput &out, CYFlags flags) const;
1517 CYExpression *expression_;
1519 CYExpress(CYExpression *expression) :
1520 expression_(expression)
1522 if (expression_ == NULL)
1526 virtual CYStatement *Replace(CYContext &context);
1527 virtual void Output(CYOutput &out, CYFlags flags) const;
1533 CYIdentifier *label_;
1535 CYContinue(CYIdentifier *label) :
1540 virtual CYStatement *Replace(CYContext &context);
1541 virtual void Output(CYOutput &out, CYFlags flags) const;
1547 CYIdentifier *label_;
1549 CYBreak(CYIdentifier *label) :
1554 virtual CYStatement *Replace(CYContext &context);
1555 virtual void Output(CYOutput &out, CYFlags flags) const;
1561 CYExpression *value_;
1563 CYReturn(CYExpression *value) :
1568 virtual CYStatement *Replace(CYContext &context);
1569 virtual void Output(CYOutput &out, CYFlags flags) const;
1575 virtual CYStatement *Replace(CYContext &context);
1576 virtual void Output(CYOutput &out, CYFlags flags) const;
1584 CYFinally(CYStatement *statements) :
1589 void Replace(CYContext &context);
1590 virtual void Output(CYOutput &out) const;
1593 struct CYTypeSpecifier :
1596 virtual CYExpression *Replace(CYContext &context) = 0;
1599 struct CYTypeError :
1605 virtual CYExpression *Replace(CYContext &context);
1606 virtual void Output(CYOutput &out) const;
1615 virtual CYExpression *Replace(CYContext &context);
1616 virtual void Output(CYOutput &out) const;
1619 struct CYTypeVariable :
1622 CYIdentifier *name_;
1624 CYTypeVariable(CYIdentifier *name) :
1629 CYTypeVariable(const char *name) :
1630 name_(new($pool) CYIdentifier(name))
1634 virtual CYExpression *Replace(CYContext &context);
1635 virtual void Output(CYOutput &out) const;
1638 struct CYTypeUnsigned :
1641 CYTypeSpecifier *specifier_;
1643 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1644 specifier_(specifier)
1648 virtual CYExpression *Replace(CYContext &context);
1649 virtual void Output(CYOutput &out) const;
1652 struct CYTypeSigned :
1655 CYTypeSpecifier *specifier_;
1657 CYTypeSigned(CYTypeSpecifier *specifier) :
1658 specifier_(specifier)
1662 virtual CYExpression *Replace(CYContext &context);
1663 virtual void Output(CYOutput &out) const;
1669 CYTypeSpecifier *specifier_;
1671 CYTypeLong(CYTypeSpecifier *specifier) :
1672 specifier_(specifier)
1676 virtual CYExpression *Replace(CYContext &context);
1677 virtual void Output(CYOutput &out) const;
1680 struct CYTypeShort :
1683 CYTypeSpecifier *specifier_;
1685 CYTypeShort(CYTypeSpecifier *specifier) :
1686 specifier_(specifier)
1690 virtual CYExpression *Replace(CYContext &context);
1691 virtual void Output(CYOutput &out) const;
1694 struct CYTypeFunctionWith;
1696 struct CYTypeModifier :
1697 CYNext<CYTypeModifier>
1699 CYTypeModifier(CYTypeModifier *next) :
1700 CYNext<CYTypeModifier>(next)
1704 virtual int Precedence() const = 0;
1706 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1707 CYExpression *Replace(CYContext &context, CYExpression *type);
1709 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1710 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1712 virtual CYTypeFunctionWith *Function() { return NULL; }
1715 struct CYTypeArrayOf :
1718 CYExpression *size_;
1720 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1721 CYTypeModifier(next),
1728 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1729 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1732 struct CYTypeConstant :
1735 CYTypeConstant(CYTypeModifier *next = NULL) :
1736 CYTypeModifier(next)
1742 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1743 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1746 struct CYTypePointerTo :
1749 CYTypePointerTo(CYTypeModifier *next = NULL) :
1750 CYTypeModifier(next)
1756 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1757 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1760 struct CYTypeVolatile :
1763 CYTypeVolatile(CYTypeModifier *next = NULL) :
1764 CYTypeModifier(next)
1770 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1771 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1774 struct CYTypedIdentifier :
1775 CYNext<CYTypedIdentifier>,
1778 CYLocation location_;
1779 CYIdentifier *identifier_;
1780 CYTypeSpecifier *specifier_;
1781 CYTypeModifier *modifier_;
1783 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1784 location_(location),
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;
1806 CYTypeFunctionWith *Function();
1809 struct CYEncodedType :
1812 CYTypedIdentifier *typed_;
1814 CYEncodedType(CYTypedIdentifier *typed) :
1821 virtual CYExpression *Replace(CYContext &context);
1822 virtual void Output(CYOutput &out, CYFlags flags) const;
1825 struct CYTypedParameter :
1826 CYNext<CYTypedParameter>,
1829 CYTypedIdentifier *typed_;
1831 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1832 CYNext<CYTypedParameter>(next),
1837 CYArgument *Argument(CYContext &context);
1838 CYFunctionParameter *Parameters(CYContext &context);
1839 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1841 virtual void Output(CYOutput &out) const;
1847 CYTypedIdentifier *typed_;
1848 CYTypedParameter *parameters_;
1849 CYStatement *statements_;
1851 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1853 parameters_(parameters),
1854 statements_(statements)
1860 virtual CYExpression *Replace(CYContext &context);
1861 virtual void Output(CYOutput &out, CYFlags flags) const;
1870 CYModule(CYWord *part, CYModule *next = NULL) :
1871 CYNext<CYModule>(next),
1876 CYString *Replace(CYContext &context, const char *separator) const;
1877 void Output(CYOutput &out) const;
1885 CYImport(CYModule *module) :
1890 virtual CYStatement *Replace(CYContext &context);
1891 virtual void Output(CYOutput &out, CYFlags flags) const;
1898 CYTypedIdentifier *typed_;
1900 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1906 virtual CYStatement *Replace(CYContext &context);
1907 virtual void Output(CYOutput &out, CYFlags flags) const;
1910 struct CYTypeDefinition :
1913 CYTypedIdentifier *typed_;
1915 CYTypeDefinition(CYTypedIdentifier *typed) :
1920 virtual CYStatement *Replace(CYContext &context);
1921 virtual void Output(CYOutput &out, CYFlags flags) const;
1924 struct CYTypeBlockWith :
1927 CYTypedParameter *parameters_;
1929 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1930 CYTypeModifier(next),
1931 parameters_(parameters)
1937 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1938 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1941 struct CYTypeFunctionWith :
1944 CYTypedParameter *parameters_;
1946 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1947 CYTypeModifier(next),
1948 parameters_(parameters)
1954 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1955 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1957 virtual CYTypeFunctionWith *Function() { return this; }
1966 CYIdentifier *name_;
1969 Catch(CYIdentifier *name, CYStatement *statements) :
1975 void Replace(CYContext &context);
1976 virtual void Output(CYOutput &out) const;
1984 CYFinally *finally_;
1986 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1993 virtual CYStatement *Replace(CYContext &context);
1994 virtual void Output(CYOutput &out, CYFlags flags) const;
2000 CYExpression *value_;
2002 Throw(CYExpression *value = NULL) :
2007 virtual CYStatement *Replace(CYContext &context);
2008 virtual void Output(CYOutput &out, CYFlags flags) const;
2016 CYExpression *scope_;
2019 CYWith(CYExpression *scope, CYStatement *code) :
2025 virtual CYStatement *Replace(CYContext &context);
2026 virtual void Output(CYOutput &out, CYFlags flags) const;
2032 CYExpression *value_;
2035 CYSwitch(CYExpression *value, CYClause *clauses) :
2041 virtual CYStatement *Replace(CYContext &context);
2042 virtual void Output(CYOutput &out, CYFlags flags) const;
2052 virtual CYStatement *Replace(CYContext &context);
2053 virtual void Output(CYOutput &out, CYFlags flags) const;
2056 struct CYCondition :
2059 CYExpression *test_;
2060 CYExpression *true_;
2061 CYExpression *false_;
2063 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2072 virtual CYExpression *Replace(CYContext &context);
2073 virtual void Output(CYOutput &out, CYFlags flags) const;
2076 struct CYAddressOf :
2079 CYAddressOf(CYExpression *rhs) :
2084 virtual const char *Operator() const {
2090 virtual CYExpression *Replace(CYContext &context);
2096 CYIndirect(CYExpression *rhs) :
2101 virtual const char *Operator() const {
2107 virtual CYExpression *Replace(CYContext &context);
2111 virtual CYExpression *Replace(CYContext &context);
2113 #define CYPostfix_(op, name, args...) \
2114 struct CY ## name : \
2117 CY ## name(CYExpression *lhs) : \
2122 virtual const char *Operator() const { \
2127 #define CYPrefix_(alphabetic, op, name, args...) \
2128 struct CY ## name : \
2131 CY ## name(CYExpression *rhs) : \
2136 CYAlphabetic(alphabetic) \
2138 virtual const char *Operator() const { \
2143 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2144 struct CY ## name : \
2147 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2152 CYAlphabetic(alphabetic) \
2153 CYPrecedence(precedence) \
2155 virtual const char *Operator() const { \
2160 #define CYAssignment_(op, name, args...) \
2161 struct CY ## name ## Assign : \
2164 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2165 CYAssignment(lhs, rhs) \
2169 virtual const char *Operator() const { \
2174 CYPostfix_("++", PostIncrement)
2175 CYPostfix_("--", PostDecrement)
2177 CYPrefix_(true, "delete", Delete)
2178 CYPrefix_(true, "void", Void)
2179 CYPrefix_(true, "typeof", TypeOf)
2180 CYPrefix_(false, "++", PreIncrement)
2181 CYPrefix_(false, "--", PreDecrement)
2182 CYPrefix_(false, "+", Affirm)
2183 CYPrefix_(false, "-", Negate)
2184 CYPrefix_(false, "~", BitwiseNot)
2185 CYPrefix_(false, "!", LogicalNot)
2187 CYInfix_(false, 5, "*", Multiply, CYReplace)
2188 CYInfix_(false, 5, "/", Divide)
2189 CYInfix_(false, 5, "%", Modulus)
2190 CYInfix_(false, 6, "+", Add, CYReplace)
2191 CYInfix_(false, 6, "-", Subtract)
2192 CYInfix_(false, 7, "<<", ShiftLeft)
2193 CYInfix_(false, 7, ">>", ShiftRightSigned)
2194 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2195 CYInfix_(false, 8, "<", Less)
2196 CYInfix_(false, 8, ">", Greater)
2197 CYInfix_(false, 8, "<=", LessOrEqual)
2198 CYInfix_(false, 8, ">=", GreaterOrEqual)
2199 CYInfix_(true, 8, "instanceof", InstanceOf)
2200 CYInfix_(true, 8, "in", In)
2201 CYInfix_(false, 9, "==", Equal)
2202 CYInfix_(false, 9, "!=", NotEqual)
2203 CYInfix_(false, 9, "===", Identical)
2204 CYInfix_(false, 9, "!==", NotIdentical)
2205 CYInfix_(false, 10, "&", BitwiseAnd)
2206 CYInfix_(false, 11, "^", BitwiseXOr)
2207 CYInfix_(false, 12, "|", BitwiseOr)
2208 CYInfix_(false, 13, "&&", LogicalAnd)
2209 CYInfix_(false, 14, "||", LogicalOr)
2211 CYAssignment_("=", )
2212 CYAssignment_("*=", Multiply)
2213 CYAssignment_("/=", Divide)
2214 CYAssignment_("%=", Modulus)
2215 CYAssignment_("+=", Add)
2216 CYAssignment_("-=", Subtract)
2217 CYAssignment_("<<=", ShiftLeft)
2218 CYAssignment_(">>=", ShiftRightSigned)
2219 CYAssignment_(">>>=", ShiftRightUnsigned)
2220 CYAssignment_("&=", BitwiseAnd)
2221 CYAssignment_("^=", BitwiseXOr)
2222 CYAssignment_("|=", BitwiseOr)
2224 #endif/*CYCRIPT_PARSER_HPP*/