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) {
64 virtual void Output(struct CYOutput &out) const = 0;
82 CYOutput(std::ostream &out, CYOptions &options) :
92 void Check(char value);
95 CYOutput &operator <<(char rhs);
96 CYOutput &operator <<(const char *rhs);
98 _finline CYOutput &operator <<(const CYThing *rhs) {
104 _finline CYOutput &operator <<(const CYThing &rhs) {
110 struct CYPropertyName {
111 virtual void PropertyName(CYOutput &out) const = 0;
113 virtual ~CYPropertyName() {
127 CYNoBrace = (1 << 0),
128 CYNoFunction = (1 << 1),
131 CYNoRightHand = (1 << 4),
132 CYNoDangle = (1 << 5),
133 CYNoInteger = (1 << 6),
134 CYNoBF = (CYNoBrace | CYNoFunction),
140 virtual ~CYStatement() {
143 void Single(CYOutput &out, CYFlags flags) const;
144 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
146 virtual CYStatement *Collapse(CYContext &context);
147 virtual CYStatement *Replace(CYContext &context) = 0;
150 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
153 struct CYStatements {
163 operator CYStatement *() const {
167 CYStatements &operator ->*(CYStatement *next) {
169 if (first_ == NULL) {
172 } else for (;; last_ = last_->next_)
173 if (last_->next_ == NULL) {
183 virtual ~CYClassName() {
186 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
187 virtual void ClassName(CYOutput &out, bool object) const = 0;
197 CYWord(const char *word) :
202 void Set(const char *value) {
206 virtual const char *Word() const;
207 virtual void Output(CYOutput &out) const;
209 virtual CYExpression *ClassName(CYContext &context, bool object);
210 virtual void ClassName(CYOutput &out, bool object) const;
211 virtual void PropertyName(CYOutput &out) const;
214 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
216 return lhs << rhs.Word();
219 struct CYIdentifier :
220 CYNext<CYIdentifier>,
223 CYIdentifier *replace_;
227 CYIdentifier(const char *word) :
235 virtual const char *Word() const;
236 CYIdentifier *Replace(CYContext &context);
244 CYComment(const char *value) :
249 virtual CYStatement *Replace(CYContext &context);
250 virtual void Output(CYOutput &out, CYFlags flags) const;
257 CYStatement *statement_;
259 CYLabel(CYIdentifier *name, CYStatement *statement) :
261 statement_(statement)
265 virtual CYStatement *Replace(CYContext &context);
266 virtual void Output(CYOutput &out, CYFlags flags) const;
269 struct CYCStringLess :
270 std::binary_function<const char *, const char *, bool>
272 _finline bool operator ()(const char *lhs, const char *rhs) const {
273 return strcmp(lhs, rhs) < 0;
277 struct CYIdentifierValueLess :
278 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
280 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
281 return CYCStringLess()(lhs->Word(), rhs->Word());
285 enum CYIdentifierFlags {
286 CYIdentifierArgument,
287 CYIdentifierVariable,
293 typedef std::set<const char *, CYCStringLess> CYCStringSet;
294 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
295 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
297 struct CYIdentifierUsage {
298 CYIdentifier *identifier_;
302 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
304 // XXX: strategy pattern, maybe subclass
315 CYStatement *&statements_;
319 CYIdentifierAddressFlagsMap internal_;
320 CYIdentifierValueSet identifiers_;
322 CYScope(CYScopeType type, CYContext &context, CYStatement *&statements);
326 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
327 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
328 void Merge(CYContext &context, CYIdentifier *identifier);
329 void Scope(CYContext &context, CYStatement *&statements);
335 CYStatement *statements_;
337 CYProgram(CYStatement *statements) :
338 statements_(statements)
342 virtual void Replace(CYContext &context);
343 virtual void Output(CYOutput &out) const;
352 CYIdentifierUsageVector rename_;
354 CYNonLocal *nonlocal_;
355 CYNonLocal *nextlocal_;
358 CYContext(CYOptions &options) :
367 virtual ~CYContext() {
370 template <typename Type_>
371 void ReplaceAll(Type_ *&values) {
372 Type_ **last(&values);
373 for (Type_ *next(values); next != NULL; next = next->next_) {
375 last = &(*last)->next_;
379 template <typename Type_>
380 void Replace(Type_ *&value) {
381 for (;;) if (value == NULL)
384 Type_ *replace(value->Replace(*this));
385 if (replace != value)
391 void NonLocal(CYStatement *&statements);
392 CYIdentifier *Unique();
396 CYIdentifier *identifier_;
403 CYIdentifier *Target(CYContext &context) {
404 if (identifier_ == NULL)
405 identifier_ = context.Unique();
414 CYStatement *statements_;
416 CYBlock(CYStatement *statements) :
417 statements_(statements)
421 operator CYStatement *() const {
425 void AddPrev(CYStatement *statement) {
426 CYStatement *last(statement);
427 while (last->next_ != NULL)
429 last->SetNext(statements_);
430 statements_ = statement;
433 virtual CYStatement *Replace(CYContext &context);
435 virtual void Output(CYOutput &out) const;
436 virtual void Output(CYOutput &out, CYFlags flags) const;
462 std::string filename_;
466 cy::location location_;
467 std::string message_;
470 typedef std::vector<Error> Errors;
478 CYExpression *context_;
480 Context(CYExpression *context) :
485 typedef std::vector<CYWord *> Words;
489 typedef std::vector<Context> Contexts;
492 CYExpression *context_;
504 void ScannerDestroy();
507 CYDriver(const std::string &filename = "");
510 Condition GetCondition();
511 void SetCondition(Condition condition);
513 void PushCondition(Condition condition);
516 void Warning(const cy::location &location, const char *message);
519 struct CYForInitialiser {
520 virtual ~CYForInitialiser() {
523 virtual void For(CYOutput &out) const = 0;
524 virtual CYExpression *Replace(CYContext &context) = 0;
527 struct CYForInInitialiser {
528 virtual ~CYForInInitialiser() {
531 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
532 virtual const char *ForEachIn() const = 0;
533 virtual CYExpression *ForEachIn(CYContext &out) = 0;
534 virtual CYExpression *Replace(CYContext &context) = 0;
540 struct CYExpression :
541 CYNext<CYExpression>,
547 virtual unsigned Precedence() const = 0;
549 virtual bool RightHand() const {
553 virtual void For(CYOutput &out) const;
554 virtual void ForIn(CYOutput &out, CYFlags flags) const;
556 virtual const char *ForEachIn() const;
557 virtual CYExpression *ForEachIn(CYContext &out);
559 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
561 virtual void Output(CYOutput &out) const;
562 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
563 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
565 virtual CYExpression *ClassName(CYContext &context, bool object);
566 virtual void ClassName(CYOutput &out, bool object) const;
568 virtual CYExpression *Replace(CYContext &context) = 0;
570 virtual CYExpression *Primitive(CYContext &context) {
574 virtual CYNumber *Number(CYContext &context) {
578 virtual CYString *String(CYContext &context) {
582 virtual const char *Word() const {
587 #define CYAlphabetic(value) \
588 virtual bool Alphabetic() const { \
592 #define CYPrecedence(value) \
593 static const unsigned Precedence_ = value; \
594 virtual unsigned Precedence() const { \
595 return Precedence_; \
598 #define CYRightHand(value) \
599 virtual bool RightHand() const { \
606 CYExpression *expressions_;
608 CYCompound(CYExpression *expressions = NULL) :
609 expressions_(expressions)
613 void AddPrev(CYExpression *expression) {
614 CYExpression *last(expression);
615 while (last->next_ != NULL)
617 last->SetNext(expressions_);
618 expressions_ = expression;
623 virtual CYExpression *Replace(CYContext &context);
624 void Output(CYOutput &out, CYFlags flags) const;
627 struct CYFunctionParameter :
628 CYNext<CYFunctionParameter>,
633 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
634 CYNext<CYFunctionParameter>(next),
639 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
640 virtual void Output(CYOutput &out) const;
643 struct CYOptionalFunctionParameter :
646 CYExpression *initializer_;
648 CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
649 CYFunctionParameter(name, next),
650 initializer_(initializer)
654 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
655 virtual void Output(CYOutput &out) const;
658 struct CYComprehension :
659 CYNext<CYComprehension>,
662 virtual const char *Name() const = 0;
664 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
665 CYFunctionParameter *Parameters(CYContext &context) const;
666 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
667 virtual void Output(CYOutput &out) const = 0;
670 struct CYForInComprehension :
676 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
682 virtual const char *Name() const {
683 return name_->Word();
686 virtual CYFunctionParameter *Parameter(CYContext &context) const;
687 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
688 virtual void Output(CYOutput &out) const;
691 struct CYForEachInComprehension :
697 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
703 virtual const char *Name() const {
704 return name_->Word();
707 virtual CYFunctionParameter *Parameter(CYContext &context) const;
708 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
709 virtual void Output(CYOutput &out) const;
712 struct CYIfComprehension :
717 CYIfComprehension(CYExpression *test) :
722 virtual const char *Name() const {
726 virtual CYFunctionParameter *Parameter(CYContext &context) const;
727 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
728 virtual void Output(CYOutput &out) const;
731 struct CYArrayComprehension :
734 CYExpression *expression_;
735 CYComprehension *comprehensions_;
737 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
738 expression_(expression),
739 comprehensions_(comprehensions)
745 virtual CYExpression *Replace(CYContext &context);
746 virtual void Output(CYOutput &out, CYFlags flags) const;
759 virtual CYExpression *Replace(CYContext &context);
773 CYRange(uint64_t lo, uint64_t hi) :
778 bool operator [](uint8_t value) const {
779 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
782 void operator()(uint8_t value) {
785 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
789 extern CYRange DigitRange_;
790 extern CYRange WordStartRange_;
791 extern CYRange WordEndRange_;
806 CYString(const char *value) :
812 CYString(const char *value, size_t size) :
818 CYString(const CYWord *word) :
819 value_(word->Word()),
820 size_(strlen(value_))
824 const char *Value() const {
828 virtual const char *Word() const;
830 virtual CYNumber *Number(CYContext &context);
831 virtual CYString *String(CYContext &context);
833 CYString *Concat(CYContext &out, CYString *rhs) const;
834 virtual void Output(CYOutput &out, CYFlags flags) const;
835 virtual void PropertyName(CYOutput &out) const;
844 CYNumber(double value) :
849 double Value() const {
853 virtual CYNumber *Number(CYContext &context);
854 virtual CYString *String(CYContext &context);
856 virtual void Output(CYOutput &out, CYFlags flags) const;
857 virtual void PropertyName(CYOutput &out) const;
865 CYRegEx(const char *value) :
870 const char *Value() const {
874 virtual void Output(CYOutput &out, CYFlags flags) const;
886 virtual CYNumber *Number(CYContext &context);
887 virtual CYString *String(CYContext &context);
889 virtual void Output(CYOutput &out, CYFlags flags) const;
901 virtual CYExpression *Replace(CYContext &context);
902 virtual void Output(CYOutput &out, CYFlags flags) const;
908 virtual bool Value() const = 0;
909 virtual void Output(CYOutput &out, CYFlags flags) const;
921 virtual bool Value() const {
925 virtual CYNumber *Number(CYContext &context);
926 virtual CYString *String(CYContext &context);
938 virtual bool Value() const {
942 virtual CYNumber *Number(CYContext &context);
943 virtual CYString *String(CYContext &context);
951 CYVariable(CYIdentifier *name) :
956 CYVariable(const char *name) :
957 name_(new($pool) CYIdentifier(name))
964 virtual CYExpression *Replace(CYContext &context);
965 virtual void Output(CYOutput &out, CYFlags flags) const;
973 CYPrefix(CYExpression *rhs) :
978 virtual bool Alphabetic() const = 0;
979 virtual const char *Operator() const = 0;
983 virtual CYExpression *Replace(CYContext &context);
984 virtual void Output(CYOutput &out, CYFlags flags) const;
993 CYInfix(CYExpression *lhs, CYExpression *rhs) :
999 void SetLeft(CYExpression *lhs) {
1003 virtual bool Alphabetic() const = 0;
1004 virtual const char *Operator() const = 0;
1006 virtual CYExpression *Replace(CYContext &context);
1007 virtual void Output(CYOutput &out, CYFlags flags) const;
1015 CYPostfix(CYExpression *lhs) :
1020 virtual const char *Operator() const = 0;
1024 virtual CYExpression *Replace(CYContext &context);
1025 virtual void Output(CYOutput &out, CYFlags flags) const;
1028 struct CYAssignment :
1034 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1040 void SetLeft(CYExpression *lhs) {
1044 virtual const char *Operator() const = 0;
1048 virtual CYExpression *Replace(CYContext &context);
1049 virtual void Output(CYOutput &out, CYFlags flags) const;
1057 CYExpression *value_;
1059 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1060 CYNext<CYArgument>(next),
1066 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1067 CYNext<CYArgument>(next),
1073 void Replace(CYContext &context);
1074 void Output(CYOutput &out) const;
1090 CYExpression *case_;
1091 CYStatement *statements_;
1093 CYClause(CYExpression *_case, CYStatement *statements) :
1095 statements_(statements)
1099 void Replace(CYContext &context);
1100 virtual void Output(CYOutput &out) const;
1107 CYExpression *value_;
1109 CYElement(CYExpression *value, CYElement *next) :
1110 CYNext<CYElement>(next),
1115 void Replace(CYContext &context);
1116 void Output(CYOutput &out) const;
1122 CYElement *elements_;
1124 CYArray(CYElement *elements = NULL) :
1129 virtual CYExpression *Replace(CYContext &context);
1130 virtual void Output(CYOutput &out, CYFlags flags) const;
1137 CYPropertyName *name_;
1138 CYExpression *value_;
1140 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1141 CYNext<CYProperty>(next),
1147 void Replace(CYContext &context);
1148 virtual void Output(CYOutput &out) const;
1151 struct CYDeclaration :
1154 CYIdentifier *identifier_;
1155 CYExpression *initialiser_;
1157 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1158 identifier_(identifier),
1159 initialiser_(initialiser)
1163 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1165 virtual const char *ForEachIn() const;
1166 virtual CYExpression *ForEachIn(CYContext &out);
1168 virtual CYExpression *Replace(CYContext &context);
1169 virtual CYAssignment *Assignment(CYContext &context);
1171 virtual void Output(CYOutput &out, CYFlags flags) const;
1174 struct CYDeclarations :
1175 CYNext<CYDeclarations>,
1179 CYDeclaration *declaration_;
1181 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1182 CYNext<CYDeclarations>(next),
1183 declaration_(declaration)
1187 virtual void For(CYOutput &out) const;
1189 virtual CYCompound *Replace(CYContext &context);
1190 CYProperty *Property(CYContext &context);
1192 virtual void Output(CYOutput &out) const;
1193 virtual void Output(CYOutput &out, CYFlags flags) const;
1199 CYDeclarations *declarations_;
1201 CYVar(CYDeclarations *declarations) :
1202 declarations_(declarations)
1206 virtual CYStatement *Replace(CYContext &context);
1207 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 CYDeclarations *declarations_;
1216 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1217 declarations_(declarations),
1222 virtual CYStatement *Replace(CYContext &context);
1223 virtual void Output(CYOutput &out, CYFlags flags) const;
1229 CYForInitialiser *initialiser_;
1230 CYExpression *test_;
1231 CYExpression *increment_;
1234 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1235 initialiser_(initialiser),
1237 increment_(increment),
1242 virtual CYStatement *Replace(CYContext &context);
1243 virtual void Output(CYOutput &out, CYFlags flags) const;
1249 CYForInInitialiser *initialiser_;
1253 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1254 initialiser_(initialiser),
1260 virtual CYStatement *Replace(CYContext &context);
1261 virtual void Output(CYOutput &out, CYFlags flags) const;
1264 struct CYForEachIn :
1267 CYForInInitialiser *initialiser_;
1271 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1272 initialiser_(initialiser),
1278 virtual CYStatement *Replace(CYContext &context);
1279 virtual void Output(CYOutput &out, CYFlags flags) const;
1285 CYProperty *properties_;
1287 CYObject(CYProperty *properties = NULL) :
1288 properties_(properties)
1292 virtual CYExpression *Replace(CYContext &context);
1293 void Output(CYOutput &out, CYFlags flags) const;
1299 CYExpression *object_;
1300 CYExpression *property_;
1302 CYMember(CYExpression *object, CYExpression *property) :
1308 void SetLeft(CYExpression *object) {
1312 void Replace_(CYContext &context);
1315 struct CYDirectMember :
1318 CYDirectMember(CYExpression *object, CYExpression *property) :
1319 CYMember(object, property)
1326 virtual CYExpression *Replace(CYContext &context);
1327 virtual void Output(CYOutput &out, CYFlags flags) const;
1330 struct CYIndirectMember :
1333 CYIndirectMember(CYExpression *object, CYExpression *property) :
1334 CYMember(object, property)
1341 virtual CYExpression *Replace(CYContext &context);
1342 virtual void Output(CYOutput &out, CYFlags flags) const;
1351 CYExpression *constructor_;
1352 CYArgument *arguments_;
1354 New(CYExpression *constructor, CYArgument *arguments) :
1355 constructor_(constructor),
1356 arguments_(arguments)
1360 virtual unsigned Precedence() const {
1361 return arguments_ == NULL ? 2 : 1;
1366 virtual CYExpression *Replace(CYContext &context);
1367 virtual void Output(CYOutput &out, CYFlags flags) const;
1369 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1377 CYExpression *function_;
1378 CYArgument *arguments_;
1380 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1381 function_(function),
1382 arguments_(arguments)
1389 virtual CYExpression *Replace(CYContext &context);
1390 virtual void Output(CYOutput &out, CYFlags flags) const;
1392 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1397 struct CYRubyBlock :
1400 CYExpression *call_;
1403 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1412 virtual CYExpression *Replace(CYContext &context);
1413 virtual void Output(CYOutput &out, CYFlags flags) const;
1419 CYExpression *test_;
1421 CYStatement *false_;
1423 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1430 virtual CYStatement *Replace(CYContext &context);
1431 virtual void Output(CYOutput &out, CYFlags flags) const;
1437 CYExpression *test_;
1440 CYDoWhile(CYExpression *test, CYStatement *code) :
1446 virtual CYStatement *Replace(CYContext &context);
1447 virtual void Output(CYOutput &out, CYFlags flags) const;
1453 CYExpression *test_;
1456 CYWhile(CYExpression *test, CYStatement *code) :
1462 virtual CYStatement *Replace(CYContext &context);
1463 virtual void Output(CYOutput &out, CYFlags flags) const;
1466 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1468 CYIdentifier *name_;
1469 CYFunctionParameter *parameters_;
1471 CYNonLocal *nonlocal_;
1473 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1475 parameters_(parameters),
1481 virtual ~CYFunction() {
1484 void Inject(CYContext &context);
1485 virtual void Replace_(CYContext &context, bool outer);
1486 virtual void Output(CYOutput &out, CYFlags flags) const;
1489 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1490 struct CYFunctionExpression :
1494 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1495 CYFunction(name, parameters, statements)
1502 virtual CYExpression *Replace(CYContext &context);
1503 virtual void Output(CYOutput &out, CYFlags flags) const;
1506 // XXX: this should derive from CYAnonymousFunctionExpression
1508 CYFunctionExpression
1510 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1511 CYFunctionExpression(NULL, parameters, statements)
1515 virtual CYExpression *Replace(CYContext &context);
1516 virtual void Output(CYOutput &out, CYFlags flags) const;
1519 // XXX: this should derive from CYNamedFunction
1520 struct CYFunctionStatement :
1524 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1525 CYFunction(name, parameters, statements)
1529 virtual CYStatement *Replace(CYContext &context);
1530 virtual void Output(CYOutput &out, CYFlags flags) const;
1536 CYExpression *expression_;
1538 CYExpress(CYExpression *expression) :
1539 expression_(expression)
1541 if (expression == NULL)
1545 virtual CYStatement *Collapse(CYContext &context);
1546 virtual CYStatement *Replace(CYContext &context);
1547 virtual void Output(CYOutput &out, CYFlags flags) const;
1553 CYIdentifier *label_;
1555 CYContinue(CYIdentifier *label) :
1560 virtual CYStatement *Replace(CYContext &context);
1561 virtual void Output(CYOutput &out, CYFlags flags) const;
1567 CYIdentifier *label_;
1569 CYBreak(CYIdentifier *label) :
1574 virtual CYStatement *Replace(CYContext &context);
1575 virtual void Output(CYOutput &out, CYFlags flags) const;
1581 CYExpression *value_;
1583 CYReturn(CYExpression *value) :
1588 virtual CYStatement *Replace(CYContext &context);
1589 virtual void Output(CYOutput &out, CYFlags flags) const;
1595 virtual CYStatement *Collapse(CYContext &context);
1596 virtual CYStatement *Replace(CYContext &context);
1597 virtual void Output(CYOutput &out, CYFlags flags) const;
1605 CYFinally(CYStatement *statements) :
1610 void Replace(CYContext &context);
1611 virtual void Output(CYOutput &out) const;
1620 CYIdentifier *name_;
1623 Catch(CYIdentifier *name, CYStatement *statements) :
1629 void Replace(CYContext &context);
1630 virtual void Output(CYOutput &out) const;
1638 CYFinally *finally_;
1640 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1647 virtual CYStatement *Replace(CYContext &context);
1648 virtual void Output(CYOutput &out, CYFlags flags) const;
1654 CYExpression *value_;
1656 Throw(CYExpression *value = NULL) :
1661 virtual CYStatement *Replace(CYContext &context);
1662 virtual void Output(CYOutput &out, CYFlags flags) const;
1670 CYExpression *scope_;
1673 CYWith(CYExpression *scope, CYStatement *code) :
1679 virtual CYStatement *Replace(CYContext &context);
1680 virtual void Output(CYOutput &out, CYFlags flags) const;
1686 CYExpression *value_;
1689 CYSwitch(CYExpression *value, CYClause *clauses) :
1695 virtual CYStatement *Replace(CYContext &context);
1696 virtual void Output(CYOutput &out, CYFlags flags) const;
1699 struct CYCondition :
1702 CYExpression *test_;
1703 CYExpression *true_;
1704 CYExpression *false_;
1706 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1715 virtual CYExpression *Replace(CYContext &context);
1716 virtual void Output(CYOutput &out, CYFlags flags) const;
1719 struct CYAddressOf :
1722 CYAddressOf(CYExpression *rhs) :
1727 virtual const char *Operator() const {
1733 virtual CYExpression *Replace(CYContext &context);
1739 CYIndirect(CYExpression *rhs) :
1744 virtual const char *Operator() const {
1750 virtual CYExpression *Replace(CYContext &context);
1754 virtual CYExpression *Replace(CYContext &context);
1756 #define CYPostfix_(op, name, args...) \
1757 struct CY ## name : \
1760 CY ## name(CYExpression *lhs) : \
1765 virtual const char *Operator() const { \
1770 #define CYPrefix_(alphabetic, op, name, args...) \
1771 struct CY ## name : \
1774 CY ## name(CYExpression *rhs) : \
1779 CYAlphabetic(alphabetic) \
1781 virtual const char *Operator() const { \
1786 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1787 struct CY ## name : \
1790 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1795 CYAlphabetic(alphabetic) \
1796 CYPrecedence(precedence) \
1798 virtual const char *Operator() const { \
1803 #define CYAssignment_(op, name, args...) \
1804 struct CY ## name ## Assign : \
1807 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1808 CYAssignment(lhs, rhs) \
1812 virtual const char *Operator() const { \
1817 CYPostfix_("++", PostIncrement)
1818 CYPostfix_("--", PostDecrement)
1820 CYPrefix_(true, "delete", Delete)
1821 CYPrefix_(true, "void", Void)
1822 CYPrefix_(true, "typeof", TypeOf)
1823 CYPrefix_(false, "++", PreIncrement)
1824 CYPrefix_(false, "--", PreDecrement)
1825 CYPrefix_(false, "+", Affirm)
1826 CYPrefix_(false, "-", Negate)
1827 CYPrefix_(false, "~", BitwiseNot)
1828 CYPrefix_(false, "!", LogicalNot)
1830 CYInfix_(false, 5, "*", Multiply)
1831 CYInfix_(false, 5, "/", Divide)
1832 CYInfix_(false, 5, "%", Modulus)
1833 CYInfix_(false, 6, "+", Add, CYReplace)
1834 CYInfix_(false, 6, "-", Subtract)
1835 CYInfix_(false, 7, "<<", ShiftLeft)
1836 CYInfix_(false, 7, ">>", ShiftRightSigned)
1837 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1838 CYInfix_(false, 8, "<", Less)
1839 CYInfix_(false, 8, ">", Greater)
1840 CYInfix_(false, 8, "<=", LessOrEqual)
1841 CYInfix_(false, 8, ">=", GreaterOrEqual)
1842 CYInfix_(true, 8, "instanceof", InstanceOf)
1843 CYInfix_(true, 8, "in", In)
1844 CYInfix_(false, 9, "==", Equal)
1845 CYInfix_(false, 9, "!=", NotEqual)
1846 CYInfix_(false, 9, "===", Identical)
1847 CYInfix_(false, 9, "!==", NotIdentical)
1848 CYInfix_(false, 10, "&", BitwiseAnd)
1849 CYInfix_(false, 11, "^", BitwiseXOr)
1850 CYInfix_(false, 12, "|", BitwiseOr)
1851 CYInfix_(false, 13, "&&", LogicalAnd)
1852 CYInfix_(false, 14, "||", LogicalOr)
1854 CYAssignment_("=", )
1855 CYAssignment_("*=", Multiply)
1856 CYAssignment_("/=", Divide)
1857 CYAssignment_("%=", Modulus)
1858 CYAssignment_("+=", Add)
1859 CYAssignment_("-=", Subtract)
1860 CYAssignment_("<<=", ShiftLeft)
1861 CYAssignment_(">>=", ShiftRightSigned)
1862 CYAssignment_(">>>=", ShiftRightUnsigned)
1863 CYAssignment_("&=", BitwiseAnd)
1864 CYAssignment_("^=", BitwiseXOr)
1865 CYAssignment_("|=", BitwiseOr)
1867 #endif/*CYPARSER_HPP*/