]> git.saurik.com Git - cycript.git/blob - Syntax.hpp
Update the copyright year now that 2016 has begun.
[cycript.git] / Syntax.hpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_SYNTAX_HPP
23 #define CYCRIPT_SYNTAX_HPP
24
25 #include <cstdio>
26 #include <cstdlib>
27
28 #include <streambuf>
29 #include <string>
30 #include <vector>
31
32 #include "List.hpp"
33 #include "Location.hpp"
34 #include "Options.hpp"
35 #include "Pooling.hpp"
36 #include "String.hpp"
37
38 double CYCastDouble(const char *value, size_t size);
39 double CYCastDouble(const char *value);
40 double CYCastDouble(CYUTF8String value);
41
42 void CYNumerify(std::ostringstream &str, double value);
43 void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c = false);
44
45 // XXX: this really should not be here ... :/
46 void *CYPoolFile(CYPool &pool, const char *path, size_t *psize);
47 CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path);
48
49 struct CYContext;
50
51 struct CYThing {
52 virtual void Output(struct CYOutput &out) const = 0;
53 };
54
55 struct CYOutput {
56 std::streambuf &out_;
57 CYPosition position_;
58
59 CYOptions &options_;
60 bool pretty_;
61 unsigned indent_;
62 unsigned recent_;
63 bool right_;
64
65 enum {
66 NoMode,
67 NoLetter,
68 NoPlus,
69 NoHyphen,
70 Terminated
71 } mode_;
72
73 CYOutput(std::streambuf &out, CYOptions &options) :
74 out_(out),
75 options_(options),
76 pretty_(false),
77 indent_(0),
78 recent_(0),
79 right_(false),
80 mode_(NoMode)
81 {
82 }
83
84 void Check(char value);
85 void Terminate();
86
87 _finline void operator ()(char value) {
88 _assert(out_.sputc(value) != EOF);
89 recent_ = indent_;
90 if (value == '\n')
91 position_.Lines(1);
92 else
93 position_.Columns(1);
94 }
95
96 _finline void operator ()(const char *data, std::streamsize size) {
97 _assert(out_.sputn(data, size) == size);
98 recent_ = indent_;
99 position_.Columns(size);
100 }
101
102 _finline void operator ()(const char *data) {
103 return operator ()(data, strlen(data));
104 }
105
106 CYOutput &operator <<(char rhs);
107 CYOutput &operator <<(const char *rhs);
108
109 _finline CYOutput &operator <<(const CYThing *rhs) {
110 if (rhs != NULL)
111 rhs->Output(*this);
112 return *this;
113 }
114
115 _finline CYOutput &operator <<(const CYThing &rhs) {
116 rhs.Output(*this);
117 return *this;
118 }
119 };
120
121 struct CYExpression;
122 struct CYAssignment;
123
124 struct CYPropertyName {
125 virtual bool Computed() const {
126 return false;
127 }
128
129 virtual bool Constructor() const {
130 return false;
131 }
132
133 virtual CYExpression *PropertyName(CYContext &context) = 0;
134 virtual void PropertyName(CYOutput &out) const = 0;
135 };
136
137 enum CYNeeded {
138 CYNever = -1,
139 CYSometimes = 0,
140 CYAlways = 1,
141 };
142
143 enum CYFlags {
144 CYNoFlags = 0,
145 CYNoBrace = (1 << 0),
146 CYNoFunction = (1 << 1),
147 CYNoClass = (1 << 2),
148 CYNoIn = (1 << 3),
149 CYNoCall = (1 << 4),
150 CYNoRightHand = (1 << 5),
151 CYNoDangle = (1 << 6),
152 CYNoInteger = (1 << 7),
153 CYNoColon = (1 << 8),
154 CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
155 };
156
157 _finline CYFlags operator ~(CYFlags rhs) {
158 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
159 }
160
161 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
162 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
163 }
164
165 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
166 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
167 }
168
169 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
170 return lhs = lhs | rhs;
171 }
172
173 _finline CYFlags CYLeft(CYFlags flags) {
174 return flags & ~(CYNoDangle | CYNoInteger);
175 }
176
177 _finline CYFlags CYRight(CYFlags flags) {
178 return flags & ~CYNoBFC;
179 }
180
181 _finline CYFlags CYCenter(CYFlags flags) {
182 return CYLeft(CYRight(flags));
183 }
184
185 enum CYCompactType {
186 CYCompactNone,
187 CYCompactLong,
188 CYCompactShort,
189 };
190
191 #define CYCompact(type) \
192 virtual CYCompactType Compact() const { \
193 return CYCompact ## type; \
194 }
195
196 struct CYStatement :
197 CYNext<CYStatement>,
198 CYThing
199 {
200 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
201 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
202 virtual void Output(CYOutput &out) const;
203
204 virtual CYStatement *Replace(CYContext &context) = 0;
205
206 virtual CYCompactType Compact() const = 0;
207 virtual CYStatement *Return();
208
209 private:
210 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
211 };
212
213 typedef CYList<CYStatement> CYStatements;
214
215 struct CYForInitializer :
216 CYStatement
217 {
218 virtual CYForInitializer *Replace(CYContext &context) = 0;
219 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
220 };
221
222 struct CYWord :
223 CYThing,
224 CYPropertyName
225 {
226 const char *word_;
227
228 CYWord(const char *word) :
229 word_(word)
230 {
231 }
232
233 virtual bool Constructor() const {
234 return strcmp(word_, "constructor") == 0;
235 }
236
237 virtual const char *Word() const;
238 virtual void Output(CYOutput &out) const;
239
240 virtual CYExpression *PropertyName(CYContext &context);
241 virtual void PropertyName(CYOutput &out) const;
242 };
243
244 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
245 lhs << &rhs << '=';
246 return lhs << rhs.Word();
247 }
248
249 enum CYIdentifierKind {
250 CYIdentifierArgument,
251 CYIdentifierCatch,
252 CYIdentifierGlobal,
253 CYIdentifierLexical,
254 CYIdentifierMagic,
255 CYIdentifierOther,
256 CYIdentifierVariable,
257 };
258
259 struct CYIdentifier :
260 CYNext<CYIdentifier>,
261 CYWord
262 {
263 CYLocation location_;
264 size_t offset_;
265 size_t usage_;
266
267 CYIdentifier(const char *word) :
268 CYWord(word),
269 offset_(0),
270 usage_(0)
271 {
272 }
273
274 virtual const char *Word() const;
275 CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
276 };
277
278 struct CYLabel :
279 CYStatement
280 {
281 CYIdentifier *name_;
282 CYStatement *statement_;
283
284 CYLabel(CYIdentifier *name, CYStatement *statement) :
285 name_(name),
286 statement_(statement)
287 {
288 }
289
290 CYCompact(Short)
291
292 virtual CYStatement *Replace(CYContext &context);
293 virtual void Output(CYOutput &out, CYFlags flags) const;
294 };
295
296 struct CYCStringLess :
297 std::binary_function<const char *, const char *, bool>
298 {
299 _finline bool operator ()(const char *lhs, const char *rhs) const {
300 return strcmp(lhs, rhs) < 0;
301 }
302 };
303
304 struct CYIdentifierValueLess :
305 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
306 {
307 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
308 return CYCStringLess()(lhs->Word(), rhs->Word());
309 }
310 };
311
312 struct CYIdentifierFlags :
313 CYNext<CYIdentifierFlags>
314 {
315 CYIdentifier *identifier_;
316 CYIdentifierKind kind_;
317 unsigned count_;
318 unsigned offset_;
319
320 CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
321 CYNext<CYIdentifierFlags>(next),
322 identifier_(identifier),
323 kind_(kind),
324 count_(0),
325 offset_(0)
326 {
327 }
328 };
329
330 struct CYScope {
331 bool transparent_;
332 CYScope *parent_;
333 bool damaged_;
334 CYIdentifierFlags *shadow_;
335
336 CYIdentifierFlags *internal_;
337
338 CYScope(bool transparent, CYContext &context);
339
340 CYIdentifierFlags *Lookup(CYContext &context, const char *word);
341 CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
342
343 CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
344 void Merge(CYContext &context, const CYIdentifierFlags *flags);
345
346 void Close(CYContext &context, CYStatement *&statements);
347 void Close(CYContext &context);
348 void Damage();
349 };
350
351 struct CYScript :
352 CYThing
353 {
354 CYStatement *code_;
355
356 CYScript(CYStatement *code) :
357 code_(code)
358 {
359 }
360
361 virtual void Replace(CYContext &context);
362 virtual void Output(CYOutput &out) const;
363 };
364
365 struct CYNonLocal;
366 struct CYThisScope;
367
368 struct CYContext {
369 CYOptions &options_;
370
371 CYScope *scope_;
372 CYThisScope *this_;
373 CYIdentifier *super_;
374
375 CYNonLocal *nonlocal_;
376 CYNonLocal *nextlocal_;
377 unsigned unique_;
378
379 std::vector<CYIdentifier *> replace_;
380
381 CYContext(CYOptions &options) :
382 options_(options),
383 scope_(NULL),
384 this_(NULL),
385 super_(NULL),
386 nonlocal_(NULL),
387 nextlocal_(NULL),
388 unique_(0)
389 {
390 }
391
392 void ReplaceAll(CYStatement *&statement) {
393 if (statement == NULL)
394 return;
395 CYStatement *next(statement->next_);
396
397 Replace(statement);
398 ReplaceAll(next);
399
400 if (statement == NULL)
401 statement = next;
402 else
403 statement->SetNext(next);
404 }
405
406 template <typename Type_>
407 void Replace(Type_ *&value) {
408 for (;;) if (value == NULL)
409 break;
410 else {
411 Type_ *replace(value->Replace(*this));
412 if (replace != value)
413 value = replace;
414 else break;
415 }
416 }
417
418 void NonLocal(CYStatement *&statements);
419 CYIdentifier *Unique();
420 };
421
422 struct CYNonLocal {
423 CYIdentifier *identifier_;
424
425 CYNonLocal() :
426 identifier_(NULL)
427 {
428 }
429
430 CYIdentifier *Target(CYContext &context) {
431 if (identifier_ == NULL)
432 identifier_ = context.Unique();
433 return identifier_;
434 }
435 };
436
437 struct CYThisScope :
438 CYNext<CYThisScope>
439 {
440 CYIdentifier *identifier_;
441
442 CYThisScope() :
443 identifier_(NULL)
444 {
445 }
446
447 CYIdentifier *Identifier(CYContext &context) {
448 if (next_ != NULL)
449 return next_->Identifier(context);
450 if (identifier_ == NULL)
451 identifier_ = context.Unique();
452 return identifier_;
453 }
454 };
455
456 struct CYBlock :
457 CYStatement
458 {
459 CYStatement *code_;
460
461 CYBlock(CYStatement *code) :
462 code_(code)
463 {
464 }
465
466 CYCompact(Short)
467
468 virtual CYStatement *Replace(CYContext &context);
469
470 virtual void Output(CYOutput &out, CYFlags flags) const;
471
472 virtual CYStatement *Return();
473 };
474
475 struct CYTarget;
476 struct CYVar;
477
478 struct CYForInInitializer {
479 virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
480
481 virtual CYTarget *Replace(CYContext &context) = 0;
482 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
483 };
484
485 struct CYFunctionParameter;
486
487 struct CYNumber;
488 struct CYString;
489
490 struct CYExpression :
491 CYThing
492 {
493 virtual int Precedence() const = 0;
494
495 virtual bool RightHand() const {
496 return true;
497 }
498
499 virtual bool Eval() const {
500 return false;
501 }
502
503 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
504
505 virtual void Output(CYOutput &out) const;
506 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
507 void Output(CYOutput &out, int precedence, CYFlags flags) const;
508
509 virtual CYExpression *Replace(CYContext &context) = 0;
510
511 virtual CYExpression *Primitive(CYContext &context) {
512 return NULL;
513 }
514
515 virtual CYFunctionParameter *Parameter() const;
516
517 virtual CYNumber *Number(CYContext &context) {
518 return NULL;
519 }
520
521 virtual CYString *String(CYContext &context) {
522 return NULL;
523 }
524
525 virtual const char *Word() const {
526 return NULL;
527 }
528 };
529
530 struct CYTarget :
531 CYExpression,
532 CYForInInitializer
533 {
534 virtual bool RightHand() const {
535 return false;
536 }
537
538 virtual bool IsNew() const {
539 return false;
540 }
541
542 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
543
544 virtual CYTarget *Replace(CYContext &context) = 0;
545 using CYExpression::Output;
546 };
547
548 #define CYAlphabetic(value) \
549 virtual bool Alphabetic() const { \
550 return value; \
551 }
552
553 #define CYPrecedence(value) \
554 static const int Precedence_ = value; \
555 virtual int Precedence() const { \
556 return Precedence_; \
557 }
558
559 struct CYCompound :
560 CYExpression
561 {
562 CYExpression *expression_;
563 CYExpression *next_;
564
565 CYCompound(CYExpression *expression, CYExpression *next) :
566 expression_(expression),
567 next_(next)
568 {
569 _assert(expression_ != NULL);
570 _assert(next != NULL);
571 }
572
573 CYPrecedence(17)
574
575 virtual CYExpression *Replace(CYContext &context);
576 void Output(CYOutput &out, CYFlags flags) const;
577
578 virtual CYFunctionParameter *Parameter() const;
579 };
580
581 struct CYParenthetical :
582 CYTarget
583 {
584 CYExpression *expression_;
585
586 CYParenthetical(CYExpression *expression) :
587 expression_(expression)
588 {
589 }
590
591 CYPrecedence(0)
592
593 virtual CYTarget *Replace(CYContext &context);
594 void Output(CYOutput &out, CYFlags flags) const;
595 };
596
597 struct CYBinding;
598
599 struct CYFunctionParameter :
600 CYNext<CYFunctionParameter>,
601 CYThing
602 {
603 CYBinding *binding_;
604
605 CYFunctionParameter(CYBinding *binding, CYFunctionParameter *next = NULL) :
606 CYNext<CYFunctionParameter>(next),
607 binding_(binding)
608 {
609 }
610
611 void Replace(CYContext &context, CYStatement *&statements);
612 void Output(CYOutput &out) const;
613 };
614
615 struct CYComprehension :
616 CYNext<CYComprehension>,
617 CYThing
618 {
619 CYComprehension(CYComprehension *next = NULL) :
620 CYNext<CYComprehension>(next)
621 {
622 }
623
624 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
625 CYFunctionParameter *Parameters(CYContext &context) const;
626 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
627 virtual void Output(CYOutput &out) const = 0;
628 };
629
630 struct CYForInComprehension :
631 CYComprehension
632 {
633 CYBinding *binding_;
634 CYExpression *iterable_;
635
636 CYForInComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
637 CYComprehension(next),
638 binding_(binding),
639 iterable_(iterable)
640 {
641 }
642
643 virtual CYFunctionParameter *Parameter(CYContext &context) const;
644 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
645 virtual void Output(CYOutput &out) const;
646 };
647
648 struct CYForOfComprehension :
649 CYComprehension
650 {
651 CYBinding *binding_;
652 CYExpression *iterable_;
653
654 CYForOfComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
655 CYComprehension(next),
656 binding_(binding),
657 iterable_(iterable)
658 {
659 }
660
661 virtual CYFunctionParameter *Parameter(CYContext &context) const;
662 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
663 virtual void Output(CYOutput &out) const;
664 };
665
666 struct CYIfComprehension :
667 CYComprehension
668 {
669 CYExpression *test_;
670
671 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
672 CYComprehension(next),
673 test_(test)
674 {
675 }
676
677 virtual CYFunctionParameter *Parameter(CYContext &context) const;
678 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
679 virtual void Output(CYOutput &out) const;
680 };
681
682 struct CYArrayComprehension :
683 CYTarget
684 {
685 CYExpression *expression_;
686 CYComprehension *comprehensions_;
687
688 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
689 expression_(expression),
690 comprehensions_(comprehensions)
691 {
692 }
693
694 CYPrecedence(0)
695
696 virtual CYTarget *Replace(CYContext &context);
697 virtual void Output(CYOutput &out, CYFlags flags) const;
698 };
699
700 struct CYLiteral :
701 CYTarget
702 {
703 CYLocation location_;
704
705 CYPrecedence(0)
706
707 virtual CYExpression *Primitive(CYContext &context) {
708 return this;
709 }
710 };
711
712 struct CYTrivial :
713 CYLiteral
714 {
715 virtual CYTarget *Replace(CYContext &context);
716 };
717
718 struct CYMagic :
719 CYTarget
720 {
721 CYPrecedence(0)
722 };
723
724 struct CYRange {
725 uint64_t lo_;
726 uint64_t hi_;
727
728 CYRange(uint64_t lo, uint64_t hi) :
729 lo_(lo), hi_(hi)
730 {
731 }
732
733 bool operator [](uint8_t value) const {
734 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
735 }
736
737 void operator()(uint8_t value) {
738 if (value >> 7)
739 return;
740 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
741 }
742 };
743
744 extern CYRange DigitRange_;
745 extern CYRange WordStartRange_;
746 extern CYRange WordEndRange_;
747
748 struct CYString :
749 CYTrivial,
750 CYPropertyName
751 {
752 const char *value_;
753 size_t size_;
754
755 CYString() :
756 value_(NULL),
757 size_(0)
758 {
759 }
760
761 CYString(const char *value) :
762 value_(value),
763 size_(strlen(value))
764 {
765 }
766
767 CYString(const char *value, size_t size) :
768 value_(value),
769 size_(size)
770 {
771 }
772
773 CYString(const CYWord *word) :
774 value_(word->Word()),
775 size_(strlen(value_))
776 {
777 }
778
779 const char *Value() const {
780 return value_;
781 }
782
783 virtual const char *Word() const;
784
785 virtual CYNumber *Number(CYContext &context);
786 virtual CYString *String(CYContext &context);
787
788 CYString *Concat(CYContext &out, CYString *rhs) const;
789 virtual void Output(CYOutput &out, CYFlags flags) const;
790
791 virtual CYExpression *PropertyName(CYContext &context);
792 virtual void PropertyName(CYOutput &out) const;
793 };
794
795 struct CYElementValue;
796
797 struct CYSpan :
798 CYNext<CYSpan>
799 {
800 CYExpression *expression_;
801 CYString *string_;
802
803 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
804 CYNext<CYSpan>(next),
805 expression_(expression),
806 string_(string)
807 {
808 }
809
810 CYElementValue *Replace(CYContext &context);
811 };
812
813 struct CYTemplate :
814 CYTarget
815 {
816 CYString *string_;
817 CYSpan *spans_;
818
819 CYTemplate(CYString *string, CYSpan *spans) :
820 string_(string),
821 spans_(spans)
822 {
823 }
824
825 CYPrecedence(0)
826
827 virtual CYTarget *Replace(CYContext &context);
828 virtual void Output(CYOutput &out, CYFlags flags) const;
829 };
830
831 struct CYNumber :
832 CYTrivial,
833 CYPropertyName
834 {
835 double value_;
836
837 CYNumber(double value) :
838 value_(value)
839 {
840 }
841
842 double Value() const {
843 return value_;
844 }
845
846 virtual CYNumber *Number(CYContext &context);
847 virtual CYString *String(CYContext &context);
848
849 virtual void Output(CYOutput &out, CYFlags flags) const;
850
851 virtual CYExpression *PropertyName(CYContext &context);
852 virtual void PropertyName(CYOutput &out) const;
853 };
854
855 struct CYComputed :
856 CYPropertyName
857 {
858 CYExpression *expression_;
859
860 CYComputed(CYExpression *expression) :
861 expression_(expression)
862 {
863 }
864
865 virtual bool Computed() const {
866 return true;
867 }
868
869 virtual CYExpression *PropertyName(CYContext &context);
870 virtual void PropertyName(CYOutput &out) const;
871 };
872
873 struct CYRegEx :
874 CYTrivial
875 {
876 const char *value_;
877 size_t size_;
878
879 CYRegEx(const char *value, size_t size) :
880 value_(value),
881 size_(size)
882 {
883 }
884
885 const char *Value() const {
886 return value_;
887 }
888
889 virtual void Output(CYOutput &out, CYFlags flags) const;
890 };
891
892 struct CYNull :
893 CYTrivial
894 {
895 virtual CYNumber *Number(CYContext &context);
896 virtual CYString *String(CYContext &context);
897
898 virtual void Output(CYOutput &out, CYFlags flags) const;
899 };
900
901 struct CYThis :
902 CYMagic
903 {
904 virtual CYTarget *Replace(CYContext &context);
905 virtual void Output(CYOutput &out, CYFlags flags) const;
906 };
907
908 struct CYBoolean :
909 CYTrivial
910 {
911 CYPrecedence(4)
912
913 virtual bool RightHand() const {
914 return true;
915 }
916
917 virtual bool Value() const = 0;
918 virtual void Output(CYOutput &out, CYFlags flags) const;
919 };
920
921 struct CYFalse :
922 CYBoolean
923 {
924 virtual bool Value() const {
925 return false;
926 }
927
928 virtual CYNumber *Number(CYContext &context);
929 virtual CYString *String(CYContext &context);
930 };
931
932 struct CYTrue :
933 CYBoolean
934 {
935 virtual bool Value() const {
936 return true;
937 }
938
939 virtual CYNumber *Number(CYContext &context);
940 virtual CYString *String(CYContext &context);
941 };
942
943 struct CYVariable :
944 CYTarget
945 {
946 CYIdentifier *name_;
947
948 CYVariable(CYIdentifier *name) :
949 name_(name)
950 {
951 }
952
953 CYVariable(const char *name) :
954 name_(new($pool) CYIdentifier(name))
955 {
956 }
957
958 CYPrecedence(0)
959
960 virtual bool Eval() const {
961 return strcmp(name_->Word(), "eval") == 0;
962 }
963
964 virtual CYTarget *Replace(CYContext &context);
965 virtual void Output(CYOutput &out, CYFlags flags) const;
966
967 virtual CYFunctionParameter *Parameter() const;
968 };
969
970 struct CYSymbol :
971 CYTarget
972 {
973 const char *name_;
974
975 CYSymbol(const char *name) :
976 name_(name)
977 {
978 }
979
980 CYPrecedence(0)
981
982 virtual CYTarget *Replace(CYContext &context);
983 virtual void Output(CYOutput &out, CYFlags flags) const;
984 };
985
986 struct CYPrefix :
987 CYExpression
988 {
989 CYExpression *rhs_;
990
991 CYPrefix(CYExpression *rhs) :
992 rhs_(rhs)
993 {
994 }
995
996 virtual bool Alphabetic() const = 0;
997 virtual const char *Operator() const = 0;
998
999 CYPrecedence(4)
1000
1001 virtual CYExpression *Replace(CYContext &context);
1002 virtual void Output(CYOutput &out, CYFlags flags) const;
1003 };
1004
1005 struct CYInfix :
1006 CYExpression
1007 {
1008 CYExpression *lhs_;
1009 CYExpression *rhs_;
1010
1011 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1012 lhs_(lhs),
1013 rhs_(rhs)
1014 {
1015 }
1016
1017 void SetLeft(CYExpression *lhs) {
1018 lhs_ = lhs;
1019 }
1020
1021 virtual bool Alphabetic() const = 0;
1022 virtual const char *Operator() const = 0;
1023
1024 virtual CYExpression *Replace(CYContext &context);
1025 virtual void Output(CYOutput &out, CYFlags flags) const;
1026 };
1027
1028 struct CYPostfix :
1029 CYExpression
1030 {
1031 CYExpression *lhs_;
1032
1033 CYPostfix(CYExpression *lhs) :
1034 lhs_(lhs)
1035 {
1036 }
1037
1038 virtual const char *Operator() const = 0;
1039
1040 CYPrecedence(3)
1041
1042 virtual CYExpression *Replace(CYContext &context);
1043 virtual void Output(CYOutput &out, CYFlags flags) const;
1044 };
1045
1046 struct CYAssignment :
1047 CYExpression
1048 {
1049 CYTarget *lhs_;
1050 CYExpression *rhs_;
1051
1052 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1053 lhs_(lhs),
1054 rhs_(rhs)
1055 {
1056 }
1057
1058 void SetRight(CYExpression *rhs) {
1059 rhs_ = rhs;
1060 }
1061
1062 virtual const char *Operator() const = 0;
1063
1064 CYPrecedence(16)
1065
1066 virtual CYExpression *Replace(CYContext &context);
1067 virtual void Output(CYOutput &out, CYFlags flags) const;
1068 };
1069
1070 struct CYArgument :
1071 CYNext<CYArgument>,
1072 CYThing
1073 {
1074 CYWord *name_;
1075 CYExpression *value_;
1076
1077 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1078 CYNext<CYArgument>(next),
1079 name_(NULL),
1080 value_(value)
1081 {
1082 }
1083
1084 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1085 CYNext<CYArgument>(next),
1086 name_(name),
1087 value_(value)
1088 {
1089 }
1090
1091 CYArgument *Replace(CYContext &context);
1092 void Output(CYOutput &out) const;
1093 };
1094
1095 struct CYClause :
1096 CYThing,
1097 CYNext<CYClause>
1098 {
1099 CYExpression *value_;
1100 CYStatement *code_;
1101
1102 CYClause(CYExpression *value, CYStatement *code) :
1103 value_(value),
1104 code_(code)
1105 {
1106 }
1107
1108 void Replace(CYContext &context);
1109 virtual void Output(CYOutput &out) const;
1110 };
1111
1112 struct CYElement :
1113 CYNext<CYElement>,
1114 CYThing
1115 {
1116 CYElement(CYElement *next) :
1117 CYNext<CYElement>(next)
1118 {
1119 }
1120
1121 virtual bool Elision() const = 0;
1122
1123 virtual void Replace(CYContext &context) = 0;
1124 };
1125
1126 struct CYElementValue :
1127 CYElement
1128 {
1129 CYExpression *value_;
1130
1131 CYElementValue(CYExpression *value, CYElement *next = NULL) :
1132 CYElement(next),
1133 value_(value)
1134 {
1135 }
1136
1137 virtual bool Elision() const {
1138 return value_ == NULL;
1139 }
1140
1141 virtual void Replace(CYContext &context);
1142 virtual void Output(CYOutput &out) const;
1143 };
1144
1145 struct CYElementSpread :
1146 CYElement
1147 {
1148 CYExpression *value_;
1149
1150 CYElementSpread(CYExpression *value, CYElement *next = NULL) :
1151 CYElement(next),
1152 value_(value)
1153 {
1154 }
1155
1156 virtual bool Elision() const {
1157 return false;
1158 }
1159
1160 virtual void Replace(CYContext &context);
1161 virtual void Output(CYOutput &out) const;
1162 };
1163
1164 struct CYArray :
1165 CYLiteral
1166 {
1167 CYElement *elements_;
1168
1169 CYArray(CYElement *elements = NULL) :
1170 elements_(elements)
1171 {
1172 }
1173
1174 virtual CYTarget *Replace(CYContext &context);
1175 virtual void Output(CYOutput &out, CYFlags flags) const;
1176 };
1177
1178 struct CYBinding {
1179 CYIdentifier *identifier_;
1180 CYExpression *initializer_;
1181
1182 CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) :
1183 identifier_(identifier),
1184 initializer_(initializer)
1185 {
1186 }
1187
1188 CYTarget *Target(CYContext &context);
1189
1190 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1191 virtual void Output(CYOutput &out, CYFlags flags) const;
1192 };
1193
1194 struct CYForLexical :
1195 CYForInInitializer
1196 {
1197 bool constant_;
1198 CYBinding *binding_;
1199
1200 CYForLexical(bool constant, CYBinding *binding) :
1201 constant_(constant),
1202 binding_(binding)
1203 {
1204 }
1205
1206 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1207
1208 virtual CYTarget *Replace(CYContext &context);
1209 virtual void Output(CYOutput &out, CYFlags flags) const;
1210 };
1211
1212 struct CYForVariable :
1213 CYForInInitializer
1214 {
1215 CYBinding *binding_;
1216
1217 CYForVariable(CYBinding *binding) :
1218 binding_(binding)
1219 {
1220 }
1221
1222 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1223
1224 virtual CYTarget *Replace(CYContext &context);
1225 virtual void Output(CYOutput &out, CYFlags flags) const;
1226 };
1227
1228 struct CYBindings :
1229 CYNext<CYBindings>,
1230 CYThing
1231 {
1232 CYBinding *binding_;
1233
1234 CYBindings(CYBinding *binding, CYBindings *next = NULL) :
1235 CYNext<CYBindings>(next),
1236 binding_(binding)
1237 {
1238 }
1239
1240 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1241
1242 CYArgument *Argument(CYContext &context);
1243 CYFunctionParameter *Parameter(CYContext &context);
1244
1245 virtual void Output(CYOutput &out) const;
1246 virtual void Output(CYOutput &out, CYFlags flags) const;
1247 };
1248
1249 struct CYVar :
1250 CYForInitializer
1251 {
1252 CYBindings *bindings_;
1253
1254 CYVar(CYBindings *bindings) :
1255 bindings_(bindings)
1256 {
1257 }
1258
1259 CYCompact(None)
1260
1261 virtual CYForInitializer *Replace(CYContext &context);
1262 virtual void Output(CYOutput &out, CYFlags flags) const;
1263 };
1264
1265 struct CYLexical :
1266 CYForInitializer
1267 {
1268 bool constant_;
1269 CYBindings *bindings_;
1270
1271 CYLexical(bool constant, CYBindings *bindings) :
1272 constant_(constant),
1273 bindings_(bindings)
1274 {
1275 }
1276
1277 CYCompact(None)
1278
1279 virtual CYForInitializer *Replace(CYContext &context);
1280 virtual void Output(CYOutput &out, CYFlags flags) const;
1281 };
1282
1283 struct CYBuilder {
1284 CYList<CYBindings> bindings_;
1285 CYList<CYStatement> statements_;
1286
1287 operator bool() const {
1288 return statements_ != NULL;
1289 }
1290 };
1291
1292 struct CYProperty :
1293 CYNext<CYProperty>,
1294 CYThing
1295 {
1296 CYPropertyName *name_;
1297
1298 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1299 CYNext<CYProperty>(next),
1300 name_(name)
1301 {
1302 }
1303
1304 virtual bool Update() const;
1305
1306 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1307 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1308
1309 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1310
1311 virtual void Replace(CYContext &context) = 0;
1312 virtual void Output(CYOutput &out) const;
1313 };
1314
1315 struct CYPropertyValue :
1316 CYProperty
1317 {
1318 CYExpression *value_;
1319
1320 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1321 CYProperty(name, next),
1322 value_(value)
1323 {
1324 }
1325
1326 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1327 virtual void Replace(CYContext &context);
1328 virtual void Output(CYOutput &out) const;
1329 };
1330
1331 struct CYFor :
1332 CYStatement
1333 {
1334 CYForInitializer *initializer_;
1335 CYExpression *test_;
1336 CYExpression *increment_;
1337 CYStatement *code_;
1338
1339 CYFor(CYForInitializer *initializer, CYExpression *test, CYExpression *increment, CYStatement *code) :
1340 initializer_(initializer),
1341 test_(test),
1342 increment_(increment),
1343 code_(code)
1344 {
1345 }
1346
1347 CYCompact(Long)
1348
1349 virtual CYStatement *Replace(CYContext &context);
1350 virtual void Output(CYOutput &out, CYFlags flags) const;
1351 };
1352
1353 struct CYForIn :
1354 CYStatement
1355 {
1356 CYForInInitializer *initializer_;
1357 CYExpression *iterable_;
1358 CYStatement *code_;
1359
1360 CYForIn(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1361 initializer_(initializer),
1362 iterable_(iterable),
1363 code_(code)
1364 {
1365 }
1366
1367 CYCompact(Long)
1368
1369 virtual CYStatement *Replace(CYContext &context);
1370 virtual void Output(CYOutput &out, CYFlags flags) const;
1371 };
1372
1373 struct CYForInitialized :
1374 CYStatement
1375 {
1376 CYBinding *binding_;
1377 CYExpression *iterable_;
1378 CYStatement *code_;
1379
1380 CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) :
1381 binding_(binding),
1382 iterable_(iterable),
1383 code_(code)
1384 {
1385 }
1386
1387 CYCompact(Long)
1388
1389 virtual CYStatement *Replace(CYContext &context);
1390 virtual void Output(CYOutput &out, CYFlags flags) const;
1391 };
1392
1393 struct CYForOf :
1394 CYStatement
1395 {
1396 CYForInInitializer *initializer_;
1397 CYExpression *iterable_;
1398 CYStatement *code_;
1399
1400 CYForOf(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1401 initializer_(initializer),
1402 iterable_(iterable),
1403 code_(code)
1404 {
1405 }
1406
1407 CYCompact(Long)
1408
1409 virtual CYStatement *Replace(CYContext &context);
1410 virtual void Output(CYOutput &out, CYFlags flags) const;
1411 };
1412
1413 struct CYObject :
1414 CYLiteral
1415 {
1416 CYProperty *properties_;
1417
1418 CYObject(CYProperty *properties = NULL) :
1419 properties_(properties)
1420 {
1421 }
1422
1423 CYTarget *Replace(CYContext &context, CYTarget *seed);
1424
1425 virtual CYTarget *Replace(CYContext &context);
1426 void Output(CYOutput &out, CYFlags flags) const;
1427 };
1428
1429 struct CYMember :
1430 CYTarget
1431 {
1432 CYExpression *object_;
1433 CYExpression *property_;
1434
1435 CYMember(CYExpression *object, CYExpression *property) :
1436 object_(object),
1437 property_(property)
1438 {
1439 }
1440
1441 void SetLeft(CYExpression *object) {
1442 object_ = object;
1443 }
1444 };
1445
1446 struct CYDirectMember :
1447 CYMember
1448 {
1449 CYDirectMember(CYExpression *object, CYExpression *property) :
1450 CYMember(object, property)
1451 {
1452 }
1453
1454 CYPrecedence(1)
1455
1456 virtual CYTarget *Replace(CYContext &context);
1457 virtual void Output(CYOutput &out, CYFlags flags) const;
1458 };
1459
1460 struct CYIndirectMember :
1461 CYMember
1462 {
1463 CYIndirectMember(CYExpression *object, CYExpression *property) :
1464 CYMember(object, property)
1465 {
1466 }
1467
1468 CYPrecedence(1)
1469
1470 virtual CYTarget *Replace(CYContext &context);
1471 virtual void Output(CYOutput &out, CYFlags flags) const;
1472 };
1473
1474 struct CYResolveMember :
1475 CYMember
1476 {
1477 CYResolveMember(CYExpression *object, CYExpression *property) :
1478 CYMember(object, property)
1479 {
1480 }
1481
1482 CYPrecedence(1)
1483
1484 virtual CYTarget *Replace(CYContext &context);
1485 virtual void Output(CYOutput &out, CYFlags flags) const;
1486 };
1487
1488 struct CYSubscriptMember :
1489 CYMember
1490 {
1491 CYSubscriptMember(CYExpression *object, CYExpression *property) :
1492 CYMember(object, property)
1493 {
1494 }
1495
1496 CYPrecedence(1)
1497
1498 virtual CYTarget *Replace(CYContext &context);
1499 virtual void Output(CYOutput &out, CYFlags flags) const;
1500 };
1501
1502 namespace cy {
1503 namespace Syntax {
1504
1505 struct New :
1506 CYTarget
1507 {
1508 CYExpression *constructor_;
1509 CYArgument *arguments_;
1510
1511 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1512 constructor_(constructor),
1513 arguments_(arguments)
1514 {
1515 }
1516
1517 virtual int Precedence() const {
1518 return arguments_ == NULL ? 2 : 1;
1519 }
1520
1521 virtual bool IsNew() const {
1522 return true;
1523 }
1524
1525 virtual CYTarget *Replace(CYContext &context);
1526 virtual void Output(CYOutput &out, CYFlags flags) const;
1527
1528 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1529 };
1530
1531 } }
1532
1533 struct CYApply :
1534 CYTarget
1535 {
1536 CYArgument *arguments_;
1537
1538 CYApply(CYArgument *arguments = NULL) :
1539 arguments_(arguments)
1540 {
1541 }
1542
1543 CYPrecedence(1)
1544
1545 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1546 };
1547
1548 struct CYCall :
1549 CYApply
1550 {
1551 CYExpression *function_;
1552
1553 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1554 CYApply(arguments),
1555 function_(function)
1556 {
1557 }
1558
1559 virtual void Output(CYOutput &out, CYFlags flags) const;
1560 virtual CYTarget *Replace(CYContext &context);
1561 };
1562
1563 struct CYEval :
1564 CYApply
1565 {
1566 CYEval(CYArgument *arguments) :
1567 CYApply(arguments)
1568 {
1569 }
1570
1571 virtual void Output(CYOutput &out, CYFlags flags) const;
1572 virtual CYTarget *Replace(CYContext &context);
1573 };
1574
1575 struct CYRubyProc;
1576
1577 struct CYBraced :
1578 CYTarget
1579 {
1580 CYTarget *lhs_;
1581
1582 CYBraced(CYTarget *lhs = NULL) :
1583 lhs_(lhs)
1584 {
1585 }
1586
1587 CYPrecedence(1)
1588
1589 void SetLeft(CYTarget *lhs) {
1590 lhs_ = lhs;
1591 }
1592 };
1593
1594 struct CYRubyBlock :
1595 CYBraced
1596 {
1597 CYRubyProc *proc_;
1598
1599 CYRubyBlock(CYTarget *lhs, CYRubyProc *proc) :
1600 CYBraced(lhs),
1601 proc_(proc)
1602 {
1603 }
1604
1605 virtual CYTarget *Replace(CYContext &context);
1606 virtual void Output(CYOutput &out, CYFlags flags) const;
1607
1608 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1609 };
1610
1611 struct CYExtend :
1612 CYBraced
1613 {
1614 CYObject object_;
1615
1616 CYExtend(CYTarget *lhs, CYProperty *properties = NULL) :
1617 CYBraced(lhs),
1618 object_(properties)
1619 {
1620 }
1621
1622 virtual CYTarget *Replace(CYContext &context);
1623 virtual void Output(CYOutput &out, CYFlags flags) const;
1624 };
1625
1626 struct CYIf :
1627 CYStatement
1628 {
1629 CYExpression *test_;
1630 CYStatement *true_;
1631 CYStatement *false_;
1632
1633 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1634 test_(test),
1635 true_(_true),
1636 false_(_false)
1637 {
1638 }
1639
1640 CYCompact(Long)
1641
1642 virtual CYStatement *Replace(CYContext &context);
1643 virtual void Output(CYOutput &out, CYFlags flags) const;
1644
1645 virtual CYStatement *Return();
1646 };
1647
1648 struct CYDoWhile :
1649 CYStatement
1650 {
1651 CYExpression *test_;
1652 CYStatement *code_;
1653
1654 CYDoWhile(CYExpression *test, CYStatement *code) :
1655 test_(test),
1656 code_(code)
1657 {
1658 }
1659
1660 CYCompact(None)
1661
1662 virtual CYStatement *Replace(CYContext &context);
1663 virtual void Output(CYOutput &out, CYFlags flags) const;
1664 };
1665
1666 struct CYWhile :
1667 CYStatement
1668 {
1669 CYExpression *test_;
1670 CYStatement *code_;
1671
1672 CYWhile(CYExpression *test, CYStatement *code) :
1673 test_(test),
1674 code_(code)
1675 {
1676 }
1677
1678 CYCompact(Long)
1679
1680 virtual CYStatement *Replace(CYContext &context);
1681 virtual void Output(CYOutput &out, CYFlags flags) const;
1682 };
1683
1684 struct CYFunction {
1685 CYFunctionParameter *parameters_;
1686 CYStatement *code_;
1687
1688 CYNonLocal *nonlocal_;
1689 bool implicit_;
1690 CYThisScope this_;
1691 CYIdentifier *super_;
1692
1693 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1694 parameters_(parameters),
1695 code_(code),
1696 nonlocal_(NULL),
1697 implicit_(false),
1698 super_(NULL)
1699 {
1700 }
1701
1702 void Replace(CYContext &context);
1703 void Output(CYOutput &out) const;
1704 };
1705
1706 struct CYFunctionExpression :
1707 CYFunction,
1708 CYTarget
1709 {
1710 CYIdentifier *name_;
1711
1712 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1713 CYFunction(parameters, code),
1714 name_(name)
1715 {
1716 }
1717
1718 CYPrecedence(0)
1719
1720 CYTarget *Replace(CYContext &context) override;
1721 virtual void Output(CYOutput &out, CYFlags flags) const;
1722 };
1723
1724 struct CYFatArrow :
1725 CYFunction,
1726 CYExpression
1727 {
1728 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1729 CYFunction(parameters, code)
1730 {
1731 }
1732
1733 CYPrecedence(0)
1734
1735 CYExpression *Replace(CYContext &context) override;
1736 virtual void Output(CYOutput &out, CYFlags flags) const;
1737 };
1738
1739 struct CYRubyProc :
1740 CYFunction,
1741 CYTarget
1742 {
1743 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1744 CYFunction(parameters, code)
1745 {
1746 }
1747
1748 CYPrecedence(0)
1749
1750 CYTarget *Replace(CYContext &context) override;
1751 virtual void Output(CYOutput &out, CYFlags flags) const;
1752 };
1753
1754 struct CYFunctionStatement :
1755 CYFunction,
1756 CYStatement
1757 {
1758 CYIdentifier *name_;
1759
1760 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1761 CYFunction(parameters, code),
1762 name_(name)
1763 {
1764 }
1765
1766 CYCompact(None)
1767
1768 CYStatement *Replace(CYContext &context) override;
1769 virtual void Output(CYOutput &out, CYFlags flags) const;
1770 };
1771
1772 struct CYPropertyMethod;
1773
1774 struct CYMethod :
1775 CYFunction,
1776 CYProperty
1777 {
1778 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1779 CYFunction(parameters, code),
1780 CYProperty(name, next)
1781 {
1782 }
1783
1784 virtual CYFunctionExpression *Constructor();
1785
1786 using CYProperty::Replace;
1787 virtual void Replace(CYContext &context);
1788 };
1789
1790 struct CYPropertyGetter :
1791 CYMethod
1792 {
1793 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1794 CYMethod(name, NULL, code, next)
1795 {
1796 }
1797
1798 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1799 virtual void Output(CYOutput &out) const;
1800 };
1801
1802 struct CYPropertySetter :
1803 CYMethod
1804 {
1805 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1806 CYMethod(name, parameters, code, next)
1807 {
1808 }
1809
1810 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1811 virtual void Output(CYOutput &out) const;
1812 };
1813
1814 struct CYPropertyMethod :
1815 CYMethod
1816 {
1817 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1818 CYMethod(name, parameters, code, next)
1819 {
1820 }
1821
1822 bool Update() const override;
1823
1824 virtual CYFunctionExpression *Constructor();
1825
1826 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1827 virtual void Output(CYOutput &out) const;
1828 };
1829
1830 struct CYClassTail :
1831 CYThing
1832 {
1833 CYExpression *extends_;
1834
1835 CYFunctionExpression *constructor_;
1836 CYList<CYProperty> instance_;
1837 CYList<CYProperty> static_;
1838
1839 CYClassTail(CYExpression *extends) :
1840 extends_(extends),
1841 constructor_(NULL)
1842 {
1843 }
1844
1845 void Output(CYOutput &out) const;
1846 };
1847
1848 struct CYClassExpression :
1849 CYTarget
1850 {
1851 CYIdentifier *name_;
1852 CYClassTail *tail_;
1853
1854 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1855 name_(name),
1856 tail_(tail)
1857 {
1858 }
1859
1860 CYPrecedence(0)
1861
1862 CYTarget *Replace(CYContext &context) override;
1863 virtual void Output(CYOutput &out, CYFlags flags) const;
1864 };
1865
1866 struct CYClassStatement :
1867 CYStatement
1868 {
1869 CYIdentifier *name_;
1870 CYClassTail *tail_;
1871
1872 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1873 name_(name),
1874 tail_(tail)
1875 {
1876 }
1877
1878 CYCompact(Long)
1879
1880 CYStatement *Replace(CYContext &context) override;
1881 virtual void Output(CYOutput &out, CYFlags flags) const;
1882 };
1883
1884 struct CYSuperCall :
1885 CYTarget
1886 {
1887 CYArgument *arguments_;
1888
1889 CYSuperCall(CYArgument *arguments) :
1890 arguments_(arguments)
1891 {
1892 }
1893
1894 CYPrecedence(2)
1895
1896 CYTarget *Replace(CYContext &context) override;
1897 virtual void Output(CYOutput &out, CYFlags flags) const;
1898 };
1899
1900 struct CYSuperAccess :
1901 CYTarget
1902 {
1903 CYExpression *property_;
1904
1905 CYSuperAccess(CYExpression *property) :
1906 property_(property)
1907 {
1908 }
1909
1910 CYPrecedence(1)
1911
1912 CYTarget *Replace(CYContext &context) override;
1913 virtual void Output(CYOutput &out, CYFlags flags) const;
1914 };
1915
1916 struct CYExpress :
1917 CYForInitializer
1918 {
1919 CYExpression *expression_;
1920
1921 CYExpress(CYExpression *expression) :
1922 expression_(expression)
1923 {
1924 if (expression_ == NULL)
1925 throw;
1926 }
1927
1928 CYCompact(None)
1929
1930 CYForInitializer *Replace(CYContext &context) override;
1931 virtual void Output(CYOutput &out, CYFlags flags) const;
1932
1933 virtual CYStatement *Return();
1934 };
1935
1936 struct CYContinue :
1937 CYStatement
1938 {
1939 CYIdentifier *label_;
1940
1941 CYContinue(CYIdentifier *label) :
1942 label_(label)
1943 {
1944 }
1945
1946 CYCompact(Short)
1947
1948 CYStatement *Replace(CYContext &context) override;
1949 virtual void Output(CYOutput &out, CYFlags flags) const;
1950 };
1951
1952 struct CYBreak :
1953 CYStatement
1954 {
1955 CYIdentifier *label_;
1956
1957 CYBreak(CYIdentifier *label) :
1958 label_(label)
1959 {
1960 }
1961
1962 CYCompact(Short)
1963
1964 CYStatement *Replace(CYContext &context) override;
1965 virtual void Output(CYOutput &out, CYFlags flags) const;
1966 };
1967
1968 struct CYReturn :
1969 CYStatement
1970 {
1971 CYExpression *value_;
1972
1973 CYReturn(CYExpression *value) :
1974 value_(value)
1975 {
1976 }
1977
1978 CYCompact(None)
1979
1980 CYStatement *Replace(CYContext &context) override;
1981 virtual void Output(CYOutput &out, CYFlags flags) const;
1982 };
1983
1984 struct CYYieldGenerator :
1985 CYExpression
1986 {
1987 CYExpression *value_;
1988
1989 CYYieldGenerator(CYExpression *value) :
1990 value_(value)
1991 {
1992 }
1993
1994 CYPrecedence(0)
1995
1996 CYExpression *Replace(CYContext &context) override;
1997 virtual void Output(CYOutput &out, CYFlags flags) const;
1998 };
1999
2000 struct CYYieldValue :
2001 CYExpression
2002 {
2003 CYExpression *value_;
2004
2005 CYYieldValue(CYExpression *value) :
2006 value_(value)
2007 {
2008 }
2009
2010 CYPrecedence(0)
2011
2012 virtual CYExpression *Replace(CYContext &context);
2013 virtual void Output(CYOutput &out, CYFlags flags) const;
2014 };
2015
2016 struct CYEmpty :
2017 CYForInitializer
2018 {
2019 CYCompact(Short)
2020
2021 virtual CYForInitializer *Replace(CYContext &context);
2022 virtual void Output(CYOutput &out, CYFlags flags) const;
2023 };
2024
2025 struct CYFinally :
2026 CYThing
2027 {
2028 CYStatement *code_;
2029
2030 CYFinally(CYStatement *code) :
2031 code_(code)
2032 {
2033 }
2034
2035 void Replace(CYContext &context);
2036 virtual void Output(CYOutput &out) const;
2037 };
2038
2039 struct CYTypeSpecifier :
2040 CYThing
2041 {
2042 virtual CYTarget *Replace(CYContext &context) = 0;
2043 };
2044
2045 struct CYTypeError :
2046 CYTypeSpecifier
2047 {
2048 CYTypeError() {
2049 }
2050
2051 virtual CYTarget *Replace(CYContext &context);
2052 virtual void Output(CYOutput &out) const;
2053 };
2054
2055 enum CYTypeSigning {
2056 CYTypeNeutral,
2057 CYTypeSigned,
2058 CYTypeUnsigned,
2059 };
2060
2061 struct CYTypeCharacter :
2062 CYTypeSpecifier
2063 {
2064 CYTypeSigning signing_;
2065
2066 CYTypeCharacter(CYTypeSigning signing) :
2067 signing_(signing)
2068 {
2069 }
2070
2071 virtual CYTarget *Replace(CYContext &context);
2072 virtual void Output(CYOutput &out) const;
2073 };
2074
2075 struct CYTypeIntegral :
2076 CYTypeSpecifier
2077 {
2078 CYTypeSigning signing_;
2079 int length_;
2080
2081 CYTypeIntegral(CYTypeSigning signing, int length = 1) :
2082 signing_(signing),
2083 length_(length)
2084 {
2085 }
2086
2087 CYTypeIntegral *Long() {
2088 if (length_ != 1 && length_ != 2)
2089 return NULL;
2090 ++length_;
2091 return this;
2092 }
2093
2094 CYTypeIntegral *Short() {
2095 if (length_ != 1)
2096 return NULL;
2097 --length_;
2098 return this;
2099 }
2100
2101 CYTypeIntegral *Signed() {
2102 if (signing_ != CYTypeNeutral)
2103 return NULL;
2104 signing_ = CYTypeSigned;
2105 return this;
2106 }
2107
2108 CYTypeIntegral *Unsigned() {
2109 if (signing_ != CYTypeNeutral)
2110 return NULL;
2111 signing_ = CYTypeUnsigned;
2112 return this;
2113 }
2114
2115 virtual CYTarget *Replace(CYContext &context);
2116 virtual void Output(CYOutput &out) const;
2117 };
2118
2119 struct CYTypeVoid :
2120 CYTypeSpecifier
2121 {
2122 CYTypeVoid() {
2123 }
2124
2125 virtual CYTarget *Replace(CYContext &context);
2126 virtual void Output(CYOutput &out) const;
2127 };
2128
2129 struct CYTypeReference :
2130 CYTypeSpecifier
2131 {
2132 CYIdentifier *name_;
2133
2134 CYTypeReference(CYIdentifier *name) :
2135 name_(name)
2136 {
2137 }
2138
2139 virtual CYTarget *Replace(CYContext &context);
2140 virtual void Output(CYOutput &out) const;
2141 };
2142
2143 struct CYTypeVariable :
2144 CYTypeSpecifier
2145 {
2146 CYIdentifier *name_;
2147
2148 CYTypeVariable(CYIdentifier *name) :
2149 name_(name)
2150 {
2151 }
2152
2153 CYTypeVariable(const char *name) :
2154 name_(new($pool) CYIdentifier(name))
2155 {
2156 }
2157
2158 virtual CYTarget *Replace(CYContext &context);
2159 virtual void Output(CYOutput &out) const;
2160 };
2161
2162 struct CYTypeFunctionWith;
2163
2164 struct CYTypeModifier :
2165 CYNext<CYTypeModifier>
2166 {
2167 CYTypeModifier(CYTypeModifier *next) :
2168 CYNext<CYTypeModifier>(next)
2169 {
2170 }
2171
2172 virtual int Precedence() const = 0;
2173
2174 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2175 CYTarget *Replace(CYContext &context, CYTarget *type);
2176
2177 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2178 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2179
2180 virtual CYTypeFunctionWith *Function() { return NULL; }
2181 };
2182
2183 struct CYTypeArrayOf :
2184 CYTypeModifier
2185 {
2186 CYExpression *size_;
2187
2188 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2189 CYTypeModifier(next),
2190 size_(size)
2191 {
2192 }
2193
2194 CYPrecedence(1)
2195
2196 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2197 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2198 };
2199
2200 struct CYTypeConstant :
2201 CYTypeModifier
2202 {
2203 CYTypeConstant(CYTypeModifier *next = NULL) :
2204 CYTypeModifier(next)
2205 {
2206 }
2207
2208 CYPrecedence(0)
2209
2210 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2211 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2212 };
2213
2214 struct CYTypePointerTo :
2215 CYTypeModifier
2216 {
2217 CYTypePointerTo(CYTypeModifier *next = NULL) :
2218 CYTypeModifier(next)
2219 {
2220 }
2221
2222 CYPrecedence(0)
2223
2224 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2225 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2226 };
2227
2228 struct CYTypeVolatile :
2229 CYTypeModifier
2230 {
2231 CYTypeVolatile(CYTypeModifier *next = NULL) :
2232 CYTypeModifier(next)
2233 {
2234 }
2235
2236 CYPrecedence(0)
2237
2238 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2239 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2240 };
2241
2242 struct CYTypedIdentifier :
2243 CYNext<CYTypedIdentifier>,
2244 CYThing
2245 {
2246 CYLocation location_;
2247 CYIdentifier *identifier_;
2248 CYTypeSpecifier *specifier_;
2249 CYTypeModifier *modifier_;
2250
2251 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2252 location_(location),
2253 identifier_(identifier),
2254 specifier_(NULL),
2255 modifier_(NULL)
2256 {
2257 }
2258
2259 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2260 identifier_(NULL),
2261 specifier_(specifier),
2262 modifier_(modifier)
2263 {
2264 }
2265
2266 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2267 CYSetLast(modifier_) = modifier;
2268 return this;
2269 }
2270
2271 virtual CYTarget *Replace(CYContext &context);
2272 virtual void Output(CYOutput &out) const;
2273
2274 CYTypeFunctionWith *Function();
2275 };
2276
2277 struct CYEncodedType :
2278 CYTarget
2279 {
2280 CYTypedIdentifier *typed_;
2281
2282 CYEncodedType(CYTypedIdentifier *typed) :
2283 typed_(typed)
2284 {
2285 }
2286
2287 CYPrecedence(1)
2288
2289 virtual CYTarget *Replace(CYContext &context);
2290 virtual void Output(CYOutput &out, CYFlags flags) const;
2291 };
2292
2293 struct CYTypedParameter :
2294 CYNext<CYTypedParameter>,
2295 CYThing
2296 {
2297 CYTypedIdentifier *typed_;
2298
2299 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2300 CYNext<CYTypedParameter>(next),
2301 typed_(typed)
2302 {
2303 }
2304
2305 CYArgument *Argument(CYContext &context);
2306 CYFunctionParameter *Parameters(CYContext &context);
2307 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2308
2309 virtual void Output(CYOutput &out) const;
2310 };
2311
2312 struct CYTypedFormal {
2313 bool variadic_;
2314 CYTypedParameter *parameters_;
2315
2316 CYTypedFormal(bool variadic) :
2317 variadic_(variadic),
2318 parameters_(NULL)
2319 {
2320 }
2321 };
2322
2323 struct CYLambda :
2324 CYTarget
2325 {
2326 CYTypedIdentifier *typed_;
2327 CYTypedParameter *parameters_;
2328 CYStatement *code_;
2329
2330 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2331 typed_(typed),
2332 parameters_(parameters),
2333 code_(code)
2334 {
2335 }
2336
2337 CYPrecedence(1)
2338
2339 virtual CYTarget *Replace(CYContext &context);
2340 virtual void Output(CYOutput &out, CYFlags flags) const;
2341 };
2342
2343 struct CYModule :
2344 CYNext<CYModule>,
2345 CYThing
2346 {
2347 CYWord *part_;
2348
2349 CYModule(CYWord *part, CYModule *next = NULL) :
2350 CYNext<CYModule>(next),
2351 part_(part)
2352 {
2353 }
2354
2355 CYString *Replace(CYContext &context, const char *separator) const;
2356 void Output(CYOutput &out) const;
2357 };
2358
2359 struct CYImport :
2360 CYStatement
2361 {
2362 CYModule *module_;
2363
2364 CYImport(CYModule *module) :
2365 module_(module)
2366 {
2367 }
2368
2369 CYCompact(None)
2370
2371 virtual CYStatement *Replace(CYContext &context);
2372 virtual void Output(CYOutput &out, CYFlags flags) const;
2373 };
2374
2375 struct CYImportSpecifier :
2376 CYNext<CYImportSpecifier>
2377 {
2378 CYWord *name_;
2379 CYIdentifier *binding_;
2380
2381 CYImportSpecifier(CYWord *name, CYIdentifier *binding) :
2382 name_(name),
2383 binding_(binding)
2384 {
2385 }
2386
2387 CYStatement *Replace(CYContext &context, CYIdentifier *module);
2388 };
2389
2390 struct CYImportDeclaration :
2391 CYStatement
2392 {
2393 CYImportSpecifier *specifiers_;
2394 CYString *module_;
2395
2396 CYImportDeclaration(CYImportSpecifier *specifiers, CYString *module) :
2397 specifiers_(specifiers),
2398 module_(module)
2399 {
2400 }
2401
2402 CYCompact(None)
2403
2404 virtual CYStatement *Replace(CYContext &context);
2405 virtual void Output(CYOutput &out, CYFlags flags) const;
2406 };
2407
2408 struct CYExternal :
2409 CYStatement
2410 {
2411 CYString *abi_;
2412 CYTypedIdentifier *typed_;
2413
2414 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2415 abi_(abi),
2416 typed_(typed)
2417 {
2418 }
2419
2420 CYCompact(None)
2421
2422 virtual CYStatement *Replace(CYContext &context);
2423 virtual void Output(CYOutput &out, CYFlags flags) const;
2424 };
2425
2426 struct CYTypeExpression :
2427 CYTarget
2428 {
2429 CYTypedIdentifier *typed_;
2430
2431 CYTypeExpression(CYTypedIdentifier *typed) :
2432 typed_(typed)
2433 {
2434 }
2435
2436 CYPrecedence(0)
2437
2438 virtual CYTarget *Replace(CYContext &context);
2439 virtual void Output(CYOutput &out, CYFlags flags) const;
2440 };
2441
2442 struct CYTypeDefinition :
2443 CYStatement
2444 {
2445 CYTypedIdentifier *typed_;
2446
2447 CYTypeDefinition(CYTypedIdentifier *typed) :
2448 typed_(typed)
2449 {
2450 }
2451
2452 CYCompact(None)
2453
2454 virtual CYStatement *Replace(CYContext &context);
2455 virtual void Output(CYOutput &out, CYFlags flags) const;
2456 };
2457
2458 struct CYTypeBlockWith :
2459 CYTypeModifier
2460 {
2461 CYTypedParameter *parameters_;
2462
2463 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2464 CYTypeModifier(next),
2465 parameters_(parameters)
2466 {
2467 }
2468
2469 CYPrecedence(0)
2470
2471 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2472 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2473 };
2474
2475 struct CYTypeFunctionWith :
2476 CYTypeModifier
2477 {
2478 bool variadic_;
2479 CYTypedParameter *parameters_;
2480
2481 CYTypeFunctionWith(bool variadic, CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2482 CYTypeModifier(next),
2483 variadic_(variadic),
2484 parameters_(parameters)
2485 {
2486 }
2487
2488 CYPrecedence(1)
2489
2490 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2491 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2492
2493 virtual CYTypeFunctionWith *Function() { return this; }
2494 };
2495
2496 struct CYTypeStructField :
2497 CYNext<CYTypeStructField>
2498 {
2499 CYTypedIdentifier *typed_;
2500
2501 CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) :
2502 CYNext<CYTypeStructField>(next),
2503 typed_(typed)
2504 {
2505 }
2506 };
2507
2508 struct CYStructTail :
2509 CYThing
2510 {
2511 CYTypeStructField *fields_;
2512
2513 CYStructTail(CYTypeStructField *fields) :
2514 fields_(fields)
2515 {
2516 }
2517
2518 CYTarget *Replace(CYContext &context);
2519 virtual void Output(CYOutput &out) const;
2520 };
2521
2522 struct CYTypeStruct :
2523 CYTypeSpecifier
2524 {
2525 CYIdentifier *name_;
2526 CYStructTail *tail_;
2527
2528 CYTypeStruct(CYIdentifier *name, CYStructTail *tail) :
2529 name_(name),
2530 tail_(tail)
2531 {
2532 }
2533
2534 virtual CYTarget *Replace(CYContext &context);
2535 virtual void Output(CYOutput &out) const;
2536 };
2537
2538 struct CYStructDefinition :
2539 CYStatement
2540 {
2541 CYIdentifier *name_;
2542 CYStructTail *tail_;
2543
2544 CYStructDefinition(CYIdentifier *name, CYStructTail *tail) :
2545 name_(name),
2546 tail_(tail)
2547 {
2548 }
2549
2550 CYCompact(None)
2551
2552 virtual CYStatement *Replace(CYContext &context);
2553 virtual void Output(CYOutput &out, CYFlags flags) const;
2554 };
2555
2556 namespace cy {
2557 namespace Syntax {
2558
2559 struct Catch :
2560 CYThing
2561 {
2562 CYIdentifier *name_;
2563 CYStatement *code_;
2564
2565 Catch(CYIdentifier *name, CYStatement *code) :
2566 name_(name),
2567 code_(code)
2568 {
2569 }
2570
2571 void Replace(CYContext &context);
2572 virtual void Output(CYOutput &out) const;
2573 };
2574
2575 struct Try :
2576 CYStatement
2577 {
2578 CYStatement *code_;
2579 Catch *catch_;
2580 CYFinally *finally_;
2581
2582 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2583 code_(code),
2584 catch_(_catch),
2585 finally_(finally)
2586 {
2587 }
2588
2589 CYCompact(Short)
2590
2591 virtual CYStatement *Replace(CYContext &context);
2592 virtual void Output(CYOutput &out, CYFlags flags) const;
2593 };
2594
2595 struct Throw :
2596 CYStatement
2597 {
2598 CYExpression *value_;
2599
2600 Throw(CYExpression *value = NULL) :
2601 value_(value)
2602 {
2603 }
2604
2605 CYCompact(None)
2606
2607 virtual CYStatement *Replace(CYContext &context);
2608 virtual void Output(CYOutput &out, CYFlags flags) const;
2609 };
2610
2611 } }
2612
2613 struct CYWith :
2614 CYStatement
2615 {
2616 CYExpression *scope_;
2617 CYStatement *code_;
2618
2619 CYWith(CYExpression *scope, CYStatement *code) :
2620 scope_(scope),
2621 code_(code)
2622 {
2623 }
2624
2625 CYCompact(Long)
2626
2627 virtual CYStatement *Replace(CYContext &context);
2628 virtual void Output(CYOutput &out, CYFlags flags) const;
2629 };
2630
2631 struct CYSwitch :
2632 CYStatement
2633 {
2634 CYExpression *value_;
2635 CYClause *clauses_;
2636
2637 CYSwitch(CYExpression *value, CYClause *clauses) :
2638 value_(value),
2639 clauses_(clauses)
2640 {
2641 }
2642
2643 CYCompact(Long)
2644
2645 virtual CYStatement *Replace(CYContext &context);
2646 virtual void Output(CYOutput &out, CYFlags flags) const;
2647 };
2648
2649 struct CYDebugger :
2650 CYStatement
2651 {
2652 CYDebugger()
2653 {
2654 }
2655
2656 CYCompact(None)
2657
2658 virtual CYStatement *Replace(CYContext &context);
2659 virtual void Output(CYOutput &out, CYFlags flags) const;
2660 };
2661
2662 struct CYCondition :
2663 CYExpression
2664 {
2665 CYExpression *test_;
2666 CYExpression *true_;
2667 CYExpression *false_;
2668
2669 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2670 test_(test),
2671 true_(_true),
2672 false_(_false)
2673 {
2674 }
2675
2676 CYPrecedence(15)
2677
2678 virtual CYExpression *Replace(CYContext &context);
2679 virtual void Output(CYOutput &out, CYFlags flags) const;
2680 };
2681
2682 struct CYAddressOf :
2683 CYPrefix
2684 {
2685 CYAddressOf(CYExpression *rhs) :
2686 CYPrefix(rhs)
2687 {
2688 }
2689
2690 virtual const char *Operator() const {
2691 return "&";
2692 }
2693
2694 CYAlphabetic(false)
2695
2696 virtual CYExpression *Replace(CYContext &context);
2697 };
2698
2699 struct CYIndirect :
2700 CYTarget
2701 {
2702 CYExpression *rhs_;
2703
2704 CYIndirect(CYExpression *rhs) :
2705 rhs_(rhs)
2706 {
2707 }
2708
2709 // XXX: this should be checked
2710 CYPrecedence(2)
2711
2712 virtual CYTarget *Replace(CYContext &context);
2713 virtual void Output(CYOutput &out, CYFlags flags) const;
2714 };
2715
2716 #define CYReplace \
2717 virtual CYExpression *Replace(CYContext &context);
2718
2719 #define CYPostfix_(op, name, args...) \
2720 struct CY ## name : \
2721 CYPostfix \
2722 { args \
2723 CY ## name(CYExpression *lhs) : \
2724 CYPostfix(lhs) \
2725 { \
2726 } \
2727 \
2728 virtual const char *Operator() const { \
2729 return op; \
2730 } \
2731 };
2732
2733 #define CYPrefix_(alphabetic, op, name, args...) \
2734 struct CY ## name : \
2735 CYPrefix \
2736 { args \
2737 CY ## name(CYExpression *rhs) : \
2738 CYPrefix(rhs) \
2739 { \
2740 } \
2741 \
2742 CYAlphabetic(alphabetic) \
2743 \
2744 virtual const char *Operator() const { \
2745 return op; \
2746 } \
2747 };
2748
2749 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2750 struct CY ## name : \
2751 CYInfix \
2752 { args \
2753 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2754 CYInfix(lhs, rhs) \
2755 { \
2756 } \
2757 \
2758 CYAlphabetic(alphabetic) \
2759 CYPrecedence(precedence) \
2760 \
2761 virtual const char *Operator() const { \
2762 return op; \
2763 } \
2764 };
2765
2766 #define CYAssignment_(op, name, args...) \
2767 struct CY ## name ## Assign : \
2768 CYAssignment \
2769 { args \
2770 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2771 CYAssignment(lhs, rhs) \
2772 { \
2773 } \
2774 \
2775 virtual const char *Operator() const { \
2776 return op; \
2777 } \
2778 };
2779
2780 CYPostfix_("++", PostIncrement)
2781 CYPostfix_("--", PostDecrement)
2782
2783 CYPrefix_(true, "delete", Delete)
2784 CYPrefix_(true, "void", Void)
2785 CYPrefix_(true, "typeof", TypeOf)
2786 CYPrefix_(false, "++", PreIncrement)
2787 CYPrefix_(false, "--", PreDecrement)
2788 CYPrefix_(false, "+", Affirm)
2789 CYPrefix_(false, "-", Negate)
2790 CYPrefix_(false, "~", BitwiseNot)
2791 CYPrefix_(false, "!", LogicalNot)
2792
2793 CYInfix_(false, 5, "*", Multiply, CYReplace)
2794 CYInfix_(false, 5, "/", Divide)
2795 CYInfix_(false, 5, "%", Modulus)
2796 CYInfix_(false, 6, "+", Add, CYReplace)
2797 CYInfix_(false, 6, "-", Subtract)
2798 CYInfix_(false, 7, "<<", ShiftLeft)
2799 CYInfix_(false, 7, ">>", ShiftRightSigned)
2800 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2801 CYInfix_(false, 8, "<", Less)
2802 CYInfix_(false, 8, ">", Greater)
2803 CYInfix_(false, 8, "<=", LessOrEqual)
2804 CYInfix_(false, 8, ">=", GreaterOrEqual)
2805 CYInfix_(true, 8, "instanceof", InstanceOf)
2806 CYInfix_(true, 8, "in", In)
2807 CYInfix_(false, 9, "==", Equal)
2808 CYInfix_(false, 9, "!=", NotEqual)
2809 CYInfix_(false, 9, "===", Identical)
2810 CYInfix_(false, 9, "!==", NotIdentical)
2811 CYInfix_(false, 10, "&", BitwiseAnd)
2812 CYInfix_(false, 11, "^", BitwiseXOr)
2813 CYInfix_(false, 12, "|", BitwiseOr)
2814 CYInfix_(false, 13, "&&", LogicalAnd)
2815 CYInfix_(false, 14, "||", LogicalOr)
2816
2817 CYAssignment_("=", )
2818 CYAssignment_("*=", Multiply)
2819 CYAssignment_("/=", Divide)
2820 CYAssignment_("%=", Modulus)
2821 CYAssignment_("+=", Add)
2822 CYAssignment_("-=", Subtract)
2823 CYAssignment_("<<=", ShiftLeft)
2824 CYAssignment_(">>=", ShiftRightSigned)
2825 CYAssignment_(">>>=", ShiftRightUnsigned)
2826 CYAssignment_("&=", BitwiseAnd)
2827 CYAssignment_("^=", BitwiseXOr)
2828 CYAssignment_("|=", BitwiseOr)
2829
2830 #endif/*CYCRIPT_PARSER_HPP*/