1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
35 #include "location.hh"
36 #include "Pooling.hpp"
37 #include "Options.hpp"
41 template <typename Type_>
55 void SetNext(Type_ *next) {
60 template <typename Type_>
61 void CYSetLast(Type_ *&list, Type_ *item) {
66 while (next->next_ != NULL)
72 #define CYForEach(value, list) \
73 for (__typeof__(*list) *value(list); value != NULL; value = value->next_)
79 virtual void Output(struct CYOutput &out) const = 0;
97 CYOutput(std::ostream &out, CYOptions &options) :
107 void Check(char value);
110 CYOutput &operator <<(char rhs);
111 CYOutput &operator <<(const char *rhs);
113 _finline CYOutput &operator <<(const CYThing *rhs) {
119 _finline CYOutput &operator <<(const CYThing &rhs) {
125 struct CYPropertyName {
126 virtual void PropertyName(CYOutput &out) const = 0;
128 virtual ~CYPropertyName() {
142 CYNoBrace = (1 << 0),
143 CYNoFunction = (1 << 1),
146 CYNoRightHand = (1 << 4),
147 CYNoDangle = (1 << 5),
148 CYNoInteger = (1 << 6),
149 CYNoBF = (CYNoBrace | CYNoFunction),
155 virtual ~CYStatement() {
158 void Single(CYOutput &out, CYFlags flags) const;
159 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
161 virtual CYStatement *Collapse(CYContext &context);
162 virtual CYStatement *Replace(CYContext &context) = 0;
165 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
168 struct CYStatements {
178 operator CYStatement *() const {
182 CYStatements &operator ->*(CYStatement *next) {
184 if (first_ == NULL) {
187 } else for (;; last_ = last_->next_)
188 if (last_->next_ == NULL) {
198 virtual ~CYClassName() {
201 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
202 virtual void ClassName(CYOutput &out, bool object) const = 0;
212 CYWord(const char *word) :
217 void Set(const char *value) {
221 virtual const char *Word() const;
222 virtual void Output(CYOutput &out) const;
224 virtual CYExpression *ClassName(CYContext &context, bool object);
225 virtual void ClassName(CYOutput &out, bool object) const;
226 virtual void PropertyName(CYOutput &out) const;
229 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
231 return lhs << rhs.Word();
234 struct CYIdentifier :
235 CYNext<CYIdentifier>,
238 CYIdentifier *replace_;
242 CYIdentifier(const char *word) :
250 virtual const char *Word() const;
251 CYIdentifier *Replace(CYContext &context);
259 CYComment(const char *value) :
264 virtual CYStatement *Replace(CYContext &context);
265 virtual void Output(CYOutput &out, CYFlags flags) const;
272 CYStatement *statement_;
274 CYLabel(CYIdentifier *name, CYStatement *statement) :
276 statement_(statement)
280 virtual CYStatement *Replace(CYContext &context);
281 virtual void Output(CYOutput &out, CYFlags flags) const;
284 struct CYCStringLess :
285 std::binary_function<const char *, const char *, bool>
287 _finline bool operator ()(const char *lhs, const char *rhs) const {
288 return strcmp(lhs, rhs) < 0;
292 struct CYIdentifierValueLess :
293 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
295 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
296 return CYCStringLess()(lhs->Word(), rhs->Word());
300 enum CYIdentifierFlags {
301 CYIdentifierArgument,
302 CYIdentifierVariable,
308 typedef std::set<const char *, CYCStringLess> CYCStringSet;
309 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
310 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
312 struct CYIdentifierUsage {
313 CYIdentifier *identifier_;
317 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
319 // XXX: strategy pattern, maybe subclass
330 CYStatement *&statements_;
334 CYIdentifierAddressFlagsMap internal_;
335 CYIdentifierValueSet identifiers_;
337 CYScope(CYScopeType type, CYContext &context, CYStatement *&statements);
341 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
342 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
343 void Merge(CYContext &context, CYIdentifier *identifier);
344 void Scope(CYContext &context, CYStatement *&statements);
350 CYStatement *statements_;
352 CYProgram(CYStatement *statements) :
353 statements_(statements)
357 virtual void Replace(CYContext &context);
358 virtual void Output(CYOutput &out) const;
367 CYIdentifierUsageVector rename_;
369 CYNonLocal *nonlocal_;
370 CYNonLocal *nextlocal_;
373 CYContext(CYOptions &options) :
382 virtual ~CYContext() {
385 template <typename Type_>
386 void ReplaceAll(Type_ *&values) {
387 Type_ **last(&values);
388 CYForEach (next, values) {
390 last = &(*last)->next_;
394 template <typename Type_>
395 void Replace(Type_ *&value) {
396 for (;;) if (value == NULL)
399 Type_ *replace(value->Replace(*this));
400 if (replace != value)
406 void NonLocal(CYStatement *&statements);
407 CYIdentifier *Unique();
411 CYIdentifier *identifier_;
418 CYIdentifier *Target(CYContext &context) {
419 if (identifier_ == NULL)
420 identifier_ = context.Unique();
429 CYStatement *statements_;
431 CYBlock(CYStatement *statements) :
432 statements_(statements)
436 operator CYStatement *() const {
440 void AddPrev(CYStatement *statement) {
441 CYSetLast(statement, statements_);
442 statements_ = statement;
445 virtual CYStatement *Replace(CYContext &context);
447 virtual void Output(CYOutput &out) const;
448 virtual void Output(CYOutput &out, CYFlags flags) const;
474 std::string filename_;
478 cy::location location_;
479 std::string message_;
482 typedef std::vector<Error> Errors;
490 CYExpression *context_;
492 Context(CYExpression *context) :
497 typedef std::vector<CYWord *> Words;
501 typedef std::vector<Context> Contexts;
504 CYExpression *context_;
516 void ScannerDestroy();
519 CYDriver(const std::string &filename = "");
522 Condition GetCondition();
523 void SetCondition(Condition condition);
525 void PushCondition(Condition condition);
528 void Warning(const cy::location &location, const char *message);
531 struct CYForInitialiser {
532 virtual ~CYForInitialiser() {
535 virtual void For(CYOutput &out) const = 0;
536 virtual CYExpression *Replace(CYContext &context) = 0;
539 struct CYForInInitialiser {
540 virtual ~CYForInInitialiser() {
543 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
544 virtual const char *ForEachIn() const = 0;
545 virtual CYExpression *ForEachIn(CYContext &out) = 0;
546 virtual CYExpression *Replace(CYContext &context) = 0;
552 struct CYExpression :
553 CYNext<CYExpression>,
559 virtual unsigned Precedence() const = 0;
561 virtual bool RightHand() const {
565 virtual void For(CYOutput &out) const;
566 virtual void ForIn(CYOutput &out, CYFlags flags) const;
568 virtual const char *ForEachIn() const;
569 virtual CYExpression *ForEachIn(CYContext &out);
571 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
573 virtual void Output(CYOutput &out) const;
574 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
575 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
577 virtual CYExpression *ClassName(CYContext &context, bool object);
578 virtual void ClassName(CYOutput &out, bool object) const;
580 virtual CYExpression *Replace(CYContext &context) = 0;
582 virtual CYExpression *Primitive(CYContext &context) {
586 virtual CYNumber *Number(CYContext &context) {
590 virtual CYString *String(CYContext &context) {
594 virtual const char *Word() const {
599 #define CYAlphabetic(value) \
600 virtual bool Alphabetic() const { \
604 #define CYPrecedence(value) \
605 static const unsigned Precedence_ = value; \
606 virtual unsigned Precedence() const { \
607 return Precedence_; \
610 #define CYRightHand(value) \
611 virtual bool RightHand() const { \
618 CYExpression *expressions_;
620 CYCompound(CYExpression *expressions = NULL) :
621 expressions_(expressions)
625 void AddPrev(CYExpression *expression) {
626 CYSetLast(expression, expressions_);
627 expressions_ = expression;
632 virtual CYExpression *Replace(CYContext &context);
633 void Output(CYOutput &out, CYFlags flags) const;
636 struct CYFunctionParameter :
637 CYNext<CYFunctionParameter>,
642 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
643 CYNext<CYFunctionParameter>(next),
648 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
649 virtual void Output(CYOutput &out) const;
652 struct CYOptionalFunctionParameter :
655 CYExpression *initializer_;
657 CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
658 CYFunctionParameter(name, next),
659 initializer_(initializer)
663 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
664 virtual void Output(CYOutput &out) const;
667 struct CYComprehension :
668 CYNext<CYComprehension>,
671 virtual const char *Name() const = 0;
673 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
674 CYFunctionParameter *Parameters(CYContext &context) const;
675 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
676 virtual void Output(CYOutput &out) const = 0;
679 struct CYForInComprehension :
685 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
691 virtual const char *Name() const {
692 return name_->Word();
695 virtual CYFunctionParameter *Parameter(CYContext &context) const;
696 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
697 virtual void Output(CYOutput &out) const;
700 struct CYForEachInComprehension :
706 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
712 virtual const char *Name() const {
713 return name_->Word();
716 virtual CYFunctionParameter *Parameter(CYContext &context) const;
717 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
718 virtual void Output(CYOutput &out) const;
721 struct CYIfComprehension :
726 CYIfComprehension(CYExpression *test) :
731 virtual const char *Name() const {
735 virtual CYFunctionParameter *Parameter(CYContext &context) const;
736 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
737 virtual void Output(CYOutput &out) const;
740 struct CYArrayComprehension :
743 CYExpression *expression_;
744 CYComprehension *comprehensions_;
746 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
747 expression_(expression),
748 comprehensions_(comprehensions)
754 virtual CYExpression *Replace(CYContext &context);
755 virtual void Output(CYOutput &out, CYFlags flags) const;
768 virtual CYExpression *Replace(CYContext &context);
782 CYRange(uint64_t lo, uint64_t hi) :
787 bool operator [](uint8_t value) const {
788 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
791 void operator()(uint8_t value) {
794 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
798 extern CYRange DigitRange_;
799 extern CYRange WordStartRange_;
800 extern CYRange WordEndRange_;
815 CYString(const char *value) :
821 CYString(const char *value, size_t size) :
827 CYString(const CYWord *word) :
828 value_(word->Word()),
829 size_(strlen(value_))
833 const char *Value() const {
837 virtual const char *Word() const;
839 virtual CYNumber *Number(CYContext &context);
840 virtual CYString *String(CYContext &context);
842 CYString *Concat(CYContext &out, CYString *rhs) const;
843 virtual void Output(CYOutput &out, CYFlags flags) const;
844 virtual void PropertyName(CYOutput &out) const;
853 CYNumber(double value) :
858 double Value() const {
862 virtual CYNumber *Number(CYContext &context);
863 virtual CYString *String(CYContext &context);
865 virtual void Output(CYOutput &out, CYFlags flags) const;
866 virtual void PropertyName(CYOutput &out) const;
874 CYRegEx(const char *value) :
879 const char *Value() const {
883 virtual void Output(CYOutput &out, CYFlags flags) const;
895 virtual CYNumber *Number(CYContext &context);
896 virtual CYString *String(CYContext &context);
898 virtual void Output(CYOutput &out, CYFlags flags) const;
910 virtual CYExpression *Replace(CYContext &context);
911 virtual void Output(CYOutput &out, CYFlags flags) const;
917 virtual bool Value() const = 0;
918 virtual void Output(CYOutput &out, CYFlags flags) const;
930 virtual bool Value() const {
934 virtual CYNumber *Number(CYContext &context);
935 virtual CYString *String(CYContext &context);
947 virtual bool Value() const {
951 virtual CYNumber *Number(CYContext &context);
952 virtual CYString *String(CYContext &context);
960 CYVariable(CYIdentifier *name) :
965 CYVariable(const char *name) :
966 name_(new($pool) CYIdentifier(name))
973 virtual CYExpression *Replace(CYContext &context);
974 virtual void Output(CYOutput &out, CYFlags flags) const;
982 CYPrefix(CYExpression *rhs) :
987 virtual bool Alphabetic() const = 0;
988 virtual const char *Operator() const = 0;
992 virtual CYExpression *Replace(CYContext &context);
993 virtual void Output(CYOutput &out, CYFlags flags) const;
1002 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1008 void SetLeft(CYExpression *lhs) {
1012 virtual bool Alphabetic() const = 0;
1013 virtual const char *Operator() const = 0;
1015 virtual CYExpression *Replace(CYContext &context);
1016 virtual void Output(CYOutput &out, CYFlags flags) const;
1024 CYPostfix(CYExpression *lhs) :
1029 virtual const char *Operator() const = 0;
1033 virtual CYExpression *Replace(CYContext &context);
1034 virtual void Output(CYOutput &out, CYFlags flags) const;
1037 struct CYAssignment :
1043 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1049 void SetLeft(CYExpression *lhs) {
1053 virtual const char *Operator() const = 0;
1057 virtual CYExpression *Replace(CYContext &context);
1058 virtual void Output(CYOutput &out, CYFlags flags) const;
1066 CYExpression *value_;
1068 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1069 CYNext<CYArgument>(next),
1075 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1076 CYNext<CYArgument>(next),
1082 void Replace(CYContext &context);
1083 void Output(CYOutput &out) const;
1099 CYExpression *case_;
1100 CYStatement *statements_;
1102 CYClause(CYExpression *_case, CYStatement *statements) :
1104 statements_(statements)
1108 void Replace(CYContext &context);
1109 virtual void Output(CYOutput &out) const;
1116 CYExpression *value_;
1118 CYElement(CYExpression *value, CYElement *next) :
1119 CYNext<CYElement>(next),
1124 void Replace(CYContext &context);
1125 void Output(CYOutput &out) const;
1131 CYElement *elements_;
1133 CYArray(CYElement *elements = NULL) :
1138 virtual CYExpression *Replace(CYContext &context);
1139 virtual void Output(CYOutput &out, CYFlags flags) const;
1146 CYPropertyName *name_;
1147 CYExpression *value_;
1149 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1150 CYNext<CYProperty>(next),
1156 void Replace(CYContext &context);
1157 virtual void Output(CYOutput &out) const;
1160 struct CYDeclaration :
1163 CYIdentifier *identifier_;
1164 CYExpression *initialiser_;
1166 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1167 identifier_(identifier),
1168 initialiser_(initialiser)
1172 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1174 virtual const char *ForEachIn() const;
1175 virtual CYExpression *ForEachIn(CYContext &out);
1177 virtual CYExpression *Replace(CYContext &context);
1178 virtual CYAssignment *Assignment(CYContext &context);
1180 virtual void Output(CYOutput &out, CYFlags flags) const;
1183 struct CYDeclarations :
1184 CYNext<CYDeclarations>,
1188 CYDeclaration *declaration_;
1190 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1191 CYNext<CYDeclarations>(next),
1192 declaration_(declaration)
1196 virtual void For(CYOutput &out) const;
1198 virtual CYCompound *Replace(CYContext &context);
1199 CYProperty *Property(CYContext &context);
1201 virtual void Output(CYOutput &out) const;
1202 virtual void Output(CYOutput &out, CYFlags flags) const;
1208 CYDeclarations *declarations_;
1210 CYVar(CYDeclarations *declarations) :
1211 declarations_(declarations)
1215 virtual CYStatement *Replace(CYContext &context);
1216 virtual void Output(CYOutput &out, CYFlags flags) const;
1222 CYDeclarations *declarations_;
1225 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1226 declarations_(declarations),
1231 virtual CYStatement *Replace(CYContext &context);
1232 virtual void Output(CYOutput &out, CYFlags flags) const;
1238 CYForInitialiser *initialiser_;
1239 CYExpression *test_;
1240 CYExpression *increment_;
1243 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1244 initialiser_(initialiser),
1246 increment_(increment),
1251 virtual CYStatement *Replace(CYContext &context);
1252 virtual void Output(CYOutput &out, CYFlags flags) const;
1258 CYForInInitialiser *initialiser_;
1262 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1263 initialiser_(initialiser),
1269 virtual CYStatement *Replace(CYContext &context);
1270 virtual void Output(CYOutput &out, CYFlags flags) const;
1273 struct CYForEachIn :
1276 CYForInInitialiser *initialiser_;
1280 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1281 initialiser_(initialiser),
1287 virtual CYStatement *Replace(CYContext &context);
1288 virtual void Output(CYOutput &out, CYFlags flags) const;
1294 CYProperty *properties_;
1296 CYObject(CYProperty *properties = NULL) :
1297 properties_(properties)
1301 virtual CYExpression *Replace(CYContext &context);
1302 void Output(CYOutput &out, CYFlags flags) const;
1308 CYExpression *object_;
1309 CYExpression *property_;
1311 CYMember(CYExpression *object, CYExpression *property) :
1317 void SetLeft(CYExpression *object) {
1321 void Replace_(CYContext &context);
1324 struct CYDirectMember :
1327 CYDirectMember(CYExpression *object, CYExpression *property) :
1328 CYMember(object, property)
1335 virtual CYExpression *Replace(CYContext &context);
1336 virtual void Output(CYOutput &out, CYFlags flags) const;
1339 struct CYIndirectMember :
1342 CYIndirectMember(CYExpression *object, CYExpression *property) :
1343 CYMember(object, property)
1350 virtual CYExpression *Replace(CYContext &context);
1351 virtual void Output(CYOutput &out, CYFlags flags) const;
1360 CYExpression *constructor_;
1361 CYArgument *arguments_;
1363 New(CYExpression *constructor, CYArgument *arguments) :
1364 constructor_(constructor),
1365 arguments_(arguments)
1369 virtual unsigned Precedence() const {
1370 return arguments_ == NULL ? 2 : 1;
1375 virtual CYExpression *Replace(CYContext &context);
1376 virtual void Output(CYOutput &out, CYFlags flags) const;
1378 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1386 CYExpression *function_;
1387 CYArgument *arguments_;
1389 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1390 function_(function),
1391 arguments_(arguments)
1398 virtual CYExpression *Replace(CYContext &context);
1399 virtual void Output(CYOutput &out, CYFlags flags) const;
1401 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1406 struct CYRubyBlock :
1409 CYExpression *call_;
1412 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1421 virtual CYExpression *Replace(CYContext &context);
1422 virtual void Output(CYOutput &out, CYFlags flags) const;
1428 CYExpression *test_;
1430 CYStatement *false_;
1432 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1439 virtual CYStatement *Replace(CYContext &context);
1440 virtual void Output(CYOutput &out, CYFlags flags) const;
1446 CYExpression *test_;
1449 CYDoWhile(CYExpression *test, CYStatement *code) :
1455 virtual CYStatement *Replace(CYContext &context);
1456 virtual void Output(CYOutput &out, CYFlags flags) const;
1462 CYExpression *test_;
1465 CYWhile(CYExpression *test, CYStatement *code) :
1471 virtual CYStatement *Replace(CYContext &context);
1472 virtual void Output(CYOutput &out, CYFlags flags) const;
1475 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1477 CYIdentifier *name_;
1478 CYFunctionParameter *parameters_;
1480 CYNonLocal *nonlocal_;
1482 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1484 parameters_(parameters),
1490 virtual ~CYFunction() {
1493 void Inject(CYContext &context);
1494 virtual void Replace_(CYContext &context, bool outer);
1495 virtual void Output(CYOutput &out, CYFlags flags) const;
1498 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1499 struct CYFunctionExpression :
1503 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1504 CYFunction(name, parameters, statements)
1511 virtual CYExpression *Replace(CYContext &context);
1512 virtual void Output(CYOutput &out, CYFlags flags) const;
1515 // XXX: this should derive from CYAnonymousFunctionExpression
1517 CYFunctionExpression
1519 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1520 CYFunctionExpression(NULL, parameters, statements)
1524 virtual CYExpression *Replace(CYContext &context);
1525 virtual void Output(CYOutput &out, CYFlags flags) const;
1528 // XXX: this should derive from CYNamedFunction
1529 struct CYFunctionStatement :
1533 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1534 CYFunction(name, parameters, statements)
1538 virtual CYStatement *Replace(CYContext &context);
1539 virtual void Output(CYOutput &out, CYFlags flags) const;
1545 CYExpression *expression_;
1547 CYExpress(CYExpression *expression) :
1548 expression_(expression)
1550 if (expression == NULL)
1554 virtual CYStatement *Collapse(CYContext &context);
1555 virtual CYStatement *Replace(CYContext &context);
1556 virtual void Output(CYOutput &out, CYFlags flags) const;
1562 CYIdentifier *label_;
1564 CYContinue(CYIdentifier *label) :
1569 virtual CYStatement *Replace(CYContext &context);
1570 virtual void Output(CYOutput &out, CYFlags flags) const;
1576 CYIdentifier *label_;
1578 CYBreak(CYIdentifier *label) :
1583 virtual CYStatement *Replace(CYContext &context);
1584 virtual void Output(CYOutput &out, CYFlags flags) const;
1590 CYExpression *value_;
1592 CYReturn(CYExpression *value) :
1597 virtual CYStatement *Replace(CYContext &context);
1598 virtual void Output(CYOutput &out, CYFlags flags) const;
1604 virtual CYStatement *Collapse(CYContext &context);
1605 virtual CYStatement *Replace(CYContext &context);
1606 virtual void Output(CYOutput &out, CYFlags flags) const;
1614 CYFinally(CYStatement *statements) :
1619 void Replace(CYContext &context);
1620 virtual void Output(CYOutput &out) const;
1629 CYIdentifier *name_;
1632 Catch(CYIdentifier *name, CYStatement *statements) :
1638 void Replace(CYContext &context);
1639 virtual void Output(CYOutput &out) const;
1647 CYFinally *finally_;
1649 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1656 virtual CYStatement *Replace(CYContext &context);
1657 virtual void Output(CYOutput &out, CYFlags flags) const;
1663 CYExpression *value_;
1665 Throw(CYExpression *value = NULL) :
1670 virtual CYStatement *Replace(CYContext &context);
1671 virtual void Output(CYOutput &out, CYFlags flags) const;
1679 CYExpression *scope_;
1682 CYWith(CYExpression *scope, CYStatement *code) :
1688 virtual CYStatement *Replace(CYContext &context);
1689 virtual void Output(CYOutput &out, CYFlags flags) const;
1695 CYExpression *value_;
1698 CYSwitch(CYExpression *value, CYClause *clauses) :
1704 virtual CYStatement *Replace(CYContext &context);
1705 virtual void Output(CYOutput &out, CYFlags flags) const;
1708 struct CYCondition :
1711 CYExpression *test_;
1712 CYExpression *true_;
1713 CYExpression *false_;
1715 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1724 virtual CYExpression *Replace(CYContext &context);
1725 virtual void Output(CYOutput &out, CYFlags flags) const;
1728 struct CYAddressOf :
1731 CYAddressOf(CYExpression *rhs) :
1736 virtual const char *Operator() const {
1742 virtual CYExpression *Replace(CYContext &context);
1748 CYIndirect(CYExpression *rhs) :
1753 virtual const char *Operator() const {
1759 virtual CYExpression *Replace(CYContext &context);
1763 virtual CYExpression *Replace(CYContext &context);
1765 #define CYPostfix_(op, name, args...) \
1766 struct CY ## name : \
1769 CY ## name(CYExpression *lhs) : \
1774 virtual const char *Operator() const { \
1779 #define CYPrefix_(alphabetic, op, name, args...) \
1780 struct CY ## name : \
1783 CY ## name(CYExpression *rhs) : \
1788 CYAlphabetic(alphabetic) \
1790 virtual const char *Operator() const { \
1795 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1796 struct CY ## name : \
1799 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1804 CYAlphabetic(alphabetic) \
1805 CYPrecedence(precedence) \
1807 virtual const char *Operator() const { \
1812 #define CYAssignment_(op, name, args...) \
1813 struct CY ## name ## Assign : \
1816 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1817 CYAssignment(lhs, rhs) \
1821 virtual const char *Operator() const { \
1826 CYPostfix_("++", PostIncrement)
1827 CYPostfix_("--", PostDecrement)
1829 CYPrefix_(true, "delete", Delete)
1830 CYPrefix_(true, "void", Void)
1831 CYPrefix_(true, "typeof", TypeOf)
1832 CYPrefix_(false, "++", PreIncrement)
1833 CYPrefix_(false, "--", PreDecrement)
1834 CYPrefix_(false, "+", Affirm)
1835 CYPrefix_(false, "-", Negate)
1836 CYPrefix_(false, "~", BitwiseNot)
1837 CYPrefix_(false, "!", LogicalNot)
1839 CYInfix_(false, 5, "*", Multiply)
1840 CYInfix_(false, 5, "/", Divide)
1841 CYInfix_(false, 5, "%", Modulus)
1842 CYInfix_(false, 6, "+", Add, CYReplace)
1843 CYInfix_(false, 6, "-", Subtract)
1844 CYInfix_(false, 7, "<<", ShiftLeft)
1845 CYInfix_(false, 7, ">>", ShiftRightSigned)
1846 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1847 CYInfix_(false, 8, "<", Less)
1848 CYInfix_(false, 8, ">", Greater)
1849 CYInfix_(false, 8, "<=", LessOrEqual)
1850 CYInfix_(false, 8, ">=", GreaterOrEqual)
1851 CYInfix_(true, 8, "instanceof", InstanceOf)
1852 CYInfix_(true, 8, "in", In)
1853 CYInfix_(false, 9, "==", Equal)
1854 CYInfix_(false, 9, "!=", NotEqual)
1855 CYInfix_(false, 9, "===", Identical)
1856 CYInfix_(false, 9, "!==", NotIdentical)
1857 CYInfix_(false, 10, "&", BitwiseAnd)
1858 CYInfix_(false, 11, "^", BitwiseXOr)
1859 CYInfix_(false, 12, "|", BitwiseOr)
1860 CYInfix_(false, 13, "&&", LogicalAnd)
1861 CYInfix_(false, 14, "||", LogicalOr)
1863 CYAssignment_("=", )
1864 CYAssignment_("*=", Multiply)
1865 CYAssignment_("/=", Divide)
1866 CYAssignment_("%=", Modulus)
1867 CYAssignment_("+=", Add)
1868 CYAssignment_("-=", Subtract)
1869 CYAssignment_("<<=", ShiftLeft)
1870 CYAssignment_(">>=", ShiftRightSigned)
1871 CYAssignment_(">>>=", ShiftRightUnsigned)
1872 CYAssignment_("&=", BitwiseAnd)
1873 CYAssignment_("^=", BitwiseXOr)
1874 CYAssignment_("|=", BitwiseOr)
1876 #endif/*CYPARSER_HPP*/