-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
922 lines (747 loc) · 29.2 KB
/
parser.c
File metadata and controls
922 lines (747 loc) · 29.2 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#define BINARY_INFIX(prec, fun, assoc) ((struct binary_infix) {{ prec, (struct base_expr * (*)(struct parse_infix *, struct parser_helper *, struct base_expr *, struct token_val)) &fun }, assoc })
#define INFIX(prec, fun) ((struct parse_infix) { prec , (struct base_expr * (*)(struct parse_infix *, struct parser_helper *, struct base_expr *, struct token_val)) &fun })
#define PREFIX(fun) ((struct parse_prefix) { &fun })
#define ARRAY_FACTOR 2
// INIT EXPRESSION PARSE
struct parse_prefix *prefixes[TOK_EXIT];
struct parse_infix *infixes[TOK_EXIT];
static struct base_expr *parse_expr(struct parser_helper *helper);
// TODO: change this to a list struct
static void add_toarray(void ***statements, int *arr_size, int *count, void *node) {
if (*count >= *arr_size) {
*arr_size *= ARRAY_FACTOR;
struct base_expr **tmp = realloc(*statements, sizeof(struct base_expr*) * *arr_size);
if (tmp == NULL) {
printf("[ERROR] Not enough memory\n");
exit(1);
}
*statements = tmp;
}
(*statements)[(*count)++] = node;
}
static int get_precedence(struct parser_helper *helper) {
struct parse_infix *prefix = infixes[ACTUAL_TOK().type];
return prefix != NULL ? prefix->precedence : 0;
}
// PARSING CALL ARGUMENTS
static struct base_expr **parse_arguments(struct parser_helper *helper, int *argument_cnt) {
if (ACTUAL_TOK().type != TOK_RIGHT_PAREN) {
int arr_size = ARRAY_FACTOR;
struct base_expr **arguments = malloc(sizeof(struct base_expr*) * arr_size);
do {
ENSURE_SIZE();
struct base_expr *expr = parse_expr(helper);
add_toarray(&arguments, &arr_size, argument_cnt, expr);
} while (READ_TOK().type == TOK_COMMA);
PREVIOUS_TOK();
CONSUME_TOK(TOK_RIGHT_PAREN, "expected )");
return arguments;
}
READ_TOK();
return NULL;
}
// PARSING ARRAY ARGUMENTS
static struct base_expr **parse_arr_arguments(struct parser_helper *helper, int *argument_cnt) {
int arr_size = ARRAY_FACTOR;
struct base_expr **arguments = malloc(sizeof(struct base_expr*) * arr_size);
struct base_expr *base = parse_expr(helper);
if (!base) {
return NULL;
}
CONSUME_TOK(TOK_RIGHT_BRACKETS, "expected ]");
add_toarray(&arguments, &arr_size, argument_cnt, base);
while (READ_TOK().type == TOK_LEFT_BRACKETS) {
ENSURE_SIZE();
if (ACTUAL_TOK().type == TOK_RIGHT_BRACKETS) {
add_toarray(&arguments, &arr_size, argument_cnt, NULL);
CONSUME_TOK(TOK_RIGHT_BRACKETS, "expected ]");
continue;
}
struct base_expr *expr = parse_expr(helper);
add_toarray(&arguments, &arr_size, argument_cnt, expr);
CONSUME_TOK(TOK_RIGHT_BRACKETS, "expected ]");
}
PREVIOUS_TOK();
return arguments;
}
// EXPRESSION
static struct base_expr *parse_expr_internal(struct parser_helper *helper, int precedence) {
ENSURE_SIZE();
struct token_val tok = READ_TOK();
struct parse_prefix *prefix = prefixes[tok.type];
if (prefix == NULL) {
add_error(helper->errors, CREATE_ERROR("unexpected token at expression", tok.line, tok.char_pos));
return NULL;
}
struct base_expr *left = prefix->parse(prefix, helper, tok);
while (precedence < get_precedence(helper)) {
tok = READ_TOK();
struct parse_infix *infix = infixes[tok.type];
left = infix->parse(infix, helper, left, tok);
}
return left;
}
// BINARY
static struct base_expr *parse_binary(struct binary_infix *infix, struct parser_helper *helper, struct base_expr *node, struct token_val val) {
struct base_expr *right = parse_expr_internal(helper, infix->base.precedence - (infix->is_right ? 1 : 0));
return init_binary_expr(val, node, right);
}
// ASSIGN
static struct base_expr *parse_assign(struct binary_infix *infix, struct parser_helper *helper, struct base_expr *node, struct token_val val) {
if (!node) return NULL;
struct base_expr *right = parse_expr_internal(helper, infix->base.precedence - (infix->is_right ? 1 : 0));
enum expr_type left_type = node->type;
if (left_type != EXPR_LITERAL && left_type != EXPR_BINARY && left_type != EXPR_ARRAY) {
add_error(helper->errors, CREATE_ERROR("invalid assign type", val.line, val.char_pos));
return NULL;
}
if (left_type == EXPR_BINARY) {
struct binary_expr *bin = (struct binary_expr*) node->as.binary;
if (bin->op.type != TOK_DOT && bin->op.type != TOK_ASSIGN) {
add_error(helper->errors, CREATE_ERROR("invalid assign type", val.line, val.char_pos));
return NULL;
}
}
return init_binary_expr(val, node, right);
}
// ACCESS
static struct base_expr *parse_access(struct parse_infix *infix, struct parser_helper *helper, struct base_expr *node, struct token_val val) {
struct base_expr *right = parse_expr_internal(helper, PREC_ACCESS);
if (right == NULL || node == NULL) {
add_error(helper->errors, CREATE_ERROR("accessing invalid value", val.line, val.char_pos));
return NULL;
}
enum expr_type left_type = node->type;
enum expr_type right_type = right->type;
if ( (left_type != EXPR_CALL && left_type != EXPR_LITERAL && left_type != EXPR_BINARY && left_type != EXPR_ARRAY) ||
(right_type != EXPR_CALL && right_type != EXPR_LITERAL && right_type != EXPR_BINARY && right_type != EXPR_ARRAY) ) {
add_error(helper->errors, CREATE_ERROR("accessing invalid value", val.line, val.char_pos));
return NULL;
}
return init_binary_expr(val, node, right);
}
// UNARY POSTFIX
static struct base_expr *parse_unary_postfix(struct parse_infix *infix, struct parser_helper *helper, struct base_expr *node, struct token_val val) {
return init_unary_expr(val, node, UT_POSTFIX);
}
// UNARY PREFIX
static struct base_expr *parse_unary(struct parse_prefix *prefix, struct parser_helper *helper, struct token_val val) {
struct base_expr *child = parse_expr_internal(helper, PREC_PREFIX);
if (val.type == TOK_INCREMENT || val.type == TOK_DECREMENT) {
if (child->type != EXPR_LITERAL && child->type != EXPR_UNARY) {
add_error(helper->errors, CREATE_ERROR("expected identifier or value access", val.line, val.char_pos));
return NULL;
}
if (child->type == EXPR_UNARY) {
struct unary_expr *unary = child->as.unary;
}
}
return init_unary_expr(val, child, UT_PREFIX);
}
// GROUPING
static struct base_expr *parse_grouping(struct parse_prefix *prefix, struct parser_helper *helper, struct token_val val) {
struct base_expr *expr = parse_expr(helper);
CONSUME_TOK(TOK_RIGHT_PAREN, "expected )");
if (expr->type == EXPR_LITERAL) {
struct base_expr *cast = parse_expr_internal(helper, PREC_CAST);
return init_cast(expr, cast);
}
return expr;
}
static struct base_expr *parse_array(struct parse_infix *infix, struct parser_helper *helper, struct base_expr *node, struct token_val val) {
int arg_count = 0;
struct base_expr **arguments = parse_arr_arguments(helper, &arg_count);
if (!arguments) {
return NULL;
}
for (int i = 0 ; i < arg_count ; i++) {
if (!arguments[i]) {
add_error(helper->errors, CREATE_ERROR("accessing invalid value", val.line, val.char_pos));
return NULL;
}
}
return init_array(node, arguments, arg_count);
}
// CALL
static struct base_expr *parse_call(struct parse_infix *infix, struct parser_helper *helper, struct base_expr *node, struct token_val val) {
int arg_cnt = 0;
struct base_expr **arguments = parse_arguments(helper, &arg_cnt);
return init_call_expr(node, arguments, arg_cnt);
}
// LITERAL
static struct base_expr *parse_literal(struct parse_prefix *prefix, struct parser_helper *helper, struct token_val val) {
return init_literal(val);
}
// CONSTRUCTOR
static struct base_expr *parse_constructor(struct parse_prefix *prefix, struct parser_helper *helper, struct token_val val) {
ENSURE_SIZE();
struct token_val name = READ_TOK();
if (name.type != TOK_IDENTIFIER) {
add_error(helper->errors, CREATE_ERROR("expected identifier", name.line, name.char_pos));
return NULL;
}
// TODO: change this!
struct base_expr *ret = malloc(sizeof(struct base_expr));
if (!ret) {
printf("[ERROR] Not enough memory\n");
exit(1);
}
ret->type = EXPR_CONS;
struct base_expr *cons = malloc(sizeof(struct base_expr));
if (!cons) {
printf("[ERROR] Not enough memory\n");
exit(1);
}
ret->as.cons = cons;
struct base_expr *call_name = init_literal(name);
if (ACTUAL_TOK().type == TOK_LEFT_PAREN) {
CONSUME_TOK(TOK_LEFT_PAREN, "expected (");
int arg_cnt = 0;
struct base_expr **arguments = parse_arguments(helper, &arg_cnt);
cons->as.call = init_call_expr(call_name, arguments, arg_cnt)->as.call;
cons->type = EXPR_CALL;
return ret;
} else if (ACTUAL_TOK().type == TOK_LEFT_BRACKETS) {
CONSUME_TOK(TOK_LEFT_BRACKETS, "expected [");
int arg_count = 0;
struct base_expr **arguments = parse_arr_arguments(helper, &arg_count);
if (!arguments) {
return NULL;
}
cons->as.arr = init_array(call_name, arguments, arg_count)->as.arr;
cons->type = EXPR_ARRAY;
return ret;
} else {
add_error(helper->errors, CREATE_ERROR("expected [ or (", name.line, name.char_pos));
return NULL;
}
}
// - | +
struct binary_infix term = BINARY_INFIX(PREC_TERM, parse_binary, false);
// * | / | %
struct binary_infix factor = BINARY_INFIX(PREC_FACTOR, parse_binary, false);
// &&
struct binary_infix and = BINARY_INFIX(PREC_AND, parse_binary, false);
// ||
struct binary_infix or = BINARY_INFIX(PREC_OR, parse_binary, false);
// == | !=
struct binary_infix equality = BINARY_INFIX(PREC_EQUALITY, parse_binary, false);
// >= | <= | < | >
struct binary_infix comparison = BINARY_INFIX(PREC_COMPARISON, parse_binary, false);
// =
struct binary_infix assign = BINARY_INFIX(PREC_ASSIGNMENT, parse_assign, false);
// ++ | --
struct parse_infix postfix = INFIX(PREC_POSTFIX, parse_unary_postfix);
// .
struct parse_infix access = INFIX(PREC_ACCESS, parse_access);
// call
struct parse_infix call = INFIX(PREC_CALL, parse_call);
// array access
struct parse_infix arr_access = INFIX(PREC_ARRAY, parse_array);
// - | ! | ++ | -- | +
struct parse_prefix prefix = PREFIX(parse_unary);
// constructor
struct parse_prefix cons = PREFIX(parse_constructor);
// ( expr )
struct parse_prefix grouping = PREFIX(parse_grouping);
// number | identifier | string
struct parse_prefix literal = PREFIX(parse_literal);
struct parser_helper init_parser_helper(struct lexer *lex, struct errors *errors) {
struct parser_helper helper;
helper.errors = errors;
helper.lex = lex;
helper.index = 0;
// init pratt parser
prefixes[TOK_STRING] = &literal;
prefixes[TOK_IDENTIFIER] = &literal;
prefixes[TOK_NUMBER] = &literal;
prefixes[TOK_FLOAT] = &literal;
prefixes[TOK_TRUE] = &literal;
prefixes[TOK_FALSE] = &literal;
prefixes[TOK_NULLV] = &literal;
prefixes[TOK_SUBSTRACT] = &prefix;
prefixes[TOK_ADD] = &prefix;
prefixes[TOK_NEGATION] = &prefix;
prefixes[TOK_INCREMENT] = &prefix;
prefixes[TOK_DECREMENT] = &prefix;
prefixes[TOK_NEW] = &cons;
prefixes[TOK_LEFT_PAREN] = &grouping;
infixes[TOK_SUBSTRACT] = (struct parse_infix*) &term;
infixes[TOK_ADD] = (struct parse_infix*) &term;
infixes[TOK_MULT] = (struct parse_infix*) &factor;
infixes[TOK_DIV] = (struct parse_infix*) &factor;
infixes[TOK_MOD] = (struct parse_infix*) &factor;
infixes[TOK_AND] = (struct parse_infix*) ∧
infixes[TOK_OR] = (struct parse_infix*) ∨
infixes[TOK_EQUAL] = (struct parse_infix*) &equality;
infixes[TOK_NOT_EQUAL] = (struct parse_infix*) &equality;
infixes[TOK_GREAT_EQUAL] = (struct parse_infix*) &comparison;
infixes[TOK_GREAT] = (struct parse_infix*) &comparison;
infixes[TOK_LESS_EQUAL] = (struct parse_infix*) &comparison;
infixes[TOK_LESS] = (struct parse_infix*) &comparison;
infixes[TOK_ASSIGN] = (struct parse_infix*) &assign;
infixes[TOK_DOT] = &access;
infixes[TOK_LEFT_BRACKETS] = &arr_access;
infixes[TOK_INCREMENT] = &postfix;
infixes[TOK_DECREMENT] = &postfix;
infixes[TOK_LEFT_PAREN] = &call;
return helper;
}
static struct base_expr *parse_expr(struct parser_helper *helper) {
return parse_expr_internal(helper, 0);
}
static struct base_expr *parse_expr_stmt(struct parser_helper *helper) {
struct base_expr *expr = parse_expr(helper);
CONSUME_TOK(TOK_SEMICOLON, "expected ;");
return expr;
}
// PARSE STATEMENTS
static struct block *parse_block(struct parser_helper *helper);
static struct prlang_type *parse_type(struct parser_helper *helper) {
struct token_val tok_type = READ_TOK();
if (tok_type.type != TOK_IDENTIFIER) {
add_error(helper->errors, CREATE_ERROR("expected type", tok_type.line, tok_type.char_pos));
return NULL;
}
struct token_val tok = ACTUAL_TOK();
int array_dim = 0;
while (tok.type == TOK_LEFT_BRACKETS) {
ENSURE_SIZE();
READ_TOK();
CONSUME_TOK(TOK_RIGHT_BRACKETS, "expected ]");
tok = ACTUAL_TOK();
array_dim++;
}
return init_type(tok_type, array_dim);
}
static struct base_stmt *parse_var(struct parser_helper *helper) {
bool constant = false;
ENSURE_SIZE();
struct token_val var = READ_TOK();
if (var.type == TOK_VAR) {
} else if (var.type == TOK_CONSTANT) {
constant = true;
} else {
add_error(helper->errors, CREATE_ERROR("expected var or const", var.line, var.char_pos));
return NULL;
}
struct prlang_type *type = parse_type(helper);
struct token_val tok = ACTUAL_TOK();
struct base_expr *assign = parse_expr_stmt(helper);
if (assign == NULL) {
return NULL;
}
enum expr_type assign_type = assign->type;
// case: var my_var;
// skip all the checks
if (assign_type == EXPR_LITERAL) {
return init_var_decl(assign, constant, type);
}
if (assign_type != EXPR_BINARY) {
add_error(helper->errors, CREATE_ERROR("invalid expr", tok.line, tok.char_pos));
return NULL;
}
struct binary_expr *bin = assign->as.binary;
if (bin->op.type != TOK_ASSIGN) {
add_error(helper->errors, CREATE_ERROR("invalid expr", tok.line, tok.char_pos));
return NULL;
}
if (bin->left->type != EXPR_LITERAL) {
add_error(helper->errors, CREATE_ERROR("invalid expr", tok.line, tok.char_pos));
return NULL;
}
return init_var_decl(assign, constant, type);
}
static struct base_stmt *parse_while(struct parser_helper *helper) {
CONSUME_TOK(TOK_WHILE, "expected while");
CONSUME_TOK(TOK_LEFT_PAREN, "expected (");
struct base_expr *cond = parse_expr(helper);
CONSUME_TOK(TOK_RIGHT_PAREN, "expected )");
struct block *body = parse_block(helper);
return init_while(cond, body);
}
static struct base_stmt *parse_for(struct parser_helper *helper) {
void *left;
bool left_stmt = false;
struct base_expr *med;
struct base_expr *right;
CONSUME_TOK(TOK_FOR, "expected for");
CONSUME_TOK(TOK_LEFT_PAREN, "expected (");
struct token_val tok = ACTUAL_TOK();
if (tok.type == TOK_VAR || tok.type == TOK_CONSTANT) {
left = parse_var(helper);
left_stmt = true;
} else if (tok.type == TOK_SEMICOLON) {
left = NULL;
ENSURE_SIZE();
READ_TOK();
} else {
left = parse_expr_stmt(helper);
}
tok = ACTUAL_TOK();
if (tok.type != TOK_SEMICOLON) {
med = parse_expr_stmt(helper);
} else {
med = NULL;
ENSURE_SIZE();
READ_TOK();
}
tok = ACTUAL_TOK();
if (tok.type != TOK_RIGHT_PAREN) {
right = parse_expr(helper);
ENSURE_SIZE();
READ_TOK();
} else {
right = NULL;
ENSURE_SIZE();
READ_TOK();
}
struct block *body = parse_block(helper);
return init_for(left, left_stmt, med, right, body);
}
static struct base_stmt *parse_if(struct parser_helper *helper) {
// condition array
int cond_cnt = 0;
int cond_arrsize = ARRAY_FACTOR;
struct base_expr **conditions = malloc(cond_arrsize * sizeof(struct base_expr*));
// block array
int block_cnt = 0;
int block_arrsize = ARRAY_FACTOR;
struct block **blocks = malloc(block_arrsize * sizeof(struct block*));
CONSUME_TOK(TOK_IF, "expected if");
CONSUME_TOK(TOK_LEFT_PAREN, "expected (");
struct base_expr *cond = parse_expr(helper);
add_toarray(&conditions, &cond_arrsize, &cond_cnt, cond);
CONSUME_TOK(TOK_RIGHT_PAREN, "expected )");
struct block *if_block = parse_block(helper);
add_toarray(&blocks, &block_arrsize, &block_cnt, if_block);
if (READ_TOK().type == TOK_ELSE) {
struct token_val next = ACTUAL_TOK();
switch (next.type) {
case TOK_IF:
{
struct if_stmt *elseif = parse_if(helper)->as.is;
if (elseif == NULL) {
return NULL;
}
for (int i = 0 ; i < elseif->cond_count ; i++) {
add_toarray(&conditions, &cond_arrsize, &cond_cnt, elseif->cond[i]);
}
for (int i = 0 ; i < elseif->body_count ; i++) {
add_toarray(&blocks, &block_arrsize, &block_cnt, elseif->blocks[i]);
}
free(elseif->cond);
free(elseif->blocks);
free(elseif);
break;
}
case TOK_LEFT_BRACES:
{
struct block *else_block = parse_block(helper);
add_toarray(&blocks, &block_arrsize, &block_cnt, else_block);
break;
}
default:
add_error(helper->errors, CREATE_ERROR("expected { or if", next.line, next.char_pos));
return NULL;
}
} else {
PREVIOUS_TOK();
}
return init_if(conditions, cond_cnt, blocks, block_cnt);
}
static struct base_stmt *parse_return(struct parser_helper *helper) {
CONSUME_TOK(TOK_RETURN, "expected return");
if (ACTUAL_TOK().type == TOK_SEMICOLON) {
ENSURE_SIZE();
READ_TOK();
return init_return_stmt(NULL);
}
struct base_expr *val = parse_expr_stmt(helper);
return init_return_stmt(val);
}
static struct base_stmt *parse_asm(struct parser_helper *helper) {
CONSUME_TOK(TOK_ASM, "expected asm");
CONSUME_TOK(TOK_LEFT_BRACES, "expected {");
size_t count = 0;
int arr_size = ARRAY_FACTOR;
char **code = malloc(sizeof(char*) * arr_size);
while (ACTUAL_TOK().type != TOK_RIGHT_BRACES && ACTUAL_TOK().type != TOK_EXIT) {
if (ACTUAL_TOK().type != TOK_STRING) {
add_error(helper->errors, CREATE_ERROR("expected string", ACTUAL_TOK().line, ACTUAL_TOK().char_pos));
return NULL;
}
add_toarray(&code, &arr_size, &count, ACTUAL_TOK().val);
ENSURE_SIZE();
READ_TOK();
}
CONSUME_TOK(TOK_RIGHT_BRACES, "expected }");
return init_asm(code, count);
}
static struct base_stmt **parse_statements(struct parser_helper *helper, int *count) {
struct token_val tok = ACTUAL_TOK();
// ARRAY DETAILS
int arr_size = ARRAY_FACTOR;
struct base_stmt **statements = malloc(arr_size * sizeof(struct base_stmt*));
while (tok.type != TOK_RIGHT_BRACES) {
switch (tok.type) {
case TOK_IF:
{
struct base_stmt *ifs = parse_if(helper);
if (ifs == NULL) return NULL;
add_toarray(&statements, &arr_size, count, ifs);
break;
}
case TOK_ASM:
{
struct base_stmt *asms = parse_asm(helper);
if (asms == NULL) return NULL;
add_toarray(&statements, &arr_size, count, asms);
break;
}
case TOK_LEFT_BRACES:
{
struct block *bl = parse_block(helper);
if (!bl) return NULL;
struct base_stmt *stmt = malloc(sizeof(struct base_stmt*));
if (!stmt) {
printf("[ERROR] Not enough memory\n");
exit(1);
}
stmt->type = STMT_BLOCK;
stmt->as.bl = bl;
add_toarray(&statements, &arr_size, count, stmt);
break;
}
case TOK_CONSTANT:
case TOK_VAR:
{
struct base_stmt *var = parse_var(helper);
if (var == NULL) return NULL;
add_toarray(&statements, &arr_size, count, var);
break;
}
case TOK_WHILE:
{
struct base_stmt *whv = parse_while(helper);
if (whv == NULL) return NULL;
add_toarray(&statements, &arr_size, count, whv);
break;
}
case TOK_FOR:
{
struct base_stmt *fr = parse_for(helper);
if (fr == NULL) return NULL;
add_toarray(&statements, &arr_size, count, fr);
break;
}
case TOK_RETURN:
{
struct base_stmt *ret = parse_return(helper);
if (ret == NULL) return NULL;
add_toarray(&statements, &arr_size, count, ret);
break;
}
case TOK_EXIT:
default:
{
struct base_expr *expr = parse_expr_stmt(helper);
if (!expr) return NULL;
struct base_stmt *stmt = malloc(sizeof(struct base_stmt*));
if (!stmt) {
printf("[ERROR] Not enough memory\n");
exit(1);
}
stmt->type = STMT_EXPR;
stmt->as.expr = expr;
add_toarray(&statements, &arr_size, count, stmt);
break;
}
}
tok = ACTUAL_TOK();
}
return statements;
}
static struct block *parse_block(struct parser_helper *helper) {
CONSUME_TOK(TOK_LEFT_BRACES, "expected {");
int count = 0;
struct base_stmt **statements = parse_statements(helper, &count);
CONSUME_TOK(TOK_RIGHT_BRACES, "expected }");
return init_block(statements, count);
}
// PARSE FUNCTION
static struct function_arg *parse_fn_arguments(struct parser_helper *helper, int *argument_count) {
if (ACTUAL_TOK().type != TOK_RIGHT_PAREN) {
int arr_size = ARRAY_FACTOR;
struct function_arg *arguments = malloc(sizeof(struct function_arg) * arr_size);
do {
if (*argument_count >= arr_size) {
arr_size *= ARRAY_FACTOR;
struct function_arg *tmp = realloc(arguments, sizeof(struct function_arg) * arr_size);
if (tmp == NULL) {
printf("[ERROR] Not enough memory\n");
exit(1);
}
arguments = tmp;
}
ENSURE_SIZE();
struct token_val tok_type = READ_TOK();
if (tok_type.type != TOK_IDENTIFIER) {
add_error(helper->errors, CREATE_ERROR("expected identifier", tok_type.line, tok_type.char_pos));
return NULL;
}
int array_dim = 0;
struct token_val tok = ACTUAL_TOK();
while (tok.type == TOK_LEFT_BRACKETS) {
ENSURE_SIZE();
READ_TOK();
CONSUME_TOK(TOK_RIGHT_BRACKETS, "expected ]");
tok = ACTUAL_TOK();
array_dim++;
}
ENSURE_SIZE();
struct token_val iden_val = READ_TOK();
if (iden_val.type != TOK_IDENTIFIER) {
add_error(helper->errors, CREATE_ERROR("expected identifier", iden_val.line, iden_val.char_pos));
return NULL;
}
struct function_arg arg = { tok_type, iden_val, array_dim };
arguments[(*argument_count)++] = arg;
} while (READ_TOK().type == TOK_COMMA);
PREVIOUS_TOK();
CONSUME_TOK(TOK_RIGHT_PAREN, "expected )");
return arguments;
}
READ_TOK();
return NULL;
}
static struct function *parse_function(struct parser_helper *helper) {
ENSURE_SIZE();
struct prlang_type *type = parse_type(helper);
ENSURE_SIZE();
struct token_val name = READ_TOK();
if (name.type != TOK_IDENTIFIER) {
add_error(helper->errors, CREATE_ERROR("expected identifier", name.line, name.char_pos));
return NULL;
}
CONSUME_TOK(TOK_LEFT_PAREN, "expected (");
int argument_count = 0;
struct function_arg *arguments = parse_fn_arguments(helper, &argument_count);
struct block *body = parse_block(helper);
return init_function(body, name, type, arguments, argument_count);
}
// PARSE CLASS
static enum access_modifier get_access_modifier(enum token_type type) {
if (type == TOK_PUBLIC) {
return PUBLIC_MOD;
} else if (type == TOK_PROTECTED) {
return PROTECTED_MOD;
} else if (type == TOK_PRIVATE) {
return PRIVATE_MOD;
}
}
static struct class_member *parse_class_member(struct parser_helper *helper) {
ENSURE_SIZE();
struct token_val tok_access = READ_TOK();
if (tok_access.type != TOK_PRIVATE && tok_access.type != TOK_PROTECTED && tok_access.type != TOK_PUBLIC) {
add_error(helper->errors, CREATE_ERROR("expected public, private or protected", tok_access.line, tok_access.char_pos));
return NULL;
}
enum access_modifier mod = get_access_modifier(tok_access.type);
struct token_val tok = ACTUAL_TOK();
if (tok.type == TOK_VAR) {
struct base_stmt *decl = parse_var(helper);
if (decl == NULL) {
return NULL;
}
return init_class_property(mod, PROPERTY_MEMBER, decl);
} else if (tok.type == TOK_IDENTIFIER) {
struct function *fn = parse_function(helper);
if (fn == NULL) {
return NULL;
}
return init_class_method(mod, FUNCTION_MEMBER, fn);
} else {
add_error(helper->errors, CREATE_ERROR("expected var function or constructor", tok.line, tok.char_pos));
return NULL;
}
}
static struct class_decl *parse_class(struct parser_helper *helper) {
CONSUME_TOK(TOK_CLASS, "expected class");
ENSURE_SIZE();
struct token_val class_name = READ_TOK();
if (class_name.type != TOK_IDENTIFIER) {
add_error(helper->errors, CREATE_ERROR("expected type", class_name.line, class_name.char_pos));
return NULL;
}
CONSUME_TOK(TOK_LEFT_BRACES, "expected {");
struct token_val tok = ACTUAL_TOK();
// methods array
int members_cnt = 0;
int members_arrsize = ARRAY_FACTOR;
struct class_member **members = malloc(members_arrsize * sizeof(struct class_member*));
while (tok.type == TOK_PRIVATE || tok.type == TOK_PROTECTED || tok.type == TOK_PUBLIC) {
struct class_member *member = parse_class_member(helper);
if (member == NULL) {
return NULL;
}
add_toarray(&members, &members_arrsize, &members_cnt, member);
tok = ACTUAL_TOK();
}
CONSUME_TOK(TOK_RIGHT_BRACES, "expected }");
return init_class(class_name, members_cnt, members);
}
struct prlang_file *parse_prlang_file(struct parser_helper *helper) {
struct token_val tok = ACTUAL_TOK();
// functions array
int function_cnt = 0;
int function_arrsize = ARRAY_FACTOR;
struct function **functions = malloc(function_arrsize * sizeof(struct function*));
// globals array
int global_cnt = 0;
int global_arrsize = ARRAY_FACTOR;
struct var_decl **globals = malloc(global_arrsize * sizeof(struct var_decl*));
// classes array
int class_cnt = 0;
int class_arrsize = ARRAY_FACTOR;
struct class_decl **classes = malloc(class_arrsize * sizeof(struct class_decl*));
while (tok.type != TOK_EXIT) {
switch (tok.type) {
case TOK_IDENTIFIER:
{
struct function *fn = parse_function(helper);
if (fn == NULL) {
return NULL;
}
add_toarray(&functions, &function_arrsize, &function_cnt, fn);
break;
}
case TOK_VAR:
{
struct var_decl *decl = parse_var(helper);
if (decl == NULL) {
return NULL;
}
add_toarray(&globals, &global_arrsize, &global_cnt, decl);
break;
}
case TOK_CLASS:
{
struct class *klass = parse_class(helper);
if (klass == NULL) {
return NULL;
}
add_toarray(&classes, &class_arrsize, &class_cnt, klass);
break;
}
default:
add_error(helper->errors, CREATE_ERROR("unexpected token", tok.line, tok.char_pos));
return NULL;
}
tok = ACTUAL_TOK();
}
return init_prlang_file(globals, global_cnt, functions, function_cnt, classes, class_cnt);
}