-
Notifications
You must be signed in to change notification settings - Fork 1
/
passes.hpp
2213 lines (2203 loc) · 98.7 KB
/
passes.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 "ast.hpp"
#include <filesystem>
// type checking, monomorphization, and constant propagation
class Pass1: public Visitor<const Expression*> {
static std::int32_t execute_binary_operation(BinaryOperation operation, std::int32_t left, std::int32_t right) {
switch (operation) {
case BinaryOperation::ADD:
return left + right;
case BinaryOperation::SUB:
return left - right;
case BinaryOperation::MUL:
return left * right;
case BinaryOperation::DIV:
return left / right;
case BinaryOperation::REM:
return left % right;
case BinaryOperation::EQ:
return left == right;
case BinaryOperation::NE:
return left != right;
case BinaryOperation::LT:
return left < right;
case BinaryOperation::LE:
return left <= right;
case BinaryOperation::GT:
return left > right;
case BinaryOperation::GE:
return left >= right;
default:
return 0;
}
}
struct FunctionTableKey {
const Function* old_function;
std::vector<const Type*> argument_types;
FunctionTableKey(const Function* old_function): old_function(old_function) {}
FunctionTableKey() {}
bool operator <(const FunctionTableKey& rhs) const {
return std::make_pair(old_function, std::ref(argument_types)) < std::make_pair(rhs.old_function, std::ref(rhs.argument_types));
}
};
using FunctionTable = std::map<FunctionTableKey, Function*>;
using FileTable = std::map<std::string, const Function*>;
Program* old_program;
Program* program;
FileTable& file_table;
FunctionTable& function_table;
const FunctionTableKey& key;
const Type* case_type;
const Expression* case_variable;
using ExpressionTable = std::map<const Expression*, const Expression*>;
ExpressionTable& expression_table;
Block* destination_block;
bool omit_return;
const Expression* result = nullptr;
Pass1(Program* old_program, Program* program, FileTable& file_table, FunctionTable& function_table, const FunctionTableKey& key, const Type* case_type, const Expression* case_variable, ExpressionTable& expression_table, Block* destination_block, bool omit_return): old_program(old_program), program(program), file_table(file_table), function_table(function_table), key(key), case_type(case_type), case_variable(case_variable), expression_table(expression_table), destination_block(destination_block), omit_return(omit_return) {}
static const Expression* evaluate(Program* old_program, Program* program, FileTable& file_table, FunctionTable& function_table, const FunctionTableKey& key, const Type* case_type, const Expression* case_variable, ExpressionTable& expression_table, Block* destination_block, const Block& source_block, bool omit_return) {
Pass1 pass1(old_program, program, file_table, function_table, key, case_type, case_variable, expression_table, destination_block, omit_return);
for (const Expression* expression: source_block) {
const Expression* new_expression = visit(pass1, expression);
if (new_expression) {
expression_table[expression] = new_expression;
}
}
return pass1.result;
}
static const Expression* evaluate(Program* old_program, Program* program, FileTable& file_table, FunctionTable& function_table, const FunctionTableKey& key, Block* destination_block, const Block& source_block) {
ExpressionTable expression_table;
return evaluate(old_program, program, file_table, function_table, key, nullptr, nullptr, expression_table, destination_block, source_block, false);
}
const Expression* evaluate(const FunctionTableKey& key, Block* destination_block, const Block& source_block) {
return evaluate(old_program, program, file_table, function_table, key, destination_block, source_block);
}
const Expression* evaluate(Block* destination_block, const Block& source_block, bool omit_return) {
return evaluate(old_program, program, file_table, function_table, key, nullptr, nullptr, expression_table, destination_block, source_block, omit_return);
}
const Expression* evaluate(const Type* case_type, Block* destination_block, const Block& source_block, const Expression* case_variable = nullptr) {
return evaluate(old_program, program, file_table, function_table, key, case_type, case_variable, expression_table, destination_block, source_block, case_variable != nullptr);
}
template <class T> [[noreturn]] void error(const Expression& expression, const T& t) {
print_error(Printer(std::cerr), key.old_function->get_path(), expression.get_position(), t);
std::exit(EXIT_FAILURE);
}
template <class T, class... A> T* create(A&&... arguments) {
T* expression = new T(std::forward<A>(arguments)...);
destination_block->add_expression(expression);
return expression;
}
static const IntLiteral* get_int_literal(const Expression* expression) {
GetInt visitor;
return visit(visitor, expression);
}
static const ArrayLiteral* get_array_literal(const Expression* expression) {
GetArray visitor;
return visit(visitor, expression);
}
static const StringLiteral* get_string_literal(const Expression* expression) {
GetString visitor;
return visit(visitor, expression);
}
static const EnumLiteral* get_enum_literal(const Expression* expression) {
GetEnum visitor;
return visit(visitor, expression);
}
public:
const Expression* visit_int_literal(const IntLiteral& int_literal) override {
return create<IntLiteral>(int_literal.get_value());
}
const Expression* visit_binary_expression(const BinaryExpression& binary_expression) override {
const Expression* left = expression_table[binary_expression.get_left()];
const Expression* right = expression_table[binary_expression.get_right()];
if (left->get_type_id() == TypeId::INT && right->get_type_id() == TypeId::INT) {
if (const IntLiteral* left_literal = get_int_literal(left)) {
if (const IntLiteral* right_literal = get_int_literal(right)) {
return create<IntLiteral>(execute_binary_operation(binary_expression.get_operation(), left_literal->get_value(), right_literal->get_value()));
}
}
return create<BinaryExpression>(binary_expression.get_operation(), left, right);
}
else if (left->get_type_id() == TypeId::TYPE && right->get_type_id() == TypeId::TYPE) {
const Type* left_type = static_cast<const TypeType*>(left->get_type())->get_type();
const Type* right_type = static_cast<const TypeType*>(right->get_type())->get_type();
if (binary_expression.get_operation() == BinaryOperation::EQ) {
return create<IntLiteral>(left_type == right_type);
}
else if (binary_expression.get_operation() == BinaryOperation::NE) {
return create<IntLiteral>(left_type != right_type);
}
}
error(binary_expression, "invalid binary expression");
}
const Expression* visit_array_literal(const ArrayLiteral& array_literal) override {
if (array_literal.get_elements().size() == 0) {
error(array_literal, "emtpy arrays are not yet supported");
}
const Type* element_type = expression_table[array_literal.get_elements()[0]]->get_type();
if (element_type->get_id() == TypeId::TYPE) {
error(array_literal, "array elements must not be types");
}
ArrayLiteral* new_array_literal = create<ArrayLiteral>(TypeInterner::get_array_type(element_type));
for (const Expression* element: array_literal.get_elements()) {
const Expression* new_element = expression_table[element];
if (new_element->get_type() != element_type) {
error(array_literal, "array elements must have the same type");
}
new_array_literal->add_element(new_element);
}
return new_array_literal;
}
const Expression* visit_string_literal(const StringLiteral& string_literal) override {
return create<StringLiteral>(string_literal.get_value());
}
const Expression* visit_if(const If& if_) override {
const Expression* condition = expression_table[if_.get_condition()];
if (condition->get_type_id() != TypeId::INT) {
error(if_, "if condition must be a number");
}
if (const IntLiteral* condition_literal = get_int_literal(condition)) {
const Block& block = condition_literal->get_value() ? if_.get_then_block() : if_.get_else_block();
return evaluate(destination_block, block, true);
}
else {
If* new_if = create<If>(condition);
const Expression* then_expression = evaluate(new_if->get_then_block(), if_.get_then_block(), false);
const Expression* else_expression = evaluate(new_if->get_else_block(), if_.get_else_block(), false);
if (then_expression->get_type() != else_expression->get_type()) {
error(if_, "if and else branches must have the same type");
}
new_if->set_type(then_expression->get_type());
return new_if;
}
}
const Expression* visit_tuple_literal(const TupleLiteral& tuple_literal) override {
TupleType type;
TupleLiteral* new_tuple_literal = create<TupleLiteral>();
for (const Expression* element: tuple_literal.get_elements()) {
const Expression* new_element = expression_table[element];
type.add_element_type(new_element->get_type());
new_tuple_literal->add_element(new_element);
}
new_tuple_literal->set_type(TypeInterner::intern(&type));
return new_tuple_literal;
}
const Expression* visit_tuple_access(const TupleAccess& tuple_access) override {
const std::size_t index = tuple_access.get_index();
const Expression* tuple = expression_table[tuple_access.get_tuple()];
if (tuple->get_type_id() != TypeId::TUPLE) {
error(tuple_access, "tuple access to non-tuple");
}
const TupleType* tuple_type = static_cast<const TupleType*>(tuple->get_type());
if (index >= tuple_type->get_element_types().size()) {
error(tuple_access, "tuple index out of bounds");
}
GetTupleElement get_tuple_element(index);
if (const Expression* element = visit(get_tuple_element, tuple)) {
return element;
}
const Type* type = tuple_type->get_element_types()[index];
return create<TupleAccess>(tuple, index, type);
}
const Expression* visit_struct_literal(const StructLiteral& struct_literal) override {
const Type* type;
if (struct_literal.get_type_expression()) {
const Expression* type_expression = expression_table[struct_literal.get_type_expression()];
if (type_expression->get_type_id() != TypeId::TYPE) {
error(struct_literal, "expression must be a type");
}
type = static_cast<const TypeType*>(type_expression->get_type())->get_type();
if (type->get_id() != TypeId::STRUCT) {
error(struct_literal, "expression must be a struct type");
}
const StructType* struct_type = static_cast<const StructType*>(type);
for (std::size_t i = 0; i < struct_type->get_fields().size(); ++i) {
const std::string& field_name = struct_type->get_fields()[i].first;
if (i >= struct_literal.get_fields().size()) {
error(struct_literal, format("missing field \"%\"", field_name));
}
const std::string& actual_field_name = struct_literal.get_fields()[i].first;
if (actual_field_name != field_name) {
error(struct_literal, format("expected field \"%\" instead of \"%\"", field_name, actual_field_name));
}
const Type* field_type = struct_type->get_fields()[i].second;
const Expression* field = expression_table[struct_literal.get_fields()[i].second];
if (field->get_type() != field_type) {
error(struct_literal, format("field \"%\" must have type %", field_name, print_type(field_type)));
}
}
if (struct_literal.get_fields().size() > struct_type->get_fields().size()) {
const std::string& actual_field_name = struct_literal.get_fields()[struct_type->get_fields().size()].first;
error(struct_literal, format("superfluous field \"%\"", actual_field_name));
}
}
else {
type = struct_literal.get_type();
}
StructLiteral* new_struct_literal = create<StructLiteral>(type);
for (const auto& field: struct_literal.get_fields()) {
const std::string& field_name = field.first;
const Expression* new_field = expression_table[field.second];
new_struct_literal->add_field(field_name, new_field);
}
return new_struct_literal;
}
const StructType* get_struct_type(const Expression* struct_) {
if (struct_->get_type_id() == TypeId::STRUCT) {
return static_cast<const StructType*>(struct_->get_type());
}
if (struct_->get_type_id() == TypeId::REFERENCE) {
const Type* type = static_cast<const ReferenceType*>(struct_->get_type())->get_type();
if (type->get_id() == TypeId::STRUCT) {
return static_cast<const StructType*>(type);
}
}
return nullptr;
}
const Expression* visit_struct_access(const StructAccess& struct_access) override {
const Expression* struct_ = expression_table[struct_access.get_struct()];
if (const StructType* struct_type = get_struct_type(struct_)) {
if (struct_type->has_field(struct_access.get_field_name())) {
const std::size_t index = struct_type->get_index(struct_access.get_field_name());
GetTupleElement get_tuple_element(index);
if (const Expression* element = visit(get_tuple_element, struct_)) {
return element;
}
const Type* type = struct_type->get_fields()[index].second;
return create<StructAccess>(struct_, struct_access.get_field_name(), type);
}
}
if (struct_->get_type_id() == TypeId::TYPE) {
const Type* type = static_cast<const TypeType*>(struct_->get_type())->get_type();
if (type->get_id() == TypeId::ENUM) {
const EnumType* enum_type = static_cast<const EnumType*>(type);
if (enum_type->has_case(struct_access.get_field_name())) {
const std::size_t index = enum_type->get_index(struct_access.get_field_name());
if (enum_type->get_cases()[index].second != TypeInterner::get_void_type()) {
error(struct_access, format("case \"%\" requires an argument", struct_access.get_field_name()));
}
VoidLiteral* void_literal = create<VoidLiteral>();
return create<EnumLiteral>(void_literal, index, enum_type);
}
}
}
error(struct_access, "invalid struct access");
}
const Expression* visit_enum_literal(const EnumLiteral& enum_literal) override {
const Expression* expression = expression_table[enum_literal.get_expression()];
return create<EnumLiteral>(expression, enum_literal.get_index(), enum_literal.get_type());
}
const EnumType* get_enum_type(const Switch& switch_, const Expression* enum_) {
if (enum_->get_type_id() == TypeId::ENUM) {
return static_cast<const EnumType*>(enum_->get_type());
}
if (enum_->get_type_id() == TypeId::REFERENCE) {
const Type* type = static_cast<const ReferenceType*>(enum_->get_type())->get_type();
if (type->get_id() == TypeId::ENUM) {
return static_cast<const EnumType*>(type);
}
}
error(switch_, "switch expression must be an enum");
}
const Expression* visit_switch(const Switch& switch_) override {
const Expression* enum_ = expression_table[switch_.get_enum()];
const EnumType* enum_type = get_enum_type(switch_, enum_);
for (std::size_t i = 0; i < enum_type->get_cases().size(); ++i) {
const std::string& case_name = enum_type->get_cases()[i].first;
if (i >= switch_.get_cases().size()) {
error(switch_, format("missing case \"%\"", case_name));
}
const std::string& actual_case_name = switch_.get_cases()[i].first;
if (actual_case_name != case_name) {
error(switch_, format("expected case \"%\" instead of \"%\"", case_name, actual_case_name));
}
}
if (switch_.get_cases().size() > enum_type->get_cases().size()) {
const std::string& actual_case_name = switch_.get_cases()[enum_type->get_cases().size()].first;
error(switch_, format("superfluous case \"%\"", actual_case_name));
}
if (const EnumLiteral* enum_literal = get_enum_literal(enum_)) {
const Type* case_type = enum_type->get_cases()[enum_literal->get_index()].second;
const Block& block = switch_.get_cases()[enum_literal->get_index()].second;
return evaluate(case_type, destination_block, block, enum_literal->get_expression());
}
Switch* new_switch = create<Switch>(enum_);
for (std::size_t i = 0; i < enum_type->get_cases().size(); ++i) {
const std::string& case_name = enum_type->get_cases()[i].first;
const Type* case_type = enum_type->get_cases()[i].second;
const Block& block = switch_.get_cases()[i].second;
const Expression* case_expression = evaluate(case_type, new_switch->add_case(case_name), block);
if (new_switch->get_type()) {
if (case_expression->get_type() != new_switch->get_type()) {
error(switch_, "cases must have the same type");
}
}
else {
new_switch->set_type(case_expression->get_type());
}
}
return new_switch;
}
const Expression* visit_case_variable(const CaseVariable& case_variable) override {
if (this->case_variable) {
return this->case_variable;
}
else {
return create<CaseVariable>(case_type);
}
}
const Expression* visit_closure(const Closure& closure) override {
ClosureType type(closure.get_function());
Closure* new_closure = create<Closure>(nullptr);
for (const Expression* expression: closure.get_environment_expressions()) {
const Expression* new_expression = expression_table[expression];
type.add_environment_type(new_expression->get_type());
new_closure->add_environment_expression(new_expression);
}
new_closure->set_type(TypeInterner::intern(&type));
return new_closure;
}
const Expression* visit_closure_access(const ClosureAccess& closure_access) override {
const std::size_t argument_index = closure_access.get_index();
const Expression* closure = expression_table[closure_access.get_closure()];
const ClosureType* closure_type = static_cast<const ClosureType*>(closure->get_type());
const Type* type = closure_type->get_environment_types()[argument_index];
return create<ClosureAccess>(closure, argument_index, type);
}
const Expression* visit_argument(const Argument& argument) override {
const std::size_t argument_index = argument.get_index();
const Type* type = key.argument_types[argument_index];
return create<Argument>(argument_index, type);
}
const Expression* visit_call(const Expression& call, const Function* function, const Expression* closure, const Expression* object, const std::vector<const Expression*>& arguments) {
FunctionCall* new_call = create<FunctionCall>();
FunctionTableKey new_key;
if (closure) {
if (closure->get_type_id() != TypeId::CLOSURE) {
error(call, "call to a value that is not a function");
}
new_key.old_function = static_cast<const ClosureType*>(closure->get_type())->get_function();
new_call->add_argument(closure);
new_key.argument_types.push_back(closure->get_type());
}
else {
new_key.old_function = function;
}
if (object) {
const Expression* new_argument = expression_table[object];
new_call->add_argument(new_argument);
new_key.argument_types.push_back(new_argument->get_type());
}
for (const Expression* argument: arguments) {
const Expression* new_argument = expression_table[argument];
new_call->add_argument(new_argument);
new_key.argument_types.push_back(new_argument->get_type());
}
if (new_key.argument_types.size() != new_key.old_function->get_arguments()) {
std::size_t expected_arguments = new_key.old_function->get_arguments() - 1;
if (object) {
expected_arguments -= 1;
}
error(call, format("call with % to a function that accepts %", print_plural("argument", arguments.size()), print_plural("argument", expected_arguments)));
}
if (function_table[new_key] == nullptr) {
Function* new_function = new Function(new_key.argument_types, new_key.old_function->get_return_type());
program->add_function(new_function);
function_table[new_key] = new_function;
const Expression* new_expression = evaluate(new_key, new_function->get_block(), new_key.old_function->get_block());
if (new_function->get_return_type() && new_function->get_return_type() != new_expression->get_type()) {
error(call, format("function does not return the declared return type %", print_type(new_function->get_return_type())));
}
new_function->set_return_type(new_expression->get_type());
}
else {
// detect recursion
if (function_table[new_key]->get_return_type() == nullptr) {
error(call, "cannot determine return type of recursive call");
}
}
new_call->set_type(function_table[new_key]->get_return_type());
new_call->set_function(function_table[new_key]);
return new_call;
}
const Expression* visit_closure_call(const ClosureCall& call) override {
const Expression* closure = expression_table[call.get_closure()];
return visit_call(call, nullptr, closure, nullptr, call.get_arguments());
}
const Expression* visit_method_call(const MethodCall& call) override {
const Expression* object = expression_table[call.get_object()];
if (object->get_type_id() == TypeId::STRUCT) {
const StructType* struct_type = static_cast<const StructType*>(object->get_type());
if (struct_type->has_field(call.get_method_name())) {
const Expression* closure;
const std::size_t index = struct_type->get_index(call.get_method_name());
GetTupleElement get_tuple_element(index);
if (const Expression* element = visit(get_tuple_element, object)) {
closure = element;
}
else {
const Type* type = struct_type->get_fields()[index].second;
closure = create<StructAccess>(object, call.get_method_name(), type);
}
return visit_call(call, nullptr, closure, nullptr, call.get_arguments());
}
}
if (object->get_type_id() == TypeId::TYPE) {
const Type* type = static_cast<const TypeType*>(object->get_type())->get_type();
if (type->get_id() == TypeId::ENUM) {
const EnumType* enum_type = static_cast<const EnumType*>(type);
if (enum_type->has_case(call.get_method_name())) {
const std::size_t index = enum_type->get_index(call.get_method_name());
if (call.get_arguments().size() != 1) {
error(call, "enum literals must have exactly one argument");
}
const Expression* argument = expression_table[call.get_arguments()[0]];
if (argument->get_type() != enum_type->get_cases()[index].second) {
error(call, "invalid argument type");
}
return create<EnumLiteral>(argument, index, enum_type);
}
}
}
if (call.get_method()) {
const Expression* closure = expression_table[call.get_method()];
if (closure->get_type_id() == TypeId::CLOSURE) {
return visit_call(call, nullptr, closure, call.get_object(), call.get_arguments());
}
}
error(call, "invalid method call");
}
const Expression* visit_function_call(const FunctionCall& call) override {
return visit_call(call, call.get_function(), nullptr, nullptr, call.get_arguments());
}
void ensure_argument_count(const Intrinsic& intrinsic, std::size_t argument_count) {
if (intrinsic.get_arguments().size() != argument_count) {
error(intrinsic, format("% must be called with %", intrinsic.get_name(), print_plural("argument", argument_count)));
}
}
void ensure_argument_types(const Intrinsic& intrinsic, std::initializer_list<const Type*> types) {
ensure_argument_count(intrinsic, types.size());
std::size_t i = 0;
for (const Type* type: types) {
const Expression* argument = expression_table[intrinsic.get_arguments()[i]];
if (argument->get_type() != type) {
error(intrinsic, format("argument % of % must have type %", print_number(i + 1), intrinsic.get_name(), print_type(type)));
}
++i;
}
}
const Type* get_element_type(const Intrinsic& intrinsic, const Type* array_type) {
if (array_type->get_id() == TypeId::ARRAY) {
return static_cast<const ArrayType*>(array_type)->get_element_type();
}
else {
error(intrinsic, format("first argument of % must be an array", intrinsic.get_name()));
}
}
Intrinsic* create_intrinsic(const Intrinsic& intrinsic, const Type* type = nullptr) {
Intrinsic* new_intrinsic = create<Intrinsic>(intrinsic.get_name(), type);
for (const Expression* argument: intrinsic.get_arguments()) {
new_intrinsic->add_argument(expression_table[argument]);
}
return new_intrinsic;
}
static std::filesystem::path get_import_path(std::filesystem::path current_file, std::filesystem::path new_file) {
if (new_file.is_absolute()) {
return new_file;
}
else {
return current_file.parent_path() / new_file;
}
}
const Expression* visit_intrinsic(const Intrinsic& intrinsic) override {
if (intrinsic.name_equals("putChar")) {
ensure_argument_types(intrinsic, {TypeInterner::get_int_type()});
return create_intrinsic(intrinsic, TypeInterner::get_void_type());
}
else if (intrinsic.name_equals("putStr")) {
ensure_argument_types(intrinsic, {TypeInterner::get_string_type()});
return create_intrinsic(intrinsic, TypeInterner::get_void_type());
}
else if (intrinsic.name_equals("getChar")) {
ensure_argument_types(intrinsic, {});
return create_intrinsic(intrinsic, TypeInterner::get_int_type());
}
else if (intrinsic.name_equals("arrayGet")) {
ensure_argument_count(intrinsic, 2);
const Expression* array = expression_table[intrinsic.get_arguments()[0]];
const Expression* index = expression_table[intrinsic.get_arguments()[1]];
if (const ArrayLiteral* array_literal = get_array_literal(array)) {
if (const IntLiteral* index_literal = get_int_literal(index)) {
if (static_cast<std::size_t>(index_literal->get_value()) >= array_literal->get_elements().size()) {
error(intrinsic, "array index out of bounds");
}
return array_literal->get_elements()[index_literal->get_value()];
}
}
const Type* element_type = get_element_type(intrinsic, array->get_type());
if (index->get_type() != TypeInterner::get_int_type()) {
error(intrinsic, "second argument of arrayGet must be a number");
}
return create_intrinsic(intrinsic, element_type);
}
else if (intrinsic.name_equals("arrayLength")) {
ensure_argument_count(intrinsic, 1);
const Expression* array = expression_table[intrinsic.get_arguments()[0]];
if (const ArrayLiteral* array_literal = get_array_literal(array)) {
return create<IntLiteral>(array_literal->get_elements().size());
}
get_element_type(intrinsic, array->get_type());
return create_intrinsic(intrinsic, TypeInterner::get_int_type());
}
else if (intrinsic.name_equals("arraySplice")) {
if (intrinsic.get_arguments().size() < 3) {
error(intrinsic, "arraySplice takes at least 3 arguments");
}
const Type* array_type = expression_table[intrinsic.get_arguments()[0]]->get_type();
const Type* element_type = get_element_type(intrinsic, array_type);
if (expression_table[intrinsic.get_arguments()[1]]->get_type() != TypeInterner::get_int_type()) {
error(intrinsic, "second argument of arraySplice must be a number");
}
if (expression_table[intrinsic.get_arguments()[2]]->get_type() != TypeInterner::get_int_type()) {
error(intrinsic, "third argument of arraySplice must be a number");
}
if (intrinsic.get_arguments().size() == 4) {
const Type* argument_type = expression_table[intrinsic.get_arguments()[3]]->get_type();
if (!(argument_type == element_type || argument_type == array_type)) {
error(intrinsic, format("argument 4 of arraySplice must have type % or %", print_type(element_type), print_type(array_type)));
}
}
else {
for (std::size_t i = 3; i < intrinsic.get_arguments().size(); ++i) {
const Type* argument_type = expression_table[intrinsic.get_arguments()[i]]->get_type();
if (argument_type != element_type) {
error(intrinsic, format("argument % of arraySplice must have type %", print_number(i + 1), print_type(element_type)));
}
}
}
return create_intrinsic(intrinsic, array_type);
}
else if (intrinsic.name_equals("stringPush")) {
ensure_argument_count(intrinsic, 2);
const Expression* string = expression_table[intrinsic.get_arguments()[0]];
const Expression* argument = expression_table[intrinsic.get_arguments()[1]];
if (const StringLiteral* string_literal = get_string_literal(string)) {
if (const StringLiteral* argument_literal = get_string_literal(argument)) {
return create<StringLiteral>(string_literal->get_value() + argument_literal->get_value());
}
else if (const IntLiteral* argument_literal = get_int_literal(argument)) {
return create<StringLiteral>(string_literal->get_value() + from_codepoint(argument_literal->get_value()));
}
}
if (string->get_type() != TypeInterner::get_string_type()) {
error(intrinsic, "first argument of stringPush must be a string");
}
const Type* argument_type = argument->get_type();
if (!(argument_type == TypeInterner::get_int_type() || argument_type == TypeInterner::get_string_type())) {
error(intrinsic, "second argument of stringPush must be a number or a string");
}
return create_intrinsic(intrinsic, TypeInterner::get_string_type());
}
else if (intrinsic.name_equals("stringIterator")) {
ensure_argument_types(intrinsic, {TypeInterner::get_string_type()});
return create_intrinsic(intrinsic, TypeInterner::get_string_iterator_type());
}
else if (intrinsic.name_equals("stringIteratorGetNext")) {
ensure_argument_types(intrinsic, {TypeInterner::get_string_iterator_type()});
TupleType type;
type.add_element_type(TypeInterner::get_string_iterator_type());
type.add_element_type(TypeInterner::get_int_type());
type.add_element_type(TypeInterner::get_int_type());
return create_intrinsic(intrinsic, TypeInterner::intern(&type));
}
else if (intrinsic.name_equals("reference")) {
ensure_argument_count(intrinsic, 1);
const Type* type = expression_table[intrinsic.get_arguments()[0]]->get_type();
if (!(type->get_id() == TypeId::STRUCT || type->get_id() == TypeId::ENUM)) {
error(intrinsic, "only references to structs and enums are currently supported");
}
return create_intrinsic(intrinsic, TypeInterner::get_reference_type(type));
}
else if (intrinsic.name_equals("typeOf")) {
ensure_argument_count(intrinsic, 1);
const Expression* expression = expression_table[intrinsic.get_arguments()[0]];
return create<TypeLiteral>(expression->get_type());
}
else if (intrinsic.name_equals("arrayType")) {
ensure_argument_count(intrinsic, 1);
const Expression* element_type_expression = expression_table[intrinsic.get_arguments()[0]];
if (element_type_expression->get_type_id() != TypeId::TYPE) {
error(intrinsic, "argument of arrayType must be a type");
}
const Type* element_type = static_cast<const TypeType*>(element_type_expression->get_type())->get_type();
return create<TypeLiteral>(TypeInterner::get_array_type(element_type));
}
else if (intrinsic.name_equals("tupleType")) {
ensure_argument_count(intrinsic, 1);
const Expression* tuple_type_expression = expression_table[intrinsic.get_arguments()[0]];
if (tuple_type_expression->get_type_id() != TypeId::TUPLE) {
error(intrinsic, "argument of tupleType must be a tuple");
}
const TupleType* tuple_type = static_cast<const TupleType*>(tuple_type_expression->get_type());
TupleType new_tuple_type;
for (const Type* element: tuple_type->get_element_types()) {
if (element->get_id() != TypeId::TYPE) {
error(intrinsic, "tuple elements must be types");
}
new_tuple_type.add_element_type(static_cast<const TypeType*>(element)->get_type());
}
return create<TypeLiteral>(TypeInterner::intern(&new_tuple_type));
}
else if (intrinsic.name_equals("referenceType")) {
ensure_argument_count(intrinsic, 1);
const Expression* type_expression = expression_table[intrinsic.get_arguments()[0]];
if (type_expression->get_type_id() != TypeId::TYPE) {
error(intrinsic, "argument of referenceType must be a type");
}
const Type* type = static_cast<const TypeType*>(type_expression->get_type())->get_type();
return create<TypeLiteral>(TypeInterner::get_reference_type(type));
}
else if (intrinsic.name_equals("error")) {
ensure_argument_count(intrinsic, 1);
const StringLiteral* error_message = get_string_literal(expression_table[intrinsic.get_arguments()[0]]);
if (error_message == nullptr) {
error(intrinsic, "error message must be a compile-time string");
}
error(intrinsic, error_message->get_value());
}
else if (intrinsic.name_equals("import")) {
ensure_argument_count(intrinsic, 1);
const Expression* path_expression = expression_table[intrinsic.get_arguments()[0]];
const StringLiteral* path_literal = get_string_literal(path_expression);
if (!path_literal) {
error(intrinsic, "import path must be a compile-time string");
}
const std::string path = get_import_path(key.old_function->get_path(), path_literal->get_value()).lexically_normal().string();
if (file_table[path] == nullptr) {
file_table[path] = MoebiusParser::parse_program(path.c_str(), old_program);
}
FunctionCall* new_call = create<FunctionCall>();
FunctionTableKey new_key(file_table[path]);
if (function_table[new_key] == nullptr) {
Function* new_function = new Function(nullptr);
program->add_function(new_function);
function_table[new_key] = new_function;
const Expression* new_expression = evaluate(new_key, new_function->get_block(), new_key.old_function->get_block());
new_function->set_return_type(new_expression->get_type());
}
else {
// detect recursion
if (function_table[new_key]->get_return_type() == nullptr) {
error(intrinsic, "cannot determine return type of recursive import");
}
}
new_call->set_type(function_table[new_key]->get_return_type());
new_call->set_function(function_table[new_key]);
return new_call;
}
else if (intrinsic.name_equals("copy")) {
const Type* type = expression_table[intrinsic.get_arguments()[0]]->get_type();
return create_intrinsic(intrinsic, type);
}
else if (intrinsic.name_equals("free")) {
return create_intrinsic(intrinsic, TypeInterner::get_void_type());
}
else {
return create_intrinsic(intrinsic, TypeInterner::get_void_type());
}
}
const Expression* visit_void_literal(const VoidLiteral&) override {
return create<VoidLiteral>();
}
const Expression* visit_bind(const Bind& bind) override {
const Expression* left = expression_table[bind.get_left()];
const Expression* right = expression_table[bind.get_right()];
return create<Bind>(left, right, right->get_type());
}
const Expression* visit_return(const Return& return_) override {
result = expression_table[return_.get_expression()];
if (omit_return) {
return result;
}
else {
return create<Return>(result);
}
}
const Expression* visit_type_literal(const TypeLiteral& type_literal) override {
const Type* type = static_cast<const TypeType*>(type_literal.get_type())->get_type();
return create<TypeLiteral>(type);
}
const Expression* visit_struct_type_declaration(const StructTypeDeclaration& struct_type_declaration) override {
StructType* new_struct_type = TypeInterner::create_struct_type();
return create<StructTypeDeclaration>(new_struct_type);
}
const Expression* visit_struct_type_definition(const StructTypeDefinition& struct_type_definition) override {
const Expression* declaration = expression_table[struct_type_definition.get_declaration()];
StructType* new_struct_type = static_cast<const StructTypeDeclaration*>(declaration)->get_struct_type();
for (const auto& field: struct_type_definition.get_fields()) {
const std::string& field_name = field.first;
if (new_struct_type->has_field(field_name)) {
error(struct_type_definition, format("duplicate field \"%\"", field_name));
}
const Expression* type_expression = expression_table[field.second];
if (type_expression->get_type_id() != TypeId::TYPE) {
error(struct_type_definition, "struct fields must be types");
}
const Type* type = static_cast<const TypeType*>(type_expression->get_type())->get_type();
new_struct_type->add_field(field_name, type);
}
return create<TypeLiteral>(new_struct_type);
}
const Expression* visit_enum_type_declaration(const EnumTypeDeclaration& enum_type_declaration) override {
EnumType* new_enum_type = TypeInterner::create_enum_type();
return create<EnumTypeDeclaration>(new_enum_type);
}
const Expression* visit_enum_type_definition(const EnumTypeDefinition& enum_type_definition) override {
const Expression* declaration = expression_table[enum_type_definition.get_declaration()];
EnumType* new_enum_type = static_cast<const EnumTypeDeclaration*>(declaration)->get_enum_type();
for (const auto& case_: enum_type_definition.get_cases()) {
const std::string& case_name = case_.first;
if (new_enum_type->has_case(case_name)) {
error(enum_type_definition, format("duplicate case \"%\"", case_name));
}
const Expression* type_expression = expression_table[case_.second];
if (type_expression->get_type_id() != TypeId::TYPE) {
error(enum_type_definition, "enum cases must be types");
}
const Type* type = static_cast<const TypeType*>(type_expression->get_type())->get_type();
new_enum_type->add_case(case_name, type);
}
return create<TypeLiteral>(new_enum_type);
}
const Expression* visit_type_assert(const TypeAssert& type_assert) override {
const Expression* expression = expression_table[type_assert.get_expression()];
const Expression* type_expression = expression_table[type_assert.get_type()];
if (type_expression->get_type_id() != TypeId::TYPE) {
error(type_assert, "expression is not a type");
}
const Type* type = static_cast<const TypeType*>(type_expression->get_type())->get_type();
if (expression->get_type() != type) {
error(type_assert, format("expression does not have the declared type %", print_type(type)));
}
return nullptr;
}
const Expression* visit_return_type(const ReturnType& return_type) override {
const Expression* type_expression = expression_table[return_type.get_type()];
if (type_expression->get_type_id() != TypeId::TYPE) {
error(return_type, "return type must be a type");
}
const Type* type = static_cast<const TypeType*>(type_expression->get_type())->get_type();
function_table[key]->set_return_type(type);
return nullptr;
}
static Program run(const char* file_name) {
Program old_program;
FileTable file_table;
const std::string path = std::filesystem::path(file_name).lexically_normal().string();
file_table[path] = MoebiusParser::parse_program(path.c_str(), &old_program);
Program new_program;
FunctionTable function_table;
FunctionTableKey new_key(file_table[path]);
Function* new_function = new Function(TypeInterner::get_void_type());
new_program.add_function(new_function);
function_table[new_key] = new_function;
evaluate(&old_program, &new_program, file_table, function_table, new_key, new_function->get_block(), new_key.old_function->get_block());
return new_program;
}
static Program run(Program& program) {
const Function* main_function = program.get_main_function();
Program new_program;
FileTable file_table;
FunctionTable function_table;
FunctionTableKey new_key(main_function);
Function* new_function = new Function(main_function->get_return_type());
new_program.add_function(new_function);
function_table[new_key] = new_function;
evaluate(&program, &new_program, file_table, function_table, new_key, new_function->get_block(), new_key.old_function->get_block());
return new_program;
}
};
// lower closures to tuples
class Lowering: public Visitor<const Expression*> {
using TypeTable = std::map<const Type*, const Type*>;
TypeTable& type_table;
using FunctionTable = std::map<const Function*, Function*>;
FunctionTable& function_table;
using ExpressionTable = std::map<const Expression*, const Expression*>;
ExpressionTable& expression_table;
Block* destination_block;
template <class T, class... A> T* create(A&&... arguments) {
T* expression = new T(std::forward<A>(arguments)...);
destination_block->add_expression(expression);
return expression;
}
static const Type* transform_type(TypeTable& type_table, const Type* type) {
auto iterator = type_table.find(type);
if (iterator != type_table.end()) {
return iterator->second;
}
if (type->get_id() == TypeId::CLOSURE) {
TupleType tuple_type;
for (const Type* environment_type: static_cast<const ClosureType*>(type)->get_environment_types()) {
tuple_type.add_element_type(transform_type(type_table, environment_type));
}
const Type* transformed_type = TypeInterner::intern(&tuple_type);
type_table[type] = transformed_type;
return transformed_type;
}
if (type->get_id() == TypeId::STRUCT) {
StructType* struct_type = TypeInterner::create_struct_type();
type_table[type] = struct_type;
for (const auto& field: static_cast<const StructType*>(type)->get_fields()) {
struct_type->add_field(field.first, transform_type(type_table, field.second));
}
return struct_type;
}
if (type->get_id() == TypeId::ENUM) {
EnumType* enum_type = TypeInterner::create_enum_type();
type_table[type] = enum_type;
for (const auto& case_: static_cast<const EnumType*>(type)->get_cases()) {
enum_type->add_case(case_.first, transform_type(type_table, case_.second));
}
return enum_type;
}
if (type->get_id() == TypeId::ARRAY) {
const Type* element_type = static_cast<const ArrayType*>(type)->get_element_type();
const Type* transformed_type = TypeInterner::get_array_type(transform_type(type_table, element_type));
type_table[type] = transformed_type;
return transformed_type;
}
if (type->get_id() == TypeId::REFERENCE) {
const Type* value_type = static_cast<const ReferenceType*>(type)->get_type();
const Type* transformed_type = TypeInterner::get_reference_type(transform_type(type_table, value_type));
type_table[type] = transformed_type;
return transformed_type;
}
type_table[type] = type;
return type;
}
const Type* transform_type(const Type* type) {
return transform_type(type_table, type);
}
public:
Lowering(TypeTable& type_table, FunctionTable& function_table, ExpressionTable& expression_table, Block* destination_block): type_table(type_table), function_table(function_table), expression_table(expression_table), destination_block(destination_block) {}
static void evaluate(TypeTable& type_table, FunctionTable& function_table, ExpressionTable& expression_table, Block* destination_block, const Block& source_block) {
Lowering lowering(type_table, function_table, expression_table, destination_block);
for (const Expression* expression: source_block) {
const Expression* new_expression = visit(lowering, expression);
if (new_expression) {
expression_table[expression] = new_expression;
}
}
}
static void evaluate(TypeTable& type_table, FunctionTable& function_table, Block* destination_block, const Block& source_block) {
ExpressionTable expression_table;
evaluate(type_table, function_table, expression_table, destination_block, source_block);
}
void evaluate(Block* destination_block, const Block& source_block) {
evaluate(type_table, function_table, expression_table, destination_block, source_block);
}
const Expression* visit_int_literal(const IntLiteral& int_literal) override {
return create<IntLiteral>(int_literal.get_value());
}
const Expression* visit_binary_expression(const BinaryExpression& binary_expression) override {
const Expression* left = expression_table[binary_expression.get_left()];
const Expression* right = expression_table[binary_expression.get_right()];
return create<BinaryExpression>(binary_expression.get_operation(), left, right);
}
const Expression* visit_array_literal(const ArrayLiteral& array_literal) override {
ArrayLiteral* new_array_literal = create<ArrayLiteral>(transform_type(array_literal.get_type()));
for (const Expression* element: array_literal.get_elements()) {
new_array_literal->add_element(expression_table[element]);
}
return new_array_literal;
}
const Expression* visit_string_literal(const StringLiteral& string_literal) override {
return create<StringLiteral>(string_literal.get_value());
}
const Expression* visit_if(const If& if_) override {
const Expression* condition = expression_table[if_.get_condition()];
If* new_if = create<If>(condition, transform_type(if_.get_type()));
evaluate(new_if->get_then_block(), if_.get_then_block());
evaluate(new_if->get_else_block(), if_.get_else_block());
return new_if;
}
const Expression* visit_tuple_literal(const TupleLiteral& tuple_literal) override {
TupleLiteral* new_tuple_literal = create<TupleLiteral>(transform_type(tuple_literal.get_type()));
for (const Expression* element: tuple_literal.get_elements()) {
new_tuple_literal->add_element(expression_table[element]);
}
return new_tuple_literal;
}
const Expression* visit_tuple_access(const TupleAccess& tuple_access) override {
const Expression* tuple = expression_table[tuple_access.get_tuple()];
return create<TupleAccess>(tuple, tuple_access.get_index(), transform_type(tuple_access.get_type()));
}
const Expression* visit_struct_literal(const StructLiteral& struct_literal) override {
StructLiteral* new_struct_literal = create<StructLiteral>(transform_type(struct_literal.get_type()));
for (const auto& field: struct_literal.get_fields()) {
new_struct_literal->add_field(field.first, expression_table[field.second]);
}
return new_struct_literal;
}
const Expression* visit_struct_access(const StructAccess& struct_access) override {
const Expression* struct_ = expression_table[struct_access.get_struct()];
return create<StructAccess>(struct_, struct_access.get_field_name(), transform_type(struct_access.get_type()));
}
const Expression* visit_enum_literal(const EnumLiteral& enum_literal) override {
const Expression* expression = expression_table[enum_literal.get_expression()];
return create<EnumLiteral>(expression, enum_literal.get_index(), transform_type(enum_literal.get_type()));
}
const Expression* visit_switch(const Switch& switch_) override {
const Expression* enum_ = expression_table[switch_.get_enum()];
Switch* new_switch = create<Switch>(enum_, transform_type(switch_.get_type()));
for (const auto& case_: switch_.get_cases()) {
evaluate(new_switch->add_case(case_.first), case_.second);
}
return new_switch;
}
const Expression* visit_case_variable(const CaseVariable& case_variable) override {
return create<CaseVariable>(transform_type(case_variable.get_type()));
}
const Expression* visit_closure(const Closure& closure) override {
TupleLiteral* tuple_literal = create<TupleLiteral>(transform_type(closure.get_type()));
for (const Expression* expression: closure.get_environment_expressions()) {
tuple_literal->add_element(expression_table[expression]);
}
return tuple_literal;
}
const Expression* visit_closure_access(const ClosureAccess& closure_access) override {
const Expression* tuple = expression_table[closure_access.get_closure()];
return create<TupleAccess>(tuple, closure_access.get_index(), transform_type(closure_access.get_type()));
}
Expression* visit_argument(const Argument& argument) override {
return create<Argument>(argument.get_index(), transform_type(argument.get_type()));
}
const Expression* visit_function_call(const FunctionCall& call) override {
FunctionCall* new_call = create<FunctionCall>(transform_type(call.get_type()));
for (const Expression* argument: call.get_arguments()) {
new_call->add_argument(expression_table[argument]);
}
new_call->set_function(function_table[call.get_function()]);
return new_call;
}
const Expression* visit_intrinsic(const Intrinsic& intrinsic) override {
Intrinsic* new_intrinsic = create<Intrinsic>(intrinsic.get_name(), transform_type(intrinsic.get_type()));
for (const Expression* argument: intrinsic.get_arguments()) {
new_intrinsic->add_argument(expression_table[argument]);
}
return new_intrinsic;
}
const Expression* visit_void_literal(const VoidLiteral&) override {
return create<VoidLiteral>();
}
const Expression* visit_bind(const Bind& bind) override {
const Expression* left = expression_table[bind.get_left()];
const Expression* right = expression_table[bind.get_right()];