-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.hpp
1335 lines (1272 loc) · 36.1 KB
/
ast.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "printer.hpp"
#include <cstdint>
#include <vector>
#include <set>
#include <memory>
class Function;
enum class TypeId {
INT,
CHAR,
CLOSURE,
STRUCT,
ENUM,
TUPLE,
ARRAY,
STRING,
STRING_ITERATOR,
VOID,
REFERENCE,
TYPE
};
class Type {
public:
virtual ~Type() = default;
virtual TypeId get_id() const = 0;
};
class IntType: public Type {
public:
TypeId get_id() const override {
return TypeId::INT;
}
};
class CharType: public Type {
public:
TypeId get_id() const override {
return TypeId::CHAR;
}
};
class ClosureType: public Type {
const Function* function;
std::vector<const Type*> environment_types;
public:
ClosureType(const Function* function): function(function) {}
TypeId get_id() const override {
return TypeId::CLOSURE;
}
bool operator <(const ClosureType& rhs) const {
return std::make_pair(function, std::ref(environment_types)) < std::make_pair(rhs.function, std::ref(rhs.environment_types));
}
void add_environment_type(const Type* type) {
environment_types.push_back(type);
}
const Function* get_function() const {
return function;
}
const std::vector<const Type*>& get_environment_types() const {
return environment_types;
}
};
class StructType: public Type {
std::vector<std::pair<std::string, const Type*>> fields;
public:
TypeId get_id() const override {
return TypeId::STRUCT;
}
bool operator <(const StructType& rhs) const {
return fields < rhs.fields;
}
void add_field(const std::string& field_name, const Type* field_type) {
fields.emplace_back(field_name, field_type);
}
const std::vector<std::pair<std::string, const Type*>>& get_fields() const {
return fields;
}
bool has_field(const std::string& field_name) const {
for (std::size_t i = 0; i < fields.size(); ++i) {
if (fields[i].first == field_name) {
return true;
}
}
return false;
}
std::size_t get_index(const std::string& field_name) const {
for (std::size_t i = 0; i < fields.size(); ++i) {
if (fields[i].first == field_name) {
return i;
}
}
return 0;
}
};
class EnumType: public Type {
std::vector<std::pair<std::string, const Type*>> cases;
public:
TypeId get_id() const override {
return TypeId::ENUM;
}
bool operator <(const EnumType& rhs) const {
return cases < rhs.cases;
}
void add_case(const std::string& case_name, const Type* case_type) {
cases.emplace_back(case_name, case_type);
}
const std::vector<std::pair<std::string, const Type*>>& get_cases() const {
return cases;
}
bool has_case(const std::string& case_name) const {
for (std::size_t i = 0; i < cases.size(); ++i) {
if (cases[i].first == case_name) {
return true;
}
}
return false;
}
std::size_t get_index(const std::string& case_name) const {
for (std::size_t i = 0; i < cases.size(); ++i) {
if (cases[i].first == case_name) {
return i;
}
}
return 0;
}
};
class TupleType: public Type {
std::vector<const Type*> element_types;
public:
TypeId get_id() const override {
return TypeId::TUPLE;
}
bool operator <(const TupleType& rhs) const {
return element_types < rhs.element_types;
}
void add_element_type(const Type* type) {
element_types.push_back(type);
}
const std::vector<const Type*>& get_element_types() const {
return element_types;
}
};
class ArrayType: public Type {
const Type* element_type;
public:
ArrayType(const Type* element_type): element_type(element_type) {}
TypeId get_id() const override {
return TypeId::ARRAY;
}
bool operator <(const ArrayType& rhs) const {
return element_type < rhs.element_type;
}
const Type* get_element_type() const {
return element_type;
}
};
class StringType: public Type {
public:
TypeId get_id() const override {
return TypeId::STRING;
}
};
class StringIteratorType: public Type {
public:
TypeId get_id() const override {
return TypeId::STRING_ITERATOR;
}
};
class VoidType: public Type {
public:
TypeId get_id() const override {
return TypeId::VOID;
}
};
class ReferenceType: public Type {
const Type* type;
public:
ReferenceType(const Type* type): type(type) {}
TypeId get_id() const override {
return TypeId::REFERENCE;
}
bool operator <(const ReferenceType& rhs) const {
return type < rhs.type;
}
const Type* get_type() const {
return type;
}
};
class TypeType: public Type {
const Type* type;
public:
TypeType(const Type* type): type(type) {}
TypeId get_id() const override {
return TypeId::TYPE;
}
bool operator <(const TypeType& rhs) const {
return type < rhs.type;
}
const Type* get_type() const {
return type;
}
};
template <class T> class TypePointer {
T* pointer;
public:
TypePointer(T* pointer = nullptr): pointer(pointer) {}
TypePointer(const TypePointer&) = delete;
TypePointer(TypePointer&& type_pointer): pointer(std::exchange(type_pointer.pointer, nullptr)) {}
~TypePointer() {
if (pointer) {
delete pointer;
}
}
TypePointer& operator =(const TypePointer&) = delete;
TypePointer& operator =(TypePointer&& type_pointer) {
std::swap(pointer, type_pointer.pointer);
return *this;
}
operator T*() const {
return pointer;
}
};
template <class T> class TypeCompare {
public:
bool operator ()(const T* type1, const T* type2) const {
return *type1 < *type2;
}
using is_transparent = std::true_type;
};
class TypeInterner {
template <class T> using TypeSet = std::set<TypePointer<T>, TypeCompare<T>>;
template <class T> using TypeVector = std::vector<TypePointer<T>>;
static inline TypePointer<IntType> int_type;
static inline TypePointer<CharType> char_type;
static inline TypeSet<ClosureType> closure_types;
static inline TypeVector<StructType> struct_types;
static inline TypeVector<EnumType> enum_types;
static inline TypeSet<TupleType> tuple_types;
static inline TypeSet<ArrayType> array_types;
static inline TypePointer<StringType> string_type;
static inline TypePointer<StringIteratorType> string_iterator_type;
static inline TypePointer<VoidType> void_type;
static inline TypeSet<ReferenceType> reference_types;
static inline TypeSet<TypeType> type_types;
template <class T> static T* get_or_set(TypePointer<T>& type) {
if (type == nullptr) {
type = new T();
}
return type;
}
template <class T> static T* get_or_insert(TypeSet<T>& types, const T* type) {
auto iterator = types.find(type);
if (iterator == types.end()) {
iterator = types.emplace(new T(*type)).first;
}
return *iterator;
}
template <class T> static T* create(TypeVector<T>& types) {
T* type = new T();
types.push_back(type);
return type;
}
public:
static const Type* get_int_type() {
return get_or_set(int_type);
}
static const Type* get_char_type() {
return get_or_set(char_type);
}
static const Type* intern(const ClosureType* closure_type) {
return get_or_insert(closure_types, closure_type);
}
static StructType* create_struct_type() {
return create(struct_types);
}
static EnumType* create_enum_type() {
return create(enum_types);
}
static const Type* intern(const TupleType* tuple_type) {
return get_or_insert(tuple_types, tuple_type);
}
static const Type* get_array_type(const Type* element_type) {
ArrayType array_type(element_type);
return get_or_insert(array_types, &array_type);
}
static const Type* get_string_type() {
return get_or_set(string_type);
}
static const Type* get_string_iterator_type() {
return get_or_set(string_iterator_type);
}
static const Type* get_void_type() {
return get_or_set(void_type);
}
static const Type* get_reference_type(const Type* type) {
ReferenceType reference_type(type);
return get_or_insert(reference_types, &reference_type);
}
static const Type* get_type_type(const Type* type) {
TypeType type_type(type);
return get_or_insert(type_types, &type_type);
}
};
class IntLiteral;
class BinaryExpression;
class ArrayLiteral;
class StringLiteral;
class If;
class TupleLiteral;
class TupleAccess;
class StructLiteral;
class StructAccess;
class EnumLiteral;
class Switch;
class CaseVariable;
class Closure;
class ClosureAccess;
class Argument;
class ClosureCall;
class MethodCall;
class FunctionCall;
class Intrinsic;
class VoidLiteral;
class Bind;
class Return;
class TypeLiteral;
class StructTypeDeclaration;
class StructTypeDefinition;
class EnumTypeDeclaration;
class EnumTypeDefinition;
class TypeAssert;
class ReturnType;
template <class T> class Visitor {
public:
virtual T visit_int_literal(const IntLiteral&) {
return T();
}
virtual T visit_binary_expression(const BinaryExpression&) {
return T();
}
virtual T visit_array_literal(const ArrayLiteral&) {
return T();
}
virtual T visit_string_literal(const StringLiteral&) {
return T();
}
virtual T visit_if(const If&) {
return T();
}
virtual T visit_tuple_literal(const TupleLiteral&) {
return T();
}
virtual T visit_tuple_access(const TupleAccess&) {
return T();
}
virtual T visit_struct_literal(const StructLiteral&) {
return T();
}
virtual T visit_struct_access(const StructAccess&) {
return T();
}
virtual T visit_enum_literal(const EnumLiteral&) {
return T();
}
virtual T visit_switch(const Switch&) {
return T();
}
virtual T visit_case_variable(const CaseVariable&) {
return T();
}
virtual T visit_closure(const Closure&) {
return T();
}
virtual T visit_closure_access(const ClosureAccess&) {
return T();
}
virtual T visit_argument(const Argument&) {
return T();
}
virtual T visit_closure_call(const ClosureCall&) {
return T();
}
virtual T visit_method_call(const MethodCall&) {
return T();
}
virtual T visit_function_call(const FunctionCall&) {
return T();
}
virtual T visit_intrinsic(const Intrinsic&) {
return T();
}
virtual T visit_void_literal(const VoidLiteral&) {
return T();
}
virtual T visit_bind(const Bind&) {
return T();
}
virtual T visit_return(const Return&) {
return T();
}
virtual T visit_type_literal(const TypeLiteral&) {
return T();
}
virtual T visit_struct_type_declaration(const StructTypeDeclaration&) {
return T();
}
virtual T visit_struct_type_definition(const StructTypeDefinition&) {
return T();
}
virtual T visit_enum_type_declaration(const EnumTypeDeclaration&) {
return T();
}
virtual T visit_enum_type_definition(const EnumTypeDefinition&) {
return T();
}
virtual T visit_type_assert(const TypeAssert&) {
return T();
}
virtual T visit_return_type(const ReturnType&) {
return T();
}
};
class Expression {
const Type* type;
std::size_t position;
public:
const Expression* next_expression = nullptr;
Expression(const Type* type = nullptr): type(type) {}
virtual ~Expression() = default;
virtual void accept(Visitor<void>& visitor) const = 0;
const Type* get_type() const {
return type;
}
void set_type(const Type* type) {
this->type = type;
}
TypeId get_type_id() const {
return get_type()->get_id();
}
void set_position(std::size_t position) {
this->position = position;
}
std::size_t get_position() const {
return position;
}
};
template <class T> T visit(Visitor<T>& visitor, const Expression* expression) {
class VoidVisitor: public Visitor<void> {
Visitor<T>& visitor;
public:
T result;
VoidVisitor(Visitor<T>& visitor): visitor(visitor) {}
void visit_int_literal(const IntLiteral& int_literal) override {
result = visitor.visit_int_literal(int_literal);
}
void visit_binary_expression(const BinaryExpression& binary_expression) override {
result = visitor.visit_binary_expression(binary_expression);
}
void visit_array_literal(const ArrayLiteral& array_literal) override {
result = visitor.visit_array_literal(array_literal);
}
void visit_string_literal(const StringLiteral& string_literal) override {
result = visitor.visit_string_literal(string_literal);
}
void visit_if(const If& if_) override {
result = visitor.visit_if(if_);
}
void visit_tuple_literal(const TupleLiteral& tuple_literal) override {
result = visitor.visit_tuple_literal(tuple_literal);
}
void visit_tuple_access(const TupleAccess& tuple_access) override {
result = visitor.visit_tuple_access(tuple_access);
}
void visit_struct_literal(const StructLiteral& struct_literal) override {
result = visitor.visit_struct_literal(struct_literal);
}
void visit_struct_access(const StructAccess& struct_access) override {
result = visitor.visit_struct_access(struct_access);
}
void visit_enum_literal(const EnumLiteral& enum_literal) override {
result = visitor.visit_enum_literal(enum_literal);
}
void visit_switch(const Switch& switch_) override {
result = visitor.visit_switch(switch_);
}
void visit_case_variable(const CaseVariable& case_variable) override {
result = visitor.visit_case_variable(case_variable);
}
void visit_closure(const Closure& closure) override {
result = visitor.visit_closure(closure);
}
void visit_closure_access(const ClosureAccess& closure_access) override {
result = visitor.visit_closure_access(closure_access);
}
void visit_argument(const Argument& argument) override {
result = visitor.visit_argument(argument);
}
void visit_closure_call(const ClosureCall& call) override {
result = visitor.visit_closure_call(call);
}
void visit_method_call(const MethodCall& call) override {
result = visitor.visit_method_call(call);
}
void visit_function_call(const FunctionCall& call) override {
result = visitor.visit_function_call(call);
}
void visit_intrinsic(const Intrinsic& intrinsic) override {
result = visitor.visit_intrinsic(intrinsic);
}
void visit_void_literal(const VoidLiteral& void_literal) override {
result = visitor.visit_void_literal(void_literal);
}
void visit_bind(const Bind& bind) override {
result = visitor.visit_bind(bind);
}
void visit_return(const Return& return_) override {
result = visitor.visit_return(return_);
}
void visit_type_literal(const TypeLiteral& type_literal) override {
result = visitor.visit_type_literal(type_literal);
}
void visit_struct_type_declaration(const StructTypeDeclaration& struct_type_declaration) override {
result = visitor.visit_struct_type_declaration(struct_type_declaration);
}
void visit_struct_type_definition(const StructTypeDefinition& struct_type_definition) override {
result = visitor.visit_struct_type_definition(struct_type_definition);
}
void visit_enum_type_declaration(const EnumTypeDeclaration& enum_type_declaration) override {
result = visitor.visit_enum_type_declaration(enum_type_declaration);
}
void visit_enum_type_definition(const EnumTypeDefinition& enum_type_definition) override {
result = visitor.visit_enum_type_definition(enum_type_definition);
}
void visit_type_assert(const TypeAssert& type_assert) override {
result = visitor.visit_type_assert(type_assert);
}
void visit_return_type(const ReturnType& return_type) override {
result = visitor.visit_return_type(return_type);
}
};
VoidVisitor void_visitor(visitor);
expression->accept(void_visitor);
return void_visitor.result;
}
inline void visit(Visitor<void>& visitor, const Expression* expression) {
expression->accept(visitor);
}
class Return: public Expression {
const Expression* expression;
public:
Return(const Expression* expression): Expression(TypeInterner::get_void_type()), expression(expression) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_return(*this);
}
const Expression* get_expression() const {
return expression;
}
};
class Block {
const Expression* first = nullptr;
Expression* last = nullptr;
public:
Block() = default;
Block(const Block&) = delete;
Block(Block&& block): first(std::exchange(block.first, nullptr)), last(std::exchange(block.last, nullptr)) {}
~Block() {
const Expression* expression = first;
while (expression) {
const Expression* next = expression->next_expression;
delete expression;
expression = next;
}
}
Block& operator =(const Block&) = delete;
Block& operator =(Block&& block) {
std::swap(first, block.first);
std::swap(last, block.last);
return *this;
}
void add_expression(Expression* expression) {
if (first == nullptr) {
first = expression;
}
if (last) {
last->next_expression = expression;
}
last = expression;
}
const Expression* get_last() const {
return last;
}
class Iterator {
const Expression* expression;
public:
constexpr Iterator(const Expression* expression): expression(expression) {}
constexpr bool operator !=(const Iterator& rhs) const {
return expression != rhs.expression;
}
Iterator& operator ++() {
expression = expression->next_expression;
return *this;
}
constexpr const Expression* operator *() const {
return expression;
}
};
constexpr Iterator begin() const {
return Iterator(first);
}
constexpr Iterator end() const {
return Iterator(nullptr);
}
};
class IntLiteral: public Expression {
std::int32_t value;
public:
IntLiteral(std::int32_t value): Expression(TypeInterner::get_int_type()), value(value) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_int_literal(*this);
}
std::int32_t get_value() const {
return value;
}
};
enum class BinaryOperation {
ADD,
SUB,
MUL,
DIV,
REM,
EQ,
NE,
LT,
LE,
GT,
GE
};
class BinaryExpression: public Expression {
BinaryOperation operation;
const Expression* left;
const Expression* right;
public:
BinaryExpression(BinaryOperation operation, const Expression* left, const Expression* right): Expression(TypeInterner::get_int_type()), operation(operation), left(left), right(right) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_binary_expression(*this);
}
BinaryOperation get_operation() const {
return operation;
}
const Expression* get_left() const {
return left;
}
const Expression* get_right() const {
return right;
}
template <BinaryOperation operation> static Expression* create(const Expression* left, const Expression* right) {
return new BinaryExpression(operation, left, right);
}
};
class ArrayLiteral: public Expression {
std::vector<const Expression*> elements;
public:
ArrayLiteral(const Type* type = nullptr): Expression(type) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_array_literal(*this);
}
void add_element(const Expression* element) {
elements.push_back(element);
}
const std::vector<const Expression*>& get_elements() const {
return elements;
}
};
class StringLiteral: public Expression {
std::string value;
public:
StringLiteral(const std::string& value): Expression(TypeInterner::get_string_type()), value(value) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_string_literal(*this);
}
const std::string& get_value() const {
return value;
}
};
class If: public Expression {
const Expression* condition;
Block then_block;
Block else_block;
public:
If(const Expression* condition, const Type* type = nullptr): Expression(type), condition(condition) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_if(*this);
}
const Expression* get_condition() const {
return condition;
}
Block* get_then_block() {
return &then_block;
}
const Block& get_then_block() const {
return then_block;
}
Block* get_else_block() {
return &else_block;
}
const Block& get_else_block() const {
return else_block;
}
};
class TupleLiteral: public Expression {
std::vector<const Expression*> elements;
public:
TupleLiteral(const Type* type = nullptr): Expression(type) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_tuple_literal(*this);
}
void add_element(const Expression* element) {
elements.push_back(element);
}
const std::vector<const Expression*>& get_elements() const {
return elements;
}
};
class TupleAccess: public Expression {
const Expression* tuple;
std::size_t index;
public:
TupleAccess(const Expression* tuple, std::size_t index, const Type* type = nullptr): Expression(type), tuple(tuple), index(index) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_tuple_access(*this);
}
const Expression* get_tuple() const {
return tuple;
}
std::size_t get_index() const {
return index;
}
};
class StructLiteral: public Expression {
const Expression* type_expression;
std::vector<std::pair<std::string, const Expression*>> fields;
public:
StructLiteral(const Expression* type_expression): Expression(nullptr), type_expression(type_expression) {}
StructLiteral(const Type* type = nullptr): Expression(type), type_expression(nullptr) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_struct_literal(*this);
}
void add_field(const std::string& field_name, const Expression* field) {
fields.emplace_back(field_name, field);
}
void add_field(const StringView& field_name, const Expression* field) {
fields.emplace_back(std::string(field_name.begin(), field_name.end()), field);
}
const Expression* get_type_expression() const {
return type_expression;
}
const std::vector<std::pair<std::string, const Expression*>>& get_fields() const {
return fields;
}
};
class StructAccess: public Expression {
const Expression* struct_;
std::string field_name;
public:
StructAccess(const Expression* struct_, const std::string& field_name, const Type* type = nullptr): Expression(type), struct_(struct_), field_name(field_name) {}
StructAccess(const Expression* struct_, const StringView& field_name, const Type* type = nullptr): Expression(type), struct_(struct_), field_name(field_name.begin(), field_name.end()) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_struct_access(*this);
}
const Expression* get_struct() const {
return struct_;
}
const std::string& get_field_name() const {
return field_name;
}
};
class EnumLiteral: public Expression {
const Expression* expression;
std::size_t index;
public:
EnumLiteral(const Expression* expression, std::size_t index, const Type* type = nullptr): Expression(type), expression(expression), index(index) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_enum_literal(*this);
}
const Expression* get_expression() const {
return expression;
}
std::size_t get_index() const {
return index;
}
};
class Switch: public Expression {
const Expression* enum_;
std::vector<std::pair<std::string, Block>> cases;
public:
Switch(const Expression* enum_, const Type* type = nullptr): Expression(type), enum_(enum_) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_switch(*this);
}
Block* add_case(const std::string& field_name) {
cases.emplace_back(std::piecewise_construct, std::forward_as_tuple(field_name), std::forward_as_tuple());
return &cases.back().second;
}
Block* add_case(const StringView& field_name) {
cases.emplace_back(std::piecewise_construct, std::forward_as_tuple(field_name.begin(), field_name.end()), std::forward_as_tuple());
return &cases.back().second;
}
const Expression* get_enum() const {
return enum_;
}
const std::vector<std::pair<std::string, Block>>& get_cases() const {
return cases;
}
};
class CaseVariable: public Expression {
public:
CaseVariable(const Type* type = nullptr): Expression(type) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_case_variable(*this);
}
};
class Function {
std::string path;
Block block;
std::size_t arguments;
std::vector<const Type*> argument_types;
const Type* return_type;
public:
const Function* next_function = nullptr;
Function(const Type* return_type = nullptr): arguments(0), return_type(return_type) {}
Function(const std::vector<const Type*>& argument_types, const Type* return_type = nullptr): arguments(argument_types.size()), argument_types(argument_types), return_type(return_type) {}
std::size_t add_argument() {
return arguments++;
}
void set_return_type(const Type* type) {
this->return_type = type;
}
std::size_t get_arguments() const {
return arguments;
}
const std::vector<const Type*>& get_argument_types() const {
return argument_types;
}
const Type* get_return_type() const {
return return_type;
}
Block* get_block() {
return █
}
const Block& get_block() const {
return block;
}
void set_path(const char *path) {
this->path = std::string(path);
}
const char* get_path() const {
return path.empty() ? nullptr : path.c_str();
}
};
class Closure: public Expression {
const Function* function;
std::vector<const Expression*> environment_expressions;
public:
Closure(const Function* function, const Type* type = nullptr): Expression(type), function(function) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_closure(*this);
}
std::size_t add_environment_expression(const Expression* expression) {
const std::size_t index = environment_expressions.size();
environment_expressions.push_back(expression);
return index;
}
const Function* get_function() const {
return function;
}
const std::vector<const Expression*>& get_environment_expressions() const {
return environment_expressions;
}
};
class ClosureAccess: public Expression {
const Expression* closure;
std::size_t index;
public:
ClosureAccess(const Expression* closure, std::size_t index, const Type* type = nullptr): Expression(type), closure(closure), index(index) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_closure_access(*this);
}
const Expression* get_closure() const {
return closure;
}
std::size_t get_index() const {
return index;
}
};
class Argument: public Expression {
std::size_t index;
public:
Argument(std::size_t index, const Type* type = nullptr): Expression(type), index(index) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_argument(*this);
}
std::size_t get_index() const {
return index;
}
};
class ClosureCall: public Expression {
const Expression* closure;
std::vector<const Expression*> arguments;
public:
ClosureCall(const Expression* closure): Expression(nullptr), closure(closure) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_closure_call(*this);
}
void add_argument(const Expression* expression) {
arguments.push_back(expression);
}
const Expression* get_closure() const {
return closure;
}
const std::vector<const Expression*>& get_arguments() const {
return arguments;
}
};
class MethodCall: public Expression {
const Expression* object;
std::string method_name;
const Expression* method;
std::vector<const Expression*> arguments;
public:
MethodCall(const Expression* object, const StringView& method_name, const Expression* method): Expression(nullptr), object(object), method_name(method_name.begin(), method_name.end()), method(method) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_method_call(*this);
}
void add_argument(const Expression* expression) {
arguments.push_back(expression);
}
const Expression* get_object() const {
return object;
}
const std::string& get_method_name() const {
return method_name;
}
const Expression* get_method() const {
return method;
}
const std::vector<const Expression*>& get_arguments() const {
return arguments;
}
};
class FunctionCall: public Expression {
std::vector<const Expression*> arguments;
const Function* function = nullptr;
public:
FunctionCall(const Type* type = nullptr): Expression(type) {}
void accept(Visitor<void>& visitor) const override {
visitor.visit_function_call(*this);
}