-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
1133 lines (1092 loc) · 38.6 KB
/
parser.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 "parser.h"
token_t next_token, curr_token;
//for parse_asgnFollow to pass into parse_expr
token_t cached_identificator;
scanner_t* token_stream;
size_t param_count;
//precedence table operators
typedef enum _tab_op_t {TAB_SP, TAB_P, TAB_R, TAB_END, TAB_ERR} tab_op_t;
//symbols for pushdown automata
typedef enum _stack_sym {S_REL=0, S_ADDSUB, S_MULDIV, S_PAR_O, S_PAR_C,
S_ID, S_END, S_SEP, S_EXPR} stack_sym_t;
//rules to parse an expression, note: rules are used contrariwise
#define RULE_REL ((S_EXPR << 16) | (S_REL << 8) | S_EXPR) //E -> E <>= E
#define RULE_ADDSUB ((S_EXPR << 16) | (S_ADDSUB << 8) | S_EXPR) //E -> E +(-) E
#define RULE_MULDIV ((S_EXPR << 16) | (S_MULDIV << 8) | S_EXPR) //E -> E *(/) E
#define RULE_PAR ((S_PAR_C << 16) | (S_EXPR << 8) | S_PAR_O) //E -> ( E )
#define RULE_ID (S_ID) //E -> id
/* Precedence table for expression parsing:
*
* < - push separator and actual token (TAB_SP)
* = - push only actual token (TAB_P)
* > - reduce the top of stack (TAB_R)
* E - syntax error (TAB_E)
* _______ _______ _____ _____ ___ ___ ____ ___
* | | <,>,= | +,- | *,/ | ( | ) | id | $ |
* |_______|_______|_____|_____|___|___|____|___|
* | <,>,= | > | < | < | < | > | < | > |
* |_______|_______|_____|_____|___|___|____|___|
* | +,- | > | > | < | < | > | < | > |
* |_______|_______|_____|_____|___|___|____|___|
* | *,/ | > | > | > | < | > | < | > |
* |_______|_______|_____|_____|___|___|____|___|
* | ( | < | < | < | < | = | < | E |
* |_______|_______|_____|_____|___|___|____|___|
* | ) | > | > | > | E | > | E | > |
* |_______|_______|_____|_____|___|___|____|___|
* | id | > | > | > | E | > | E | > |
* |_______|_______|_____|_____|___|___|____|___|
* | $ | < | < | < | < | E | < | & |
* |_______|_______|_____|_____|___|___|____|___|
*
*/
tab_op_t prec_table[][7] = {
{ TAB_R, TAB_SP, TAB_SP, TAB_SP, TAB_R, TAB_SP, TAB_R },
{ TAB_R, TAB_R, TAB_SP, TAB_SP, TAB_R, TAB_SP, TAB_R },
{ TAB_R, TAB_R, TAB_R, TAB_SP, TAB_R, TAB_SP, TAB_R },
{ TAB_SP, TAB_SP, TAB_SP, TAB_SP, TAB_P, TAB_SP, TAB_ERR },
{ TAB_R, TAB_R, TAB_R, TAB_ERR, TAB_R, TAB_ERR, TAB_R },
{ TAB_R, TAB_R, TAB_R, TAB_ERR, TAB_R, TAB_ERR, TAB_R },
{ TAB_SP, TAB_SP, TAB_SP, TAB_SP, TAB_ERR, TAB_SP, TAB_END }
};
void parse_asgn();
expression_t parse_asgnFollow();
void parse_block(bool new_scope);
void parse_cinStmt();
void parse_cinStmtFollow();
void parse_coutStmt();
void parse_coutStmtFollow();
expression_t parse_expr();
void parse_forClause();
void parse_funcBody();
void parse_funcHead();
void parse_funcs();
void parse_ifClause();
void parse_paramList(symbol_t* func);
void parse_paramListFollow(unode_str_t* param);
void parse_paramSpec();
void parse_paramSpecFollow();
void parse_stmt();
void parse_stmts();
void parse_term();
void parse_type();
void parse_varDef();
void parse_varDefFollow(symbol_t* var);
//entry point function
void parse_program(scanner_t* s) {
token_stream = s;
next_token = get_next_token(token_stream);
cached_identificator.type = TT_NONE;
parse_funcs();
}
void match(enum e_token_t token) {
if (next_token.type != token)
error("Syntactic error: Failed to parse the program", ERROR_SYN);
curr_token = next_token;
next_token = get_next_token(token_stream);
}
void match_deduction(enum e_token_t token) {
if (next_token.type != token)
error("Unitinitialized auto variable", ERROR_TYPE_DEDUCTION);
curr_token = next_token;
next_token = get_next_token(token_stream);
}
stack_sym_t token_to_sym(token_t *tok) {
//count an open parentheses in expression
static int parentheses_pairs = 0;
switch(tok->type) {
case TT_OP_RELATIONAL:
return S_REL;
case TT_OP_ARITHMETIC:
switch (tok->op_arith) {
case OP_ARITH_ADD:
case OP_ARITH_SUBTRACT:
return S_ADDSUB;
case OP_ARITH_MULTIPLY:
case OP_ARITH_DIVIDE:
return S_MULDIV;
}
case TT_PARENTHESES_OPEN:
parentheses_pairs++;
return S_PAR_O;
case TT_PARENTHESES_CLOSE:
if (parentheses_pairs == 0) {
//not an expression close parenthesis
return S_END;
} else {
parentheses_pairs--;
return S_PAR_C;
}
case TT_IDENTIFICATOR:
case TT_LIT_INT:
case TT_LIT_DOUBLE:
case TT_LIT_STRING:
return S_ID;
default:
return S_END;
}
}
int reduce_sequence(vector_char_t* stack) {
stack_sym_t sym;
int seq = 0;
while ((sym = vector_pop(stack)) != S_SEP) {
seq |= sym;
seq <<= 8;
}
return seq >> 8;
}
void parse_asgn() {
switch(next_token.type) {
case TT_IDENTIFICATOR:
match(TT_IDENTIFICATOR);
symbol_t* var = var_table_find(curr_token.str);
if (var == NULL)
error("Trying to assign to undefined variable", ERROR_SEM);
match(TT_OP_ASSIGNMENT);
parse_asgnFollow(var);
var->def = true;
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
expression_t parse_asgnFollow(symbol_t* var) {
expression_t expr;
expr.type = NONE_DT;
switch(next_token.type) {
case TT_IDENTIFICATOR:
cached_identificator = next_token;
match(TT_IDENTIFICATOR);
// TODO REMOVE IF FUNEXP
if (next_token.type == TT_PARENTHESES_OPEN) {
func_call_init();
func_call_set_name(curr_token.str);
symbol_t* func_def = func_table_find(curr_token.str);
size_t return_value_addr;
if (func_def){
if (!func_def->def)
error("Function call for an undefined function", ERROR_SEM);
//alocate space for return value
return_value_addr = generate_call_init();
} else {
error("Call to unknown function", ERROR_SEM);
}
match(TT_PARENTHESES_OPEN);
parse_paramList(func_def);
match(TT_PARENTHESES_CLOSE);
func_call_t* func_call = func_call_finish();
if (!is_valid_func_call(func_call, func_def))
error("Bad function call parameters types/count", ERROR_TYPE_COMPAT);
generate_call(func_def->addr, param_count);
expr.type = func_def->type;
expr.addr = return_value_addr;
cached_identificator.type = TT_NONE;
} else {
expr = parse_expr();
}
break;
case TT_LIT_INT:
case TT_LIT_DOUBLE:
case TT_LIT_STRING:
case TT_PARENTHESES_OPEN:
expr = parse_expr();
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
//semantic analysis
switch (expr.type) {
case DOUBLE_DT:
case DOUBLE_LIT_DT:
if (var->type != DOUBLE_DT &&
var->type != INT_DT)
error("Incompatible type variable assignment",
ERROR_TYPE_COMPAT);
break;
case INT_DT:
case INT_LIT_DT:
if (var->type != DOUBLE_DT &&
var->type != INT_DT)
error("Incompatible type variable assignment",
ERROR_TYPE_COMPAT);
break;
case STRING_DT:
case STRING_LIT_DT:
if (var->type != STRING_DT)
error("Incompatible type variable assignment",
ERROR_TYPE_COMPAT);
break;
default:
;
}
//generate assignment instruction
if (expr.type != NONE_DT) { //TODO: REMOVE ?!
switch (expr.type) {
case DOUBLE_DT:
if(var->type == DOUBLE_DT) {
generate_mov(var->addr, expr.addr);
} else {
size_t conv_expr = generate_double_to_int(expr.addr);
generate_mov(var->addr, conv_expr);
}
break;
case INT_DT:
if(var->type == INT_DT) {
generate_mov(var->addr, expr.addr);
} else {
size_t conv_expr = generate_int_to_double(expr.addr);
generate_mov(var->addr, conv_expr);
}
break;
case STRING_DT:
generate_mov(var->addr, expr.addr);
break;
case DOUBLE_LIT_DT:
if(var->type == DOUBLE_DT)
generate_mov_double(var->addr, expr.double_val);
else
generate_mov_double(var->addr, (int)expr.double_val);
break;
case INT_LIT_DT:
if(var->type == INT_DT)
generate_mov_int(var->addr, expr.int_val);
else
generate_mov_int(var->addr, (double)expr.int_val);
break;
case STRING_LIT_DT:
generate_mov_string(var->addr, expr.str_val);
break;
default:
;
}
}
return expr;
}
void parse_block(bool new_scope) {
switch(next_token.type) {
case TT_BLOCK_START:
match(TT_BLOCK_START);
bool emptyBlock = false;
if (next_token.type == TT_BLOCK_END)
emptyBlock = true;
if (new_scope && !emptyBlock)
var_table_scope_enter();
parse_stmts();
match(TT_BLOCK_END);
if (!new_scope || (new_scope && !emptyBlock))
var_table_scope_exit();
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_cinStmt() {
switch(next_token.type) {
case TT_KW_CIN:
match(TT_KW_CIN);
match(TT_OP_STREAM_IN);
match(TT_IDENTIFICATOR);
symbol_t* var = var_table_find(curr_token.str);
if (var == NULL)
error("Trying to use undefined variable", ERROR_SEM);
switch(var->type) {
case INT_DT:
generate_cin_int(var->addr);
break;
case DOUBLE_DT:
generate_cin_double(var->addr);
break;
case STRING_DT:
generate_cin_string(var->addr);
break;
default:
;
}
var->def = true;
parse_cinStmtFollow();
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_cinStmtFollow() {
switch(next_token.type) {
case TT_OP_STREAM_IN:
match(TT_OP_STREAM_IN);
match(TT_IDENTIFICATOR);
symbol_t* var = var_table_find(curr_token.str);
if (var == NULL)
error("Trying to use undefined variable", ERROR_SEM);
switch(var->type) {
case INT_DT:
generate_cin_int(var->addr);
break;
case DOUBLE_DT:
generate_cin_double(var->addr);
break;
case STRING_DT:
generate_cin_string(var->addr);
break;
default:
;
}
var->def = true;
parse_cinStmtFollow();
break;
default:
return;
}
}
void parse_coutStmt() {
switch(next_token.type) {
case TT_KW_COUT:
match(TT_KW_COUT);
match(TT_OP_STREAM_OUT);
parse_term();
symbol_t* var;
if (curr_token.type == TT_IDENTIFICATOR) {
var = var_table_find(curr_token.str);
if (var == NULL) {
error("Reference to undefined variable", ERROR_SEM);
} else if (var->def != true) {
error("Trying to use uninitialized variable", ERROR_UNDEF);
}
switch(var->type) {
case INT_DT:
generate_cout_int(var->addr);
break;
case DOUBLE_DT:
generate_cout_double(var->addr);
break;
case STRING_DT:
generate_cout_string(var->addr);
break;
default:
;
}
}
switch(curr_token.type) {
case TT_LIT_INT:
generate_cout_int_lit(curr_token.int_val);
break;
case TT_LIT_DOUBLE:
generate_cout_double_lit(curr_token.double_val);
break;
case TT_LIT_STRING:
generate_cout_string_lit(curr_token.str);
break;
default:
;
}
parse_coutStmtFollow();
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_coutStmtFollow() {
switch(next_token.type) {
case TT_OP_STREAM_OUT:
match(TT_OP_STREAM_OUT);
parse_term();
symbol_t* var;
if (curr_token.type == TT_IDENTIFICATOR) {
var = var_table_find(curr_token.str);
if (var == NULL) {
error("Reference to undefined variable", ERROR_SEM);
} else if (var->def != true) {
error("Trying to use uninitialized variable", ERROR_UNDEF);
}
switch(var->type) {
case INT_DT:
generate_cout_int(var->addr);
break;
case DOUBLE_DT:
generate_cout_double(var->addr);
break;
case STRING_DT:
generate_cout_string(var->addr);
break;
default:
;
}
}
switch(curr_token.type) {
case TT_LIT_INT:
generate_cout_int_lit(curr_token.int_val);
break;
case TT_LIT_DOUBLE:
generate_cout_double_lit(curr_token.double_val);
break;
case TT_LIT_STRING:
generate_cout_string_lit(curr_token.str);
break;
default:
;
}
parse_coutStmtFollow();
break;
default:
return;
}
}
void parse_forClause() {
expression_t expr;
size_t conv_expr;
size_t loop_start;
size_t loop_end;
size_t asgn_start;
size_t block_start;
size_t jc_addr;
size_t jmp_to_block_addr;
size_t asgn_frame;
size_t block_frame;
bool jmp_used = true;
switch(next_token.type) {
case TT_KW_FOR:
match(TT_KW_FOR);
match(TT_PARENTHESES_OPEN);
var_table_scope_enter();
block_frame = store_stack_frame();
parse_varDef();
match(TT_SEMICOLON);
//loop start
loop_start = get_code_seg_top();
generate_data_seg_restore(store_stack_frame());
expr = parse_expr();
match(TT_SEMICOLON);
//gen jc
jc_addr = get_code_seg_top();
switch (expr.type) {
case DOUBLE_LIT_DT:
if(!((int)expr.double_val))
generate_jump(0); //address is unknown yet
else
jmp_used = false;
break;
case INT_LIT_DT:
if(!expr.int_val)
generate_jump(0); //address is unknown yet
else
jmp_used = false;
break;
case DOUBLE_DT:
conv_expr = generate_double_to_int(expr.addr);
generate_neg_cond_jump(0, conv_expr); //address is unknown yet
break;
case INT_DT:
generate_neg_cond_jump(0, expr.addr); //address is unknown yet
break;
case STRING_LIT_DT:
case STRING_DT:
error("String value in a if statement", ERROR_TYPE_COMPAT);
break;
default:
;
}
//jump to block start
jmp_to_block_addr = get_code_seg_top();
generate_jump(0); //address is unknown yet
//assignment start
asgn_start = get_code_seg_top();
asgn_frame = store_stack_frame();
parse_asgn();
load_stack_frame(asgn_frame);
//jump to loop start
generate_jump(loop_start);
match(TT_PARENTHESES_CLOSE);
//block start
block_start = get_code_seg_top();
set_jump_addr(jmp_to_block_addr, block_start);
parse_block(false);
//jump to assignment
generate_jump(asgn_start);
//loop end
loop_end = get_code_seg_top();
if (jmp_used)
set_jump_addr(jc_addr, loop_end);
break;
load_stack_frame(block_frame);
generate_data_seg_restore(block_frame);
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_funcBody(symbol_t* funcRef) {
symbol_t* func;
switch(next_token.type) {
case TT_SEMICOLON:
match(TT_SEMICOLON);
break;
case TT_BLOCK_START:
func = func_table_find(func_get_name());
func->def = true; //function is defined
func->addr = get_code_seg_top();
var_table_scope_enter();
init_new_stack_frame(param_count);
if (funcRef->paramList) {
unode_str_t* paramIter = funcRef->paramList->front;
while (paramIter) {
var_table_add(&(paramIter->item));
paramIter = paramIter->next;
}
}
parse_block(false);
generate_no_return_exception();
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_funcHead() {
switch(next_token.type) {
case TT_TYPE_DOUBLE:
case TT_TYPE_INT:
case TT_TYPE_STRING:
func_init();
match(next_token.type);
func_set_return_type(curr_token.type);
match(TT_IDENTIFICATOR);
func_set_name(curr_token.str);
match(TT_PARENTHESES_OPEN);
parse_paramSpec();
func_set_param_count(param_count);
match(TT_PARENTHESES_CLOSE);
symbol_t* funcRef = func_finish();
func_table_add(funcRef);
parse_funcBody(funcRef);
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_funcs() {
switch(next_token.type) {
case TT_TYPE_DOUBLE:
case TT_TYPE_INT:
case TT_TYPE_STRING:
parse_funcHead();
parse_funcs();
break;
case TT_EOF:
//printf("Successful parse\n");
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_ifClause() {
expression_t expr;
size_t conv_expr;
size_t jc_addr;
size_t jmp_addr;
size_t stack_frame;
bool jmp_used = true;
switch(next_token.type) {
case TT_KW_IF:
match(TT_KW_IF);
match(TT_PARENTHESES_OPEN);
expr = parse_expr();
match(TT_PARENTHESES_CLOSE);
jc_addr = get_code_seg_top();
switch (expr.type) {
case DOUBLE_LIT_DT:
if(!((int)expr.double_val))
generate_jump(0); //address is unknown yet
else
jmp_used = false;
break;
case INT_LIT_DT:
if(!expr.int_val)
generate_jump(0); //address is unknown yet
else
jmp_used = false;
break;
case DOUBLE_DT:
conv_expr = generate_double_to_int(expr.addr);
generate_neg_cond_jump(0, conv_expr); //address is unknown yet
break;
case INT_DT:
generate_neg_cond_jump(0, expr.addr); //address is unknown yet
break;
case STRING_LIT_DT:
case STRING_DT:
error("String value in a if statement", ERROR_TYPE_COMPAT);
break;
default:
;
}
stack_frame = store_stack_frame();
parse_block(true);
load_stack_frame(stack_frame);
generate_data_seg_restore(stack_frame);
jmp_addr = get_code_seg_top();
generate_jump(0); //address is unknown yet
if (jmp_used)
set_jump_addr(jc_addr, get_code_seg_top());
match(TT_KW_ELSE);
stack_frame = store_stack_frame();
parse_block(true);
load_stack_frame(stack_frame);
generate_data_seg_restore(stack_frame);
set_jump_addr(jmp_addr, get_code_seg_top());
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_paramList(symbol_t* func) {
param_count = 0; //initialize param counter
symbol_t* var;
unode_str_t* param;
e_data_t func_param_type;
switch(next_token.type) {
case TT_LIT_INT:
case TT_LIT_DOUBLE:
case TT_LIT_STRING:
case TT_IDENTIFICATOR:
parse_term();
if (func->paramList && (param = func->paramList->back))
func_param_type = param->item.type;
else
error("Bad function call parameters types/count", ERROR_TYPE_COMPAT);
//var_init(); //TODO: ???
switch (curr_token.type) {
case TT_LIT_INT:
func_call_add_param(INT_DT);
if (func_param_type == INT_DT)
generate_param_push_int(curr_token.int_val);
else
generate_param_push_double((double)curr_token.int_val);
break;
case TT_LIT_DOUBLE:
func_call_add_param(DOUBLE_DT);
if (func_param_type == DOUBLE_DT)
generate_param_push_double(curr_token.double_val);
else
generate_param_push_int((int)curr_token.double_val);
break;
case TT_LIT_STRING:
func_call_add_param(STRING_DT);
generate_param_push_string(curr_token.str);
break;
case TT_IDENTIFICATOR:
var = var_table_find(curr_token.str);
if (var == NULL) {
error("Reference to undefined variable", ERROR_SEM);
} else if (var->def != true) {
error("Trying to use uninitialized variable", ERROR_UNDEF);
} else {
func_call_add_param(var->type);
if (var->type == DOUBLE_DT && func_param_type == INT_DT)
generate_param_conv_push(var->addr, true, false);
else if (var->type == INT_DT && func_param_type == DOUBLE_DT)
generate_param_conv_push(var->addr, false, true);
else
generate_param_push(var->addr);
}
break;
default:
;
}
++param_count;
parse_paramListFollow(param->prev);
break;
default:
return;
}
}
void parse_paramListFollow(unode_str_t* param) {
symbol_t* var;
e_data_t func_param_type;
switch(next_token.type) {
case TT_COMMA:
match(TT_COMMA);
parse_term();
if (param)
func_param_type = param->item.type;
else
error("Bad function call parameters types/count", ERROR_TYPE_COMPAT);
//var_init(); //TODO: ???
switch (curr_token.type) {
case TT_LIT_INT:
func_call_add_param(INT_DT);
if (func_param_type == INT_DT)
generate_param_push_int(curr_token.int_val);
else
generate_param_push_double((double)curr_token.int_val);
break;
case TT_LIT_DOUBLE:
func_call_add_param(DOUBLE_DT);
if (func_param_type == DOUBLE_DT)
generate_param_push_double(curr_token.double_val);
else
generate_param_push_int((int)curr_token.double_val);
break;
case TT_LIT_STRING:
func_call_add_param(STRING_DT);
generate_param_push_string(curr_token.str);
break;
case TT_IDENTIFICATOR:
var = var_table_find(curr_token.str);
if (var == NULL) {
error("Reference to undefined variable", ERROR_SEM);
} else if (var->def != true) {
error("Trying to use uninitialized variable", ERROR_UNDEF);
} else {
func_call_add_param(var->type);
if (var->type == DOUBLE_DT && func_param_type == INT_DT)
generate_param_conv_push(var->addr, true, false);
else if (var->type == INT_DT && func_param_type == DOUBLE_DT)
generate_param_conv_push(var->addr, false, true);
else
generate_param_push(var->addr);
}
break;
default:
;
}
++param_count;
parse_paramListFollow(param->prev);
break;
default:
return;
}
}
void parse_paramSpec() {
param_count = 0; //initialize param counter
switch(next_token.type) {
case TT_TYPE_DOUBLE:
case TT_TYPE_INT:
case TT_TYPE_STRING:
match(next_token.type);
var_init();
var_set_type(curr_token.type);
match(TT_IDENTIFICATOR);
var_set_name(curr_token.str);
var_set_addr(++param_count);
var_set_initialized();
func_add_param(var_finish());
parse_paramSpecFollow();
break;
default:
return;
}
}
void parse_paramSpecFollow() {
switch(next_token.type) {
case TT_COMMA:
match(TT_COMMA);
var_init();
parse_type();
var_set_type(curr_token.type);
match(TT_IDENTIFICATOR);
var_set_name(curr_token.str);
var_set_addr(++param_count);
var_set_initialized();
func_add_param(var_finish());
parse_paramSpecFollow();
break;
default:
return;
}
}
void parse_stmt() {
switch(next_token.type) {
case TT_TYPE_DOUBLE:
case TT_TYPE_INT:
case TT_TYPE_STRING:
case TT_TYPE_AUTO:
parse_varDef();
match(TT_SEMICOLON);
break;
case TT_IDENTIFICATOR:
parse_asgn();
match(TT_SEMICOLON);
break;
case TT_KW_RETURN:
match(TT_KW_RETURN);
expression_t expr = parse_expr();
switch (expr.type) {
case DOUBLE_DT:
case DOUBLE_LIT_DT:
if (func_get_return_type() != DOUBLE_DT &&
func_get_return_type() != INT_DT)
error("Variable with bad type in a return statement",
ERROR_TYPE_COMPAT);
break;
case INT_DT:
case INT_LIT_DT:
if (func_get_return_type() != DOUBLE_DT &&
func_get_return_type() != INT_DT)
error("Variable with bad type in a return statement",
ERROR_TYPE_COMPAT);
break;
case STRING_DT:
case STRING_LIT_DT:
if (func_get_return_type() != STRING_DT)
error("Variable with bad type in a return statement",
ERROR_TYPE_COMPAT);
break;
default:
;
}
match(TT_SEMICOLON);
if (str_equal(func_get_name(), "main"))
generate_halt();
else {
switch (expr.type) {
case DOUBLE_DT:
if(func_get_return_type() == DOUBLE_DT) {
generate_return(expr.addr, func_get_param_count());
} else {
size_t conv_expr = generate_double_to_int(expr.addr);
generate_return(conv_expr, func_get_param_count());
}
break;
case INT_DT:
if(func_get_return_type() == INT_DT) {
generate_return(expr.addr, func_get_param_count());
} else {
size_t conv_expr = generate_int_to_double(expr.addr);
generate_return(conv_expr, func_get_param_count());
}
break;
case STRING_DT:
generate_return(expr.addr, func_get_param_count());
break;
case DOUBLE_LIT_DT:
if(func_get_return_type() == DOUBLE_DT)
generate_return_double(expr.double_val, func_get_param_count());
else
generate_return_double((int)expr.double_val, func_get_param_count());
break;
case INT_LIT_DT:
if(func_get_return_type() == INT_DT)
generate_return_int(expr.int_val, func_get_param_count());
else
generate_return_int((double)expr.int_val, func_get_param_count());
break;
case STRING_LIT_DT:
generate_return_string(expr.str_val, func_get_param_count());
break;
default:
;
}
}
break;
case TT_KW_COUT:
parse_coutStmt();
match(TT_SEMICOLON);
break;
case TT_KW_CIN:
parse_cinStmt();
match(TT_SEMICOLON);
break;
case TT_BLOCK_START:
parse_block(true);
break;
case TT_KW_IF:
parse_ifClause();
break;
case TT_KW_FOR:
parse_forClause();
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_stmts() {
switch(next_token.type) {
case TT_TYPE_DOUBLE:
case TT_TYPE_INT:
case TT_TYPE_STRING:
case TT_TYPE_AUTO:
case TT_IDENTIFICATOR:
case TT_KW_RETURN:
case TT_KW_COUT:
case TT_KW_CIN:
case TT_BLOCK_START:
case TT_KW_IF:
case TT_KW_FOR:
parse_stmt();
parse_stmts();
break;
default:
return;
}
}
void parse_term() {
switch(next_token.type) {
case TT_LIT_INT:
match(TT_LIT_INT);
break;
case TT_LIT_DOUBLE:
match(TT_LIT_DOUBLE);
break;
case TT_LIT_STRING:
match(TT_LIT_STRING);
break;
case TT_IDENTIFICATOR:
match(TT_IDENTIFICATOR);
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_type() {
switch(next_token.type) {
case TT_TYPE_DOUBLE:
match(TT_TYPE_DOUBLE);
break;
case TT_TYPE_INT:
match(TT_TYPE_INT);
break;
case TT_TYPE_STRING:
match(TT_TYPE_STRING);
break;
default:
error("Syntactic error: Failed to parse the program", ERROR_SYN);
}
}
void parse_varDef() {
expression_t expr;
size_t addr;
switch(next_token.type) {
case TT_TYPE_DOUBLE:
case TT_TYPE_INT:
case TT_TYPE_STRING:
var_init();
parse_type();
var_set_type(curr_token.type);
match(TT_IDENTIFICATOR);
var_set_name(curr_token.str);
addr = generate_push();
var_set_addr(addr);