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