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);
387 last = &(*last)->next_;
391 template <typename Type_>
392 void Replace(Type_ *&value) {
393 for (;;) if (value == NULL)
396 Type_ *replace(value->Replace(*this));
397 if (replace != value)
403 void NonLocal(CYStatement *&statements);
404 CYIdentifier *Unique();
408 CYIdentifier *identifier_;
415 CYIdentifier *Target(CYContext &context) {
416 if (identifier_ == NULL)
417 identifier_ = context.Unique();
426 CYStatement *statements_;
428 CYBlock(CYStatement *statements) :
429 statements_(statements)
433 operator CYStatement *() const {
437 void AddPrev(CYStatement *statement) {
438 CYSetLast(statement, statements_);
439 statements_ = statement;
442 virtual CYStatement *Replace(CYContext &context);
444 virtual void Output(CYOutput &out) const;
445 virtual void Output(CYOutput &out, CYFlags flags) const;
471 std::string filename_;
475 cy::location location_;
476 std::string message_;
479 typedef std::vector<Error> Errors;
487 CYExpression *context_;
489 Context(CYExpression *context) :
494 typedef std::vector<CYWord *> Words;
498 typedef std::vector<Context> Contexts;
501 CYExpression *context_;
513 void ScannerDestroy();
516 CYDriver(const std::string &filename = "");
519 Condition GetCondition();
520 void SetCondition(Condition condition);
522 void PushCondition(Condition condition);
525 void Warning(const cy::location &location, const char *message);
528 struct CYForInitialiser {
529 virtual ~CYForInitialiser() {
532 virtual void For(CYOutput &out) const = 0;
533 virtual CYExpression *Replace(CYContext &context) = 0;
536 struct CYForInInitialiser {
537 virtual ~CYForInInitialiser() {
540 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
541 virtual const char *ForEachIn() const = 0;
542 virtual CYExpression *ForEachIn(CYContext &out) = 0;
543 virtual CYExpression *Replace(CYContext &context) = 0;
549 struct CYExpression :
550 CYNext<CYExpression>,
556 virtual unsigned Precedence() const = 0;
558 virtual bool RightHand() const {
562 virtual void For(CYOutput &out) const;
563 virtual void ForIn(CYOutput &out, CYFlags flags) const;
565 virtual const char *ForEachIn() const;
566 virtual CYExpression *ForEachIn(CYContext &out);
568 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
570 virtual void Output(CYOutput &out) const;
571 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
572 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
574 virtual CYExpression *ClassName(CYContext &context, bool object);
575 virtual void ClassName(CYOutput &out, bool object) const;
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 static const unsigned Precedence_ = value; \
603 virtual unsigned Precedence() const { \
604 return Precedence_; \
607 #define CYRightHand(value) \
608 virtual bool RightHand() const { \
615 CYExpression *expressions_;
617 CYCompound(CYExpression *expressions = NULL) :
618 expressions_(expressions)
622 void AddPrev(CYExpression *expression) {
623 CYSetLast(expression, expressions_);
624 expressions_ = expression;
629 virtual CYExpression *Replace(CYContext &context);
630 void Output(CYOutput &out, CYFlags flags) const;
633 struct CYFunctionParameter :
634 CYNext<CYFunctionParameter>,
639 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
640 CYNext<CYFunctionParameter>(next),
645 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
646 virtual void Output(CYOutput &out) const;
649 struct CYOptionalFunctionParameter :
652 CYExpression *initializer_;
654 CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
655 CYFunctionParameter(name, next),
656 initializer_(initializer)
660 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
661 virtual void Output(CYOutput &out) const;
664 struct CYComprehension :
665 CYNext<CYComprehension>,
668 virtual const char *Name() const = 0;
670 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
671 CYFunctionParameter *Parameters(CYContext &context) const;
672 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
673 virtual void Output(CYOutput &out) const = 0;
676 struct CYForInComprehension :
682 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
688 virtual const char *Name() const {
689 return name_->Word();
692 virtual CYFunctionParameter *Parameter(CYContext &context) const;
693 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
694 virtual void Output(CYOutput &out) const;
697 struct CYForEachInComprehension :
703 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
709 virtual const char *Name() const {
710 return name_->Word();
713 virtual CYFunctionParameter *Parameter(CYContext &context) const;
714 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
715 virtual void Output(CYOutput &out) const;
718 struct CYIfComprehension :
723 CYIfComprehension(CYExpression *test) :
728 virtual const char *Name() const {
732 virtual CYFunctionParameter *Parameter(CYContext &context) const;
733 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
734 virtual void Output(CYOutput &out) const;
737 struct CYArrayComprehension :
740 CYExpression *expression_;
741 CYComprehension *comprehensions_;
743 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
744 expression_(expression),
745 comprehensions_(comprehensions)
751 virtual CYExpression *Replace(CYContext &context);
752 virtual void Output(CYOutput &out, CYFlags flags) const;
765 virtual CYExpression *Replace(CYContext &context);
779 CYRange(uint64_t lo, uint64_t hi) :
784 bool operator [](uint8_t value) const {
785 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
788 void operator()(uint8_t value) {
791 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
795 extern CYRange DigitRange_;
796 extern CYRange WordStartRange_;
797 extern CYRange WordEndRange_;
812 CYString(const char *value) :
818 CYString(const char *value, size_t size) :
824 CYString(const CYWord *word) :
825 value_(word->Word()),
826 size_(strlen(value_))
830 const char *Value() const {
834 virtual const char *Word() const;
836 virtual CYNumber *Number(CYContext &context);
837 virtual CYString *String(CYContext &context);
839 CYString *Concat(CYContext &out, CYString *rhs) const;
840 virtual void Output(CYOutput &out, CYFlags flags) const;
841 virtual void PropertyName(CYOutput &out) const;
850 CYNumber(double value) :
855 double Value() const {
859 virtual CYNumber *Number(CYContext &context);
860 virtual CYString *String(CYContext &context);
862 virtual void Output(CYOutput &out, CYFlags flags) const;
863 virtual void PropertyName(CYOutput &out) const;
871 CYRegEx(const char *value) :
876 const char *Value() const {
880 virtual void Output(CYOutput &out, CYFlags flags) const;
892 virtual CYNumber *Number(CYContext &context);
893 virtual CYString *String(CYContext &context);
895 virtual void Output(CYOutput &out, CYFlags flags) const;
907 virtual CYExpression *Replace(CYContext &context);
908 virtual void Output(CYOutput &out, CYFlags flags) const;
914 virtual bool Value() const = 0;
915 virtual void Output(CYOutput &out, CYFlags flags) const;
927 virtual bool Value() const {
931 virtual CYNumber *Number(CYContext &context);
932 virtual CYString *String(CYContext &context);
944 virtual bool Value() const {
948 virtual CYNumber *Number(CYContext &context);
949 virtual CYString *String(CYContext &context);
957 CYVariable(CYIdentifier *name) :
962 CYVariable(const char *name) :
963 name_(new($pool) CYIdentifier(name))
970 virtual CYExpression *Replace(CYContext &context);
971 virtual void Output(CYOutput &out, CYFlags flags) const;
979 CYPrefix(CYExpression *rhs) :
984 virtual bool Alphabetic() const = 0;
985 virtual const char *Operator() const = 0;
989 virtual CYExpression *Replace(CYContext &context);
990 virtual void Output(CYOutput &out, CYFlags flags) const;
999 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1005 void SetLeft(CYExpression *lhs) {
1009 virtual bool Alphabetic() const = 0;
1010 virtual const char *Operator() const = 0;
1012 virtual CYExpression *Replace(CYContext &context);
1013 virtual void Output(CYOutput &out, CYFlags flags) const;
1021 CYPostfix(CYExpression *lhs) :
1026 virtual const char *Operator() const = 0;
1030 virtual CYExpression *Replace(CYContext &context);
1031 virtual void Output(CYOutput &out, CYFlags flags) const;
1034 struct CYAssignment :
1040 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1046 void SetLeft(CYExpression *lhs) {
1050 virtual const char *Operator() const = 0;
1054 virtual CYExpression *Replace(CYContext &context);
1055 virtual void Output(CYOutput &out, CYFlags flags) const;
1063 CYExpression *value_;
1065 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1066 CYNext<CYArgument>(next),
1072 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1073 CYNext<CYArgument>(next),
1079 void Replace(CYContext &context);
1080 void Output(CYOutput &out) const;
1096 CYExpression *case_;
1097 CYStatement *statements_;
1099 CYClause(CYExpression *_case, CYStatement *statements) :
1101 statements_(statements)
1105 void Replace(CYContext &context);
1106 virtual void Output(CYOutput &out) const;
1113 CYExpression *value_;
1115 CYElement(CYExpression *value, CYElement *next) :
1116 CYNext<CYElement>(next),
1121 void Replace(CYContext &context);
1122 void Output(CYOutput &out) const;
1128 CYElement *elements_;
1130 CYArray(CYElement *elements = NULL) :
1135 virtual CYExpression *Replace(CYContext &context);
1136 virtual void Output(CYOutput &out, CYFlags flags) const;
1143 CYPropertyName *name_;
1144 CYExpression *value_;
1146 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1147 CYNext<CYProperty>(next),
1153 void Replace(CYContext &context);
1154 virtual void Output(CYOutput &out) const;
1157 struct CYDeclaration :
1160 CYIdentifier *identifier_;
1161 CYExpression *initialiser_;
1163 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1164 identifier_(identifier),
1165 initialiser_(initialiser)
1169 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1171 virtual const char *ForEachIn() const;
1172 virtual CYExpression *ForEachIn(CYContext &out);
1174 virtual CYExpression *Replace(CYContext &context);
1175 virtual CYAssignment *Assignment(CYContext &context);
1177 virtual void Output(CYOutput &out, CYFlags flags) const;
1180 struct CYDeclarations :
1181 CYNext<CYDeclarations>,
1185 CYDeclaration *declaration_;
1187 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1188 CYNext<CYDeclarations>(next),
1189 declaration_(declaration)
1193 virtual void For(CYOutput &out) const;
1195 virtual CYCompound *Replace(CYContext &context);
1196 CYProperty *Property(CYContext &context);
1198 virtual void Output(CYOutput &out) const;
1199 virtual void Output(CYOutput &out, CYFlags flags) const;
1205 CYDeclarations *declarations_;
1207 CYVar(CYDeclarations *declarations) :
1208 declarations_(declarations)
1212 virtual CYStatement *Replace(CYContext &context);
1213 virtual void Output(CYOutput &out, CYFlags flags) const;
1219 CYDeclarations *declarations_;
1222 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1223 declarations_(declarations),
1228 virtual CYStatement *Replace(CYContext &context);
1229 virtual void Output(CYOutput &out, CYFlags flags) const;
1235 CYForInitialiser *initialiser_;
1236 CYExpression *test_;
1237 CYExpression *increment_;
1240 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1241 initialiser_(initialiser),
1243 increment_(increment),
1248 virtual CYStatement *Replace(CYContext &context);
1249 virtual void Output(CYOutput &out, CYFlags flags) const;
1255 CYForInInitialiser *initialiser_;
1259 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1260 initialiser_(initialiser),
1266 virtual CYStatement *Replace(CYContext &context);
1267 virtual void Output(CYOutput &out, CYFlags flags) const;
1270 struct CYForEachIn :
1273 CYForInInitialiser *initialiser_;
1277 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1278 initialiser_(initialiser),
1284 virtual CYStatement *Replace(CYContext &context);
1285 virtual void Output(CYOutput &out, CYFlags flags) const;
1291 CYProperty *properties_;
1293 CYObject(CYProperty *properties = NULL) :
1294 properties_(properties)
1298 virtual CYExpression *Replace(CYContext &context);
1299 void Output(CYOutput &out, CYFlags flags) const;
1305 CYExpression *object_;
1306 CYExpression *property_;
1308 CYMember(CYExpression *object, CYExpression *property) :
1314 void SetLeft(CYExpression *object) {
1318 void Replace_(CYContext &context);
1321 struct CYDirectMember :
1324 CYDirectMember(CYExpression *object, CYExpression *property) :
1325 CYMember(object, property)
1332 virtual CYExpression *Replace(CYContext &context);
1333 virtual void Output(CYOutput &out, CYFlags flags) const;
1336 struct CYIndirectMember :
1339 CYIndirectMember(CYExpression *object, CYExpression *property) :
1340 CYMember(object, property)
1347 virtual CYExpression *Replace(CYContext &context);
1348 virtual void Output(CYOutput &out, CYFlags flags) const;
1357 CYExpression *constructor_;
1358 CYArgument *arguments_;
1360 New(CYExpression *constructor, CYArgument *arguments) :
1361 constructor_(constructor),
1362 arguments_(arguments)
1366 virtual unsigned Precedence() const {
1367 return arguments_ == NULL ? 2 : 1;
1372 virtual CYExpression *Replace(CYContext &context);
1373 virtual void Output(CYOutput &out, CYFlags flags) const;
1375 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1383 CYExpression *function_;
1384 CYArgument *arguments_;
1386 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1387 function_(function),
1388 arguments_(arguments)
1395 virtual CYExpression *Replace(CYContext &context);
1396 virtual void Output(CYOutput &out, CYFlags flags) const;
1398 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1403 struct CYRubyBlock :
1406 CYExpression *call_;
1409 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1418 virtual CYExpression *Replace(CYContext &context);
1419 virtual void Output(CYOutput &out, CYFlags flags) const;
1425 CYExpression *test_;
1427 CYStatement *false_;
1429 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1436 virtual CYStatement *Replace(CYContext &context);
1437 virtual void Output(CYOutput &out, CYFlags flags) const;
1443 CYExpression *test_;
1446 CYDoWhile(CYExpression *test, CYStatement *code) :
1452 virtual CYStatement *Replace(CYContext &context);
1453 virtual void Output(CYOutput &out, CYFlags flags) const;
1459 CYExpression *test_;
1462 CYWhile(CYExpression *test, CYStatement *code) :
1468 virtual CYStatement *Replace(CYContext &context);
1469 virtual void Output(CYOutput &out, CYFlags flags) const;
1472 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1474 CYIdentifier *name_;
1475 CYFunctionParameter *parameters_;
1477 CYNonLocal *nonlocal_;
1479 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1481 parameters_(parameters),
1487 virtual ~CYFunction() {
1490 void Inject(CYContext &context);
1491 virtual void Replace_(CYContext &context, bool outer);
1492 virtual void Output(CYOutput &out, CYFlags flags) const;
1495 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1496 struct CYFunctionExpression :
1500 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1501 CYFunction(name, parameters, statements)
1508 virtual CYExpression *Replace(CYContext &context);
1509 virtual void Output(CYOutput &out, CYFlags flags) const;
1512 // XXX: this should derive from CYAnonymousFunctionExpression
1514 CYFunctionExpression
1516 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1517 CYFunctionExpression(NULL, parameters, statements)
1521 virtual CYExpression *Replace(CYContext &context);
1522 virtual void Output(CYOutput &out, CYFlags flags) const;
1525 // XXX: this should derive from CYNamedFunction
1526 struct CYFunctionStatement :
1530 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1531 CYFunction(name, parameters, statements)
1535 virtual CYStatement *Replace(CYContext &context);
1536 virtual void Output(CYOutput &out, CYFlags flags) const;
1542 CYExpression *expression_;
1544 CYExpress(CYExpression *expression) :
1545 expression_(expression)
1547 if (expression == NULL)
1551 virtual CYStatement *Collapse(CYContext &context);
1552 virtual CYStatement *Replace(CYContext &context);
1553 virtual void Output(CYOutput &out, CYFlags flags) const;
1559 CYIdentifier *label_;
1561 CYContinue(CYIdentifier *label) :
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1573 CYIdentifier *label_;
1575 CYBreak(CYIdentifier *label) :
1580 virtual CYStatement *Replace(CYContext &context);
1581 virtual void Output(CYOutput &out, CYFlags flags) const;
1587 CYExpression *value_;
1589 CYReturn(CYExpression *value) :
1594 virtual CYStatement *Replace(CYContext &context);
1595 virtual void Output(CYOutput &out, CYFlags flags) const;
1601 virtual CYStatement *Collapse(CYContext &context);
1602 virtual CYStatement *Replace(CYContext &context);
1603 virtual void Output(CYOutput &out, CYFlags flags) const;
1611 CYFinally(CYStatement *statements) :
1616 void Replace(CYContext &context);
1617 virtual void Output(CYOutput &out) const;
1626 CYIdentifier *name_;
1629 Catch(CYIdentifier *name, CYStatement *statements) :
1635 void Replace(CYContext &context);
1636 virtual void Output(CYOutput &out) const;
1644 CYFinally *finally_;
1646 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1653 virtual CYStatement *Replace(CYContext &context);
1654 virtual void Output(CYOutput &out, CYFlags flags) const;
1660 CYExpression *value_;
1662 Throw(CYExpression *value = NULL) :
1667 virtual CYStatement *Replace(CYContext &context);
1668 virtual void Output(CYOutput &out, CYFlags flags) const;
1676 CYExpression *scope_;
1679 CYWith(CYExpression *scope, CYStatement *code) :
1685 virtual CYStatement *Replace(CYContext &context);
1686 virtual void Output(CYOutput &out, CYFlags flags) const;
1692 CYExpression *value_;
1695 CYSwitch(CYExpression *value, CYClause *clauses) :
1701 virtual CYStatement *Replace(CYContext &context);
1702 virtual void Output(CYOutput &out, CYFlags flags) const;
1705 struct CYCondition :
1708 CYExpression *test_;
1709 CYExpression *true_;
1710 CYExpression *false_;
1712 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1721 virtual CYExpression *Replace(CYContext &context);
1722 virtual void Output(CYOutput &out, CYFlags flags) const;
1725 struct CYAddressOf :
1728 CYAddressOf(CYExpression *rhs) :
1733 virtual const char *Operator() const {
1739 virtual CYExpression *Replace(CYContext &context);
1745 CYIndirect(CYExpression *rhs) :
1750 virtual const char *Operator() const {
1756 virtual CYExpression *Replace(CYContext &context);
1760 virtual CYExpression *Replace(CYContext &context);
1762 #define CYPostfix_(op, name, args...) \
1763 struct CY ## name : \
1766 CY ## name(CYExpression *lhs) : \
1771 virtual const char *Operator() const { \
1776 #define CYPrefix_(alphabetic, op, name, args...) \
1777 struct CY ## name : \
1780 CY ## name(CYExpression *rhs) : \
1785 CYAlphabetic(alphabetic) \
1787 virtual const char *Operator() const { \
1792 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1793 struct CY ## name : \
1796 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1801 CYAlphabetic(alphabetic) \
1802 CYPrecedence(precedence) \
1804 virtual const char *Operator() const { \
1809 #define CYAssignment_(op, name, args...) \
1810 struct CY ## name ## Assign : \
1813 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1814 CYAssignment(lhs, rhs) \
1818 virtual const char *Operator() const { \
1823 CYPostfix_("++", PostIncrement)
1824 CYPostfix_("--", PostDecrement)
1826 CYPrefix_(true, "delete", Delete)
1827 CYPrefix_(true, "void", Void)
1828 CYPrefix_(true, "typeof", TypeOf)
1829 CYPrefix_(false, "++", PreIncrement)
1830 CYPrefix_(false, "--", PreDecrement)
1831 CYPrefix_(false, "+", Affirm)
1832 CYPrefix_(false, "-", Negate)
1833 CYPrefix_(false, "~", BitwiseNot)
1834 CYPrefix_(false, "!", LogicalNot)
1836 CYInfix_(false, 5, "*", Multiply)
1837 CYInfix_(false, 5, "/", Divide)
1838 CYInfix_(false, 5, "%", Modulus)
1839 CYInfix_(false, 6, "+", Add, CYReplace)
1840 CYInfix_(false, 6, "-", Subtract)
1841 CYInfix_(false, 7, "<<", ShiftLeft)
1842 CYInfix_(false, 7, ">>", ShiftRightSigned)
1843 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1844 CYInfix_(false, 8, "<", Less)
1845 CYInfix_(false, 8, ">", Greater)
1846 CYInfix_(false, 8, "<=", LessOrEqual)
1847 CYInfix_(false, 8, ">=", GreaterOrEqual)
1848 CYInfix_(true, 8, "instanceof", InstanceOf)
1849 CYInfix_(true, 8, "in", In)
1850 CYInfix_(false, 9, "==", Equal)
1851 CYInfix_(false, 9, "!=", NotEqual)
1852 CYInfix_(false, 9, "===", Identical)
1853 CYInfix_(false, 9, "!==", NotIdentical)
1854 CYInfix_(false, 10, "&", BitwiseAnd)
1855 CYInfix_(false, 11, "^", BitwiseXOr)
1856 CYInfix_(false, 12, "|", BitwiseOr)
1857 CYInfix_(false, 13, "&&", LogicalAnd)
1858 CYInfix_(false, 14, "||", LogicalOr)
1860 CYAssignment_("=", )
1861 CYAssignment_("*=", Multiply)
1862 CYAssignment_("/=", Divide)
1863 CYAssignment_("%=", Modulus)
1864 CYAssignment_("+=", Add)
1865 CYAssignment_("-=", Subtract)
1866 CYAssignment_("<<=", ShiftLeft)
1867 CYAssignment_(">>=", ShiftRightSigned)
1868 CYAssignment_(">>>=", ShiftRightUnsigned)
1869 CYAssignment_("&=", BitwiseAnd)
1870 CYAssignment_("^=", BitwiseXOr)
1871 CYAssignment_("|=", BitwiseOr)
1873 #endif/*CYCRIPT_PARSER_HPP*/