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