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/>.
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
35 #include "location.hh"
38 #include "Pooling.hpp"
39 #include "Options.hpp"
47 virtual void Output(struct CYOutput &out) const = 0;
65 CYOutput(std::ostream &out, CYOptions &options) :
75 void Check(char value);
78 CYOutput &operator <<(char rhs);
79 CYOutput &operator <<(const char *rhs);
81 _finline CYOutput &operator <<(const CYThing *rhs) {
87 _finline CYOutput &operator <<(const CYThing &rhs) {
93 struct CYPropertyName {
94 virtual void PropertyName(CYOutput &out) const = 0;
96 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 *Collapse(CYContext &context);
158 virtual CYStatement *Replace(CYContext &context) = 0;
161 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
164 struct CYStatements {
174 operator CYStatement *() const {
178 CYStatements &operator ->*(CYStatement *next) {
180 if (first_ == NULL) {
183 } else for (;; last_ = last_->next_)
184 if (last_->next_ == NULL) {
194 virtual ~CYClassName() {
197 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
198 virtual void ClassName(CYOutput &out, bool object) const = 0;
208 CYWord(const char *word) :
213 void Set(const char *value) {
217 virtual const char *Word() const;
218 virtual void Output(CYOutput &out) const;
220 virtual CYExpression *ClassName(CYContext &context, bool object);
221 virtual void ClassName(CYOutput &out, bool object) const;
222 virtual void PropertyName(CYOutput &out) const;
225 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
227 return lhs << rhs.Word();
230 struct CYIdentifier :
231 CYNext<CYIdentifier>,
234 CYIdentifier *replace_;
238 CYIdentifier(const char *word) :
246 virtual const char *Word() const;
247 CYIdentifier *Replace(CYContext &context);
255 CYComment(const char *value) :
260 virtual CYStatement *Replace(CYContext &context);
261 virtual void Output(CYOutput &out, CYFlags flags) const;
268 CYStatement *statement_;
270 CYLabel(CYIdentifier *name, CYStatement *statement) :
272 statement_(statement)
276 virtual CYStatement *Replace(CYContext &context);
277 virtual void Output(CYOutput &out, CYFlags flags) const;
280 struct CYCStringLess :
281 std::binary_function<const char *, const char *, bool>
283 _finline bool operator ()(const char *lhs, const char *rhs) const {
284 return strcmp(lhs, rhs) < 0;
288 struct CYIdentifierValueLess :
289 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
291 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
292 return CYCStringLess()(lhs->Word(), rhs->Word());
296 enum CYIdentifierFlags {
297 CYIdentifierArgument,
298 CYIdentifierVariable,
304 typedef std::set<const char *, CYCStringLess> CYCStringSet;
305 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
306 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
308 struct CYIdentifierUsage {
309 CYIdentifier *identifier_;
313 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
315 // XXX: strategy pattern, maybe subclass
326 CYStatement *&statements_;
330 CYIdentifierAddressFlagsMap internal_;
331 CYIdentifierValueSet identifiers_;
333 CYScope(CYScopeType type, CYContext &context, CYStatement *&statements);
338 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
339 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
340 void Merge(CYContext &context, CYIdentifier *identifier);
341 void Scope(CYContext &context, CYStatement *&statements);
347 CYStatement *statements_;
349 CYProgram(CYStatement *statements) :
350 statements_(statements)
354 virtual void Replace(CYContext &context);
355 virtual void Output(CYOutput &out) const;
364 CYIdentifierUsageVector rename_;
366 CYNonLocal *nonlocal_;
367 CYNonLocal *nextlocal_;
370 CYContext(CYOptions &options) :
379 virtual ~CYContext() {
382 template <typename Type_>
383 void ReplaceAll(Type_ *&values) {
384 Type_ **last(&values);
385 CYForEach (next, values) {
386 Replace(*last = next);
388 last = &(*last)->next_;
392 template <typename Type_>
393 void Replace(Type_ *&value) {
394 for (;;) if (value == NULL)
397 Type_ *replace(value->Replace(*this));
398 if (replace != value)
404 void NonLocal(CYStatement *&statements);
405 CYIdentifier *Unique();
409 CYIdentifier *identifier_;
416 CYIdentifier *Target(CYContext &context) {
417 if (identifier_ == NULL)
418 identifier_ = context.Unique();
427 CYStatement *statements_;
429 CYBlock(CYStatement *statements) :
430 statements_(statements)
434 operator CYStatement *() const {
438 void AddPrev(CYStatement *statement) {
439 CYSetLast(statement, statements_);
440 statements_ = statement;
443 virtual CYStatement *Replace(CYContext &context);
445 virtual void Output(CYOutput &out) const;
446 virtual void Output(CYOutput &out, CYFlags flags) const;
472 std::string filename_;
476 cy::location location_;
477 std::string message_;
480 typedef std::vector<Error> Errors;
488 CYExpression *context_;
490 Context(CYExpression *context) :
495 typedef std::vector<CYWord *> Words;
499 typedef std::vector<Context> Contexts;
502 CYExpression *context_;
514 void ScannerDestroy();
517 CYDriver(const std::string &filename = "");
520 Condition GetCondition();
521 void SetCondition(Condition condition);
523 void PushCondition(Condition condition);
526 void Warning(const cy::location &location, const char *message);
529 struct CYForInitialiser {
530 virtual ~CYForInitialiser() {
533 virtual void For(CYOutput &out) const = 0;
534 virtual CYExpression *Replace(CYContext &context) = 0;
537 struct CYForInInitialiser {
538 virtual ~CYForInInitialiser() {
541 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
542 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
544 virtual CYExpression *Replace(CYContext &context) = 0;
550 struct CYExpression :
551 CYNext<CYExpression>,
557 virtual unsigned Precedence() const = 0;
559 virtual bool RightHand() const {
563 virtual void For(CYOutput &out) const;
564 virtual void ForIn(CYOutput &out, CYFlags flags) const;
565 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
567 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
569 virtual void Output(CYOutput &out) const;
570 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
571 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
573 virtual CYExpression *ClassName(CYContext &context, bool object);
574 virtual void ClassName(CYOutput &out, bool object) const;
576 virtual CYExpression *Replace(CYContext &context) = 0;
578 virtual CYExpression *Primitive(CYContext &context) {
582 virtual CYNumber *Number(CYContext &context) {
586 virtual CYString *String(CYContext &context) {
590 virtual const char *Word() const {
595 #define CYAlphabetic(value) \
596 virtual bool Alphabetic() const { \
600 #define CYPrecedence(value) \
601 static const unsigned Precedence_ = value; \
602 virtual unsigned Precedence() const { \
603 return Precedence_; \
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 CYSetLast(expression, expressions_);
623 expressions_ = expression;
628 virtual CYExpression *Replace(CYContext &context);
629 void Output(CYOutput &out, CYFlags flags) const;
632 struct CYFunctionParameter :
633 CYNext<CYFunctionParameter>,
638 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
639 CYNext<CYFunctionParameter>(next),
644 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
645 virtual void Output(CYOutput &out) const;
648 struct CYOptionalFunctionParameter :
651 CYExpression *initializer_;
653 CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
654 CYFunctionParameter(name, next),
655 initializer_(initializer)
659 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
660 virtual void Output(CYOutput &out) const;
663 struct CYComprehension :
664 CYNext<CYComprehension>,
667 virtual const char *Name() const = 0;
669 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
670 CYFunctionParameter *Parameters(CYContext &context) const;
671 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
672 virtual void Output(CYOutput &out) const = 0;
675 struct CYForInComprehension :
681 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
687 virtual const char *Name() const {
688 return name_->Word();
691 virtual CYFunctionParameter *Parameter(CYContext &context) const;
692 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
693 virtual void Output(CYOutput &out) const;
696 struct CYForEachInComprehension :
702 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
708 virtual const char *Name() const {
709 return name_->Word();
712 virtual CYFunctionParameter *Parameter(CYContext &context) const;
713 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
714 virtual void Output(CYOutput &out) const;
717 struct CYIfComprehension :
722 CYIfComprehension(CYExpression *test) :
727 virtual const char *Name() const {
731 virtual CYFunctionParameter *Parameter(CYContext &context) const;
732 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
733 virtual void Output(CYOutput &out) const;
736 struct CYArrayComprehension :
739 CYExpression *expression_;
740 CYComprehension *comprehensions_;
742 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
743 expression_(expression),
744 comprehensions_(comprehensions)
750 virtual CYExpression *Replace(CYContext &context);
751 virtual void Output(CYOutput &out, CYFlags flags) const;
764 virtual CYExpression *Replace(CYContext &context);
778 CYRange(uint64_t lo, uint64_t hi) :
783 bool operator [](uint8_t value) const {
784 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
787 void operator()(uint8_t value) {
790 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
794 extern CYRange DigitRange_;
795 extern CYRange WordStartRange_;
796 extern CYRange WordEndRange_;
811 CYString(const char *value) :
817 CYString(const char *value, size_t size) :
823 CYString(const CYWord *word) :
824 value_(word->Word()),
825 size_(strlen(value_))
829 const char *Value() const {
833 virtual const char *Word() const;
835 virtual CYNumber *Number(CYContext &context);
836 virtual CYString *String(CYContext &context);
838 CYString *Concat(CYContext &out, CYString *rhs) const;
839 virtual void Output(CYOutput &out, CYFlags flags) const;
840 virtual void PropertyName(CYOutput &out) const;
849 CYNumber(double value) :
854 double Value() const {
858 virtual CYNumber *Number(CYContext &context);
859 virtual CYString *String(CYContext &context);
861 virtual void Output(CYOutput &out, CYFlags flags) const;
862 virtual void PropertyName(CYOutput &out) const;
870 CYRegEx(const char *value) :
875 const char *Value() const {
879 virtual void Output(CYOutput &out, CYFlags flags) const;
891 virtual CYNumber *Number(CYContext &context);
892 virtual CYString *String(CYContext &context);
894 virtual void Output(CYOutput &out, CYFlags flags) const;
906 virtual CYExpression *Replace(CYContext &context);
907 virtual void Output(CYOutput &out, CYFlags flags) const;
913 virtual bool Value() const = 0;
914 virtual void Output(CYOutput &out, CYFlags flags) const;
926 virtual bool Value() const {
930 virtual CYNumber *Number(CYContext &context);
931 virtual CYString *String(CYContext &context);
943 virtual bool Value() const {
947 virtual CYNumber *Number(CYContext &context);
948 virtual CYString *String(CYContext &context);
956 CYVariable(CYIdentifier *name) :
961 CYVariable(const char *name) :
962 name_(new($pool) CYIdentifier(name))
969 virtual CYExpression *Replace(CYContext &context);
970 virtual void Output(CYOutput &out, CYFlags flags) const;
978 CYPrefix(CYExpression *rhs) :
983 virtual bool Alphabetic() const = 0;
984 virtual const char *Operator() const = 0;
988 virtual CYExpression *Replace(CYContext &context);
989 virtual void Output(CYOutput &out, CYFlags flags) const;
998 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1004 void SetLeft(CYExpression *lhs) {
1008 virtual bool Alphabetic() const = 0;
1009 virtual const char *Operator() const = 0;
1011 virtual CYExpression *Replace(CYContext &context);
1012 virtual void Output(CYOutput &out, CYFlags flags) const;
1020 CYPostfix(CYExpression *lhs) :
1025 virtual const char *Operator() const = 0;
1029 virtual CYExpression *Replace(CYContext &context);
1030 virtual void Output(CYOutput &out, CYFlags flags) const;
1033 struct CYAssignment :
1039 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1045 void SetLeft(CYExpression *lhs) {
1049 virtual const char *Operator() const = 0;
1053 virtual CYExpression *Replace(CYContext &context);
1054 virtual void Output(CYOutput &out, CYFlags flags) const;
1062 CYExpression *value_;
1064 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1065 CYNext<CYArgument>(next),
1071 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1072 CYNext<CYArgument>(next),
1078 void Replace(CYContext &context);
1079 void Output(CYOutput &out) const;
1095 CYExpression *case_;
1096 CYStatement *statements_;
1098 CYClause(CYExpression *_case, CYStatement *statements) :
1100 statements_(statements)
1104 void Replace(CYContext &context);
1105 virtual void Output(CYOutput &out) const;
1112 CYExpression *value_;
1114 CYElement(CYExpression *value, CYElement *next) :
1115 CYNext<CYElement>(next),
1120 void Replace(CYContext &context);
1121 void Output(CYOutput &out) const;
1127 CYElement *elements_;
1129 CYArray(CYElement *elements = NULL) :
1134 virtual CYExpression *Replace(CYContext &context);
1135 virtual void Output(CYOutput &out, CYFlags flags) const;
1142 CYPropertyName *name_;
1143 CYExpression *value_;
1145 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1146 CYNext<CYProperty>(next),
1152 void Replace(CYContext &context);
1153 virtual void Output(CYOutput &out) const;
1156 struct CYDeclaration :
1159 CYIdentifier *identifier_;
1160 CYExpression *initialiser_;
1162 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1163 identifier_(identifier),
1164 initialiser_(initialiser)
1168 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1169 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
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;
1354 CYExpression *constructor_;
1355 CYArgument *arguments_;
1357 New(CYExpression *constructor, CYArgument *arguments) :
1358 constructor_(constructor),
1359 arguments_(arguments)
1363 virtual unsigned Precedence() const {
1364 return arguments_ == NULL ? 2 : 1;
1369 virtual CYExpression *Replace(CYContext &context);
1370 virtual void Output(CYOutput &out, CYFlags flags) const;
1372 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1380 CYExpression *function_;
1381 CYArgument *arguments_;
1383 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1384 function_(function),
1385 arguments_(arguments)
1392 virtual CYExpression *Replace(CYContext &context);
1393 virtual void Output(CYOutput &out, CYFlags flags) const;
1395 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1400 struct CYRubyBlock :
1403 CYExpression *call_;
1406 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1415 virtual CYExpression *Replace(CYContext &context);
1416 virtual void Output(CYOutput &out, CYFlags flags) const;
1422 CYExpression *test_;
1424 CYStatement *false_;
1426 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1433 virtual CYStatement *Replace(CYContext &context);
1434 virtual void Output(CYOutput &out, CYFlags flags) const;
1440 CYExpression *test_;
1443 CYDoWhile(CYExpression *test, CYStatement *code) :
1449 virtual CYStatement *Replace(CYContext &context);
1450 virtual void Output(CYOutput &out, CYFlags flags) const;
1456 CYExpression *test_;
1459 CYWhile(CYExpression *test, CYStatement *code) :
1465 virtual CYStatement *Replace(CYContext &context);
1466 virtual void Output(CYOutput &out, CYFlags flags) const;
1469 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1471 CYIdentifier *name_;
1472 CYFunctionParameter *parameters_;
1474 CYNonLocal *nonlocal_;
1476 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1478 parameters_(parameters),
1484 virtual ~CYFunction() {
1487 void Inject(CYContext &context);
1488 virtual void Replace_(CYContext &context, bool outer);
1489 virtual void Output(CYOutput &out, CYFlags flags) const;
1492 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1493 struct CYFunctionExpression :
1497 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1498 CYFunction(name, parameters, statements)
1505 virtual CYExpression *Replace(CYContext &context);
1506 virtual void Output(CYOutput &out, CYFlags flags) const;
1509 // XXX: this should derive from CYAnonymousFunctionExpression
1511 CYFunctionExpression
1513 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1514 CYFunctionExpression(NULL, parameters, statements)
1518 virtual CYExpression *Replace(CYContext &context);
1519 virtual void Output(CYOutput &out, CYFlags flags) const;
1522 // XXX: this should derive from CYNamedFunction
1523 struct CYFunctionStatement :
1527 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1528 CYFunction(name, parameters, statements)
1532 virtual CYStatement *Replace(CYContext &context);
1533 virtual void Output(CYOutput &out, CYFlags flags) const;
1539 CYExpression *expression_;
1541 CYExpress(CYExpression *expression) :
1542 expression_(expression)
1544 if (expression == NULL)
1548 virtual CYStatement *Collapse(CYContext &context);
1549 virtual CYStatement *Replace(CYContext &context);
1550 virtual void Output(CYOutput &out, CYFlags flags) const;
1556 CYIdentifier *label_;
1558 CYContinue(CYIdentifier *label) :
1563 virtual CYStatement *Replace(CYContext &context);
1564 virtual void Output(CYOutput &out, CYFlags flags) const;
1570 CYIdentifier *label_;
1572 CYBreak(CYIdentifier *label) :
1577 virtual CYStatement *Replace(CYContext &context);
1578 virtual void Output(CYOutput &out, CYFlags flags) const;
1584 CYExpression *value_;
1586 CYReturn(CYExpression *value) :
1591 virtual CYStatement *Replace(CYContext &context);
1592 virtual void Output(CYOutput &out, CYFlags flags) const;
1598 virtual CYStatement *Collapse(CYContext &context);
1599 virtual CYStatement *Replace(CYContext &context);
1600 virtual void Output(CYOutput &out, CYFlags flags) const;
1608 CYFinally(CYStatement *statements) :
1613 void Replace(CYContext &context);
1614 virtual void Output(CYOutput &out) const;
1623 CYIdentifier *name_;
1626 Catch(CYIdentifier *name, CYStatement *statements) :
1632 void Replace(CYContext &context);
1633 virtual void Output(CYOutput &out) const;
1641 CYFinally *finally_;
1643 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1650 virtual CYStatement *Replace(CYContext &context);
1651 virtual void Output(CYOutput &out, CYFlags flags) const;
1657 CYExpression *value_;
1659 Throw(CYExpression *value = NULL) :
1664 virtual CYStatement *Replace(CYContext &context);
1665 virtual void Output(CYOutput &out, CYFlags flags) const;
1673 CYExpression *scope_;
1676 CYWith(CYExpression *scope, CYStatement *code) :
1682 virtual CYStatement *Replace(CYContext &context);
1683 virtual void Output(CYOutput &out, CYFlags flags) const;
1689 CYExpression *value_;
1692 CYSwitch(CYExpression *value, CYClause *clauses) :
1698 virtual CYStatement *Replace(CYContext &context);
1699 virtual void Output(CYOutput &out, CYFlags flags) const;
1702 struct CYCondition :
1705 CYExpression *test_;
1706 CYExpression *true_;
1707 CYExpression *false_;
1709 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1718 virtual CYExpression *Replace(CYContext &context);
1719 virtual void Output(CYOutput &out, CYFlags flags) const;
1722 struct CYAddressOf :
1725 CYAddressOf(CYExpression *rhs) :
1730 virtual const char *Operator() const {
1736 virtual CYExpression *Replace(CYContext &context);
1742 CYIndirect(CYExpression *rhs) :
1747 virtual const char *Operator() const {
1753 virtual CYExpression *Replace(CYContext &context);
1757 virtual CYExpression *Replace(CYContext &context);
1759 #define CYPostfix_(op, name, args...) \
1760 struct CY ## name : \
1763 CY ## name(CYExpression *lhs) : \
1768 virtual const char *Operator() const { \
1773 #define CYPrefix_(alphabetic, op, name, args...) \
1774 struct CY ## name : \
1777 CY ## name(CYExpression *rhs) : \
1782 CYAlphabetic(alphabetic) \
1784 virtual const char *Operator() const { \
1789 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1790 struct CY ## name : \
1793 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1798 CYAlphabetic(alphabetic) \
1799 CYPrecedence(precedence) \
1801 virtual const char *Operator() const { \
1806 #define CYAssignment_(op, name, args...) \
1807 struct CY ## name ## Assign : \
1810 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1811 CYAssignment(lhs, rhs) \
1815 virtual const char *Operator() const { \
1820 CYPostfix_("++", PostIncrement)
1821 CYPostfix_("--", PostDecrement)
1823 CYPrefix_(true, "delete", Delete)
1824 CYPrefix_(true, "void", Void)
1825 CYPrefix_(true, "typeof", TypeOf)
1826 CYPrefix_(false, "++", PreIncrement)
1827 CYPrefix_(false, "--", PreDecrement)
1828 CYPrefix_(false, "+", Affirm)
1829 CYPrefix_(false, "-", Negate)
1830 CYPrefix_(false, "~", BitwiseNot)
1831 CYPrefix_(false, "!", LogicalNot)
1833 CYInfix_(false, 5, "*", Multiply)
1834 CYInfix_(false, 5, "/", Divide)
1835 CYInfix_(false, 5, "%", Modulus)
1836 CYInfix_(false, 6, "+", Add, CYReplace)
1837 CYInfix_(false, 6, "-", Subtract)
1838 CYInfix_(false, 7, "<<", ShiftLeft)
1839 CYInfix_(false, 7, ">>", ShiftRightSigned)
1840 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1841 CYInfix_(false, 8, "<", Less)
1842 CYInfix_(false, 8, ">", Greater)
1843 CYInfix_(false, 8, "<=", LessOrEqual)
1844 CYInfix_(false, 8, ">=", GreaterOrEqual)
1845 CYInfix_(true, 8, "instanceof", InstanceOf)
1846 CYInfix_(true, 8, "in", In)
1847 CYInfix_(false, 9, "==", Equal)
1848 CYInfix_(false, 9, "!=", NotEqual)
1849 CYInfix_(false, 9, "===", Identical)
1850 CYInfix_(false, 9, "!==", NotIdentical)
1851 CYInfix_(false, 10, "&", BitwiseAnd)
1852 CYInfix_(false, 11, "^", BitwiseXOr)
1853 CYInfix_(false, 12, "|", BitwiseOr)
1854 CYInfix_(false, 13, "&&", LogicalAnd)
1855 CYInfix_(false, 14, "||", LogicalOr)
1857 CYAssignment_("=", )
1858 CYAssignment_("*=", Multiply)
1859 CYAssignment_("/=", Divide)
1860 CYAssignment_("%=", Modulus)
1861 CYAssignment_("+=", Add)
1862 CYAssignment_("-=", Subtract)
1863 CYAssignment_("<<=", ShiftLeft)
1864 CYAssignment_(">>=", ShiftRightSigned)
1865 CYAssignment_(">>>=", ShiftRightUnsigned)
1866 CYAssignment_("&=", BitwiseAnd)
1867 CYAssignment_("^=", BitwiseXOr)
1868 CYAssignment_("|=", BitwiseOr)
1870 #endif/*CYCRIPT_PARSER_HPP*/