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 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 *Collapse(CYContext &context);
1548 virtual CYStatement *Replace(CYContext &context);
1549 virtual void Output(CYOutput &out, CYFlags flags) const;
1555 CYIdentifier *label_;
1557 CYContinue(CYIdentifier *label) :
1562 virtual CYStatement *Replace(CYContext &context);
1563 virtual void Output(CYOutput &out, CYFlags flags) const;
1569 CYIdentifier *label_;
1571 CYBreak(CYIdentifier *label) :
1576 virtual CYStatement *Replace(CYContext &context);
1577 virtual void Output(CYOutput &out, CYFlags flags) const;
1583 CYExpression *value_;
1585 CYReturn(CYExpression *value) :
1590 virtual CYStatement *Replace(CYContext &context);
1591 virtual void Output(CYOutput &out, CYFlags flags) const;
1597 virtual CYStatement *Collapse(CYContext &context);
1598 virtual CYStatement *Replace(CYContext &context);
1599 virtual void Output(CYOutput &out, CYFlags flags) const;
1607 CYFinally(CYStatement *statements) :
1612 void Replace(CYContext &context);
1613 virtual void Output(CYOutput &out) const;
1622 CYIdentifier *name_;
1625 Catch(CYIdentifier *name, CYStatement *statements) :
1631 void Replace(CYContext &context);
1632 virtual void Output(CYOutput &out) const;
1640 CYFinally *finally_;
1642 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1649 virtual CYStatement *Replace(CYContext &context);
1650 virtual void Output(CYOutput &out, CYFlags flags) const;
1656 CYExpression *value_;
1658 Throw(CYExpression *value = NULL) :
1663 virtual CYStatement *Replace(CYContext &context);
1664 virtual void Output(CYOutput &out, CYFlags flags) const;
1672 CYExpression *scope_;
1675 CYWith(CYExpression *scope, CYStatement *code) :
1681 virtual CYStatement *Replace(CYContext &context);
1682 virtual void Output(CYOutput &out, CYFlags flags) const;
1688 CYExpression *value_;
1691 CYSwitch(CYExpression *value, CYClause *clauses) :
1697 virtual CYStatement *Replace(CYContext &context);
1698 virtual void Output(CYOutput &out, CYFlags flags) const;
1701 struct CYCondition :
1704 CYExpression *test_;
1705 CYExpression *true_;
1706 CYExpression *false_;
1708 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1717 virtual CYExpression *Replace(CYContext &context);
1718 virtual void Output(CYOutput &out, CYFlags flags) const;
1721 struct CYAddressOf :
1724 CYAddressOf(CYExpression *rhs) :
1729 virtual const char *Operator() const {
1735 virtual CYExpression *Replace(CYContext &context);
1741 CYIndirect(CYExpression *rhs) :
1746 virtual const char *Operator() const {
1752 virtual CYExpression *Replace(CYContext &context);
1756 virtual CYExpression *Replace(CYContext &context);
1758 #define CYPostfix_(op, name, args...) \
1759 struct CY ## name : \
1762 CY ## name(CYExpression *lhs) : \
1767 virtual const char *Operator() const { \
1772 #define CYPrefix_(alphabetic, op, name, args...) \
1773 struct CY ## name : \
1776 CY ## name(CYExpression *rhs) : \
1781 CYAlphabetic(alphabetic) \
1783 virtual const char *Operator() const { \
1788 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1789 struct CY ## name : \
1792 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1797 CYAlphabetic(alphabetic) \
1798 CYPrecedence(precedence) \
1800 virtual const char *Operator() const { \
1805 #define CYAssignment_(op, name, args...) \
1806 struct CY ## name ## Assign : \
1809 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1810 CYAssignment(lhs, rhs) \
1814 virtual const char *Operator() const { \
1819 CYPostfix_("++", PostIncrement)
1820 CYPostfix_("--", PostDecrement)
1822 CYPrefix_(true, "delete", Delete)
1823 CYPrefix_(true, "void", Void)
1824 CYPrefix_(true, "typeof", TypeOf)
1825 CYPrefix_(false, "++", PreIncrement)
1826 CYPrefix_(false, "--", PreDecrement)
1827 CYPrefix_(false, "+", Affirm)
1828 CYPrefix_(false, "-", Negate)
1829 CYPrefix_(false, "~", BitwiseNot)
1830 CYPrefix_(false, "!", LogicalNot)
1832 CYInfix_(false, 5, "*", Multiply)
1833 CYInfix_(false, 5, "/", Divide)
1834 CYInfix_(false, 5, "%", Modulus)
1835 CYInfix_(false, 6, "+", Add, CYReplace)
1836 CYInfix_(false, 6, "-", Subtract)
1837 CYInfix_(false, 7, "<<", ShiftLeft)
1838 CYInfix_(false, 7, ">>", ShiftRightSigned)
1839 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1840 CYInfix_(false, 8, "<", Less)
1841 CYInfix_(false, 8, ">", Greater)
1842 CYInfix_(false, 8, "<=", LessOrEqual)
1843 CYInfix_(false, 8, ">=", GreaterOrEqual)
1844 CYInfix_(true, 8, "instanceof", InstanceOf)
1845 CYInfix_(true, 8, "in", In)
1846 CYInfix_(false, 9, "==", Equal)
1847 CYInfix_(false, 9, "!=", NotEqual)
1848 CYInfix_(false, 9, "===", Identical)
1849 CYInfix_(false, 9, "!==", NotIdentical)
1850 CYInfix_(false, 10, "&", BitwiseAnd)
1851 CYInfix_(false, 11, "^", BitwiseXOr)
1852 CYInfix_(false, 12, "|", BitwiseOr)
1853 CYInfix_(false, 13, "&&", LogicalAnd)
1854 CYInfix_(false, 14, "||", LogicalOr)
1856 CYAssignment_("=", )
1857 CYAssignment_("*=", Multiply)
1858 CYAssignment_("/=", Divide)
1859 CYAssignment_("%=", Modulus)
1860 CYAssignment_("+=", Add)
1861 CYAssignment_("-=", Subtract)
1862 CYAssignment_("<<=", ShiftLeft)
1863 CYAssignment_(">>=", ShiftRightSigned)
1864 CYAssignment_(">>>=", ShiftRightUnsigned)
1865 CYAssignment_("&=", BitwiseAnd)
1866 CYAssignment_("^=", BitwiseXOr)
1867 CYAssignment_("|=", BitwiseOr)
1869 #endif/*CYCRIPT_PARSER_HPP*/