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 *Replace(CYContext &context) = 0;
160 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
163 struct CYStatements {
173 operator CYStatement *() const {
177 CYStatements &operator ->*(CYStatement *next) {
179 if (first_ == NULL) {
182 } else for (;; last_ = last_->next_)
183 if (last_->next_ == NULL) {
193 virtual ~CYClassName() {
196 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
197 virtual void ClassName(CYOutput &out, bool object) const = 0;
207 CYWord(const char *word) :
212 void Set(const char *value) {
216 virtual const char *Word() const;
217 virtual void Output(CYOutput &out) const;
219 virtual CYExpression *ClassName(CYContext &context, bool object);
220 virtual void ClassName(CYOutput &out, bool object) const;
221 virtual void PropertyName(CYOutput &out) const;
224 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
226 return lhs << rhs.Word();
229 struct CYIdentifier :
230 CYNext<CYIdentifier>,
233 CYIdentifier *replace_;
237 CYIdentifier(const char *word) :
245 virtual const char *Word() const;
246 CYIdentifier *Replace(CYContext &context);
254 CYComment(const char *value) :
259 virtual CYStatement *Replace(CYContext &context);
260 virtual void Output(CYOutput &out, CYFlags flags) const;
267 CYStatement *statement_;
269 CYLabel(CYIdentifier *name, CYStatement *statement) :
271 statement_(statement)
275 virtual CYStatement *Replace(CYContext &context);
276 virtual void Output(CYOutput &out, CYFlags flags) const;
279 struct CYCStringLess :
280 std::binary_function<const char *, const char *, bool>
282 _finline bool operator ()(const char *lhs, const char *rhs) const {
283 return strcmp(lhs, rhs) < 0;
287 struct CYIdentifierValueLess :
288 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
290 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
291 return CYCStringLess()(lhs->Word(), rhs->Word());
295 enum CYIdentifierFlags {
296 CYIdentifierArgument,
297 CYIdentifierVariable,
303 typedef std::set<const char *, CYCStringLess> CYCStringSet;
304 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
305 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
307 struct CYIdentifierUsage {
308 CYIdentifier *identifier_;
312 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
314 // XXX: strategy pattern, maybe subclass
325 CYStatement *&statements_;
329 CYIdentifierAddressFlagsMap internal_;
330 CYIdentifierValueSet identifiers_;
332 CYScope(CYScopeType type, CYContext &context, CYStatement *&statements);
337 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
338 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
339 void Merge(CYContext &context, CYIdentifier *identifier);
340 void Scope(CYContext &context, CYStatement *&statements);
346 CYStatement *statements_;
348 CYProgram(CYStatement *statements) :
349 statements_(statements)
353 virtual void Replace(CYContext &context);
354 virtual void Output(CYOutput &out) const;
363 CYIdentifierUsageVector rename_;
365 CYNonLocal *nonlocal_;
366 CYNonLocal *nextlocal_;
369 CYContext(CYOptions &options) :
378 virtual ~CYContext() {
381 template <typename Type_>
382 void ReplaceAll(Type_ *&values) {
383 Type_ **last(&values);
384 CYForEach (next, values) {
385 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 CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 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;
564 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
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 virtual CYExpression *Replace(CYContext &context) = 0;
577 virtual CYExpression *Primitive(CYContext &context) {
581 virtual CYNumber *Number(CYContext &context) {
585 virtual CYString *String(CYContext &context) {
589 virtual const char *Word() const {
594 #define CYAlphabetic(value) \
595 virtual bool Alphabetic() const { \
599 #define CYPrecedence(value) \
600 static const unsigned Precedence_ = value; \
601 virtual unsigned Precedence() const { \
602 return Precedence_; \
605 #define CYRightHand(value) \
606 virtual bool RightHand() const { \
613 CYExpression *expressions_;
615 CYCompound(CYExpression *expressions = NULL) :
616 expressions_(expressions)
620 void AddPrev(CYExpression *expression) {
621 CYSetLast(expression, expressions_);
622 expressions_ = expression;
627 virtual CYExpression *Replace(CYContext &context);
628 void Output(CYOutput &out, CYFlags flags) const;
631 struct CYFunctionParameter :
632 CYNext<CYFunctionParameter>,
637 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
638 CYNext<CYFunctionParameter>(next),
643 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
644 virtual void Output(CYOutput &out) const;
647 struct CYOptionalFunctionParameter :
650 CYExpression *initializer_;
652 CYOptionalFunctionParameter(CYIdentifier *name, CYExpression *initializer, CYFunctionParameter *next = NULL) :
653 CYFunctionParameter(name, next),
654 initializer_(initializer)
658 virtual CYFunctionParameter *Replace(CYContext &context, CYBlock &code);
659 virtual void Output(CYOutput &out) const;
662 struct CYComprehension :
663 CYNext<CYComprehension>,
666 virtual const char *Name() const = 0;
668 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
669 CYFunctionParameter *Parameters(CYContext &context) const;
670 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
671 virtual void Output(CYOutput &out) const = 0;
674 struct CYForInComprehension :
680 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
686 virtual const char *Name() const {
687 return name_->Word();
690 virtual CYFunctionParameter *Parameter(CYContext &context) const;
691 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
692 virtual void Output(CYOutput &out) const;
695 struct CYForEachInComprehension :
701 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
707 virtual const char *Name() const {
708 return name_->Word();
711 virtual CYFunctionParameter *Parameter(CYContext &context) const;
712 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
713 virtual void Output(CYOutput &out) const;
716 struct CYIfComprehension :
721 CYIfComprehension(CYExpression *test) :
726 virtual const char *Name() const {
730 virtual CYFunctionParameter *Parameter(CYContext &context) const;
731 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
732 virtual void Output(CYOutput &out) const;
735 struct CYArrayComprehension :
738 CYExpression *expression_;
739 CYComprehension *comprehensions_;
741 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
742 expression_(expression),
743 comprehensions_(comprehensions)
749 virtual CYExpression *Replace(CYContext &context);
750 virtual void Output(CYOutput &out, CYFlags flags) const;
763 virtual CYExpression *Replace(CYContext &context);
777 CYRange(uint64_t lo, uint64_t hi) :
782 bool operator [](uint8_t value) const {
783 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
786 void operator()(uint8_t value) {
789 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
793 extern CYRange DigitRange_;
794 extern CYRange WordStartRange_;
795 extern CYRange WordEndRange_;
810 CYString(const char *value) :
816 CYString(const char *value, size_t size) :
822 CYString(const CYWord *word) :
823 value_(word->Word()),
824 size_(strlen(value_))
828 const char *Value() const {
832 virtual const char *Word() const;
834 virtual CYNumber *Number(CYContext &context);
835 virtual CYString *String(CYContext &context);
837 CYString *Concat(CYContext &out, CYString *rhs) const;
838 virtual void Output(CYOutput &out, CYFlags flags) const;
839 virtual void PropertyName(CYOutput &out) const;
848 CYNumber(double value) :
853 double Value() const {
857 virtual CYNumber *Number(CYContext &context);
858 virtual CYString *String(CYContext &context);
860 virtual void Output(CYOutput &out, CYFlags flags) const;
861 virtual void PropertyName(CYOutput &out) const;
869 CYRegEx(const char *value) :
874 const char *Value() const {
878 virtual void Output(CYOutput &out, CYFlags flags) const;
890 virtual CYNumber *Number(CYContext &context);
891 virtual CYString *String(CYContext &context);
893 virtual void Output(CYOutput &out, CYFlags flags) const;
905 virtual CYExpression *Replace(CYContext &context);
906 virtual void Output(CYOutput &out, CYFlags flags) const;
912 virtual bool Value() const = 0;
913 virtual void Output(CYOutput &out, CYFlags flags) const;
925 virtual bool Value() const {
929 virtual CYNumber *Number(CYContext &context);
930 virtual CYString *String(CYContext &context);
942 virtual bool Value() const {
946 virtual CYNumber *Number(CYContext &context);
947 virtual CYString *String(CYContext &context);
955 CYVariable(CYIdentifier *name) :
960 CYVariable(const char *name) :
961 name_(new($pool) CYIdentifier(name))
968 virtual CYExpression *Replace(CYContext &context);
969 virtual void Output(CYOutput &out, CYFlags flags) const;
977 CYPrefix(CYExpression *rhs) :
982 virtual bool Alphabetic() const = 0;
983 virtual const char *Operator() const = 0;
987 virtual CYExpression *Replace(CYContext &context);
988 virtual void Output(CYOutput &out, CYFlags flags) const;
997 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1003 void SetLeft(CYExpression *lhs) {
1007 virtual bool Alphabetic() const = 0;
1008 virtual const char *Operator() const = 0;
1010 virtual CYExpression *Replace(CYContext &context);
1011 virtual void Output(CYOutput &out, CYFlags flags) const;
1019 CYPostfix(CYExpression *lhs) :
1024 virtual const char *Operator() const = 0;
1028 virtual CYExpression *Replace(CYContext &context);
1029 virtual void Output(CYOutput &out, CYFlags flags) const;
1032 struct CYAssignment :
1038 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1044 void SetLeft(CYExpression *lhs) {
1048 virtual const char *Operator() const = 0;
1052 virtual CYExpression *Replace(CYContext &context);
1053 virtual void Output(CYOutput &out, CYFlags flags) const;
1061 CYExpression *value_;
1063 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1064 CYNext<CYArgument>(next),
1070 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1071 CYNext<CYArgument>(next),
1077 void Replace(CYContext &context);
1078 void Output(CYOutput &out) const;
1094 CYExpression *case_;
1095 CYStatement *statements_;
1097 CYClause(CYExpression *_case, CYStatement *statements) :
1099 statements_(statements)
1103 void Replace(CYContext &context);
1104 virtual void Output(CYOutput &out) const;
1111 CYExpression *value_;
1113 CYElement(CYExpression *value, CYElement *next) :
1114 CYNext<CYElement>(next),
1119 void Replace(CYContext &context);
1120 void Output(CYOutput &out) const;
1126 CYElement *elements_;
1128 CYArray(CYElement *elements = NULL) :
1133 virtual CYExpression *Replace(CYContext &context);
1134 virtual void Output(CYOutput &out, CYFlags flags) const;
1141 CYPropertyName *name_;
1142 CYExpression *value_;
1144 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1145 CYNext<CYProperty>(next),
1151 void Replace(CYContext &context);
1152 virtual void Output(CYOutput &out) const;
1155 struct CYDeclaration :
1158 CYIdentifier *identifier_;
1159 CYExpression *initialiser_;
1161 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1162 identifier_(identifier),
1163 initialiser_(initialiser)
1167 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1168 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1170 virtual CYExpression *Replace(CYContext &context);
1171 virtual CYAssignment *Assignment(CYContext &context);
1173 virtual void Output(CYOutput &out, CYFlags flags) const;
1176 struct CYDeclarations :
1177 CYNext<CYDeclarations>,
1181 CYDeclaration *declaration_;
1183 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1184 CYNext<CYDeclarations>(next),
1185 declaration_(declaration)
1189 virtual void For(CYOutput &out) const;
1191 virtual CYCompound *Replace(CYContext &context);
1192 CYProperty *Property(CYContext &context);
1194 virtual void Output(CYOutput &out) const;
1195 virtual void Output(CYOutput &out, CYFlags flags) const;
1201 CYDeclarations *declarations_;
1203 CYVar(CYDeclarations *declarations) :
1204 declarations_(declarations)
1208 virtual CYStatement *Replace(CYContext &context);
1209 virtual void Output(CYOutput &out, CYFlags flags) const;
1215 CYDeclarations *declarations_;
1218 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1219 declarations_(declarations),
1224 virtual CYStatement *Replace(CYContext &context);
1225 virtual void Output(CYOutput &out, CYFlags flags) const;
1231 CYForInitialiser *initialiser_;
1232 CYExpression *test_;
1233 CYExpression *increment_;
1236 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1237 initialiser_(initialiser),
1239 increment_(increment),
1244 virtual CYStatement *Replace(CYContext &context);
1245 virtual void Output(CYOutput &out, CYFlags flags) const;
1251 CYForInInitialiser *initialiser_;
1255 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1256 initialiser_(initialiser),
1262 virtual CYStatement *Replace(CYContext &context);
1263 virtual void Output(CYOutput &out, CYFlags flags) const;
1266 struct CYForEachIn :
1269 CYForInInitialiser *initialiser_;
1273 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1274 initialiser_(initialiser),
1280 virtual CYStatement *Replace(CYContext &context);
1281 virtual void Output(CYOutput &out, CYFlags flags) const;
1287 CYProperty *properties_;
1289 CYObject(CYProperty *properties = NULL) :
1290 properties_(properties)
1294 virtual CYExpression *Replace(CYContext &context);
1295 void Output(CYOutput &out, CYFlags flags) const;
1301 CYExpression *object_;
1302 CYExpression *property_;
1304 CYMember(CYExpression *object, CYExpression *property) :
1310 void SetLeft(CYExpression *object) {
1314 void Replace_(CYContext &context);
1317 struct CYDirectMember :
1320 CYDirectMember(CYExpression *object, CYExpression *property) :
1321 CYMember(object, property)
1328 virtual CYExpression *Replace(CYContext &context);
1329 virtual void Output(CYOutput &out, CYFlags flags) const;
1332 struct CYIndirectMember :
1335 CYIndirectMember(CYExpression *object, CYExpression *property) :
1336 CYMember(object, property)
1343 virtual CYExpression *Replace(CYContext &context);
1344 virtual void Output(CYOutput &out, CYFlags flags) const;
1353 CYExpression *constructor_;
1354 CYArgument *arguments_;
1356 New(CYExpression *constructor, CYArgument *arguments) :
1357 constructor_(constructor),
1358 arguments_(arguments)
1362 virtual unsigned Precedence() const {
1363 return arguments_ == NULL ? 2 : 1;
1368 virtual CYExpression *Replace(CYContext &context);
1369 virtual void Output(CYOutput &out, CYFlags flags) const;
1371 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1379 CYExpression *function_;
1380 CYArgument *arguments_;
1382 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1383 function_(function),
1384 arguments_(arguments)
1391 virtual CYExpression *Replace(CYContext &context);
1392 virtual void Output(CYOutput &out, CYFlags flags) const;
1394 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1399 struct CYRubyBlock :
1402 CYExpression *call_;
1405 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1414 virtual CYExpression *Replace(CYContext &context);
1415 virtual void Output(CYOutput &out, CYFlags flags) const;
1421 CYExpression *test_;
1423 CYStatement *false_;
1425 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1432 virtual CYStatement *Replace(CYContext &context);
1433 virtual void Output(CYOutput &out, CYFlags flags) const;
1439 CYExpression *test_;
1442 CYDoWhile(CYExpression *test, CYStatement *code) :
1448 virtual CYStatement *Replace(CYContext &context);
1449 virtual void Output(CYOutput &out, CYFlags flags) const;
1455 CYExpression *test_;
1458 CYWhile(CYExpression *test, CYStatement *code) :
1464 virtual CYStatement *Replace(CYContext &context);
1465 virtual void Output(CYOutput &out, CYFlags flags) const;
1468 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1470 CYIdentifier *name_;
1471 CYFunctionParameter *parameters_;
1473 CYNonLocal *nonlocal_;
1475 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1477 parameters_(parameters),
1483 virtual ~CYFunction() {
1486 void Inject(CYContext &context);
1487 virtual void Replace_(CYContext &context, bool outer);
1488 virtual void Output(CYOutput &out, CYFlags flags) const;
1491 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1492 struct CYFunctionExpression :
1496 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1497 CYFunction(name, parameters, statements)
1504 virtual CYExpression *Replace(CYContext &context);
1505 virtual void Output(CYOutput &out, CYFlags flags) const;
1508 // XXX: this should derive from CYAnonymousFunctionExpression
1510 CYFunctionExpression
1512 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1513 CYFunctionExpression(NULL, parameters, statements)
1517 virtual CYExpression *Replace(CYContext &context);
1518 virtual void Output(CYOutput &out, CYFlags flags) const;
1521 // XXX: this should derive from CYNamedFunction
1522 struct CYFunctionStatement :
1526 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1527 CYFunction(name, parameters, statements)
1531 virtual CYStatement *Replace(CYContext &context);
1532 virtual void Output(CYOutput &out, CYFlags flags) const;
1538 CYExpression *expression_;
1540 CYExpress(CYExpression *expression) :
1541 expression_(expression)
1543 if (expression == NULL)
1547 virtual CYStatement *Replace(CYContext &context);
1548 virtual void Output(CYOutput &out, CYFlags flags) const;
1554 CYIdentifier *label_;
1556 CYContinue(CYIdentifier *label) :
1561 virtual CYStatement *Replace(CYContext &context);
1562 virtual void Output(CYOutput &out, CYFlags flags) const;
1568 CYIdentifier *label_;
1570 CYBreak(CYIdentifier *label) :
1575 virtual CYStatement *Replace(CYContext &context);
1576 virtual void Output(CYOutput &out, CYFlags flags) const;
1582 CYExpression *value_;
1584 CYReturn(CYExpression *value) :
1589 virtual CYStatement *Replace(CYContext &context);
1590 virtual void Output(CYOutput &out, CYFlags flags) const;
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/*CYCRIPT_PARSER_HPP*/