-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.hpp
1133 lines (1113 loc) · 33.1 KB
/
parser.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 "ast.hpp"
#include <map>
#include <cstdlib>
struct BinaryOperator {
const char* string;
using Create = Expression* (*)(const Expression* left, const Expression* right);
Create create;
constexpr BinaryOperator(const char* string, Create create): string(string), create(create) {}
};
using OperatorLevel = std::initializer_list<BinaryOperator>;
constexpr std::initializer_list<OperatorLevel> operators = {
{
BinaryOperator(">>", Bind::create)
},
{
BinaryOperator("==", BinaryExpression::create<BinaryOperation::EQ>),
BinaryOperator("!=", BinaryExpression::create<BinaryOperation::NE>)
},
{
BinaryOperator("<", BinaryExpression::create<BinaryOperation::LT>),
BinaryOperator("<=", BinaryExpression::create<BinaryOperation::LE>),
BinaryOperator(">", BinaryExpression::create<BinaryOperation::GT>),
BinaryOperator(">=", BinaryExpression::create<BinaryOperation::GE>)
},
{
BinaryOperator("+", BinaryExpression::create<BinaryOperation::ADD>),
BinaryOperator("-", BinaryExpression::create<BinaryOperation::SUB>)
},
{
BinaryOperator("*", BinaryExpression::create<BinaryOperation::MUL>),
BinaryOperator("/", BinaryExpression::create<BinaryOperation::DIV>),
BinaryOperator("%", BinaryExpression::create<BinaryOperation::REM>)
}
};
struct UnaryOperator {
const char* string;
using Create = Expression* (*)(const Expression* expression);
Create create;
constexpr UnaryOperator(const char* string, Create create): string(string), create(create) {}
};
constexpr std::initializer_list<UnaryOperator> unary_operators = {};
constexpr const char* intrinsics[] = {
"putChar",
"putStr",
"getChar",
"arrayGet",
"arrayLength",
"arraySplice",
"stringPush",
"stringIterator",
"stringIteratorGetNext",
"reference",
"typeOf",
"arrayType",
"tupleType",
"referenceType",
"error",
"import"
};
class Scope {
Scope*& current_scope;
Scope* parent;
std::map<StringView, const Expression*> variables;
Closure* closure;
const Expression* self = nullptr;
Block* block;
public:
Scope(Scope*& current_scope, Closure* closure, Block* block): current_scope(current_scope), closure(closure), block(block) {
parent = current_scope;
current_scope = this;
}
Scope(Scope*& current_scope, Block* block): Scope(current_scope, nullptr, block) {}
Scope(Scope*& current_scope): Scope(current_scope, nullptr, nullptr) {}
~Scope() {
current_scope = parent;
}
void set_self(const Expression* self) {
this->self = self;
}
void add_variable(const StringView& name, const Expression* value) {
variables[name] = value;
}
const Expression* look_up(const StringView& name) {
auto iterator = variables.find(name);
if (iterator != variables.end()) {
return iterator->second;
}
if (closure) {
if (parent) {
if (const Expression* expression = parent->look_up(name)) {
const std::size_t index = closure->add_environment_expression(expression);
const Expression* argument = create<ClosureAccess>(self, index);
add_variable(name, argument);
return argument;
}
}
}
else if (parent) {
return parent->look_up(name);
}
return nullptr;
}
void add_expression(Expression* expression) {
if (block) {
block->add_expression(expression);
}
else {
parent->add_expression(expression);
}
}
template <class T, class... A> T* create(A&&... arguments) {
T* expression = new T(std::forward<A>(arguments)...);
add_expression(expression);
return expression;
}
};
class Cursor {
const SourceFile* file;
const char* position;
public:
Cursor(const SourceFile* file): file(file), position(file->begin()) {}
constexpr Cursor(const SourceFile* file, const char* position): file(file), position(position) {}
operator bool() const {
return position < file->end();
}
constexpr bool operator <(const Cursor& rhs) const {
return position < rhs.position;
}
constexpr char operator *() const {
return *position;
}
Cursor& operator ++() {
++position;
return *this;
}
constexpr StringView operator -(const Cursor& start) const {
return StringView(start.position, position - start.position);
}
const char* get_path() const {
return file->get_path();
}
std::size_t get_position() const {
return position - file->begin();
}
};
template <class F> class CharParser {
F f;
public:
constexpr CharParser(F f): f(f) {}
template <class C> bool parse(C& cursor) {
if (cursor && f(*cursor)) {
++cursor;
return true;
}
return false;
}
};
class StringParser {
StringView s;
public:
constexpr StringParser(const StringView& s): s(s) {}
template <class C> bool parse(C& cursor) {
C copy = cursor;
for (char c: s) {
if (!(copy && *copy == c)) {
return false;
}
++copy;
}
cursor = copy;
return true;
}
};
template <class P0, class P1> class SequenceParser {
P0 p0;
P1 p1;
public:
constexpr SequenceParser(P0 p0, P1 p1): p0(p0), p1(p1) {}
template <class C> bool parse(C& cursor) {
C copy = cursor;
if (!p0.parse(copy)) {
return false;
}
if (!p1.parse(copy)) {
return false;
}
cursor = copy;
return true;
}
};
template <class P0, class P1> class ChoiceParser {
P0 p0;
P1 p1;
public:
constexpr ChoiceParser(P0 p0, P1 p1): p0(p0), p1(p1) {}
template <class C> bool parse(C& cursor) {
if (p0.parse(cursor)) {
return true;
}
if (p1.parse(cursor)) {
return true;
}
return false;
}
};
template <class P> class RepeatParser {
P p;
public:
constexpr RepeatParser(P p): p(p) {}
template <class C> bool parse(C& cursor) {
while (p.parse(cursor)) {}
return true;
}
};
template <class P> class NotParser {
P p;
public:
constexpr NotParser(P p): p(p) {}
template <class C> bool parse(C& cursor) {
C copy = cursor;
if (p.parse(copy)) {
return false;
}
return true;
}
};
template <class P> class PeekParser {
P p;
public:
constexpr PeekParser(P p): p(p) {}
template <class C> bool parse(C& cursor) {
C copy = cursor;
if (p.parse(copy)) {
return true;
}
return false;
}
};
template <class P, class = bool> struct is_parser: std::false_type {};
template <class P> struct is_parser<P, decltype(std::declval<P>().parse(std::declval<Cursor&>()))>: std::true_type {};
template <class F, class = bool> struct is_char_class: std::false_type {};
template <class F> struct is_char_class<F, decltype(std::declval<F>()(std::declval<char>()))>: std::true_type {};
class Parser {
Cursor cursor;
public:
static constexpr auto get_parser(char c) {
return CharParser([c](char c2) {
return c == c2;
});
}
static constexpr StringParser get_parser(const StringView& s) {
return StringParser(s);
}
static constexpr StringParser get_parser(const char* s) {
return StringParser(s);
}
template <class P> static constexpr std::enable_if_t<is_parser<P>::value, P> get_parser(P p) {
return p;
}
template <class F> static constexpr std::enable_if_t<is_char_class<F>::value, CharParser<F>> get_parser(F f) {
return CharParser(f);
}
static constexpr auto range(char first, char last) {
return CharParser([first, last](char c) {
return c >= first && c <= last;
});
}
template <class P0, class P1> static constexpr auto sequence(P0 p0, P1 p1) {
return SequenceParser(get_parser(p0), get_parser(p1));
}
template <class P0, class P1, class P2, class... P> static constexpr auto sequence(P0 p0, P1 p1, P2 p2, P... p) {
return sequence(sequence(p0, p1), p2, p...);
}
template <class P0, class P1> static constexpr auto choice(P0 p0, P1 p1) {
return ChoiceParser(get_parser(p0), get_parser(p1));
}
template <class P0, class P1, class P2, class... P> static constexpr auto choice(P0 p0, P1 p1, P2 p2, P... p) {
return choice(choice(p0, p1), p2, p...);
}
template <class P> static constexpr auto zero_or_more(P p) {
return RepeatParser(get_parser(p));
}
template <class P> static constexpr auto one_or_more(P p) {
return sequence(p, zero_or_more(p));
}
template <class P> static constexpr auto not_(P p) {
return NotParser(get_parser(p));
}
template <class P> static constexpr auto peek(P p) {
return PeekParser(get_parser(p));
}
static constexpr bool any_char(char c) {
return true;
}
static constexpr bool white_space(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
static constexpr bool numeric(char c) {
return c >= '0' && c <= '9';
}
static constexpr bool alphabetic(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
}
static constexpr bool alphanumeric(char c) {
return alphabetic(c) || numeric(c);
}
static constexpr bool operator_char(char c) {
return StringView("+-*/%=<>!&|~^?:").contains(c);
}
template <class P> StringView parse(P p) {
const Cursor start = cursor;
if (get_parser(p).parse(cursor)) {
return cursor - start;
}
else {
return StringView();
}
}
Parser(const SourceFile* file): cursor(file) {}
Parser(const Cursor& cursor): cursor(cursor) {}
const char* get_path() const {
return cursor.get_path();
}
std::size_t get_position() const {
return cursor.get_position();
}
};
class MoebiusParser: private Parser {
using SourcePosition = std::size_t;
Program* program;
Scope* current_scope = nullptr;
static constexpr auto keyword(const StringView& s) {
return sequence(s, not_(alphanumeric));
}
template <class T> [[noreturn]] void error(std::size_t position, const T& t) {
print_error(Printer(std::cerr), get_path(), position, t);
std::exit(EXIT_FAILURE);
}
template <class T> [[noreturn]] void error(const T& t) {
error(get_position(), t);
}
void expect(const StringView& s) {
if (!parse(s)) {
error(format("expected \"%\"", s));
}
}
void expect_keyword(const StringView& s) {
if (!parse(keyword(s))) {
error(format("expected \"%\"", s));
}
}
bool parse_comment() {
if (parse("//")) {
parse(zero_or_more(sequence(not_("\n"), any_char)));
return true;
}
if (parse("/*")) {
parse(zero_or_more(sequence(not_("*/"), any_char)));
expect("*/");
return true;
}
return false;
}
void parse_white_space() {
parse(zero_or_more(white_space));
while (parse_comment()) {
parse(zero_or_more(white_space));
}
}
char parse_character() {
if (parse('\\')) {
if (parse('n')) {
return '\n';
}
else if (parse('r')) {
return '\r';
}
else if (parse('t')) {
return '\t';
}
else if (parse('v')) {
return '\v';
}
else if (StringView s = parse(choice('\'', '\"', '\\', '$'))) {
return *s;
}
else {
error("invalid escape");
}
}
else if (StringView s = parse(any_char)) {
return *s;
}
else {
error("unexpected end");
}
}
const Expression* parse_string_segment() {
const SourcePosition position = get_position();
constexpr auto string_segment_end_char = [](char c) constexpr {
return c == '"' || c == '$';
};
if (parse('$')) {
const Expression* expression;
if (parse("{")) {
parse_white_space();
expression = parse_expression();
parse_white_space();
expect("}");
}
else {
StringView identifier = parse_identifier();
expression = current_scope->look_up(identifier);
if (expression == nullptr) {
error(position, format("undefined variable \"%\"", identifier));
}
}
const Expression* function = current_scope->look_up("toString");
if (function == nullptr) {
error(position, "function toString not defined");
}
ClosureCall* call = current_scope->create<ClosureCall>(function);
call->set_position(position);
call->add_argument(expression);
return call;
}
else {
std::string string;
while (parse(sequence(not_(string_segment_end_char), peek(any_char)))) {
string.push_back(parse_character());
}
StringLiteral* string_literal = current_scope->create<StringLiteral>(string);
//string_literal->set_position(position);
return string_literal;
}
}
StringView parse_identifier() {
if (!parse(peek(alphabetic))) {
error("expected an identifier");
}
return parse(zero_or_more(alphanumeric));
}
const BinaryOperator* parse_binary_operator(const OperatorLevel* level) {
for (const BinaryOperator& op: *level) {
if (parse(sequence(op.string, not_(operator_char)))) {
return &op;
}
}
return nullptr;
}
const UnaryOperator* parse_unary_operator() {
for (const UnaryOperator& op: unary_operators) {
if (parse(sequence(op.string, not_(operator_char)))) {
return &op;
}
}
return nullptr;
}
const char* parse_intrinsic_name() {
StringView name = parse_identifier();
for (const char* intrinsic_name: intrinsics) {
if (name == intrinsic_name) {
return intrinsic_name;
}
}
error(format("unknown intrinsic \"%\"", name));
}
const Expression* parse_expression_last() {
const SourcePosition position = get_position();
if (parse("{")) {
parse_white_space();
if (parse(peek(keyword("let"))) || parse(peek(keyword("return"))) || parse(peek(keyword("func"))) || parse(peek(keyword("struct"))) || parse(peek(keyword("enum")))) {
const Expression* expression = parse_scope();
parse_white_space();
expect("}");
return expression;
}
else {
StructTypeDeclaration* struct_type_declaration = current_scope->create<StructTypeDeclaration>();
StructTypeDefinition* struct_type_definition = new StructTypeDefinition(struct_type_declaration);
StructLiteral* struct_literal = new StructLiteral(struct_type_definition);
struct_type_definition->set_position(position);
struct_literal->set_position(position);
while (parse(not_("}"))) {
const StringView field_name = parse_identifier();
parse_white_space();
const Expression* field;
if (parse(":")) {
parse_white_space();
field = parse_expression();
parse_white_space();
}
else {
field = current_scope->look_up(field_name);
if (field == nullptr) {
error(format("undefined variable \"%\"", field_name));
}
}
Intrinsic* field_type = current_scope->create<Intrinsic>("typeOf");
field_type->add_argument(field);
struct_type_definition->add_field(field_name, field_type);
struct_literal->add_field(field_name, field);
if (!parse(",")) {
break;
}
parse_white_space();
}
expect("}");
current_scope->add_expression(struct_type_definition);
current_scope->add_expression(struct_literal);
return struct_literal;
}
}
else if (parse("(")) {
parse_white_space();
std::vector<const Expression*> elements;
while (parse(not_(")"))) {
elements.push_back(parse_expression());
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect(")");
if (elements.size() == 1) {
return elements[0];
}
else {
TupleLiteral* tuple = current_scope->create<TupleLiteral>();
tuple->set_position(position);
for (const Expression* element: elements) {
tuple->add_element(element);
}
return tuple;
}
}
else if (parse(keyword("if"))) {
parse_white_space();
expect("(");
parse_white_space();
const Expression* condition = parse_expression();
parse_white_space();
expect(")");
parse_white_space();
If* if_ = new If(condition);
if_->set_position(position);
{
Scope scope(current_scope, if_->get_then_block());
const Expression* then_expression = parse_expression();
current_scope->create<Return>(then_expression);
}
parse_white_space();
expect_keyword("else");
parse_white_space();
{
Scope scope(current_scope, if_->get_else_block());
const Expression* else_expression = parse_expression();
current_scope->create<Return>(else_expression);
}
current_scope->add_expression(if_);
return if_;
}
else if (parse(keyword("switch"))) {
parse_white_space();
expect("(");
parse_white_space();
const Expression* enum_ = parse_expression();
parse_white_space();
expect(")");
parse_white_space();
expect("{");
parse_white_space();
Switch* switch_ = new Switch(enum_);
switch_->set_position(position);
while (parse(not_("}"))) {
const StringView case_name = parse_identifier();
parse_white_space();
expect(":");
parse_white_space();
Scope scope(current_scope, switch_->add_case(case_name));
current_scope->add_variable(case_name, current_scope->create<CaseVariable>());
const Expression* case_expression = parse_expression();
current_scope->create<Return>(case_expression);
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect("}");
current_scope->add_expression(switch_);
return switch_;
}
else if (parse(keyword("func"))) {
parse_white_space();
expect("(");
parse_white_space();
Function* function = new Function();
function->set_path(get_path());
program->add_function(function);
Closure* closure = new Closure(function);
closure->set_position(position);
{
Scope scope(current_scope, closure, function->get_block());
current_scope->set_self(current_scope->create<Argument>(function->add_argument()));
while (parse(not_(")"))) {
auto [argument_name, type_assert_position, argument_type] = parse_name();
const Expression* argument = current_scope->create<Argument>(function->add_argument());
current_scope->add_variable(argument_name, argument);
if (argument_type) {
TypeAssert* type_assert = current_scope->create<TypeAssert>(argument, argument_type);
type_assert->set_position(type_assert_position);
}
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect(")");
parse_white_space();
const SourcePosition return_type_position = get_position();
if (parse(":")) {
parse_white_space();
const Expression* type = parse_expression();
ReturnType* return_type = current_scope->create<ReturnType>(type);
return_type->set_position(return_type_position);
parse_white_space();
}
expect("=>");
parse_white_space();
const Expression* expression = parse_expression();
current_scope->create<Return>(expression);
}
current_scope->add_expression(closure);
return closure;
}
else if (parse(keyword("struct"))) {
parse_white_space();
expect("{");
parse_white_space();
StructTypeDeclaration* struct_type_declaration = current_scope->create<StructTypeDeclaration>();
StructTypeDefinition* struct_type_definition = new StructTypeDefinition(struct_type_declaration);
struct_type_definition->set_position(position);
while (parse(not_("}"))) {
const StringView field_name = parse_identifier();
parse_white_space();
expect(":");
parse_white_space();
const Expression* field_type = parse_expression();
struct_type_definition->add_field(field_name, field_type);
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect("}");
current_scope->add_expression(struct_type_definition);
return struct_type_definition;
}
else if (parse(keyword("enum"))) {
parse_white_space();
expect("{");
parse_white_space();
EnumTypeDeclaration* enum_type_declaration = current_scope->create<EnumTypeDeclaration>();
EnumTypeDefinition* enum_type_definition = new EnumTypeDefinition(enum_type_declaration);
enum_type_definition->set_position(position);
while (parse(not_("}"))) {
const StringView case_name = parse_identifier();
parse_white_space();
if (parse(":")) {
parse_white_space();
const Expression* case_type = parse_expression();
parse_white_space();
enum_type_definition->add_case(case_name, case_type);
}
else {
const Expression* case_type = current_scope->create<TypeLiteral>(TypeInterner::get_void_type());
enum_type_definition->add_case(case_name, case_type);
}
if (!parse(",")) {
break;
}
parse_white_space();
}
expect("}");
current_scope->add_expression(enum_type_definition);
return enum_type_definition;
}
else if (parse("\"")) {
const Expression* left = parse_string_segment();
while (parse(sequence(not_("\""), peek(any_char)))) {
const Expression* right = parse_string_segment();
Intrinsic* intrinsic = current_scope->create<Intrinsic>("stringPush");
intrinsic->add_argument(left);
intrinsic->add_argument(right);
left = intrinsic;
}
expect("\"");
return left;
}
else if (parse("'")) {
IntLiteral* int_literal = current_scope->create<IntLiteral>(parse_character());
int_literal->set_position(position);
expect("'");
return int_literal;
}
else if (parse("[")) {
parse_white_space();
ArrayLiteral* array_literal = new ArrayLiteral();
array_literal->set_position(position);
while (parse(not_("]"))) {
array_literal->add_element(parse_expression());
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect("]");
current_scope->add_expression(array_literal);
return array_literal;
}
else if (parse(keyword("false"))) {
Expression* expression = current_scope->create<IntLiteral>(0);
expression->set_position(position);
return expression;
}
else if (parse(keyword("true"))) {
Expression* expression = current_scope->create<IntLiteral>(1);
expression->set_position(position);
return expression;
}
else if (parse(keyword("void"))) {
Expression* expression = current_scope->create<VoidLiteral>();
expression->set_position(position);
return expression;
}
else if (parse(keyword("Int"))) {
Expression* expression = current_scope->create<TypeLiteral>(TypeInterner::get_int_type());
expression->set_position(position);
return expression;
}
else if (parse(keyword("String"))) {
Expression* expression = current_scope->create<TypeLiteral>(TypeInterner::get_string_type());
expression->set_position(position);
return expression;
}
else if (parse(keyword("StringIterator"))) {
Expression* expression = current_scope->create<TypeLiteral>(TypeInterner::get_string_iterator_type());
expression->set_position(position);
return expression;
}
else if (parse(keyword("Void"))) {
Expression* expression = current_scope->create<TypeLiteral>(TypeInterner::get_void_type());
expression->set_position(position);
return expression;
}
else if (parse(peek(numeric))) {
std::int32_t number = 0;
for (char c: parse(zero_or_more(numeric))) {
number *= 10;
number += c - '0';
}
Expression* expression = current_scope->create<IntLiteral>(number);
expression->set_position(position);
return expression;
}
else if (parse(peek(alphabetic))) {
StringView identifier = parse_identifier();
const Expression* expression = current_scope->look_up(identifier);
if (expression == nullptr) {
error(position, format("undefined variable \"%\"", identifier));
}
return expression;
}
else if (parse("@")) {
const char* name = parse_intrinsic_name();
parse_white_space();
expect("(");
parse_white_space();
Intrinsic* intrinsic = new Intrinsic(name);
intrinsic->set_position(position);
while (parse(not_(")"))) {
intrinsic->add_argument(parse_expression());
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect(")");
current_scope->add_expression(intrinsic);
return intrinsic;
}
else {
error("expected an expression");
}
}
const Expression* parse_expression(const OperatorLevel* level = operators.begin()) {
if (level == operators.end()) {
const SourcePosition position = get_position();
if (const UnaryOperator* op = parse_unary_operator()) {
parse_white_space();
Expression* expression = op->create(parse_expression(level));
expression->set_position(position);
current_scope->add_expression(expression);
return expression;
}
const Expression* expression = parse_expression_last();
parse_white_space();
while (true) {
const SourcePosition position = get_position();
if (parse("(")) {
parse_white_space();
ClosureCall* call = new ClosureCall(expression);
call->set_position(position);
while (parse(not_(")"))) {
call->add_argument(parse_expression());
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect(")");
current_scope->add_expression(call);
expression = call;
parse_white_space();
}
else if (parse(".")) {
parse_white_space();
StringView name = parse_identifier();
parse_white_space();
const SourcePosition method_call_position = get_position();
if (parse("(")) {
parse_white_space();
const Expression* method = current_scope->look_up(name);
MethodCall* call = new MethodCall(expression, name, method);
call->set_position(method_call_position);
while (parse(not_(")"))) {
call->add_argument(parse_expression());
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect(")");
current_scope->add_expression(call);
expression = call;
parse_white_space();
}
else {
StructAccess* struct_access = current_scope->create<StructAccess>(expression, name);
struct_access->set_position(position);
expression = struct_access;
}
}
else if (parse("{")) {
parse_white_space();
StructLiteral* struct_literal = new StructLiteral(expression);
struct_literal->set_position(position);
while (parse(not_("}"))) {
const StringView field_name = parse_identifier();
parse_white_space();
const Expression* field;
if (parse(":")) {
parse_white_space();
field = parse_expression();
parse_white_space();
}
else {
field = current_scope->look_up(field_name);
if (field == nullptr) {
error(format("undefined variable \"%\"", field_name));
}
}
struct_literal->add_field(field_name, field);
if (!parse(",")) {
break;
}
parse_white_space();
}
expect("}");
current_scope->add_expression(struct_literal);
TypeAssert* type_assert = current_scope->create<TypeAssert>(struct_literal, expression);
type_assert->set_position(position);
expression = struct_literal;
parse_white_space();
}
else {
break;
}
}
return expression;
}
const Expression* left = parse_expression(level + 1);
parse_white_space();
SourcePosition position = get_position();
while (const BinaryOperator* op = parse_binary_operator(level)) {
parse_white_space();
const Expression* right = parse_expression(level + 1);
Expression* expression = op->create(left, right);
expression->set_position(position);
current_scope->add_expression(expression);
left = expression;
parse_white_space();
position = get_position();
}
return left;
}
std::tuple<StringView, SourcePosition, const Expression*> parse_name() {
const StringView name = parse_identifier();
parse_white_space();
const SourcePosition type_assert_position = get_position();
if (parse(":")) {
parse_white_space();
const Expression* type = parse_expression();
return std::make_tuple(name, type_assert_position, type);
}
else {
return std::make_tuple(name, type_assert_position, nullptr);
}
}
const Expression* parse_scope() {
Scope scope(current_scope);
while (true) {
const SourcePosition position = get_position();
if (parse(keyword("let"))) {
parse_white_space();
std::vector<std::tuple<StringView, SourcePosition, const Expression*>> element_names;
if (parse("(")) {
parse_white_space();
while (parse(not_(")"))) {
element_names.push_back(parse_name());
parse_white_space();
if (!parse(",")) {
break;
}
parse_white_space();
}
expect(")");
}
else {
element_names.push_back(parse_name());
}
parse_white_space();
expect("=");
parse_white_space();
const Expression* expression = parse_expression();
if (element_names.size() == 1) {
auto [name, type_assert_position, type] = element_names[0];
if (type) {
TypeAssert* type_assert = current_scope->create<TypeAssert>(expression, type);
type_assert->set_position(type_assert_position);
}
current_scope->add_variable(name, expression);
}
else for (std::size_t i = 0; i < element_names.size(); ++i) {
auto [name, type_assert_position, type] = element_names[i];
TupleAccess* tuple_access = current_scope->create<TupleAccess>(expression, i);
tuple_access->set_position(position);
if (type) {
TypeAssert* type_assert = current_scope->create<TypeAssert>(tuple_access, type);
type_assert->set_position(type_assert_position);
}
current_scope->add_variable(name, tuple_access);
}
parse_white_space();
}
else if (parse(keyword("func"))) {
parse_white_space();
const StringView name = parse_identifier();
parse_white_space();
expect("(");