1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 // XXX: wtf is this here?!
56 #include "location.hh"
57 #include "Pooling.hpp"
58 #include "Options.hpp"
62 template <typename Type_>
76 void SetNext(Type_ *next) {
85 virtual void Output(struct CYOutput &out) const = 0;
103 CYOutput(std::ostream &out, CYOptions &options) :
113 void Check(char value);
116 CYOutput &operator <<(char rhs);
117 CYOutput &operator <<(const char *rhs);
119 _finline CYOutput &operator <<(const CYThing *rhs) {
125 _finline CYOutput &operator <<(const CYThing &rhs) {
131 struct CYPropertyName {
132 virtual void PropertyName(CYOutput &out) const = 0;
134 virtual ~CYPropertyName() {
148 CYNoBrace = (1 << 0),
149 CYNoFunction = (1 << 1),
152 CYNoRightHand = (1 << 4),
153 CYNoDangle = (1 << 5),
154 CYNoInteger = (1 << 6),
155 CYNoBF = (CYNoBrace | CYNoFunction),
161 virtual ~CYStatement() {
164 void Single(CYOutput &out, CYFlags flags) const;
165 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
167 CYStatement *ReplaceAll(CYContext &context);
168 virtual CYStatement *Collapse(CYContext &context);
170 virtual CYStatement *Replace(CYContext &context) = 0;
173 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
176 struct CYStatements {
186 operator CYStatement *() const {
190 CYStatements &operator ->*(CYStatement *next) {
192 if (first_ == NULL) {
195 } else for (;; last_ = last_->next_)
196 if (last_->next_ == NULL) {
206 virtual ~CYClassName() {
209 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
210 virtual void ClassName(CYOutput &out, bool object) const = 0;
220 CYWord(const char *word) :
225 void Set(const char *value) {
229 virtual const char *Word() const;
230 virtual void Output(CYOutput &out) const;
232 virtual CYExpression *ClassName(CYContext &context, bool object);
233 virtual void ClassName(CYOutput &out, bool object) const;
234 virtual void PropertyName(CYOutput &out) const;
237 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
239 return lhs << rhs.Word();
242 struct CYIdentifier :
243 CYNext<CYIdentifier>,
246 CYIdentifier *replace_;
250 CYIdentifier(const char *word) :
258 virtual const char *Word() const;
259 CYIdentifier *Replace(CYContext &context);
267 CYComment(const char *value) :
272 virtual CYStatement *Replace(CYContext &context);
273 virtual void Output(CYOutput &out, CYFlags flags) const;
280 CYStatement *statement_;
282 CYLabel(CYIdentifier *name, CYStatement *statement) :
284 statement_(statement)
288 virtual CYStatement *Replace(CYContext &context);
289 virtual void Output(CYOutput &out, CYFlags flags) const;
292 struct CYCStringLess :
293 std::binary_function<const char *, const char *, bool>
295 _finline bool operator ()(const char *lhs, const char *rhs) const {
296 return strcmp(lhs, rhs) < 0;
300 struct CYIdentifierValueLess :
301 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
303 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
304 return CYCStringLess()(lhs->Word(), rhs->Word());
308 enum CYIdentifierFlags {
309 CYIdentifierArgument,
310 CYIdentifierVariable,
315 typedef std::set<const char *, CYCStringLess> CYCStringSet;
316 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
317 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
319 struct CYIdentifierUsage {
320 CYIdentifier *identifier_;
324 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
329 CYIdentifierAddressFlagsMap internal_;
330 CYIdentifierValueSet identifiers_;
340 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
341 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
342 void Merge(CYContext &context, CYIdentifier *identifier);
343 void Scope(CYContext &context, CYStatement *&statements);
349 CYStatement *statements_;
351 CYProgram(CYStatement *statements) :
352 statements_(statements)
356 virtual void Replace(CYContext &context);
357 virtual void Output(CYOutput &out) const;
367 CYIdentifierUsageVector rename_;
369 CYNonLocal *nonlocal_;
372 CYContext(apr_pool_t *pool, CYOptions &options) :
381 virtual ~CYContext() {
384 template <typename Type_>
385 void Replace(Type_ *&value) {
386 for (;;) if (value == NULL)
389 Type_ *replace(value->Replace(*this));
390 if (replace != value)
396 void NonLocal(CYStatement *&statements);
397 CYIdentifier *Unique();
401 CYIdentifier *identifier_;
408 CYIdentifier *Target(CYContext &context) {
409 if (identifier_ == NULL)
410 identifier_ = context.Unique();
419 CYStatement *statements_;
421 CYBlock(CYStatement *statements) :
422 statements_(statements)
426 operator CYStatement *() const {
430 void AddPrev(CYStatement *statement) {
431 CYStatement *last(statement);
432 while (last->next_ != NULL)
434 last->SetNext(statements_);
435 statements_ = statement;
438 virtual CYStatement *Replace(CYContext &context);
440 virtual void Output(CYOutput &out) const;
441 virtual void Output(CYOutput &out, CYFlags flags) const;
469 std::string filename_;
473 cy::location location_;
474 std::string message_;
477 typedef std::vector<Error> Errors;
485 CYExpression *context_;
487 Context(CYExpression *context) :
492 typedef std::vector<CYWord *> Words;
496 typedef std::vector<Context> Contexts;
499 CYExpression *context_;
511 void ScannerDestroy();
514 CYDriver(apr_pool_t *pool = NULL, const std::string &filename = "");
517 Condition GetCondition();
518 void SetCondition(Condition condition);
520 void PushCondition(Condition condition);
523 void Warning(const cy::location &location, const char *message);
526 struct CYForInitialiser {
527 virtual ~CYForInitialiser() {
530 virtual void For(CYOutput &out) const = 0;
531 virtual CYExpression *Replace(CYContext &context) = 0;
534 struct CYForInInitialiser {
535 virtual ~CYForInInitialiser() {
538 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
539 virtual const char *ForEachIn() const = 0;
540 virtual CYExpression *ForEachIn(CYContext &out) = 0;
541 virtual CYExpression *Replace(CYContext &context) = 0;
547 struct CYExpression :
548 CYNext<CYExpression>,
554 virtual unsigned Precedence() const = 0;
556 virtual bool RightHand() const {
560 virtual void For(CYOutput &out) const;
561 virtual void ForIn(CYOutput &out, CYFlags flags) const;
563 virtual const char *ForEachIn() const;
564 virtual CYExpression *ForEachIn(CYContext &out);
566 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
568 virtual void Output(CYOutput &out) const;
569 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
570 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
572 virtual CYExpression *ClassName(CYContext &context, bool object);
573 virtual void ClassName(CYOutput &out, bool object) const;
575 CYExpression *ReplaceAll(CYContext &context);
577 virtual CYExpression *Replace(CYContext &context) = 0;
579 virtual CYExpression *Primitive(CYContext &context) {
583 virtual CYNumber *Number(CYContext &context) {
587 virtual CYString *String(CYContext &context) {
591 virtual const char *Word() const {
596 #define CYAlphabetic(value) \
597 virtual bool Alphabetic() const { \
601 #define CYPrecedence(value) \
602 virtual unsigned Precedence() const { \
606 #define CYRightHand(value) \
607 virtual bool RightHand() const { \
614 CYExpression *expressions_;
616 CYCompound(CYExpression *expressions = NULL) :
617 expressions_(expressions)
621 void AddPrev(CYExpression *expression) {
622 CYExpression *last(expression);
623 while (last->next_ != NULL)
625 last->SetNext(expressions_);
626 expressions_ = expression;
631 virtual CYExpression *Replace(CYContext &context);
632 void Output(CYOutput &out, CYFlags flags) const;
635 struct CYFunctionParameter :
636 CYNext<CYFunctionParameter>,
641 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
642 CYNext<CYFunctionParameter>(next),
647 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
648 virtual void Output(CYOutput &out) const;
651 struct CYOptionalFunctionParameter :
654 CYExpression *initializer_;
656 CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
657 CYFunctionParameter(name, next),
658 initializer_(initializer)
662 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
663 virtual void Output(CYOutput &out) const;
666 struct CYComprehension :
667 CYNext<CYComprehension>,
670 virtual const char *Name() const = 0;
672 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
673 CYFunctionParameter *Parameters(CYContext &context) const;
674 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
675 virtual void Output(CYOutput &out) const = 0;
678 struct CYForInComprehension :
684 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
690 virtual const char *Name() const {
691 return name_->Word();
694 virtual CYFunctionParameter *Parameter(CYContext &context) const;
695 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
696 virtual void Output(CYOutput &out) const;
699 struct CYForEachInComprehension :
705 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
711 virtual const char *Name() const {
712 return name_->Word();
715 virtual CYFunctionParameter *Parameter(CYContext &context) const;
716 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
717 virtual void Output(CYOutput &out) const;
720 struct CYIfComprehension :
725 CYIfComprehension(CYExpression *test) :
730 virtual const char *Name() const {
734 virtual CYFunctionParameter *Parameter(CYContext &context) const;
735 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
736 virtual void Output(CYOutput &out) const;
739 struct CYArrayComprehension :
742 CYExpression *expression_;
743 CYComprehension *comprehensions_;
745 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
746 expression_(expression),
747 comprehensions_(comprehensions)
753 virtual CYExpression *Replace(CYContext &context);
754 virtual void Output(CYOutput &out, CYFlags flags) const;
767 virtual CYExpression *Replace(CYContext &context);
781 CYRange(uint64_t lo, uint64_t hi) :
786 bool operator [](uint8_t value) const {
787 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
790 void operator()(uint8_t value) {
793 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
797 extern CYRange DigitRange_;
798 extern CYRange WordStartRange_;
799 extern CYRange WordEndRange_;
814 CYString(const char *value) :
820 CYString(const char *value, size_t size) :
826 CYString(const CYWord *word) :
827 value_(word->Word()),
828 size_(strlen(value_))
832 const char *Value() const {
836 virtual const char *Word() const;
838 virtual CYNumber *Number(CYContext &context);
839 virtual CYString *String(CYContext &context);
841 CYString *Concat(CYContext &out, CYString *rhs) const;
842 virtual void Output(CYOutput &out, CYFlags flags) const;
843 virtual void PropertyName(CYOutput &out) const;
852 CYNumber(double value) :
857 double Value() const {
861 virtual CYNumber *Number(CYContext &context);
862 virtual CYString *String(CYContext &context);
864 virtual void Output(CYOutput &out, CYFlags flags) const;
865 virtual void PropertyName(CYOutput &out) const;
873 CYRegEx(const char *value) :
878 const char *Value() const {
882 virtual void Output(CYOutput &out, CYFlags flags) const;
894 virtual CYNumber *Number(CYContext &context);
895 virtual CYString *String(CYContext &context);
897 virtual void Output(CYOutput &out, CYFlags flags) const;
909 virtual CYExpression *Replace(CYContext &context);
910 virtual void Output(CYOutput &out, CYFlags flags) const;
916 virtual bool Value() const = 0;
917 virtual void Output(CYOutput &out, CYFlags flags) const;
929 virtual bool Value() const {
933 virtual CYNumber *Number(CYContext &context);
934 virtual CYString *String(CYContext &context);
946 virtual bool Value() const {
950 virtual CYNumber *Number(CYContext &context);
951 virtual CYString *String(CYContext &context);
959 CYVariable(CYIdentifier *name) :
967 virtual CYExpression *Replace(CYContext &context);
968 virtual void Output(CYOutput &out, CYFlags flags) const;
976 CYPrefix(CYExpression *rhs) :
981 virtual bool Alphabetic() const = 0;
982 virtual const char *Operator() const = 0;
986 virtual CYExpression *Replace(CYContext &context);
987 virtual void Output(CYOutput &out, CYFlags flags) const;
996 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1002 void SetLeft(CYExpression *lhs) {
1006 virtual bool Alphabetic() const = 0;
1007 virtual const char *Operator() const = 0;
1009 virtual CYExpression *Replace(CYContext &context);
1010 virtual void Output(CYOutput &out, CYFlags flags) const;
1018 CYPostfix(CYExpression *lhs) :
1023 virtual const char *Operator() const = 0;
1027 virtual CYExpression *Replace(CYContext &context);
1028 virtual void Output(CYOutput &out, CYFlags flags) const;
1031 struct CYAssignment :
1037 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1043 void SetLeft(CYExpression *lhs) {
1047 virtual const char *Operator() const = 0;
1051 virtual CYExpression *Replace(CYContext &context);
1052 virtual void Output(CYOutput &out, CYFlags flags) const;
1060 CYExpression *value_;
1062 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1063 CYNext<CYArgument>(next),
1069 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1070 CYNext<CYArgument>(next),
1076 void Replace(CYContext &context);
1077 void Output(CYOutput &out) const;
1093 CYExpression *case_;
1094 CYStatement *statements_;
1096 CYClause(CYExpression *_case, CYStatement *statements) :
1098 statements_(statements)
1102 void Replace(CYContext &context);
1103 virtual void Output(CYOutput &out) const;
1110 CYExpression *value_;
1112 CYElement(CYExpression *value, CYElement *next) :
1113 CYNext<CYElement>(next),
1118 void Replace(CYContext &context);
1119 void Output(CYOutput &out) const;
1125 CYElement *elements_;
1127 CYArray(CYElement *elements = NULL) :
1132 virtual CYExpression *Replace(CYContext &context);
1133 virtual void Output(CYOutput &out, CYFlags flags) const;
1140 CYPropertyName *name_;
1141 CYExpression *value_;
1143 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1144 CYNext<CYProperty>(next),
1150 void Replace(CYContext &context);
1151 virtual void Output(CYOutput &out) const;
1154 struct CYDeclaration :
1157 CYIdentifier *identifier_;
1158 CYExpression *initialiser_;
1160 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1161 identifier_(identifier),
1162 initialiser_(initialiser)
1166 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1168 virtual const char *ForEachIn() const;
1169 virtual CYExpression *ForEachIn(CYContext &out);
1171 virtual CYExpression *Replace(CYContext &context);
1172 virtual CYAssignment *Assignment(CYContext &context);
1174 virtual void Output(CYOutput &out, CYFlags flags) const;
1177 struct CYDeclarations :
1178 CYNext<CYDeclarations>,
1182 CYDeclaration *declaration_;
1184 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1185 CYNext<CYDeclarations>(next),
1186 declaration_(declaration)
1190 virtual void For(CYOutput &out) const;
1192 virtual CYCompound *Replace(CYContext &context);
1193 CYProperty *Property(CYContext &context);
1195 virtual void Output(CYOutput &out) const;
1196 virtual void Output(CYOutput &out, CYFlags flags) const;
1202 CYDeclarations *declarations_;
1204 CYVar(CYDeclarations *declarations) :
1205 declarations_(declarations)
1209 virtual CYStatement *Replace(CYContext &context);
1210 virtual void Output(CYOutput &out, CYFlags flags) const;
1216 CYDeclarations *declarations_;
1219 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1220 declarations_(declarations),
1225 virtual CYStatement *Replace(CYContext &context);
1226 virtual void Output(CYOutput &out, CYFlags flags) const;
1232 CYForInitialiser *initialiser_;
1233 CYExpression *test_;
1234 CYExpression *increment_;
1237 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1238 initialiser_(initialiser),
1240 increment_(increment),
1245 virtual CYStatement *Replace(CYContext &context);
1246 virtual void Output(CYOutput &out, CYFlags flags) const;
1252 CYForInInitialiser *initialiser_;
1256 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1257 initialiser_(initialiser),
1263 virtual CYStatement *Replace(CYContext &context);
1264 virtual void Output(CYOutput &out, CYFlags flags) const;
1267 struct CYForEachIn :
1270 CYForInInitialiser *initialiser_;
1274 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1275 initialiser_(initialiser),
1281 virtual CYStatement *Replace(CYContext &context);
1282 virtual void Output(CYOutput &out, CYFlags flags) const;
1288 CYProperty *properties_;
1290 CYObject(CYProperty *properties = NULL) :
1291 properties_(properties)
1295 virtual CYExpression *Replace(CYContext &context);
1296 void Output(CYOutput &out, CYFlags flags) const;
1302 CYExpression *object_;
1303 CYExpression *property_;
1305 CYMember(CYExpression *object, CYExpression *property) :
1311 void SetLeft(CYExpression *object) {
1315 void Replace_(CYContext &context);
1318 struct CYDirectMember :
1321 CYDirectMember(CYExpression *object, CYExpression *property) :
1322 CYMember(object, property)
1329 virtual CYExpression *Replace(CYContext &context);
1330 virtual void Output(CYOutput &out, CYFlags flags) const;
1333 struct CYIndirectMember :
1336 CYIndirectMember(CYExpression *object, CYExpression *property) :
1337 CYMember(object, property)
1344 virtual CYExpression *Replace(CYContext &context);
1345 virtual void Output(CYOutput &out, CYFlags flags) const;
1351 CYExpression *constructor_;
1352 CYArgument *arguments_;
1354 CYNew(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);
1375 CYExpression *function_;
1376 CYArgument *arguments_;
1378 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1379 function_(function),
1380 arguments_(arguments)
1387 virtual CYExpression *Replace(CYContext &context);
1388 virtual void Output(CYOutput &out, CYFlags flags) const;
1390 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1395 struct CYRubyBlock :
1398 CYExpression *call_;
1401 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1410 virtual CYExpression *Replace(CYContext &context);
1411 virtual void Output(CYOutput &out, CYFlags flags) const;
1417 CYExpression *test_;
1419 CYStatement *false_;
1421 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1428 virtual CYStatement *Replace(CYContext &context);
1429 virtual void Output(CYOutput &out, CYFlags flags) const;
1435 CYExpression *test_;
1438 CYDoWhile(CYExpression *test, CYStatement *code) :
1444 virtual CYStatement *Replace(CYContext &context);
1445 virtual void Output(CYOutput &out, CYFlags flags) const;
1451 CYExpression *test_;
1454 CYWhile(CYExpression *test, CYStatement *code) :
1460 virtual CYStatement *Replace(CYContext &context);
1461 virtual void Output(CYOutput &out, CYFlags flags) const;
1464 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1466 CYIdentifier *name_;
1467 CYFunctionParameter *parameters_;
1469 CYNonLocal *nonlocal_;
1471 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1473 parameters_(parameters),
1479 virtual ~CYFunction() {
1482 void Inject(CYContext &context);
1483 virtual void Replace_(CYContext &context, bool outer);
1484 virtual void Output(CYOutput &out, CYFlags flags) const;
1487 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1488 struct CYFunctionExpression :
1492 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1493 CYFunction(name, parameters, statements)
1500 virtual CYExpression *Replace(CYContext &context);
1501 virtual void Output(CYOutput &out, CYFlags flags) const;
1504 // XXX: this should derive from CYAnonymousFunctionExpression
1506 CYFunctionExpression
1508 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1509 CYFunctionExpression(NULL, parameters, statements)
1513 virtual CYExpression *Replace(CYContext &context);
1514 virtual void Output(CYOutput &out, CYFlags flags) const;
1517 // XXX: this should derive from CYNamedFunction
1518 struct CYFunctionStatement :
1522 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1523 CYFunction(name, parameters, statements)
1527 virtual CYStatement *Replace(CYContext &context);
1528 virtual void Output(CYOutput &out, CYFlags flags) const;
1534 CYExpression *expression_;
1536 CYExpress(CYExpression *expression) :
1537 expression_(expression)
1539 if (expression == NULL)
1543 virtual CYStatement *Collapse(CYContext &context);
1544 virtual CYStatement *Replace(CYContext &context);
1545 virtual void Output(CYOutput &out, CYFlags flags) const;
1551 CYIdentifier *label_;
1553 CYContinue(CYIdentifier *label) :
1558 virtual CYStatement *Replace(CYContext &context);
1559 virtual void Output(CYOutput &out, CYFlags flags) const;
1565 CYIdentifier *label_;
1567 CYBreak(CYIdentifier *label) :
1572 virtual CYStatement *Replace(CYContext &context);
1573 virtual void Output(CYOutput &out, CYFlags flags) const;
1579 CYExpression *value_;
1581 CYReturn(CYExpression *value) :
1586 virtual CYStatement *Replace(CYContext &context);
1587 virtual void Output(CYOutput &out, CYFlags flags) const;
1593 virtual CYStatement *Collapse(CYContext &context);
1594 virtual CYStatement *Replace(CYContext &context);
1595 virtual void Output(CYOutput &out, CYFlags flags) const;
1603 CYFinally(CYStatement *statements) :
1608 void Replace(CYContext &context);
1609 virtual void Output(CYOutput &out) const;
1618 CYIdentifier *name_;
1621 Catch(CYIdentifier *name, CYStatement *statements) :
1627 void Replace(CYContext &context);
1628 virtual void Output(CYOutput &out) const;
1636 CYFinally *finally_;
1638 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1645 virtual CYStatement *Replace(CYContext &context);
1646 virtual void Output(CYOutput &out, CYFlags flags) const;
1652 CYExpression *value_;
1654 Throw(CYExpression *value = NULL) :
1659 virtual CYStatement *Replace(CYContext &context);
1660 virtual void Output(CYOutput &out, CYFlags flags) const;
1668 CYExpression *scope_;
1671 CYWith(CYExpression *scope, CYStatement *code) :
1677 virtual CYStatement *Replace(CYContext &context);
1678 virtual void Output(CYOutput &out, CYFlags flags) const;
1684 CYExpression *value_;
1687 CYSwitch(CYExpression *value, CYClause *clauses) :
1693 virtual CYStatement *Replace(CYContext &context);
1694 virtual void Output(CYOutput &out, CYFlags flags) const;
1697 struct CYCondition :
1700 CYExpression *test_;
1701 CYExpression *true_;
1702 CYExpression *false_;
1704 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1713 virtual CYExpression *Replace(CYContext &context);
1714 virtual void Output(CYOutput &out, CYFlags flags) const;
1717 struct CYAddressOf :
1720 CYAddressOf(CYExpression *rhs) :
1725 virtual const char *Operator() const {
1731 virtual CYExpression *Replace(CYContext &context);
1737 CYIndirect(CYExpression *rhs) :
1742 virtual const char *Operator() const {
1748 virtual CYExpression *Replace(CYContext &context);
1752 virtual CYExpression *Replace(CYContext &context);
1754 #define CYPostfix_(op, name, args...) \
1755 struct CY ## name : \
1758 CY ## name(CYExpression *lhs) : \
1763 virtual const char *Operator() const { \
1768 #define CYPrefix_(alphabetic, op, name, args...) \
1769 struct CY ## name : \
1772 CY ## name(CYExpression *rhs) : \
1777 CYAlphabetic(alphabetic) \
1779 virtual const char *Operator() const { \
1784 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1785 struct CY ## name : \
1788 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1793 CYAlphabetic(alphabetic) \
1794 CYPrecedence(precedence) \
1796 virtual const char *Operator() const { \
1801 #define CYAssignment_(op, name, args...) \
1802 struct CY ## name ## Assign : \
1805 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1806 CYAssignment(lhs, rhs) \
1810 virtual const char *Operator() const { \
1815 CYPostfix_("++", PostIncrement)
1816 CYPostfix_("--", PostDecrement)
1818 CYPrefix_(true, "delete", Delete)
1819 CYPrefix_(true, "void", Void)
1820 CYPrefix_(true, "typeof", TypeOf)
1821 CYPrefix_(false, "++", PreIncrement)
1822 CYPrefix_(false, "--", PreDecrement)
1823 CYPrefix_(false, "+", Affirm)
1824 CYPrefix_(false, "-", Negate)
1825 CYPrefix_(false, "~", BitwiseNot)
1826 CYPrefix_(false, "!", LogicalNot)
1828 CYInfix_(false, 5, "*", Multiply)
1829 CYInfix_(false, 5, "/", Divide)
1830 CYInfix_(false, 5, "%", Modulus)
1831 CYInfix_(false, 6, "+", Add, CYReplace)
1832 CYInfix_(false, 6, "-", Subtract)
1833 CYInfix_(false, 7, "<<", ShiftLeft)
1834 CYInfix_(false, 7, ">>", ShiftRightSigned)
1835 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1836 CYInfix_(false, 8, "<", Less)
1837 CYInfix_(false, 8, ">", Greater)
1838 CYInfix_(false, 8, "<=", LessOrEqual)
1839 CYInfix_(false, 8, ">=", GreaterOrEqual)
1840 CYInfix_(true, 8, "instanceof", InstanceOf)
1841 CYInfix_(true, 8, "in", In)
1842 CYInfix_(false, 9, "==", Equal)
1843 CYInfix_(false, 9, "!=", NotEqual)
1844 CYInfix_(false, 9, "===", Identical)
1845 CYInfix_(false, 9, "!==", NotIdentical)
1846 CYInfix_(false, 10, "&", BitwiseAnd)
1847 CYInfix_(false, 11, "^", BitwiseXOr)
1848 CYInfix_(false, 12, "|", BitwiseOr)
1849 CYInfix_(false, 13, "&&", LogicalAnd)
1850 CYInfix_(false, 14, "||", LogicalOr)
1852 CYAssignment_("=", )
1853 CYAssignment_("*=", Multiply)
1854 CYAssignment_("/=", Divide)
1855 CYAssignment_("%=", Modulus)
1856 CYAssignment_("+=", Add)
1857 CYAssignment_("-=", Subtract)
1858 CYAssignment_("<<=", ShiftLeft)
1859 CYAssignment_(">>=", ShiftRightSigned)
1860 CYAssignment_(">>>=", ShiftRightUnsigned)
1861 CYAssignment_("&=", BitwiseAnd)
1862 CYAssignment_("^=", BitwiseXOr)
1863 CYAssignment_("|=", BitwiseOr)
1865 #endif/*CYPARSER_HPP*/