-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
1407 lines (995 loc) · 40.6 KB
/
ast.c
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 "ast.h"
#include <string.h>
#include <signal.h>
#include "bool.h"
#include "lexer.h"
#include "utils.h"
#define MAX_BUFFER_SIZE 10000
void printf_token(Token token) {
switch (token.token_type) {
case NULL_TOKEN: {
printf(" null ");
break;
}
case IDENTIFIER_TOKEN: {
printf(" %s ", ((StringLiteral *)token.literal)->string_literal);
break;
}
case INTEGER_TOKEN: {
printf(" %lld ", ((IntegerLiteral *)token.literal)->integer_literal);
break;
}
case FLOAT_TOKEN: {
printf(" %Lf ", ((FloatLiteral *)token.literal)->float_literal);
break;
}
case BOOL_TOKEN: {
printf(" %s ", ((BoolLiteral *)token.literal)->bool_literal ? "true" : "false");
break;
}
case STRING_LITERAL_TOKEN: {
printf(" \"%s\" ", ((StringLiteral *)token.literal)->string_literal);
break;
}
}
}
void printf_alignment(unsigned int alignment) {
if (alignment != 0u) {
for (unsigned int i = 0; i < alignment; i += 1u) {
printf(" ");
}
}
}
void printf_expression(Expression *expression, unsigned int alignment) {
switch (expression->expression_type) {
case ExpressionTypeInfixExpression: {
InfixExpression *infix_expression = (InfixExpression *) expression;
printf(" (");
printf_expression(infix_expression->left_expression, alignment);
if (infix_expression->operator == ADDITION_OP) { printf(" + "); }
else if (infix_expression->operator == SUBTRACTION_OP) { printf(" - "); }
else if (infix_expression->operator == MULTIPLICATION_OP) { printf(" * "); }
else if (infix_expression->operator == DIVISION_OP) { printf(" / "); }
else if (infix_expression->operator == MOD_OP) { printf(" %% "); }
else if (infix_expression->operator == EXPONENT_OP) { printf(" ^ "); }
else if (infix_expression->operator == ASSIGN_OP) { printf(" = "); }
else if (infix_expression->operator == ASSIGN_ADDITION_OP) { printf(" += "); }
else if (infix_expression->operator == ASSIGN_SUBTRACTION_OP) { printf(" -= "); }
else if (infix_expression->operator == ASSIGN_MULTIPLICATION_OP) { printf(" *= "); }
else if (infix_expression->operator == ASSIGN_DIVISION_OP) { printf(" /= "); }
else if (infix_expression->operator == ASSIGN_MOD_OP) { printf(" %%= "); }
else if (infix_expression->operator == ASSIGN_EXPONENT_OP) { printf(" ^= "); }
else if (infix_expression->operator == CHECK_EQUALITY_OP) { printf(" == "); }
else if (infix_expression->operator == CHECK_NOT_EQUALITY_OP) { printf(" != "); }
else if (infix_expression->operator == CHECK_SMALLER_OP) { printf(" < "); }
else if (infix_expression->operator == CHECK_SMALLER_EQUAL_OP) { printf(" <= "); }
else if (infix_expression->operator == CHECK_BIGGER_OP) { printf(" > "); }
else if (infix_expression->operator == CHECK_BIGGER_EQUAL_OP) { printf(" >= "); }
else if (infix_expression->operator == MEMBER_OP) { printf(" . "); }
else if (infix_expression->operator == IN_OP) { printf(" in "); }
else if (infix_expression->operator == COMMA_OP) { printf(" , "); }
printf_expression(infix_expression->right_expression, alignment);
printf(") ");
break;
}
case ExpressionTypePrefixExpression: {
PrefixExpression *prefix_expression = (PrefixExpression *) expression;
printf(" (");
if (prefix_expression->operator == NOT_OP) { printf("!"); }
else if (prefix_expression->operator == ADDITION_OP) { printf("+"); }
else if (prefix_expression->operator == SUBTRACTION_OP) { printf("-"); }
else if (prefix_expression->operator == ASYNC_OP) { printf(" async "); }
else if (prefix_expression->operator == AWAIT_OP) { printf(" await "); }
printf_expression(prefix_expression->right_expression, alignment);
printf(") ");
break;
}
case ExpressionTypeIndexExpression: {
IndexExpression *index_expression = (IndexExpression *)expression;
printf_expression(index_expression->left_expression, alignment);
printf("[");
printf_expression(index_expression->right_expression, alignment);
printf("] ");
break;
}
case ExpressionTypeCallExpression: {
CallExpression *call_expression = (CallExpression *) expression;
printf(" `");
printf_expression(call_expression->identifier_expression, alignment);
printf("->");
printf_expression(call_expression->tuple_expression, alignment);
printf("` ");
break;
}
case ExpressionTypeArrayExpression: {
ArrayExpression *array_expression = (ArrayExpression *) expression;
if (array_expression->array_expression_type == ArrayExpressionTypeList) {
printf(" [");
} else {
printf(" T(");
}
for (size_t i = 0; i < array_expression->expression_count; i++) {
printf_expression(array_expression->expressions[i], alignment);
if (i < array_expression->expression_count - 1) {
printf(",");
}
}
if (array_expression->array_expression_type == ArrayExpressionTypeList) {
printf("] ");
} else {
printf(") ");
}
break;
}
case ExpressionTypeFunctionExpression: {
FunctionExpression *function_expression = (FunctionExpression *)expression;
printf("FUNCTION");
if (function_expression->identifier) printf_expression(function_expression->identifier, alignment);
printf("(");
for (size_t i = 0; i < function_expression->argument_count; i++) {
printf("%s", function_expression->arguments[i]);
if (i != function_expression->argument_count - 1) {
printf(", ");
}
}
printf(")");
printf("\n");
printf_block(function_expression->block, alignment);
break;
}
case ExpressionTypeBoolExpression: {
printf(" %s ", ((BoolLiteral *)expression->literal)->bool_literal ? "true" : "false");
break;
}
case ExpressionTypeIntegerExpression: {
printf(" %lld ", ((IntegerLiteral *)expression->literal)->integer_literal);
break;
}
case ExpressionTypeFloatExpression: {
printf(" %Lf ", ((FloatLiteral *)expression->literal)->float_literal);
break;
}
case ExpressionTypeStringExpression: {
printf(" \"%s\" ", ((StringLiteral *)expression->literal)->string_literal);
break;
}
case ExpressionTypeIdentifierExpression: {
printf(" %s ", ((StringLiteral *)expression->literal)->string_literal);
break;
}
case ExpressionTypeNullExpression: {
printf(" %s ", "null");
break;
}
default : {
}
}
}
void printf_block(Block *block, unsigned int alignment) {
printf("{\n");
for (size_t i = 0; i < block->statement_count; i++) {
printf_statement(block->statements[i], alignment + 1u);
}
printf("}\n");
}
void printf_block_definition(BlockDefinition *block_definition, unsigned int alignment) {
switch (block_definition->block_definition_type) {
case BlockDefinitionTypeIfElseGroupBlock: {
IfElseGroupBlockDefinition *if_else_group_block_definition = (IfElseGroupBlockDefinition *)block_definition;
for (size_t i = 0; i < if_else_group_block_definition->if_block_definitions_length; i++) {
printf_block_definition((BlockDefinition *) if_else_group_block_definition->if_block_definitions[i], alignment);
}
if (if_else_group_block_definition->else_block_definition) {
printf_block_definition((BlockDefinition *) if_else_group_block_definition->else_block_definition, alignment);
}
break;
}
case BlockDefinitionTypeIfBlock: {
IfBlockDefinition *if_block_definition = (IfBlockDefinition *)block_definition;
printf_alignment(alignment);
printf("IF");
if (if_block_definition->pre_expression) {
printf_expression(if_block_definition->pre_expression, alignment);
printf(";");
}
printf_expression(if_block_definition->condition, alignment);
printf("\n");
printf_block(if_block_definition->block, alignment);
break;
}
case BlockDefinitionTypeElseBlock: {
ElseBlockDefinition *else_block_definition = (ElseBlockDefinition *) block_definition;
printf_block(else_block_definition->block, alignment);
break;
}
case BlockDefinitionTypeForBlock: {
ForBlockDefinition *for_block_definition = (ForBlockDefinition *)block_definition;
printf_alignment(alignment);
printf("FOR");
if (for_block_definition->pre_expression) {
printf_expression(for_block_definition->pre_expression, alignment);
printf(";");
}
printf_expression(for_block_definition->condition, alignment);
if (for_block_definition->post_expression) {
printf(";");
printf_expression(for_block_definition->post_expression, alignment);
}
printf("\n");
printf_block(for_block_definition->block, alignment);
break;
}
}
}
void printf_statement(Statement *statement, unsigned int alignment) {
switch (statement->statement_type) {
case StatementTypeReturnStatement: {
ReturnStatement *return_statement = (ReturnStatement *)statement;
printf_alignment(alignment);
printf("return ");
printf_expression(return_statement->right_expression, alignment);
printf("\n");
break;
}
case StatementTypeImportStatement: {
ImportStatement *import_statement = (ImportStatement *)statement;
printf_alignment(alignment);
printf("import ");
printf_expression(import_statement->right_expression, alignment);
printf("\n");
break;
}
case StatementTypeBlockDefinitionStatement: {
BlockDefinitionStatement *block_definition_statement = (BlockDefinitionStatement *)statement;
printf_block_definition(block_definition_statement->block_definition, alignment);
break;
}
case StatementTypeExpressionStatement: {
ExpressionStatement *expression_Statement = (ExpressionStatement *)statement;
printf_alignment(alignment);
printf_expression(expression_Statement->expression, alignment);
printf("\n");
break;
}
default : {
}
}
}
void printf_ast(AST *ast) {
for (size_t i = 0; i < ast->block->statement_count; i++) {
printf_statement(ast->block->statements[i], 0);
}
printf("\n");
}
void free_expression(Expression *expression) {
switch (expression->expression_type) {
case ExpressionTypeNullExpression: {
break;
}
case ExpressionTypeIndexExpression: {
IndexExpression *index_expression = (IndexExpression *) expression;
free_expression(index_expression->left_expression);
free_expression(index_expression->right_expression);
free(index_expression);
break;
}
case ExpressionTypeInfixExpression: {
InfixExpression *infix_expression = (InfixExpression *)expression;
free_expression(infix_expression->left_expression);
free_expression(infix_expression->right_expression);
free(infix_expression);
break;
}
case ExpressionTypePrefixExpression: {
PrefixExpression *prefix_expression = (PrefixExpression *)expression;
free_expression(prefix_expression->right_expression);
free(prefix_expression);
break;
}
case ExpressionTypeCallExpression: {
CallExpression *call_expression = (CallExpression *)expression;
free_expression(call_expression->identifier_expression);
free_expression(call_expression->tuple_expression);
free(call_expression);
break;
}
case ExpressionTypeArrayExpression: {
ArrayExpression *array_expression = (ArrayExpression *)expression;
for (size_t i = 0; i < array_expression->expression_count; i++) {
free_expression(array_expression->expressions[i]);
}
free(array_expression->expressions);
free(array_expression);
break;
}
case ExpressionTypeFunctionExpression: {
FunctionExpression *function_expression = (FunctionExpression *)expression;
for (size_t i = 0; i < function_expression->argument_count; i++) {
free(function_expression->arguments[i]);
}
free(function_expression->arguments);
if (function_expression->identifier) free_expression(function_expression->identifier);
free_block(function_expression->block);
free(function_expression);
break;
}
case ExpressionTypeBoolExpression:
case ExpressionTypeIntegerExpression:
case ExpressionTypeFloatExpression: {
free(expression->literal);
free(expression);
break;
}
case ExpressionTypeStringExpression:
case ExpressionTypeIdentifierExpression: {
free(((StringLiteral *) expression->literal)->string_literal);
free(expression->literal);
free(expression);
break;
}
}
}
void free_block(Block *block) {
for (size_t i = 0; i < block->statement_count; i++) {
free_statement(block->statements[i]);
}
free(block->statements);
free(block);
}
void free_block_definition(BlockDefinition *block_definition) {
switch (block_definition->block_definition_type) {
case BlockDefinitionTypeIfElseGroupBlock: {
IfElseGroupBlockDefinition *if_else_group_block_definition = (IfElseGroupBlockDefinition *)block_definition;
for (size_t i = 0; i < if_else_group_block_definition->if_block_definitions_length; i++) {
free_block_definition((BlockDefinition *) if_else_group_block_definition->if_block_definitions[i]);
}
free(if_else_group_block_definition->if_block_definitions);
if (if_else_group_block_definition->else_block_definition) free_block_definition((BlockDefinition *) if_else_group_block_definition->else_block_definition);
free(if_else_group_block_definition);
break;
}
case BlockDefinitionTypeIfBlock: {
IfBlockDefinition *if_block_definition = (IfBlockDefinition *)block_definition;
free_expression(if_block_definition->condition);
if (if_block_definition->pre_expression != NULL) free_expression(if_block_definition->pre_expression);
free_block(if_block_definition->block);
free(if_block_definition);
break;
}
case BlockDefinitionTypeElseBlock: {
break;
}
case BlockDefinitionTypeForBlock: {
ForBlockDefinition *for_block_definition = (ForBlockDefinition *)block_definition;
free_expression(for_block_definition->condition);
if (for_block_definition->pre_expression != NULL) free_expression(for_block_definition->pre_expression);
if (for_block_definition->post_expression != NULL) free_expression(for_block_definition->post_expression);
free_block(for_block_definition->block);
free(for_block_definition);
break;
}
default : {
}
}
}
void free_statement(Statement *statement) {
switch (statement->statement_type) {
case StatementTypeExpressionStatement: {
ExpressionStatement *expression_statement = (ExpressionStatement *)statement;
free_expression(expression_statement->expression);
free(expression_statement);
break;
}
case StatementTypeImportStatement: {
ImportStatement *import_statement = (ImportStatement *)statement;
free_expression(import_statement->right_expression);
free(import_statement);
break;
}
case StatementTypeReturnStatement: {
ReturnStatement *return_statement = (ReturnStatement *)statement;
free_expression(return_statement->right_expression);
free(return_statement);
break;
}
case StatementTypeBlockDefinitionStatement: {
BlockDefinitionStatement *block_definition_statement = (BlockDefinitionStatement *)statement;
free_block_definition(block_definition_statement->block_definition);
free(block_definition_statement);
break;
}
}
}
void free_ast(AST *ast) {
// no need to free block, because scope automatically frees it, TODO move maybe again here
free_block(ast->block);
free(ast);
}
Operator token_to_operator(Token token) {
switch (token.token_type) {
case EQUAL_TOKEN:
return ASSIGN_OP;
case PLUS_TOKEN:
return ADDITION_OP;
case PLUS_EQUAL_TOKEN:
return ASSIGN_ADDITION_OP;
case MINUS_TOKEN:
return SUBTRACTION_OP;
case MINUS_EQUAL_TOKEN:
return ASSIGN_SUBTRACTION_OP;
case STAR_TOKEN:
return MULTIPLICATION_OP;
case STAR_EQUAL_TOKEN:
return ASSIGN_MULTIPLICATION_OP;
case DOUBLE_STAR_TOKEN:
return EXPONENT_OP;
case DOUBLE_STAR_EQUAL_TOKEN:
return ASSIGN_EXPONENT_OP;
case SLASH_TOKEN:
return DIVISION_OP;
case SLASH_EQUAL_TOKEN:
return ASSIGN_DIVISION_OP;
case DOUBLE_SLASH_TOKEN:
return INTEGER_DIVISION_OP;
case DOUBLE_SLASH_EQUAL_TOKEN:
return ASSIGN_INTEGER_DIVISION_OP;
case PERCENTAGE_TOKEN:
return MOD_OP;
case PERCENTAGE_EQUAL_TOKEN:
return ASSIGN_MOD_OP;
case CARROT_TOKEN:
return EXPONENT_OP;
case CARROT_EQUAL_TOKEN:
return ASSIGN_EXPONENT_OP;
case DOUBLE_EQUAL_TOKEN:
return CHECK_EQUALITY_OP;
case EXCLAMATION_EQUAL_TOKEN:
return CHECK_NOT_EQUALITY_OP;
case SMALLER_TOKEN:
return CHECK_SMALLER_OP;
case SMALLER_EQUAL_TOKEN:
return CHECK_SMALLER_EQUAL_OP;
case BIGGER_TOKEN:
return CHECK_BIGGER_OP;
case BIGGER_EQUAL_TOKEN:
return CHECK_BIGGER_EQUAL_OP;
case L_PARENTHESIS_TOKEN:
return L_PARENTHESIS_OP;
case L_BRACKET_TOKEN:
return L_BRACKET_OP;
case EXCLAMATION_TOKEN:
return NOT_OP;
case MEMBER_TOKEN:
return MEMBER_OP;
case IN_TOKEN:
return IN_OP;
case COMMA_TOKEN:
return COMMA_OP;
case RANGE_TOKEN:
return RANGE_OP;
case ASYNC_TOKEN:
return ASYNC_OP;
case AWAIT_TOKEN:
return AWAIT_OP;
default:
return -1;
}
}
bool check_if_token_is_operator(Token token) {
return token_to_operator(token) != -1;
}
bool is_operator_right_associative(Operator operator) {
return operator == EXPONENT_OP || operator == ASSIGN_OP;
}
unsigned short get_operator_precedence(Operator operator, bool next) {
unsigned short result;
switch (operator) {
case ASSIGN_OP:
case ASSIGN_ADDITION_OP:
case ASSIGN_SUBTRACTION_OP:
case ASSIGN_MULTIPLICATION_OP:
case ASSIGN_DIVISION_OP:
case ASSIGN_INTEGER_DIVISION_OP:
case ASSIGN_MOD_OP:
case ASSIGN_EXPONENT_OP: {
result = ASSIGN_PRECEDENCE;
break;
}
case CHECK_EQUALITY_OP:
case CHECK_NOT_EQUALITY_OP:
case CHECK_BIGGER_OP:
case CHECK_BIGGER_EQUAL_OP:
case CHECK_SMALLER_OP:
case CHECK_SMALLER_EQUAL_OP: {
result = CONDITIONAL_PRECEDENCE;
break;
}
case ADDITION_OP:
case SUBTRACTION_OP: {
result = ADDITION_SUBTRACTION_PRECEDENCE;
break;
}
case MULTIPLICATION_OP:
case DIVISION_OP:
case INTEGER_DIVISION_OP:
case MOD_OP: {
result = MULTIPLICATION_DIVISION_MOD_PRECEDENCE;
break;
}
case EXPONENT_OP: {
result = EXPONENT_PRECEDENCE;
break;
}
case MEMBER_OP: {
result = MEMBER_PRECEDENCE;
break;
}
case L_PARENTHESIS_OP: {
result = CALL_PRECEDENCE;
break;
}
case L_BRACKET_OP: {
result = LIST_PRECEDENCE;
break;
}
case IN_OP: {
result = IN_OP_PRECEDENCE;
break;
}
case RANGE_OP: {
result = RANGE_PRECEDENCE;
break;
}
case COMMA_OP: {
result = COMMA_PRECEDENCE;
break;
}
case ASYNC_OP:
case AWAIT_OP: {
result = ASYNC_AWAIT_PRECEDENCE;
break;
}
default: {
result = 0;
}
}
if (next && is_operator_right_associative(operator)) result -= 1u;
return result;
}
// parser starts here
bool has_finished(Token token, ParserLimiter limiter) {
if (token.token_type == EOF_TOKEN) {
return true;
}
switch (limiter) {
case DEFAULT_BLOCK_PARSER_LIMITER:
return token.token_type == R_BRACE_TOKEN;
case DEFAULT_EXPRESSION_PARSER_LIMITER:
return token.token_type == EOL_TOKEN;
case GROUPED_EXPRESSION_PARSER_LIMITER:
return token.token_type == R_PARENTHESIS_TOKEN;
case ARRAY_EXPRESSION_PARSER_LIMITER:
return token.token_type == R_BRACKET_TOKEN || token.token_type == R_PARENTHESIS_TOKEN;
case INDEX_EXPRESSION_PARSER_LIMITER:
return token.token_type == R_BRACKET_TOKEN;
case IF_BLOCK_EXPRESSION_PARSER_LIMITER:
case FOR_BLOCK_EXPRESSION_PARSER_LIMITER:
return token.token_type == L_BRACE_TOKEN || token.token_type == SEMICOLON_TOKEN;
default:
return false;
}
}
void *parser_error() {
raise(SIGUSR1);
return NULL;
}
void *parser_error_undefined_position() { //Lexer *lexer
printf("Undefined position of token ");
//printf_token(peek_token(lexer));
// at line
parser_error();
}
Expression *eval_token(Token token) {
static Expression *null_expression = NULL;
if (token.token_type == NULL_TOKEN) {
if (null_expression == NULL) {
null_expression = malloc(sizeof(Expression));
null_expression->literal = NULL;
null_expression->expression_type = ExpressionTypeNullExpression;
}
return null_expression;
}
Expression *expression = malloc(sizeof(Expression));
expression->literal = token.literal;
switch (token.token_type) {
case IDENTIFIER_TOKEN: {
expression->expression_type = ExpressionTypeIdentifierExpression;
break;
}
case INTEGER_TOKEN: {
expression->expression_type = ExpressionTypeIntegerExpression;
break;
}
case FLOAT_TOKEN: {
expression->expression_type = ExpressionTypeFloatExpression;
break;
}
case BOOL_TOKEN: {
expression->expression_type = ExpressionTypeBoolExpression;
break;
}
case STRING_LITERAL_TOKEN: {
expression->expression_type = ExpressionTypeStringExpression;
break;
}
default: {
return parser_error_undefined_position();
}
}
return expression;
}
Expression *force_array_expression(Expression *expression, ArrayExpressionType array_expression_type) {
if (expression == NULL) {
ArrayExpression *array_expression = malloc(sizeof(ArrayExpression));
array_expression->expression = (Expression){.expression_type = ExpressionTypeArrayExpression};
array_expression->expression_count = 0;
array_expression->expressions = malloc(0);
array_expression->array_expression_type = array_expression_type;
array_expression->has_finished = false;
return (Expression *) array_expression;
}
else if (expression->expression_type == ExpressionTypeArrayExpression) {
if (!((ArrayExpression *) expression)->has_finished) {
((ArrayExpression *) expression)->array_expression_type = array_expression_type;
return expression;
}
ArrayExpression *array_expression = malloc(sizeof(ArrayExpression));
array_expression->expression = (Expression){.expression_type = ExpressionTypeArrayExpression};
array_expression->expression_count = 1;
array_expression->expressions = malloc(sizeof(Expression *));
array_expression->array_expression_type = array_expression_type;
array_expression->has_finished = false;
array_expression->expressions[0] = expression;
return (Expression *) array_expression;
}
else {
ArrayExpression *array_expression = malloc(sizeof(ArrayExpression));
array_expression->expression = (Expression){.expression_type = ExpressionTypeArrayExpression};
array_expression->expression_count = 1;
array_expression->expressions = malloc(sizeof(Expression *));
array_expression->array_expression_type = array_expression_type;
array_expression->has_finished = false;
array_expression->expressions[0] = expression;
return (Expression *) array_expression;
}
}
Expression *parse_grouped_expression(Lexer *lexer) {
if (peek_token(lexer).token_type != L_PARENTHESIS_TOKEN) return parser_error_undefined_position();
next_token(lexer);
Expression *result = parse_expression(lexer, 0, GROUPED_EXPRESSION_PARSER_LIMITER);
if (result->expression_type == ExpressionTypeArrayExpression) {
ArrayExpression *array_expression = (ArrayExpression *) result;
array_expression->has_finished = true;
}
if (peek_token(lexer).token_type != R_PARENTHESIS_TOKEN) return parser_error_undefined_position();
next_token(lexer);
return result;
}
Expression *parse_list_expression(Lexer *lexer) {
if (peek_token(lexer).token_type != L_BRACKET_TOKEN) return parser_error_undefined_position();
next_token(lexer);
Expression *result = force_array_expression(parse_expression(lexer, 0, ARRAY_EXPRESSION_PARSER_LIMITER), ArrayExpressionTypeList);
ArrayExpression *array_expression = (ArrayExpression *)result;
array_expression->array_expression_type = ArrayExpressionTypeList;
array_expression->has_finished = true;
if (peek_token(lexer).token_type != R_BRACKET_TOKEN) return parser_error_undefined_position();
next_token(lexer);
return result;
}
Expression *parse_prefix_expression(Lexer *lexer, ParserLimiter limiter) {
Token curr_token = peek_token(lexer);
if (has_finished(curr_token, limiter)) return NULL; // TODO
if (curr_token.token_type == L_PARENTHESIS_TOKEN) {
return parse_grouped_expression(lexer);
} else if (curr_token.token_type == L_BRACKET_TOKEN) {
return parse_list_expression(lexer);
}
next_token(lexer);
PrefixExpression *prefix_expression = malloc(sizeof(PrefixExpression));
prefix_expression->expression = (Expression){.expression_type = ExpressionTypePrefixExpression};
prefix_expression->operator = token_to_operator(curr_token);
prefix_expression->right_expression = parse_expression(lexer, get_operator_precedence(prefix_expression->operator, true), limiter);
return (Expression *)prefix_expression;
}
Expression *parse_array_expression(Lexer *lexer, Expression *left) {
ArrayExpression *array_expression = NULL;
if (left->expression_type == ExpressionTypeArrayExpression && !(((ArrayExpression *) left)->has_finished)) {
array_expression = (ArrayExpression *)left;
}
else {
array_expression = malloc(sizeof(ArrayExpression));
array_expression->expression = (Expression){.expression_type = ExpressionTypeArrayExpression};
array_expression->expression_count = 1;
array_expression->expressions = malloc(array_expression->expression_count * sizeof(Expression *));
array_expression->array_expression_type = ArrayExpressionTypeTuple;
array_expression->has_finished = false;
array_expression->expressions[0] = left;
}
if (has_finished(peek_token(lexer), ARRAY_EXPRESSION_PARSER_LIMITER)) return (Expression *) array_expression;