forked from see-sharp265/cornpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.cpp
1332 lines (1235 loc) · 59.6 KB
/
compile.cpp
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
#include "compile.hpp"
#include <cstdlib>
#include <functional>
#include <iostream>
#include <vector>
std::string DEBUG_TOKEN_TYPES[] = {"str", "identifier", "number", "decimal",
"bracket", "semi", "sep", "sym"};
entry_bracket::entry_bracket(char f, char s) {
first = f;
second = s;
}
bool check_id_constraints(std::string id, char c) {
bool retval = isalpha(c) || c == '_';
if (!id.empty()) retval |= isdigit(c);
return retval;
}
std::vector<token> tokenize_program(std::string program, int length,
logger::logger *logger) {
class {
public:
int row = 0;
int col = 0;
int chr_c = 0;
std::vector<token> tokens;
std::string full_token = "";
bool number = false;
struct {
bool going = false;
bool escaping = false;
} string;
struct {
bool going = false;
std::string id = "";
} identifier;
bool symbol = false;
bool comment = false;
struct {
bool going = false;
bool escaping = false;
} chr;
void reset() {
full_token = "";
number = false;
string.going = false;
string.escaping = false;
identifier.going = false;
identifier.id = "";
symbol = false;
comment = false;
chr.going = false;
chr.escaping = false;
}
void push(token_type t) {
tokens.push_back(token(t, full_token, row, col, chr_c));
}
} status;
auto error_out = [&](std::string errmsg,
logger::LOG_LEVEL level = logger::LOG_LEVEL::ERROR,
bool _exit = true) {
logger->log(level, errmsg);
std::string line = "";
std::string error_show = "^";
int k = status.chr_c;
while (k >= 0 && program[k] != '\n') {
line = program[k] + line;
error_show = "-" + error_show;
k--;
}
k = status.chr_c + 1;
while (k < length && program[k] != '\n') {
line += program[k];
k++;
}
logger->log(level, "");
logger->log(level, std::to_string(status.row + 1) + " | " + line);
for (int j = 0;
j < std::string(std::to_string(status.row + 1) + " | ").length();
j++) {
error_show = " " + error_show;
}
logger->log(level, error_show);
if (_exit) exit(-1);
};
status.full_token = "{";
status.push(token_type::bracket);
status.reset();
// traverse through the file char by char
for (int i = 0; i < length; i++) {
char c = program[i];
if (c == '\n') {
status.row++;
status.col = 0;
}
auto escape_chr = [&]() {
if (c == '\\') {
status.full_token += '\\';
} else if (c == '"') {
status.full_token += c;
} else if (c == 'n') {
status.full_token += '\n';
} else if (c == 't') {
status.full_token += '\t';
} else if (c == 'r') {
status.full_token += '\r';
} else {
error_out(std::string("Invalid escape character: \\") + (c));
}
};
if (status.comment) {
if (c == '\n') status.comment = false;
continue;
}
std::string old_full_token =
status
.full_token; // set old full token here to check for symbols later
if (status.string.going) {
if (status.string.escaping) {
escape_chr();
status.string.escaping = false;
} else {
if (c == '"') {
status.push(token_type::string);
status.reset();
} else if (c == '\\') {
status.string.escaping = true;
} else {
status.full_token += c;
}
}
} else if (status.chr.going) {
if (status.chr.escaping) {
escape_chr();
status.chr.escaping = false;
} else {
if (c == '\'') {
if (status.full_token.length() != 1) {
error_out("Invalid character literal size: " +
std::to_string(status.full_token.length()));
} else {
status.full_token = std::to_string(status.full_token[0]);
if (status.full_token.find('.') == std::string::npos) {
status.push(token_type::number);
} else {
status.push(token_type::decimal);
}
status.reset();
}
} else if (c == '\\') {
status.chr.escaping = true;
} else {
status.full_token += c;
}
}
} else if (check_id_constraints(status.identifier.id, c)) {
if (status.symbol) {
status.push(token_type::sym);
status.reset();
}
status.identifier.going = true;
status.identifier.id += c;
status.full_token += c;
} else {
if (status.identifier.going) {
status.push(token_type::identifier);
status.reset();
}
if (isdigit(c)) {
if (status.symbol) {
status.push(token_type::sym);
status.reset();
}
status.number = true;
status.full_token += c;
} else {
if (c == '.' && status.number) {
status.full_token += c;
} else {
if (status.number) {
if (status.full_token.find('.') == std::string::npos) {
status.push(token_type::number);
} else {
status.push(token_type::decimal);
}
status.reset();
}
if (c == '"') {
status.string.going = true;
} else if (c == '\'') {
status.chr.going = true;
} else if (std::string("()[]{}").find(c) != std::string::npos) {
status.full_token += c;
status.push(token_type::bracket);
status.reset();
} else if (c == ';') {
status.full_token += c;
status.push(token_type::semi);
status.reset();
} else if (c == ',') {
status.full_token += c;
status.push(token_type::sep);
status.reset();
} else if (c == '#') {
status.reset();
status.comment = true;
} else if (!isspace(c)) {
status.symbol = true;
status.full_token += c;
} else if (status.symbol) {
status.push(token_type::sym);
status.reset();
}
}
}
}
status.chr_c++;
status.col++;
}
status.full_token = "}";
status.push(token_type::bracket);
status.reset();
for (auto &t : status.tokens) {
logger->log(logger::LOG_LEVEL::DEBUG, "Token: " + t.value + "\t\t\tType: " +
DEBUG_TOKEN_TYPES[(int)t.type]);
}
return status.tokens;
}
ast_types::global_scope lex_program(file_object input_file,
std::vector<token> program_tokens,
logger::logger *logger) {
ast_types::global_scope globals;
std::function<int(int, std::vector<scope_element>, parsing_modes,
entry_bracket)>
recursive_lex = [&](int old_itt, std::vector<scope_element> scope,
parsing_modes parsing_mode,
entry_bracket entr) { // returns the new itt
auto error_out = [&](std::string error_message, token tk,
logger::LOG_LEVEL level = logger::LOG_LEVEL::ERROR,
bool _exit = true) {
logger->log(level, error_message);
std::string line = "";
std::string error_show = "^";
int k = tk.chr;
while (k >= 0 && input_file.contents[k] != '\n') {
line = input_file.contents[k] + line;
error_show = "-" + error_show;
k--;
}
k = tk.chr + 1;
while (k < input_file.length && input_file.contents[k] != '\n') {
line += input_file.contents[k];
k++;
}
logger->log(level, "");
logger->log(level, std::to_string(tk.row + 1) + " | " + line);
for (int j = 0;
j < std::string(std::to_string(tk.row + 1) + " | ").length();
j++) {
error_show = " " + error_show;
}
logger->log(level, error_show);
if (_exit) exit(-1);
};
auto goto_ast_scope = [&](std::vector<scope_element> in_scope) {
logger->log(logger::LOG_LEVEL::DEBUG, "Reaching into",
logger::SETTINGS::TYPE);
AST *that_ast = &(globals);
int scope_i = 0;
for (auto s : in_scope) { // copy, do not reference
if ((int)s >= 0) { // int
logger->log(logger::LOG_LEVEL::DEBUG,
" > [" + std::to_string((int)s) + "]",
logger::SETTINGS::NONE);
ast_body *_temp_body = dynamic_cast<ast_body *>(that_ast);
std::vector<AST *> _temp_inhere = _temp_body->body;
that_ast = _temp_inhere[(int)s];
} else {
switch ((scope_element)s) {
case scope_element::global:
that_ast =
&((dynamic_cast<ast_types::global_scope *>(that_ast))
->body);
logger->log(logger::LOG_LEVEL::DEBUG, " > global",
logger::SETTINGS::NONE);
break;
case scope_element::args:
that_ast =
&((dynamic_cast<ast_types::with_args *>(that_ast))->args);
logger->log(logger::LOG_LEVEL::DEBUG, " > args",
logger::SETTINGS::NONE);
break;
case scope_element::body:
that_ast =
&((dynamic_cast<ast_types::with_body *>(that_ast))->body);
logger->log(logger::LOG_LEVEL::DEBUG, " > body",
logger::SETTINGS::NONE);
break;
case scope_element::second_body:
that_ast =
&((dynamic_cast<ast_types::with_second_body *>(that_ast))
->second_body);
logger->log(logger::LOG_LEVEL::DEBUG, " > second_body",
logger::SETTINGS::NONE);
break;
case scope_element::type:
that_ast =
&((dynamic_cast<ast_types::with_type *>(that_ast))->type);
logger->log(logger::LOG_LEVEL::DEBUG, " > type",
logger::SETTINGS::NONE);
break;
case scope_element::return_type:
that_ast =
&((dynamic_cast<ast_types::with_return_type *>(that_ast))
->return_type);
logger->log(logger::LOG_LEVEL::DEBUG, " > return_type",
logger::SETTINGS::NONE);
break;
case scope_element::arr_index:
that_ast =
&((dynamic_cast<ast_types::arrget *>(that_ast))->index);
logger->log(logger::LOG_LEVEL::DEBUG, " > arr_index",
logger::SETTINGS::NONE);
break;
case scope_element::arr_array:
that_ast =
&((dynamic_cast<ast_types::arrget *>(that_ast))->array);
logger->log(logger::LOG_LEVEL::DEBUG, " > arr_array",
logger::SETTINGS::NONE);
break;
case scope_element::argtype:
that_ast = &(
(dynamic_cast<ast_types::with_args_with_type *>(that_ast))
->args);
logger->log(logger::LOG_LEVEL::DEBUG, " > argtype",
logger::SETTINGS::NONE);
break;
case scope_element::out_length:
that_ast = &(
(dynamic_cast<ast_types::out_type *>(that_ast))->length);
logger->log(logger::LOG_LEVEL::DEBUG, " > out_length",
logger::SETTINGS::NONE);
break;
case scope_element::values:
that_ast =
&((dynamic_cast<ast_types::with_values *>(that_ast))
->values);
logger->log(logger::LOG_LEVEL::DEBUG, " > values",
logger::SETTINGS::NONE);
break;
case scope_element::second_args:
that_ast =
&((dynamic_cast<ast_types::with_second_args *>(that_ast))
->second_args);
logger->log(logger::LOG_LEVEL::DEBUG, " > second_args",
logger::SETTINGS::NONE);
break;
default:
logger->log(
logger::LOG_LEVEL::ERROR,
"\nInvalid scope element: " + std::to_string((int)s));
exit(0);
}
}
}
logger->log(logger::LOG_LEVEL::DEBUG, "", logger::SETTINGS::NEWLINE);
return that_ast;
};
auto set_ast_scope = [&](std::vector<scope_element> scope, AST *val) {
*goto_ast_scope(scope) = *val;
};
auto append_ast_scope = [&](std::vector<scope_element> scope,
AST *val) {
int index_inserted;
AST *that_ast = goto_ast_scope(scope);
index_inserted = (dynamic_cast<ast_body *>(that_ast))->body.size();
(dynamic_cast<ast_body *>(that_ast))->body.push_back(val);
return index_inserted;
};
auto get_ast_scope = [&](std::vector<scope_element>
scope) { // returns a pointer to that ast
return goto_ast_scope(scope);
};
auto check_if_operation = [](token kword) {
return (
kword.value == "+" || kword.value == "-" || kword.value == "*" ||
kword.value == "/" || kword.value == ">" || kword.value == "<" ||
kword.value == "==" || kword.value == "<=" ||
kword.value == ">=" || kword.value == "&&" ||
kword.value == "||" || kword.value == "&" || kword.value == "|" ||
kword.value == "^" || kword.value == "!=" || kword.value == "!" ||
kword.value == "%");
};
int itt = old_itt;
while (true) {
bool try_continue = true;
auto look_ahead = [&](int count = 1) {
itt += count;
if (itt >= program_tokens.size()) {
error_out("Premature end-of-file reached",
program_tokens[itt - count]);
}
return program_tokens[itt];
};
auto look_behind = [&](int count = 1) {
itt -= count;
if (itt < 0) {
error_out("Premature end-of-file reached",
program_tokens[itt + count]);
}
return program_tokens[itt];
};
token initial_token = look_ahead(); // get next token
logger->log(logger::LOG_LEVEL::DEBUG,
"recursive lexer loop entry, itt: " +
std::to_string(itt) + " total token amount: " +
std::to_string(program_tokens.size()));
logger->log(logger::LOG_LEVEL::DEBUG,
"Token info: " + initial_token.value + "\t\t\t" +
DEBUG_TOKEN_TYPES[(int)initial_token.type]);
if (parsing_mode == parsing_modes::type) {
if (initial_token.value == "ptr" || initial_token.value == "arr" ||
initial_token.value == "unsigned") {
// outer type
ast_types::out_type *to_append = new ast_types::out_type;
to_append->name = initial_token.value;
int appended_index = append_ast_scope(scope, to_append);
if (initial_token.value == "arr") {
if (look_ahead().value != "[") {
error_out("array type must be followed by [",
program_tokens[itt]);
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::out_length);
itt = recursive_lex(
itt, new_scope, parsing_modes::type,
entry_bracket('[', ']')); // array length lexing
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
} else if (initial_token.value == "fun") {
ast_types::fun_type *to_append = new ast_types::fun_type;
int appended_index = append_ast_scope(scope, to_append);
look_ahead();
while (program_tokens[itt].value != "=>") {
std::string arg_name = program_tokens[itt].value;
if (look_ahead().value != ":") {
error_out("expected ':' in function argument list",
program_tokens[itt]);
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::argtype);
ast_types::arg_with_type_t *arg_type_t =
new ast_types::arg_with_type_t;
arg_type_t->name = ast_types::string_t(arg_name);
new_scope.push_back(
(scope_element)append_ast_scope(new_scope, arg_type_t));
new_scope.push_back(scope_element::type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
look_ahead();
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::return_type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')'));
} else {
ast_types::in_type *to_append = new ast_types::in_type;
to_append->type = initial_token.value;
append_ast_scope(scope, to_append);
}
break;
}
switch (initial_token.type) {
case token_type::identifier: {
if (initial_token.value == "for") {
ast_types::statement_with_body *to_append =
new ast_types::statement_with_body;
ast_types::scope *to_append_scope = new ast_types::scope;
int appended_scope_id =
append_ast_scope(scope, to_append_scope);
to_append->name = ast_types::string_t("while");
int appended_index = -1;
look_ahead();
int incr_cnt = 0;
while (program_tokens[itt].value != ")") {
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_scope_id);
new_scope.push_back(scope_element::body);
if (incr_cnt == 1) {
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::args);
} else if (incr_cnt == 2) {
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::body);
}
itt = recursive_lex(itt, new_scope, parsing_modes::argument,
entry_bracket('(', ')'));
if (incr_cnt == 0) {
appended_index = append_ast_scope(new_scope, to_append);
}
incr_cnt++;
}
if (incr_cnt != 3) {
error_out("Invalid argument count in for loop",
initial_token);
}
look_ahead();
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_scope_id);
new_scope.push_back(scope_element::body);
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::body);
itt = recursive_lex(itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}'));
} else if (initial_token.value == "if") {
// double body statement
ast_types::statement_with_two_bodies *to_append =
new ast_types::statement_with_two_bodies;
to_append->name = ast_types::string_t("if");
int appended_index = append_ast_scope(scope, to_append);
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::args);
while (program_tokens[itt].value != ")") {
itt = recursive_lex(itt, new_scope, parsing_modes::argument,
entry_bracket('(', ')')); // args lexing
}
new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::body);
itt = recursive_lex(itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}')); // body lexing
if (look_ahead().value == "else") {
if (look_ahead().value == "if") {
new_scope = scope;
scope.push_back((scope_element)appended_index);
scope.push_back(scope_element::second_body);
itt = recursive_lex(
--itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}')); // else if body lexing
} else {
new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::second_body);
itt = recursive_lex(
--itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}')); // else body lexing
}
} else {
--itt;
}
} else if (initial_token.value == "while" ||
initial_token.value == "foreach") {
// single body statement
ast_types::statement_with_body *to_append =
new ast_types::statement_with_body;
to_append->name = ast_types::string_t(initial_token.value);
int appended_index = append_ast_scope(scope, to_append);
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::args);
while (program_tokens[itt].value != ")") {
itt = recursive_lex(itt, new_scope, parsing_modes::argument,
entry_bracket('(', ')')); // args lexing
}
new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::body);
itt = recursive_lex(itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}')); // body lexing
} else if (initial_token.value == "return" ||
initial_token.value == "break" ||
initial_token.value == "continue" ||
initial_token.value == "deref") {
// statement
ast_types::statement *to_append = new ast_types::statement;
to_append->name = ast_types::string_t(initial_token.value);
int appended_index = append_ast_scope(scope, to_append);
if (look_ahead().value == "(") {
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::args);
itt = recursive_lex(--itt, new_scope, parsing_modes::argument,
entry_bracket('(', ')')); // args lexing
} else {
--itt;
}
} else if (initial_token.value == "ref") {
// varop
ast_types::varop *to_append = new ast_types::varop;
to_append->name = ast_types::string_t(initial_token.value);
to_append->var = ast_types::string_t(look_ahead().value);
append_ast_scope(scope, to_append);
} else if (initial_token.value == "var") {
// vardef
AST *to_append;
// = new ast_types::vardef;
if (scope.size() == 1) {
to_append = new ast_types::glbdef;
} else {
to_append = new ast_types::vardef;
}
std::vector<scope_element> new_scope = scope;
int appended_index = append_ast_scope(scope, to_append);
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::type);
itt =
recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // var type lexing
if (scope.size() == 1) {
dynamic_cast<ast_types::glbdef *>(to_append)->name =
ast_types::string_t(look_ahead().value);
} else {
dynamic_cast<ast_types::vardef *>(to_append)->name =
ast_types::string_t(look_ahead().value);
}
if (look_ahead().value == "=") {
new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::args);
itt = recursive_lex(itt, new_scope, parsing_modes::arg_one,
entry_bracket('(', ')'));
} else {
--itt;
}
} else if (initial_token.value == "fun") {
// have FUN! jk function
ast_types::fundef *to_append = new ast_types::fundef;
to_append->name = look_ahead().value;
int appended_index = append_ast_scope(scope, to_append);
look_ahead();
while (program_tokens[itt].value != "=>") {
std::string arg_name = program_tokens[itt].value;
if (look_ahead().value != ":") {
error_out("expected ':' in function argument list",
program_tokens[itt]);
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::argtype);
ast_types::arg_with_type_t *arg_type_t =
new ast_types::arg_with_type_t;
arg_type_t->name = ast_types::string_t(arg_name);
new_scope.push_back(
(scope_element)append_ast_scope(new_scope, arg_type_t));
new_scope.push_back(scope_element::type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
look_ahead();
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::return_type);
itt = recursive_lex(
itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // return type lexing
if (look_ahead().value != "{") {
error_out("expected '{' in function definition",
program_tokens[itt - 1]);
}
new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::body);
itt = recursive_lex(itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}')); // body lexing
} else if (initial_token.value ==
"ext") { // TODO: autodetect and deprecate
// external function
ast_types::extdef *to_append = new ast_types::extdef;
to_append->name = look_ahead().value;
int appended_index = append_ast_scope(scope, to_append);
while (program_tokens[itt].value != "=>") {
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::argtype);
ast_types::arg_with_type_t *arg_type_t =
new ast_types::arg_with_type_t;
new_scope.push_back(
(scope_element)append_ast_scope(new_scope, arg_type_t));
new_scope.push_back(scope_element::type);
int tmp_itt =
recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
itt = tmp_itt;
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::return_type);
itt = recursive_lex(
itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // return type lexing
} else if (initial_token.value == "class") {
ast_types::class_n *to_append = new ast_types::class_n;
to_append->name = look_ahead().value;
int appended_index = append_ast_scope(scope, to_append);
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::values);
look_ahead();
while (look_ahead().value != "}") {
look_behind();
if (look_ahead().value == "fun") {
ast_types::fundef *to_append_fun = new ast_types::fundef;
ast_types::value *to_append_val = new ast_types::value;
ast_types::fun_type *to_append_fun_type =
new ast_types::fun_type;
int appended_index_val =
append_ast_scope(new_scope, to_append_val);
std::vector<scope_element> new_scope_fun_type = new_scope;
new_scope_fun_type.push_back(
(scope_element)appended_index_val);
new_scope_fun_type.push_back(scope_element::type);
int appended_index_fun_type = append_ast_scope(
new_scope_fun_type, to_append_fun_type);
to_append_fun->name = ast_types::string_t(
"__anonymous_lambda_at_" + std::to_string(itt));
to_append_val->name = look_ahead().value;
std::vector<scope_element> new_scope_val_args = new_scope;
new_scope_val_args.push_back(
(scope_element)appended_index_val);
new_scope_val_args.push_back(scope_element::args);
ast_types::varop *to_append_funptr = new ast_types::varop;
to_append_funptr->var = to_append_fun->name;
to_append_funptr->name = ast_types::string_t("funptr");
append_ast_scope(new_scope_val_args, to_append_funptr);
int appended_index_fun = append_ast_scope(
{scope_element::global}, to_append_fun);
look_ahead();
int old_itt = itt;
ast_types::arg_with_type_t *this_arg_type_t =
new ast_types::arg_with_type_t;
this_arg_type_t->name = ast_types::string_t("this");
ast_types::out_type *this_out_type =
new ast_types::out_type;
this_out_type->name = ast_types::string_t("ptr");
ast_types::in_type *this_in_type = new ast_types::in_type;
this_in_type->type = to_append->name;
this_out_type->type.body.push_back(this_in_type);
this_arg_type_t->type.body.push_back(this_out_type);
to_append_fun->args.body.push_back(this_arg_type_t);
while (program_tokens[itt].value != "=>") {
std::string arg_name = program_tokens[itt].value;
if (look_ahead().value != ":") {
error_out("expected ':' in function argument list",
program_tokens[itt]);
}
std::vector<scope_element> new_scope = {
scope_element::global};
new_scope.push_back((scope_element)appended_index_fun);
new_scope.push_back(scope_element::argtype);
ast_types::arg_with_type_t *arg_type_t =
new ast_types::arg_with_type_t;
arg_type_t->name = ast_types::string_t(arg_name);
new_scope.push_back((scope_element)append_ast_scope(
new_scope, arg_type_t));
new_scope.push_back(scope_element::type);
itt = recursive_lex(
itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
look_ahead();
}
itt = old_itt;
to_append_fun_type->args.body.push_back(this_arg_type_t);
while (program_tokens[itt].value != "=>") {
std::string arg_name = program_tokens[itt].value;
if (look_ahead().value != ":") {
error_out("expected ':' in function argument list",
program_tokens[itt]);
}
std::vector<scope_element> new_scope = new_scope_fun_type;
new_scope.push_back(
(scope_element)appended_index_fun_type);
new_scope.push_back(scope_element::argtype);
ast_types::arg_with_type_t *arg_type_t =
new ast_types::arg_with_type_t;
arg_type_t->name = ast_types::string_t(arg_name);
new_scope.push_back((scope_element)append_ast_scope(
new_scope, arg_type_t));
new_scope.push_back(scope_element::type);
itt = recursive_lex(
itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
look_ahead();
}
old_itt = itt;
std::vector<scope_element> new_scope = {
scope_element::global};
new_scope.push_back((scope_element)appended_index_fun);
new_scope.push_back(scope_element::return_type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')'));
itt = old_itt;
new_scope = new_scope_fun_type;
new_scope.push_back((scope_element)appended_index_fun_type);
new_scope.push_back(scope_element::return_type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')'));
if (look_ahead().value != "{") {
error_out("expected '{' in function definition",
program_tokens[itt - 1]);
}
new_scope = {scope_element::global};
new_scope.push_back((scope_element)appended_index_fun);
new_scope.push_back(scope_element::body);
itt =
recursive_lex(itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}'));
continue;
}
look_behind();
ast_types::value *to_append_val = new ast_types::value;
int appended_index_val =
append_ast_scope(new_scope, to_append_val);
std::vector<scope_element> new_scope_type = new_scope;
new_scope_type.push_back((scope_element)appended_index_val);
new_scope_type.push_back(scope_element::type);
itt = recursive_lex(itt, new_scope_type, parsing_modes::type,
entry_bracket('(', ')'));
to_append_val->name = ast_types::string_t(look_ahead().value);
if (look_ahead().value == "=") {
std::vector<scope_element> new_scope_val = new_scope;
new_scope_val.push_back((scope_element)appended_index_val);
new_scope_val.push_back(scope_element::args);
itt = recursive_lex(itt, new_scope_val,
parsing_modes::arg_one,
entry_bracket('(', ')'));
} else {
look_behind();
}
}
} else if (initial_token.value == "lambda") {
ast_types::fundef *to_append = new ast_types::fundef;
to_append->name = ast_types::string_t(
" __anonymous_lambda_at_" +
std::to_string(itt)); // the unicode character in the
// function is illegal in the syntax,
// so we use it to make it anonymous
int appended_index =
append_ast_scope({scope_element::global}, to_append);
look_ahead();
while (program_tokens[itt].value != "=>") {
std::string arg_name = program_tokens[itt].value;
if (look_ahead().value != ":") {
error_out("expected ':' in function argument list",
program_tokens[itt]);
}
std::vector<scope_element> new_scope = {
scope_element::global};
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::argtype);
ast_types::arg_with_type_t *arg_type_t =
new ast_types::arg_with_type_t;
arg_type_t->name = ast_types::string_t(arg_name);
new_scope.push_back(
(scope_element)append_ast_scope(new_scope, arg_type_t));
new_scope.push_back(scope_element::type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')')); // type lexing
look_ahead();
}
std::vector<scope_element> new_scope = {scope_element::global};
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::return_type);
itt = recursive_lex(itt, new_scope, parsing_modes::type,
entry_bracket('(', ')'));
if (look_ahead().value != "{") {
error_out("expected '{' in function definition",
program_tokens[itt - 1]);
}
new_scope = {scope_element::global};
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::body);
itt = recursive_lex(itt, new_scope, parsing_modes::statement,
entry_bracket('{', '}')); // body lexing
ast_types::varop *to_append_varop = new ast_types::varop;
to_append_varop->name = ast_types::string_t("funptr");
to_append_varop->var = to_append->name;
append_ast_scope(scope, to_append_varop);
} else {
look_ahead();
if (program_tokens[itt].value == "(") {
// function call
bool class_function = program_tokens[itt - 1].value == ".";
ast_types::call *to_append = new ast_types::call;
to_append->name = ast_types::string_t(initial_token.value);
int appended_index = append_ast_scope(scope, to_append);
if (class_function) {
// to_append->args.body.push_back(); TODO
}
std::vector<scope_element> new_scope = scope;
new_scope.push_back((scope_element)appended_index);
new_scope.push_back(scope_element::args);
while (program_tokens[itt].value != ")") {
itt =
recursive_lex(itt, new_scope, parsing_modes::argument,
entry_bracket('(', ')')); // args lexing
}
} else {
// this should be a variable
// since we have already incremented itt, we can check whether
// it is a assignment or a reference right away
if (program_tokens[itt].value == "=" ||
(program_tokens[itt].value[1] == '=' &&
program_tokens[itt].value[0] != '=' &&
program_tokens[itt].value[0] != '!' &&
program_tokens[itt].value[0] != '<' &&
program_tokens[itt].value[0] != '>')) {
// assignment
token operation = program_tokens[itt];
token var_name = program_tokens[itt - 1];
if (operation.value == "=") {