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