]> git.saurik.com Git - cycript.git/blame - Parser.hpp
Added output generation.
[cycript.git] / Parser.hpp
CommitLineData
63b4c5a8
JF
1#ifndef CYPARSER_HPP
2#define CYPARSER_HPP
3
cf7d4c69 4#include <cstdlib>
5999c315 5#include <string>
cf7d4c69 6
5999c315 7#include "Pooling.hpp"
924f67b2 8
5999c315
JF
9template <typename Type_>
10struct CYNext {
11 Type_ *next_;
63b4c5a8 12
5999c315
JF
13 CYNext() :
14 next_(NULL)
15 {
16 }
cf7d4c69 17
5999c315 18 void SetNext(Type_ *next) {
cf7d4c69
JF
19 next_ = next;
20 }
21};
22
5999c315
JF
23struct CYThing {
24 virtual void Output(std::ostream &out) const = 0;
25};
26
27_finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) {
28 rhs.Output(out);
29 return out;
30}
31
32struct CYPart {
33 virtual void Part(std::ostream &out) const = 0;
63b4c5a8
JF
34};
35
5999c315
JF
36struct CYSource :
37 CYNext<CYSource>,
38 CYPart
39{
40 virtual void Part(std::ostream &out) const;
41 virtual void Output(std::ostream &out) const = 0;
42 virtual void Output(std::ostream &out, bool block) const;
43};
44
45struct CYName :
46 CYThing
47{
48 virtual const char *Name() const = 0;
63b4c5a8
JF
49};
50
cf7d4c69 51struct CYWord :
cf7d4c69 52 CYName
63b4c5a8 53{
cf7d4c69
JF
54 const char *word_;
55
56 CYWord(const char *word) :
57 word_(word)
58 {
59 }
60
5999c315 61 const char *Value() const {
cf7d4c69
JF
62 return word_;
63 }
64
65 virtual const char *Name() const {
5999c315 66 return Value();
cf7d4c69 67 }
5999c315
JF
68
69 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
70};
71
cf7d4c69
JF
72struct CYIdentifier :
73 CYWord
63b4c5a8 74{
5999c315
JF
75 CYIdentifier(const char *word) :
76 CYWord(word)
77 {
cf7d4c69 78 }
63b4c5a8
JF
79};
80
cf7d4c69
JF
81struct CYLabel {
82 CYIdentifier *identifier_;
83 CYLabel *next_;
84
85 CYLabel(CYIdentifier *identifier, CYLabel *next) :
86 identifier_(identifier),
87 next_(next)
88 {
89 }
90};
91
92struct CYStatement :
93 CYSource
63b4c5a8 94{
cf7d4c69
JF
95 CYLabel *label_;
96
97 void AddLabel(CYIdentifier *identifier) {
98 label_ = new CYLabel(identifier, label_);
99 }
100};
101
5999c315
JF
102class CYDriver {
103 public:
104 CYPool pool_;
105 std::string filename_;
106 CYSource *source_;
107 void *scanner_;
108
109 private:
110 void ScannerInit();
111 void ScannerDestroy();
112
113 public:
114 CYDriver(const std::string &filename);
115 ~CYDriver();
116};
117
118struct CYForInitialiser :
119 CYPart
120{
cf7d4c69
JF
121};
122
5999c315
JF
123struct CYForInInitialiser :
124 CYPart
125{
63b4c5a8
JF
126};
127
cf7d4c69 128struct CYExpression :
5999c315 129 CYNext<CYExpression>,
cf7d4c69
JF
130 CYForInitialiser,
131 CYForInInitialiser
63b4c5a8 132{
5999c315
JF
133 virtual void Part(std::ostream &out) const;
134 virtual void Output(std::ostream &out) const = 0;
135 void Output(std::ostream &out, bool raw) const;
63b4c5a8
JF
136};
137
5999c315
JF
138_finline std::ostream &operator <<(std::ostream &out, const CYExpression &rhs) {
139 rhs.Output(out, false);
140 return out;
141}
142
cf7d4c69
JF
143struct CYLiteral :
144 CYExpression
63b4c5a8 145{
cf7d4c69 146};
63b4c5a8 147
cf7d4c69
JF
148struct CYString :
149 CYLiteral,
150 CYName
151{
152 const char *value_;
5999c315 153 size_t size_;
cf7d4c69 154
5999c315
JF
155 CYString(const char *value, size_t size) :
156 value_(value),
157 size_(size)
cf7d4c69
JF
158 {
159 }
160
161 CYString(const CYIdentifier *identifier) :
5999c315
JF
162 value_(identifier->Value()),
163 size_(strlen(value_))
cf7d4c69
JF
164 {
165 }
166
5999c315 167 const char *Value() const {
cf7d4c69
JF
168 return value_;
169 }
170
171 virtual const char *Name() const {
5999c315 172 return Value();
63b4c5a8 173 }
5999c315
JF
174
175 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
176};
177
cf7d4c69 178struct CYNumber :
cf7d4c69
JF
179 CYLiteral,
180 CYName
181{
5999c315
JF
182 double value_;
183
184 CYNumber(double value) :
185 value_(value)
186 {
187 }
188
189 double Value() const {
190 return value_;
cf7d4c69
JF
191 }
192
193 virtual const char *Name() const {
194 throw;
195 }
5999c315
JF
196
197 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
198};
199
200struct CYNull :
201 CYWord,
202 CYLiteral
203{
204 CYNull() :
205 CYWord("null")
206 {
207 }
5999c315
JF
208
209 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
210};
211
212struct CYThis :
213 CYWord,
214 CYExpression
215{
216 CYThis() :
217 CYWord("this")
218 {
219 }
5999c315
JF
220
221 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
222};
223
224struct CYBoolean :
225 CYLiteral
226{
5999c315
JF
227 virtual bool Value() const = 0;
228 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
229};
230
231struct CYFalse :
232 CYWord,
233 CYBoolean
234{
235 CYFalse() :
236 CYWord("false")
237 {
238 }
5999c315
JF
239
240 virtual bool Value() const {
241 return false;
242 }
cf7d4c69
JF
243};
244
245struct CYTrue :
246 CYWord,
247 CYBoolean
248{
249 CYTrue() :
250 CYWord("true")
251 {
252 }
5999c315
JF
253
254 virtual bool Value() const {
255 return true;
256 }
cf7d4c69
JF
257};
258
259struct CYVariable :
260 CYExpression
261{
262 CYIdentifier *name_;
263
264 CYVariable(CYIdentifier *name) :
265 name_(name)
266 {
267 }
5999c315
JF
268
269 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
270};
271
272struct CYPrefix :
63b4c5a8
JF
273 CYExpression
274{
275 CYExpression *rhs_;
276
cf7d4c69 277 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
278 rhs_(rhs)
279 {
280 }
5999c315
JF
281
282 virtual const char *Operator() const = 0;
283
284 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
285};
286
cf7d4c69 287struct CYInfix :
63b4c5a8
JF
288 CYExpression
289{
290 CYExpression *lhs_;
291 CYExpression *rhs_;
292
cf7d4c69 293 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
294 lhs_(lhs),
295 rhs_(rhs)
296 {
297 }
5999c315
JF
298
299 virtual const char *Operator() const = 0;
300
301 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
302};
303
cf7d4c69 304struct CYPostfix :
63b4c5a8
JF
305 CYExpression
306{
307 CYExpression *lhs_;
308
cf7d4c69 309 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
310 lhs_(lhs)
311 {
312 }
5999c315
JF
313
314 virtual const char *Operator() const = 0;
315
316 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
317};
318
cf7d4c69
JF
319struct CYAssignment :
320 CYInfix
321{
322 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
323 CYInfix(lhs, rhs)
324 {
325 }
5999c315
JF
326
327 virtual const char *Operator() const = 0;
cf7d4c69
JF
328};
329
330struct CYArgument {
331 CYWord *name_;
332 CYExpression *value_;
333 CYArgument *next_;
334
335 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
336 name_(name),
337 value_(value),
338 next_(next)
339 {
340 }
5999c315
JF
341
342 void Output(std::ostream &out, bool send) const;
cf7d4c69
JF
343};
344
345struct CYBlank :
346 public CYWord
347{
348 CYBlank() :
349 CYWord("")
350 {
351 }
352};
353
5999c315
JF
354struct CYClause :
355 CYThing,
356 CYNext<CYClause>
357{
cf7d4c69
JF
358 CYExpression *case_;
359 CYStatement *code_;
cf7d4c69
JF
360
361 CYClause(CYExpression *_case, CYStatement *code) :
362 case_(_case),
363 code_(code)
364 {
365 }
366
5999c315 367 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
368};
369
370struct CYElement :
371 CYLiteral
372{
373 CYExpression *value_;
374 CYElement *next_;
375
376 CYElement(CYExpression *value, CYElement *next) :
377 value_(value),
378 next_(next)
379 {
380 }
5999c315
JF
381
382 void Output(std::ostream &out, bool raw) const;
383 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
384};
385
386struct CYDeclaration :
5999c315 387 CYThing,
cf7d4c69
JF
388 CYForInInitialiser
389{
390 CYIdentifier *identifier_;
391 CYExpression *initialiser_;
392
393 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
394 identifier_(identifier),
395 initialiser_(initialiser)
396 {
397 }
5999c315
JF
398
399 virtual void Part(std::ostream &out) const;
400 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
401};
402
403struct CYDeclarations :
404 CYStatement,
405 CYForInitialiser
406{
407 CYDeclaration *declaration_;
408 CYDeclarations *next_;
409
410 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
411 declaration_(declaration),
412 next_(next)
413 {
414 }
5999c315
JF
415
416 virtual void Part(std::ostream &out) const;
417 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
418};
419
5999c315
JF
420struct CYParameter :
421 CYThing
422{
cf7d4c69
JF
423 CYIdentifier *name_;
424 CYParameter *next_;
425
426 CYParameter(CYIdentifier *name, CYParameter *next) :
427 name_(name),
428 next_(next)
429 {
430 }
5999c315
JF
431
432 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
433};
434
435struct CYFor :
436 CYStatement
437{
438 CYForInitialiser *initialiser_;
439 CYExpression *test_;
440 CYExpression *increment_;
441 CYStatement *code_;
442
443 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
444 initialiser_(initialiser),
445 test_(test),
446 increment_(increment),
447 code_(code)
448 {
449 }
5999c315
JF
450
451 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
452};
453
454struct CYForIn :
455 CYStatement
456{
457 CYForInInitialiser *initialiser_;
458 CYExpression *set_;
459 CYStatement *code_;
460
461 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
462 initialiser_(initialiser),
463 set_(set),
464 code_(code)
465 {
466 }
5999c315
JF
467
468 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
469};
470
471struct CYProperty :
472 CYLiteral
473{
474 CYName *name_;
475 CYExpression *value_;
476 CYProperty *next_;
477
478 CYProperty(CYName *name, CYExpression *value, CYProperty *next) :
479 name_(name),
480 value_(value),
481 next_(next)
482 {
483 }
5999c315
JF
484
485 void Output(std::ostream &out, bool raw) const;
486 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
487};
488
5999c315
JF
489struct CYCatch :
490 CYThing
491{
cf7d4c69
JF
492 CYIdentifier *name_;
493 CYStatement *code_;
494
495 CYCatch(CYIdentifier *name, CYStatement *code) :
496 name_(name),
497 code_(code)
498 {
499 }
5999c315
JF
500
501 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
502};
503
504struct CYMessage :
63b4c5a8
JF
505 CYExpression
506{
cf7d4c69
JF
507 CYExpression *self_;
508 CYArgument *arguments_;
63b4c5a8 509
cf7d4c69
JF
510 CYMessage(CYExpression *self, CYArgument *arguments) :
511 self_(self),
512 arguments_(arguments)
63b4c5a8
JF
513 {
514 }
5999c315
JF
515
516 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
517};
518
cf7d4c69
JF
519struct CYMember :
520 CYExpression
521{
522 CYExpression *object_;
523 CYExpression *property_;
524
525 CYMember(CYExpression *object, CYExpression *property) :
526 object_(object),
527 property_(property)
528 {
529 }
5999c315
JF
530
531 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
532};
533
534struct CYNew :
535 CYExpression
536{
537 CYExpression *constructor_;
538 CYArgument *arguments_;
539
540 CYNew(CYExpression *constructor, CYArgument *arguments) :
541 constructor_(constructor),
542 arguments_(arguments)
543 {
544 }
5999c315
JF
545
546 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
547};
548
549struct CYCall :
550 CYExpression
551{
552 CYExpression *function_;
553 CYArgument *arguments_;
554
555 CYCall(CYExpression *function, CYArgument *arguments) :
556 function_(function),
557 arguments_(arguments)
558 {
559 }
5999c315
JF
560
561 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
562};
563
564struct CYIf :
565 CYStatement
566{
567 CYExpression *test_;
568 CYStatement *true_;
569 CYStatement *false_;
570
571 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
572 test_(test),
573 true_(_true),
574 false_(_false)
575 {
576 }
5999c315
JF
577
578 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
579};
580
581struct CYDoWhile :
582 CYStatement
583{
584 CYExpression *test_;
585 CYStatement *code_;
586
587 CYDoWhile(CYExpression *test, CYStatement *code) :
588 test_(test),
589 code_(code)
590 {
591 }
5999c315
JF
592
593 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
594};
595
596struct CYWhile :
597 CYStatement
598{
599 CYExpression *test_;
600 CYStatement *code_;
601
602 CYWhile(CYExpression *test, CYStatement *code) :
603 test_(test),
604 code_(code)
605 {
606 }
5999c315
JF
607
608 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
609};
610
611struct CYLambda :
612 CYExpression
613{
614 CYIdentifier *name_;
615 CYParameter *parameters_;
616 CYSource *body_;
617
618 CYLambda(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
619 name_(name),
620 parameters_(parameters),
621 body_(body)
622 {
623 }
5999c315
JF
624
625 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
626};
627
628struct CYFunction :
5999c315
JF
629 CYLambda,
630 CYSource
cf7d4c69
JF
631{
632 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
633 CYLambda(name, parameters, body)
634 {
635 }
5999c315
JF
636
637 virtual void Output(std::ostream &out) const;
638};
639
640struct CYExpress :
641 CYStatement
642{
643 CYExpression *expression_;
644
645 CYExpress(CYExpression *expression) :
646 expression_(expression)
647 {
648 }
649
650 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
651};
652
653struct CYContinue :
654 CYStatement
655{
656 CYIdentifier *label_;
657
658 CYContinue(CYIdentifier *label) :
659 label_(label)
660 {
661 }
5999c315
JF
662
663 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
664};
665
666struct CYBreak :
667 CYStatement
668{
669 CYIdentifier *label_;
670
671 CYBreak(CYIdentifier *label) :
672 label_(label)
673 {
674 }
5999c315
JF
675
676 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
677};
678
679struct CYReturn :
680 CYStatement
681{
682 CYExpression *value_;
683
684 CYReturn(CYExpression *value) :
685 value_(value)
686 {
687 }
5999c315
JF
688
689 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
690};
691
692struct CYEmpty :
693 CYStatement
694{
5999c315
JF
695 virtual void Output(std::ostream &out) const;
696 virtual void Output(std::ostream &out, bool block) const;
cf7d4c69
JF
697};
698
699struct CYTry :
700 CYStatement
701{
702 CYStatement *try_;
703 CYCatch *catch_;
704 CYStatement *finally_;
705
706 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
707 try_(_try),
708 catch_(_catch),
709 finally_(finally)
710 {
711 }
5999c315
JF
712
713 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
714};
715
716struct CYThrow :
717 CYStatement
718{
719 CYExpression *value_;
720
721 CYThrow(CYExpression *value) :
722 value_(value)
723 {
724 }
5999c315
JF
725
726 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
727};
728
729struct CYWith :
730 CYStatement
731{
732 CYExpression *scope_;
733 CYStatement *code_;
734
735 CYWith(CYExpression *scope, CYStatement *code) :
736 scope_(scope),
737 code_(code)
738 {
739 }
5999c315
JF
740
741 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
742};
743
744struct CYSwitch :
745 CYStatement
746{
747 CYExpression *value_;
748 CYClause *clauses_;
749
750 CYSwitch(CYExpression *value, CYClause *clauses) :
751 value_(value),
752 clauses_(clauses)
753 {
754 }
5999c315
JF
755
756 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
757};
758
759struct CYCondition :
760 CYExpression
761{
762 CYExpression *test_;
763 CYExpression *true_;
764 CYExpression *false_;
765
766 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
767 true_(_true),
768 false_(_false)
769 {
770 }
5999c315
JF
771
772 virtual void Output(std::ostream &out) const;
773};
774
775struct CYAddressOf :
776 CYPrefix
777{
778 CYAddressOf(CYExpression *rhs) :
779 CYPrefix(rhs)
780 {
781 }
782
783 virtual const char *Operator() const {
784 return "&";
785 }
786
787 virtual void Output(std::ostream &out) const;
788};
789
790struct CYIndirect :
791 CYPrefix
792{
793 CYIndirect(CYExpression *rhs) :
794 CYPrefix(rhs)
795 {
796 }
797
798 virtual const char *Operator() const {
799 return "*";
800 }
801
802 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
803};
804
805#define CYPostfix_(op, name) \
806 struct CY ## name : \
807 CYPostfix \
808 { \
809 CY ## name(CYExpression *lhs) : \
810 CYPostfix(lhs) \
811 { \
812 } \
5999c315
JF
813 \
814 virtual const char *Operator() const { \
815 return op; \
816 } \
cf7d4c69
JF
817 };
818
819#define CYPrefix_(op, name) \
820 struct CY ## name : \
821 CYPrefix \
822 { \
823 CY ## name(CYExpression *rhs) : \
824 CYPrefix(rhs) \
825 { \
826 } \
5999c315
JF
827 \
828 virtual const char *Operator() const { \
829 return op; \
830 } \
cf7d4c69
JF
831 };
832
833#define CYInfix_(op, name) \
834 struct CY ## name : \
835 CYInfix \
836 { \
837 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
838 CYInfix(lhs, rhs) \
839 { \
840 } \
5999c315
JF
841 \
842 virtual const char *Operator() const { \
843 return op; \
844 } \
cf7d4c69
JF
845 };
846
847#define CYAssignment_(op, name) \
848 struct CY ## name ## Assign : \
849 CYAssignment \
850 { \
851 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
852 CYAssignment(lhs, rhs) \
853 { \
854 } \
5999c315
JF
855 \
856 virtual const char *Operator() const { \
857 return op; \
858 } \
cf7d4c69
JF
859 };
860
861CYPostfix_("++", PostIncrement)
862CYPostfix_("--", PostDecrement)
863
864CYPrefix_("delete", Delete)
865CYPrefix_("void", Void)
866CYPrefix_("typeof", TypeOf)
867CYPrefix_("++", PreIncrement)
868CYPrefix_("--", PreDecrement)
869CYPrefix_("-", Negate)
870CYPrefix_("~", BitwiseNot)
871CYPrefix_("!", LogicalNot)
cf7d4c69
JF
872
873CYInfix_("*", Multiply)
874CYInfix_("/", Divide)
875CYInfix_("%", Modulus)
876CYInfix_("+", Add)
877CYInfix_("-", Subtract)
878CYInfix_("<<", ShiftLeft)
879CYInfix_(">>", ShiftRightSigned)
880CYInfix_(">>>", ShiftRightUnsigned)
881CYInfix_("<", Less)
882CYInfix_(">", Greater)
883CYInfix_("<=", LessOrEqual)
884CYInfix_(">=", GreaterOrEqual)
885CYInfix_("instanceof", InstanceOf)
886CYInfix_("in", In)
887CYInfix_("==", Equal)
888CYInfix_("!=", NotEqual)
889CYInfix_("===", Identical)
890CYInfix_("!==", NotIdentical)
891CYInfix_("&", BitwiseAnd)
892CYInfix_("^", BitwiseXOr)
893CYInfix_("|", BitwiseOr)
894CYInfix_("&&", LogicalAnd)
895CYInfix_("||", LogicalOr)
896
897CYAssignment_("=", )
898CYAssignment_("*=", Multiply)
899CYAssignment_("/=", Divide)
900CYAssignment_("%=", Modulus)
901CYAssignment_("+=", Add)
902CYAssignment_("-=", Subtract)
903CYAssignment_("<<=", ShiftLeft)
904CYAssignment_(">>=", ShiftRightSigned)
905CYAssignment_(">>>=", ShiftRightUnsigned)
906CYAssignment_("&=", BitwiseAnd)
907CYAssignment_("^=", BitwiseXOr)
908CYAssignment_("|=", BitwiseOr)
909
63b4c5a8 910#endif/*CYPARSER_HPP*/