]> git.saurik.com Git - cycript.git/blob - Syntax.hpp
Move lexer hacks for ++ and -- to LexCrement rule.
[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 CYPrecedence(4)
900
901 virtual bool RightHand() const {
902 return true;
903 }
904
905 virtual bool Value() const = 0;
906 virtual void Output(CYOutput &out, CYFlags flags) const;
907 };
908
909 struct CYFalse :
910 CYBoolean
911 {
912 virtual bool Value() const {
913 return false;
914 }
915
916 virtual CYNumber *Number(CYContext &context);
917 virtual CYString *String(CYContext &context);
918 };
919
920 struct CYTrue :
921 CYBoolean
922 {
923 virtual bool Value() const {
924 return true;
925 }
926
927 virtual CYNumber *Number(CYContext &context);
928 virtual CYString *String(CYContext &context);
929 };
930
931 struct CYVariable :
932 CYTarget
933 {
934 CYIdentifier *name_;
935
936 CYVariable(CYIdentifier *name) :
937 name_(name)
938 {
939 }
940
941 CYVariable(const char *name) :
942 name_(new($pool) CYIdentifier(name))
943 {
944 }
945
946 CYPrecedence(0)
947
948 virtual bool Eval() const {
949 return strcmp(name_->Word(), "eval") == 0;
950 }
951
952 virtual CYTarget *Replace(CYContext &context);
953 virtual void Output(CYOutput &out, CYFlags flags) const;
954
955 virtual CYFunctionParameter *Parameter() const;
956 };
957
958 struct CYPrefix :
959 CYExpression
960 {
961 CYExpression *rhs_;
962
963 CYPrefix(CYExpression *rhs) :
964 rhs_(rhs)
965 {
966 }
967
968 virtual bool Alphabetic() const = 0;
969 virtual const char *Operator() const = 0;
970
971 CYPrecedence(4)
972
973 virtual CYExpression *Replace(CYContext &context);
974 virtual void Output(CYOutput &out, CYFlags flags) const;
975 };
976
977 struct CYInfix :
978 CYExpression
979 {
980 CYExpression *lhs_;
981 CYExpression *rhs_;
982
983 CYInfix(CYExpression *lhs, CYExpression *rhs) :
984 lhs_(lhs),
985 rhs_(rhs)
986 {
987 }
988
989 void SetLeft(CYExpression *lhs) {
990 lhs_ = lhs;
991 }
992
993 virtual bool Alphabetic() const = 0;
994 virtual const char *Operator() const = 0;
995
996 virtual CYExpression *Replace(CYContext &context);
997 virtual void Output(CYOutput &out, CYFlags flags) const;
998 };
999
1000 struct CYPostfix :
1001 CYExpression
1002 {
1003 CYExpression *lhs_;
1004
1005 CYPostfix(CYExpression *lhs) :
1006 lhs_(lhs)
1007 {
1008 }
1009
1010 virtual const char *Operator() const = 0;
1011
1012 CYPrecedence(3)
1013
1014 virtual CYExpression *Replace(CYContext &context);
1015 virtual void Output(CYOutput &out, CYFlags flags) const;
1016 };
1017
1018 struct CYAssignment :
1019 CYExpression
1020 {
1021 CYTarget *lhs_;
1022 CYExpression *rhs_;
1023
1024 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1025 lhs_(lhs),
1026 rhs_(rhs)
1027 {
1028 }
1029
1030 void SetRight(CYExpression *rhs) {
1031 rhs_ = rhs;
1032 }
1033
1034 virtual const char *Operator() const = 0;
1035
1036 CYPrecedence(16)
1037
1038 virtual CYExpression *Replace(CYContext &context);
1039 virtual void Output(CYOutput &out, CYFlags flags) const;
1040 };
1041
1042 struct CYArgument :
1043 CYNext<CYArgument>,
1044 CYThing
1045 {
1046 CYWord *name_;
1047 CYExpression *value_;
1048
1049 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1050 CYNext<CYArgument>(next),
1051 name_(NULL),
1052 value_(value)
1053 {
1054 }
1055
1056 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1057 CYNext<CYArgument>(next),
1058 name_(name),
1059 value_(value)
1060 {
1061 }
1062
1063 CYArgument *Replace(CYContext &context);
1064 void Output(CYOutput &out) const;
1065 };
1066
1067 struct CYClause :
1068 CYThing,
1069 CYNext<CYClause>
1070 {
1071 CYExpression *case_;
1072 CYStatement *code_;
1073
1074 CYClause(CYExpression *_case, CYStatement *code) :
1075 case_(_case),
1076 code_(code)
1077 {
1078 }
1079
1080 void Replace(CYContext &context);
1081 virtual void Output(CYOutput &out) const;
1082 };
1083
1084 struct CYElement :
1085 CYThing
1086 {
1087 virtual bool Elision() const = 0;
1088
1089 virtual void Replace(CYContext &context) = 0;
1090 };
1091
1092 struct CYElementValue :
1093 CYNext<CYElement>,
1094 CYElement
1095 {
1096 CYExpression *value_;
1097
1098 CYElementValue(CYExpression *value, CYElement *next) :
1099 CYNext<CYElement>(next),
1100 value_(value)
1101 {
1102 }
1103
1104 virtual bool Elision() const {
1105 return value_ == NULL;
1106 }
1107
1108 virtual void Replace(CYContext &context);
1109 virtual void Output(CYOutput &out) const;
1110 };
1111
1112 struct CYElementSpread :
1113 CYElement
1114 {
1115 CYExpression *value_;
1116
1117 CYElementSpread(CYExpression *value) :
1118 value_(value)
1119 {
1120 }
1121
1122 virtual bool Elision() const {
1123 return false;
1124 }
1125
1126 virtual void Replace(CYContext &context);
1127 virtual void Output(CYOutput &out) const;
1128 };
1129
1130 struct CYArray :
1131 CYLiteral
1132 {
1133 CYElement *elements_;
1134
1135 CYArray(CYElement *elements = NULL) :
1136 elements_(elements)
1137 {
1138 }
1139
1140 virtual CYTarget *Replace(CYContext &context);
1141 virtual void Output(CYOutput &out, CYFlags flags) const;
1142 };
1143
1144 struct CYDeclaration {
1145 CYIdentifier *identifier_;
1146 CYExpression *initialiser_;
1147
1148 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1149 identifier_(identifier),
1150 initialiser_(initialiser)
1151 {
1152 }
1153
1154 CYTarget *Target(CYContext &context);
1155
1156 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1157 virtual void Output(CYOutput &out, CYFlags flags) const;
1158 };
1159
1160 struct CYForLexical :
1161 CYForInInitializer
1162 {
1163 bool constant_;
1164 CYDeclaration *declaration_;
1165
1166 CYForLexical(bool constant, CYDeclaration *declaration) :
1167 constant_(constant),
1168 declaration_(declaration)
1169 {
1170 }
1171
1172 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1173
1174 virtual CYTarget *Replace(CYContext &context);
1175 virtual void Output(CYOutput &out, CYFlags flags) const;
1176 };
1177
1178 struct CYForVariable :
1179 CYForInInitializer
1180 {
1181 CYDeclaration *declaration_;
1182
1183 CYForVariable(CYDeclaration *declaration) :
1184 declaration_(declaration)
1185 {
1186 }
1187
1188 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1189
1190 virtual CYTarget *Replace(CYContext &context);
1191 virtual void Output(CYOutput &out, CYFlags flags) const;
1192 };
1193
1194 struct CYDeclarations :
1195 CYNext<CYDeclarations>,
1196 CYThing
1197 {
1198 CYDeclaration *declaration_;
1199
1200 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1201 CYNext<CYDeclarations>(next),
1202 declaration_(declaration)
1203 {
1204 }
1205
1206 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1207
1208 CYArgument *Argument(CYContext &context);
1209 CYFunctionParameter *Parameter(CYContext &context);
1210
1211 virtual void Output(CYOutput &out) const;
1212 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 };
1214
1215 struct CYVar :
1216 CYForInitializer
1217 {
1218 CYDeclarations *declarations_;
1219
1220 CYVar(CYDeclarations *declarations) :
1221 declarations_(declarations)
1222 {
1223 }
1224
1225 CYCompact(None)
1226
1227 virtual CYForInitializer *Replace(CYContext &context);
1228 virtual void Output(CYOutput &out, CYFlags flags) const;
1229 };
1230
1231 struct CYLet :
1232 CYForInitializer
1233 {
1234 bool constant_;
1235 CYDeclarations *declarations_;
1236
1237 CYLet(bool constant, CYDeclarations *declarations) :
1238 constant_(constant),
1239 declarations_(declarations)
1240 {
1241 }
1242
1243 CYCompact(None)
1244
1245 virtual CYForInitializer *Replace(CYContext &context);
1246 virtual void Output(CYOutput &out, CYFlags flags) const;
1247 };
1248
1249 struct CYBuilder {
1250 CYList<CYDeclarations> declarations_;
1251 CYList<CYStatement> statements_;
1252
1253 operator bool() const {
1254 return statements_ != NULL;
1255 }
1256 };
1257
1258 struct CYProperty :
1259 CYNext<CYProperty>,
1260 CYThing
1261 {
1262 CYPropertyName *name_;
1263
1264 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1265 CYNext<CYProperty>(next),
1266 name_(name)
1267 {
1268 }
1269
1270 virtual bool Update() const;
1271
1272 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1273 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1274
1275 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1276
1277 virtual void Replace(CYContext &context) = 0;
1278 virtual void Output(CYOutput &out) const;
1279 };
1280
1281 struct CYPropertyValue :
1282 CYProperty
1283 {
1284 CYExpression *value_;
1285
1286 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1287 CYProperty(name, next),
1288 value_(value)
1289 {
1290 }
1291
1292 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1293 virtual void Replace(CYContext &context);
1294 virtual void Output(CYOutput &out) const;
1295 };
1296
1297 struct CYFor :
1298 CYStatement
1299 {
1300 CYForInitializer *initialiser_;
1301 CYExpression *test_;
1302 CYExpression *increment_;
1303 CYStatement *code_;
1304
1305 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1306 initialiser_(initialiser),
1307 test_(test),
1308 increment_(increment),
1309 code_(code)
1310 {
1311 }
1312
1313 CYCompact(Long)
1314
1315 virtual CYStatement *Replace(CYContext &context);
1316 virtual void Output(CYOutput &out, CYFlags flags) const;
1317 };
1318
1319 struct CYForIn :
1320 CYStatement
1321 {
1322 CYForInInitializer *initialiser_;
1323 CYExpression *set_;
1324 CYStatement *code_;
1325
1326 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1327 initialiser_(initialiser),
1328 set_(set),
1329 code_(code)
1330 {
1331 }
1332
1333 CYCompact(Long)
1334
1335 virtual CYStatement *Replace(CYContext &context);
1336 virtual void Output(CYOutput &out, CYFlags flags) const;
1337 };
1338
1339 struct CYForInitialized :
1340 CYStatement
1341 {
1342 CYDeclaration *declaration_;
1343 CYExpression *set_;
1344 CYStatement *code_;
1345
1346 CYForInitialized(CYDeclaration *declaration, CYExpression *set, CYStatement *code) :
1347 declaration_(declaration),
1348 set_(set),
1349 code_(code)
1350 {
1351 }
1352
1353 CYCompact(Long)
1354
1355 virtual CYStatement *Replace(CYContext &context);
1356 virtual void Output(CYOutput &out, CYFlags flags) const;
1357 };
1358
1359 struct CYForOf :
1360 CYStatement
1361 {
1362 CYForInInitializer *initialiser_;
1363 CYExpression *set_;
1364 CYStatement *code_;
1365
1366 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1367 initialiser_(initialiser),
1368 set_(set),
1369 code_(code)
1370 {
1371 }
1372
1373 CYCompact(Long)
1374
1375 virtual CYStatement *Replace(CYContext &context);
1376 virtual void Output(CYOutput &out, CYFlags flags) const;
1377 };
1378
1379 struct CYObject :
1380 CYLiteral
1381 {
1382 CYProperty *properties_;
1383
1384 CYObject(CYProperty *properties = NULL) :
1385 properties_(properties)
1386 {
1387 }
1388
1389 virtual CYTarget *Replace(CYContext &context);
1390 void Output(CYOutput &out, CYFlags flags) const;
1391 };
1392
1393 struct CYMember :
1394 CYTarget
1395 {
1396 CYExpression *object_;
1397 CYExpression *property_;
1398
1399 CYMember(CYExpression *object, CYExpression *property) :
1400 object_(object),
1401 property_(property)
1402 {
1403 }
1404
1405 void SetLeft(CYExpression *object) {
1406 object_ = object;
1407 }
1408 };
1409
1410 struct CYDirectMember :
1411 CYMember
1412 {
1413 CYDirectMember(CYExpression *object, CYExpression *property) :
1414 CYMember(object, property)
1415 {
1416 }
1417
1418 CYPrecedence(1)
1419
1420 virtual CYTarget *Replace(CYContext &context);
1421 virtual void Output(CYOutput &out, CYFlags flags) const;
1422 };
1423
1424 struct CYIndirectMember :
1425 CYMember
1426 {
1427 CYIndirectMember(CYExpression *object, CYExpression *property) :
1428 CYMember(object, property)
1429 {
1430 }
1431
1432 CYPrecedence(1)
1433
1434 virtual CYTarget *Replace(CYContext &context);
1435 virtual void Output(CYOutput &out, CYFlags flags) const;
1436 };
1437
1438 namespace cy {
1439 namespace Syntax {
1440
1441 struct New :
1442 CYTarget
1443 {
1444 CYExpression *constructor_;
1445 CYArgument *arguments_;
1446
1447 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1448 constructor_(constructor),
1449 arguments_(arguments)
1450 {
1451 }
1452
1453 virtual int Precedence() const {
1454 return arguments_ == NULL ? 2 : 1;
1455 }
1456
1457
1458 virtual CYTarget *Replace(CYContext &context);
1459 virtual void Output(CYOutput &out, CYFlags flags) const;
1460
1461 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1462 };
1463
1464 } }
1465
1466 struct CYApply :
1467 CYTarget
1468 {
1469 CYArgument *arguments_;
1470
1471 CYApply(CYArgument *arguments = NULL) :
1472 arguments_(arguments)
1473 {
1474 }
1475
1476 CYPrecedence(1)
1477
1478 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1479 };
1480
1481 struct CYCall :
1482 CYApply
1483 {
1484 CYExpression *function_;
1485
1486 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1487 CYApply(arguments),
1488 function_(function)
1489 {
1490 }
1491
1492 virtual void Output(CYOutput &out, CYFlags flags) const;
1493 virtual CYTarget *Replace(CYContext &context);
1494 };
1495
1496 struct CYEval :
1497 CYApply
1498 {
1499 CYEval(CYArgument *arguments) :
1500 CYApply(arguments)
1501 {
1502 }
1503
1504 virtual void Output(CYOutput &out, CYFlags flags) const;
1505 virtual CYTarget *Replace(CYContext &context);
1506 };
1507
1508 struct CYRubyProc;
1509
1510 struct CYRubyBlock :
1511 CYTarget
1512 {
1513 CYExpression *call_;
1514 CYRubyProc *proc_;
1515
1516 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1517 call_(call),
1518 proc_(proc)
1519 {
1520 }
1521
1522 CYPrecedence(1)
1523
1524 virtual CYTarget *Replace(CYContext &context);
1525 virtual void Output(CYOutput &out, CYFlags flags) const;
1526
1527 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1528 };
1529
1530 struct CYIf :
1531 CYStatement
1532 {
1533 CYExpression *test_;
1534 CYStatement *true_;
1535 CYStatement *false_;
1536
1537 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1538 test_(test),
1539 true_(_true),
1540 false_(_false)
1541 {
1542 }
1543
1544 CYCompact(Long)
1545
1546 virtual CYStatement *Replace(CYContext &context);
1547 virtual void Output(CYOutput &out, CYFlags flags) const;
1548
1549 virtual CYStatement *Return();
1550 };
1551
1552 struct CYDoWhile :
1553 CYStatement
1554 {
1555 CYExpression *test_;
1556 CYStatement *code_;
1557
1558 CYDoWhile(CYExpression *test, CYStatement *code) :
1559 test_(test),
1560 code_(code)
1561 {
1562 }
1563
1564 CYCompact(None)
1565
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1568 };
1569
1570 struct CYWhile :
1571 CYStatement
1572 {
1573 CYExpression *test_;
1574 CYStatement *code_;
1575
1576 CYWhile(CYExpression *test, CYStatement *code) :
1577 test_(test),
1578 code_(code)
1579 {
1580 }
1581
1582 CYCompact(Long)
1583
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1586 };
1587
1588 struct CYFunction {
1589 CYFunctionParameter *parameters_;
1590 CYStatement *code_;
1591
1592 CYNonLocal *nonlocal_;
1593 bool implicit_;
1594 CYThisScope this_;
1595 CYIdentifier *super_;
1596
1597 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1598 parameters_(parameters),
1599 code_(code),
1600 nonlocal_(NULL),
1601 implicit_(false),
1602 super_(NULL)
1603 {
1604 }
1605
1606 void Replace(CYContext &context);
1607 void Output(CYOutput &out) const;
1608 };
1609
1610 struct CYFunctionExpression :
1611 CYFunction,
1612 CYTarget
1613 {
1614 CYIdentifier *name_;
1615
1616 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1617 CYFunction(parameters, code),
1618 name_(name)
1619 {
1620 }
1621
1622 CYPrecedence(0)
1623
1624 CYTarget *Replace(CYContext &context) override;
1625 virtual void Output(CYOutput &out, CYFlags flags) const;
1626 };
1627
1628 struct CYFatArrow :
1629 CYFunction,
1630 CYExpression
1631 {
1632 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1633 CYFunction(parameters, code)
1634 {
1635 }
1636
1637 CYPrecedence(0)
1638
1639 CYExpression *Replace(CYContext &context) override;
1640 virtual void Output(CYOutput &out, CYFlags flags) const;
1641 };
1642
1643 struct CYRubyProc :
1644 CYFunction,
1645 CYTarget
1646 {
1647 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1648 CYFunction(parameters, code)
1649 {
1650 }
1651
1652 CYPrecedence(0)
1653
1654 CYTarget *Replace(CYContext &context) override;
1655 virtual void Output(CYOutput &out, CYFlags flags) const;
1656 };
1657
1658 struct CYFunctionStatement :
1659 CYFunction,
1660 CYStatement
1661 {
1662 CYIdentifier *name_;
1663
1664 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1665 CYFunction(parameters, code),
1666 name_(name)
1667 {
1668 }
1669
1670 CYCompact(None)
1671
1672 CYStatement *Replace(CYContext &context) override;
1673 virtual void Output(CYOutput &out, CYFlags flags) const;
1674 };
1675
1676 struct CYPropertyMethod;
1677
1678 struct CYMethod :
1679 CYFunction,
1680 CYProperty
1681 {
1682 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1683 CYFunction(parameters, code),
1684 CYProperty(name, next)
1685 {
1686 }
1687
1688 virtual CYFunctionExpression *Constructor();
1689
1690 using CYProperty::Replace;
1691 virtual void Replace(CYContext &context);
1692 };
1693
1694 struct CYPropertyGetter :
1695 CYMethod
1696 {
1697 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1698 CYMethod(name, NULL, code, next)
1699 {
1700 }
1701
1702 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1703 virtual void Output(CYOutput &out) const;
1704 };
1705
1706 struct CYPropertySetter :
1707 CYMethod
1708 {
1709 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1710 CYMethod(name, parameters, code, next)
1711 {
1712 }
1713
1714 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1715 virtual void Output(CYOutput &out) const;
1716 };
1717
1718 struct CYPropertyMethod :
1719 CYMethod
1720 {
1721 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1722 CYMethod(name, parameters, code, next)
1723 {
1724 }
1725
1726 bool Update() const override;
1727
1728 virtual CYFunctionExpression *Constructor();
1729
1730 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1731 virtual void Output(CYOutput &out) const;
1732 };
1733
1734 struct CYClassTail :
1735 CYThing
1736 {
1737 CYExpression *extends_;
1738
1739 CYFunctionExpression *constructor_;
1740 CYList<CYProperty> instance_;
1741 CYList<CYProperty> static_;
1742
1743 CYClassTail(CYExpression *extends) :
1744 extends_(extends),
1745 constructor_(NULL)
1746 {
1747 }
1748
1749 void Output(CYOutput &out) const;
1750 };
1751
1752 struct CYClassExpression :
1753 CYTarget
1754 {
1755 CYIdentifier *name_;
1756 CYClassTail *tail_;
1757
1758 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1759 name_(name),
1760 tail_(tail)
1761 {
1762 }
1763
1764 CYPrecedence(0)
1765
1766 CYTarget *Replace(CYContext &context) override;
1767 virtual void Output(CYOutput &out, CYFlags flags) const;
1768 };
1769
1770 struct CYClassStatement :
1771 CYStatement
1772 {
1773 CYIdentifier *name_;
1774 CYClassTail *tail_;
1775
1776 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1777 name_(name),
1778 tail_(tail)
1779 {
1780 }
1781
1782 CYCompact(Long)
1783
1784 CYStatement *Replace(CYContext &context) override;
1785 virtual void Output(CYOutput &out, CYFlags flags) const;
1786 };
1787
1788 struct CYSuperCall :
1789 CYTarget
1790 {
1791 CYArgument *arguments_;
1792
1793 CYSuperCall(CYArgument *arguments) :
1794 arguments_(arguments)
1795 {
1796 }
1797
1798 CYPrecedence(2)
1799
1800 CYTarget *Replace(CYContext &context) override;
1801 virtual void Output(CYOutput &out, CYFlags flags) const;
1802 };
1803
1804 struct CYSuperAccess :
1805 CYTarget
1806 {
1807 CYExpression *property_;
1808
1809 CYSuperAccess(CYExpression *property) :
1810 property_(property)
1811 {
1812 }
1813
1814 CYPrecedence(1)
1815
1816 CYTarget *Replace(CYContext &context) override;
1817 virtual void Output(CYOutput &out, CYFlags flags) const;
1818 };
1819
1820 struct CYExpress :
1821 CYForInitializer
1822 {
1823 CYExpression *expression_;
1824
1825 CYExpress(CYExpression *expression) :
1826 expression_(expression)
1827 {
1828 if (expression_ == NULL)
1829 throw;
1830 }
1831
1832 CYCompact(None)
1833
1834 CYForInitializer *Replace(CYContext &context) override;
1835 virtual void Output(CYOutput &out, CYFlags flags) const;
1836
1837 virtual CYStatement *Return();
1838 };
1839
1840 struct CYContinue :
1841 CYStatement
1842 {
1843 CYIdentifier *label_;
1844
1845 CYContinue(CYIdentifier *label) :
1846 label_(label)
1847 {
1848 }
1849
1850 CYCompact(Short)
1851
1852 CYStatement *Replace(CYContext &context) override;
1853 virtual void Output(CYOutput &out, CYFlags flags) const;
1854 };
1855
1856 struct CYBreak :
1857 CYStatement
1858 {
1859 CYIdentifier *label_;
1860
1861 CYBreak(CYIdentifier *label) :
1862 label_(label)
1863 {
1864 }
1865
1866 CYCompact(Short)
1867
1868 CYStatement *Replace(CYContext &context) override;
1869 virtual void Output(CYOutput &out, CYFlags flags) const;
1870 };
1871
1872 struct CYReturn :
1873 CYStatement
1874 {
1875 CYExpression *value_;
1876
1877 CYReturn(CYExpression *value) :
1878 value_(value)
1879 {
1880 }
1881
1882 CYCompact(None)
1883
1884 CYStatement *Replace(CYContext &context) override;
1885 virtual void Output(CYOutput &out, CYFlags flags) const;
1886 };
1887
1888 struct CYYieldGenerator :
1889 CYExpression
1890 {
1891 CYExpression *value_;
1892
1893 CYYieldGenerator(CYExpression *value) :
1894 value_(value)
1895 {
1896 }
1897
1898 CYPrecedence(0)
1899
1900 CYExpression *Replace(CYContext &context) override;
1901 virtual void Output(CYOutput &out, CYFlags flags) const;
1902 };
1903
1904 struct CYYieldValue :
1905 CYExpression
1906 {
1907 CYExpression *value_;
1908
1909 CYYieldValue(CYExpression *value) :
1910 value_(value)
1911 {
1912 }
1913
1914 CYPrecedence(0)
1915
1916 virtual CYExpression *Replace(CYContext &context);
1917 virtual void Output(CYOutput &out, CYFlags flags) const;
1918 };
1919
1920 struct CYEmpty :
1921 CYForInitializer
1922 {
1923 CYCompact(Short)
1924
1925 virtual CYForInitializer *Replace(CYContext &context);
1926 virtual void Output(CYOutput &out, CYFlags flags) const;
1927 };
1928
1929 struct CYFinally :
1930 CYThing
1931 {
1932 CYStatement *code_;
1933
1934 CYFinally(CYStatement *code) :
1935 code_(code)
1936 {
1937 }
1938
1939 void Replace(CYContext &context);
1940 virtual void Output(CYOutput &out) const;
1941 };
1942
1943 struct CYTypeSpecifier :
1944 CYThing
1945 {
1946 virtual CYTarget *Replace(CYContext &context) = 0;
1947 };
1948
1949 struct CYTypeError :
1950 CYTypeSpecifier
1951 {
1952 CYTypeError() {
1953 }
1954
1955 virtual CYTarget *Replace(CYContext &context);
1956 virtual void Output(CYOutput &out) const;
1957 };
1958
1959 struct CYTypeVoid :
1960 CYTypeSpecifier
1961 {
1962 CYTypeVoid() {
1963 }
1964
1965 virtual CYTarget *Replace(CYContext &context);
1966 virtual void Output(CYOutput &out) const;
1967 };
1968
1969 struct CYTypeVariable :
1970 CYTypeSpecifier
1971 {
1972 CYIdentifier *name_;
1973
1974 CYTypeVariable(CYIdentifier *name) :
1975 name_(name)
1976 {
1977 }
1978
1979 CYTypeVariable(const char *name) :
1980 name_(new($pool) CYIdentifier(name))
1981 {
1982 }
1983
1984 virtual CYTarget *Replace(CYContext &context);
1985 virtual void Output(CYOutput &out) const;
1986 };
1987
1988 struct CYTypeUnsigned :
1989 CYTypeSpecifier
1990 {
1991 CYTypeSpecifier *specifier_;
1992
1993 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1994 specifier_(specifier)
1995 {
1996 }
1997
1998 virtual CYTarget *Replace(CYContext &context);
1999 virtual void Output(CYOutput &out) const;
2000 };
2001
2002 struct CYTypeSigned :
2003 CYTypeSpecifier
2004 {
2005 CYTypeSpecifier *specifier_;
2006
2007 CYTypeSigned(CYTypeSpecifier *specifier) :
2008 specifier_(specifier)
2009 {
2010 }
2011
2012 virtual CYTarget *Replace(CYContext &context);
2013 virtual void Output(CYOutput &out) const;
2014 };
2015
2016 struct CYTypeLong :
2017 CYTypeSpecifier
2018 {
2019 CYTypeSpecifier *specifier_;
2020
2021 CYTypeLong(CYTypeSpecifier *specifier) :
2022 specifier_(specifier)
2023 {
2024 }
2025
2026 virtual CYTarget *Replace(CYContext &context);
2027 virtual void Output(CYOutput &out) const;
2028 };
2029
2030 struct CYTypeShort :
2031 CYTypeSpecifier
2032 {
2033 CYTypeSpecifier *specifier_;
2034
2035 CYTypeShort(CYTypeSpecifier *specifier) :
2036 specifier_(specifier)
2037 {
2038 }
2039
2040 virtual CYTarget *Replace(CYContext &context);
2041 virtual void Output(CYOutput &out) const;
2042 };
2043
2044 struct CYTypeFunctionWith;
2045
2046 struct CYTypeModifier :
2047 CYNext<CYTypeModifier>
2048 {
2049 CYTypeModifier(CYTypeModifier *next) :
2050 CYNext<CYTypeModifier>(next)
2051 {
2052 }
2053
2054 virtual int Precedence() const = 0;
2055
2056 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2057 CYTarget *Replace(CYContext &context, CYTarget *type);
2058
2059 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2060 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2061
2062 virtual CYTypeFunctionWith *Function() { return NULL; }
2063 };
2064
2065 struct CYTypeArrayOf :
2066 CYTypeModifier
2067 {
2068 CYExpression *size_;
2069
2070 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2071 CYTypeModifier(next),
2072 size_(size)
2073 {
2074 }
2075
2076 CYPrecedence(1)
2077
2078 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2079 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2080 };
2081
2082 struct CYTypeConstant :
2083 CYTypeModifier
2084 {
2085 CYTypeConstant(CYTypeModifier *next = NULL) :
2086 CYTypeModifier(next)
2087 {
2088 }
2089
2090 CYPrecedence(0)
2091
2092 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2093 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2094 };
2095
2096 struct CYTypePointerTo :
2097 CYTypeModifier
2098 {
2099 CYTypePointerTo(CYTypeModifier *next = NULL) :
2100 CYTypeModifier(next)
2101 {
2102 }
2103
2104 CYPrecedence(0)
2105
2106 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2107 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2108 };
2109
2110 struct CYTypeVolatile :
2111 CYTypeModifier
2112 {
2113 CYTypeVolatile(CYTypeModifier *next = NULL) :
2114 CYTypeModifier(next)
2115 {
2116 }
2117
2118 CYPrecedence(0)
2119
2120 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2121 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2122 };
2123
2124 struct CYTypedIdentifier :
2125 CYNext<CYTypedIdentifier>,
2126 CYThing
2127 {
2128 CYLocation location_;
2129 CYIdentifier *identifier_;
2130 CYTypeSpecifier *specifier_;
2131 CYTypeModifier *modifier_;
2132
2133 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2134 location_(location),
2135 identifier_(identifier),
2136 specifier_(NULL),
2137 modifier_(NULL)
2138 {
2139 }
2140
2141 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2142 identifier_(NULL),
2143 specifier_(specifier),
2144 modifier_(modifier)
2145 {
2146 }
2147
2148 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2149 CYSetLast(modifier_) = modifier;
2150 return this;
2151 }
2152
2153 virtual CYTarget *Replace(CYContext &context);
2154 virtual void Output(CYOutput &out) const;
2155
2156 CYTypeFunctionWith *Function();
2157 };
2158
2159 struct CYEncodedType :
2160 CYTarget
2161 {
2162 CYTypedIdentifier *typed_;
2163
2164 CYEncodedType(CYTypedIdentifier *typed) :
2165 typed_(typed)
2166 {
2167 }
2168
2169 CYPrecedence(1)
2170
2171 virtual CYTarget *Replace(CYContext &context);
2172 virtual void Output(CYOutput &out, CYFlags flags) const;
2173 };
2174
2175 struct CYTypedParameter :
2176 CYNext<CYTypedParameter>,
2177 CYThing
2178 {
2179 CYTypedIdentifier *typed_;
2180
2181 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2182 CYNext<CYTypedParameter>(next),
2183 typed_(typed)
2184 {
2185 }
2186
2187 CYArgument *Argument(CYContext &context);
2188 CYFunctionParameter *Parameters(CYContext &context);
2189 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2190
2191 virtual void Output(CYOutput &out) const;
2192 };
2193
2194 struct CYLambda :
2195 CYTarget
2196 {
2197 CYTypedIdentifier *typed_;
2198 CYTypedParameter *parameters_;
2199 CYStatement *code_;
2200
2201 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2202 typed_(typed),
2203 parameters_(parameters),
2204 code_(code)
2205 {
2206 }
2207
2208 CYPrecedence(1)
2209
2210 virtual CYTarget *Replace(CYContext &context);
2211 virtual void Output(CYOutput &out, CYFlags flags) const;
2212 };
2213
2214 struct CYModule :
2215 CYNext<CYModule>,
2216 CYThing
2217 {
2218 CYWord *part_;
2219
2220 CYModule(CYWord *part, CYModule *next = NULL) :
2221 CYNext<CYModule>(next),
2222 part_(part)
2223 {
2224 }
2225
2226 CYString *Replace(CYContext &context, const char *separator) const;
2227 void Output(CYOutput &out) const;
2228 };
2229
2230 struct CYImport :
2231 CYStatement
2232 {
2233 CYModule *module_;
2234
2235 CYImport(CYModule *module) :
2236 module_(module)
2237 {
2238 }
2239
2240 CYCompact(None)
2241
2242 virtual CYStatement *Replace(CYContext &context);
2243 virtual void Output(CYOutput &out, CYFlags flags) const;
2244 };
2245
2246 struct CYExternal :
2247 CYStatement
2248 {
2249 CYString *abi_;
2250 CYTypedIdentifier *typed_;
2251
2252 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2253 abi_(abi),
2254 typed_(typed)
2255 {
2256 }
2257
2258 CYCompact(None)
2259
2260 virtual CYStatement *Replace(CYContext &context);
2261 virtual void Output(CYOutput &out, CYFlags flags) const;
2262 };
2263
2264 struct CYTypeDefinition :
2265 CYStatement
2266 {
2267 CYTypedIdentifier *typed_;
2268
2269 CYTypeDefinition(CYTypedIdentifier *typed) :
2270 typed_(typed)
2271 {
2272 }
2273
2274 CYCompact(None)
2275
2276 virtual CYStatement *Replace(CYContext &context);
2277 virtual void Output(CYOutput &out, CYFlags flags) const;
2278 };
2279
2280 struct CYTypeBlockWith :
2281 CYTypeModifier
2282 {
2283 CYTypedParameter *parameters_;
2284
2285 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2286 CYTypeModifier(next),
2287 parameters_(parameters)
2288 {
2289 }
2290
2291 CYPrecedence(0)
2292
2293 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2294 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2295 };
2296
2297 struct CYTypeFunctionWith :
2298 CYTypeModifier
2299 {
2300 CYTypedParameter *parameters_;
2301
2302 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2303 CYTypeModifier(next),
2304 parameters_(parameters)
2305 {
2306 }
2307
2308 CYPrecedence(1)
2309
2310 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2311 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2312
2313 virtual CYTypeFunctionWith *Function() { return this; }
2314 };
2315
2316 namespace cy {
2317 namespace Syntax {
2318
2319 struct Catch :
2320 CYThing
2321 {
2322 CYIdentifier *name_;
2323 CYStatement *code_;
2324
2325 Catch(CYIdentifier *name, CYStatement *code) :
2326 name_(name),
2327 code_(code)
2328 {
2329 }
2330
2331 void Replace(CYContext &context);
2332 virtual void Output(CYOutput &out) const;
2333 };
2334
2335 struct Try :
2336 CYStatement
2337 {
2338 CYStatement *code_;
2339 Catch *catch_;
2340 CYFinally *finally_;
2341
2342 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2343 code_(code),
2344 catch_(_catch),
2345 finally_(finally)
2346 {
2347 }
2348
2349 CYCompact(Short)
2350
2351 virtual CYStatement *Replace(CYContext &context);
2352 virtual void Output(CYOutput &out, CYFlags flags) const;
2353 };
2354
2355 struct Throw :
2356 CYStatement
2357 {
2358 CYExpression *value_;
2359
2360 Throw(CYExpression *value = NULL) :
2361 value_(value)
2362 {
2363 }
2364
2365 CYCompact(None)
2366
2367 virtual CYStatement *Replace(CYContext &context);
2368 virtual void Output(CYOutput &out, CYFlags flags) const;
2369 };
2370
2371 } }
2372
2373 struct CYWith :
2374 CYStatement
2375 {
2376 CYExpression *scope_;
2377 CYStatement *code_;
2378
2379 CYWith(CYExpression *scope, CYStatement *code) :
2380 scope_(scope),
2381 code_(code)
2382 {
2383 }
2384
2385 CYCompact(Long)
2386
2387 virtual CYStatement *Replace(CYContext &context);
2388 virtual void Output(CYOutput &out, CYFlags flags) const;
2389 };
2390
2391 struct CYSwitch :
2392 CYStatement
2393 {
2394 CYExpression *value_;
2395 CYClause *clauses_;
2396
2397 CYSwitch(CYExpression *value, CYClause *clauses) :
2398 value_(value),
2399 clauses_(clauses)
2400 {
2401 }
2402
2403 CYCompact(Long)
2404
2405 virtual CYStatement *Replace(CYContext &context);
2406 virtual void Output(CYOutput &out, CYFlags flags) const;
2407 };
2408
2409 struct CYDebugger :
2410 CYStatement
2411 {
2412 CYDebugger()
2413 {
2414 }
2415
2416 CYCompact(None)
2417
2418 virtual CYStatement *Replace(CYContext &context);
2419 virtual void Output(CYOutput &out, CYFlags flags) const;
2420 };
2421
2422 struct CYCondition :
2423 CYExpression
2424 {
2425 CYExpression *test_;
2426 CYExpression *true_;
2427 CYExpression *false_;
2428
2429 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2430 test_(test),
2431 true_(_true),
2432 false_(_false)
2433 {
2434 }
2435
2436 CYPrecedence(15)
2437
2438 virtual CYExpression *Replace(CYContext &context);
2439 virtual void Output(CYOutput &out, CYFlags flags) const;
2440 };
2441
2442 struct CYAddressOf :
2443 CYPrefix
2444 {
2445 CYAddressOf(CYExpression *rhs) :
2446 CYPrefix(rhs)
2447 {
2448 }
2449
2450 virtual const char *Operator() const {
2451 return "&";
2452 }
2453
2454 CYAlphabetic(false)
2455
2456 virtual CYExpression *Replace(CYContext &context);
2457 };
2458
2459 struct CYIndirect :
2460 CYTarget
2461 {
2462 CYExpression *rhs_;
2463
2464 CYIndirect(CYExpression *rhs) :
2465 rhs_(rhs)
2466 {
2467 }
2468
2469 // XXX: this should be checked
2470 CYPrecedence(2)
2471
2472 virtual CYTarget *Replace(CYContext &context);
2473 virtual void Output(CYOutput &out, CYFlags flags) const;
2474 };
2475
2476 #define CYReplace \
2477 virtual CYExpression *Replace(CYContext &context);
2478
2479 #define CYPostfix_(op, name, args...) \
2480 struct CY ## name : \
2481 CYPostfix \
2482 { args \
2483 CY ## name(CYExpression *lhs) : \
2484 CYPostfix(lhs) \
2485 { \
2486 } \
2487 \
2488 virtual const char *Operator() const { \
2489 return op; \
2490 } \
2491 };
2492
2493 #define CYPrefix_(alphabetic, op, name, args...) \
2494 struct CY ## name : \
2495 CYPrefix \
2496 { args \
2497 CY ## name(CYExpression *rhs) : \
2498 CYPrefix(rhs) \
2499 { \
2500 } \
2501 \
2502 CYAlphabetic(alphabetic) \
2503 \
2504 virtual const char *Operator() const { \
2505 return op; \
2506 } \
2507 };
2508
2509 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2510 struct CY ## name : \
2511 CYInfix \
2512 { args \
2513 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2514 CYInfix(lhs, rhs) \
2515 { \
2516 } \
2517 \
2518 CYAlphabetic(alphabetic) \
2519 CYPrecedence(precedence) \
2520 \
2521 virtual const char *Operator() const { \
2522 return op; \
2523 } \
2524 };
2525
2526 #define CYAssignment_(op, name, args...) \
2527 struct CY ## name ## Assign : \
2528 CYAssignment \
2529 { args \
2530 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2531 CYAssignment(lhs, rhs) \
2532 { \
2533 } \
2534 \
2535 virtual const char *Operator() const { \
2536 return op; \
2537 } \
2538 };
2539
2540 CYPostfix_("++", PostIncrement)
2541 CYPostfix_("--", PostDecrement)
2542
2543 CYPrefix_(true, "delete", Delete)
2544 CYPrefix_(true, "void", Void)
2545 CYPrefix_(true, "typeof", TypeOf)
2546 CYPrefix_(false, "++", PreIncrement)
2547 CYPrefix_(false, "--", PreDecrement)
2548 CYPrefix_(false, "+", Affirm)
2549 CYPrefix_(false, "-", Negate)
2550 CYPrefix_(false, "~", BitwiseNot)
2551 CYPrefix_(false, "!", LogicalNot)
2552
2553 CYInfix_(false, 5, "*", Multiply, CYReplace)
2554 CYInfix_(false, 5, "/", Divide)
2555 CYInfix_(false, 5, "%", Modulus)
2556 CYInfix_(false, 6, "+", Add, CYReplace)
2557 CYInfix_(false, 6, "-", Subtract)
2558 CYInfix_(false, 7, "<<", ShiftLeft)
2559 CYInfix_(false, 7, ">>", ShiftRightSigned)
2560 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2561 CYInfix_(false, 8, "<", Less)
2562 CYInfix_(false, 8, ">", Greater)
2563 CYInfix_(false, 8, "<=", LessOrEqual)
2564 CYInfix_(false, 8, ">=", GreaterOrEqual)
2565 CYInfix_(true, 8, "instanceof", InstanceOf)
2566 CYInfix_(true, 8, "in", In)
2567 CYInfix_(false, 9, "==", Equal)
2568 CYInfix_(false, 9, "!=", NotEqual)
2569 CYInfix_(false, 9, "===", Identical)
2570 CYInfix_(false, 9, "!==", NotIdentical)
2571 CYInfix_(false, 10, "&", BitwiseAnd)
2572 CYInfix_(false, 11, "^", BitwiseXOr)
2573 CYInfix_(false, 12, "|", BitwiseOr)
2574 CYInfix_(false, 13, "&&", LogicalAnd)
2575 CYInfix_(false, 14, "||", LogicalOr)
2576
2577 CYAssignment_("=", )
2578 CYAssignment_("*=", Multiply)
2579 CYAssignment_("/=", Divide)
2580 CYAssignment_("%=", Modulus)
2581 CYAssignment_("+=", Add)
2582 CYAssignment_("-=", Subtract)
2583 CYAssignment_("<<=", ShiftLeft)
2584 CYAssignment_(">>=", ShiftRightSigned)
2585 CYAssignment_(">>>=", ShiftRightUnsigned)
2586 CYAssignment_("&=", BitwiseAnd)
2587 CYAssignment_("^=", BitwiseXOr)
2588 CYAssignment_("|=", BitwiseOr)
2589
2590 #endif/*CYCRIPT_PARSER_HPP*/