-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.c
More file actions
1959 lines (1650 loc) · 49 KB
/
parse.c
File metadata and controls
1959 lines (1650 loc) · 49 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
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 <stdio.h> //for fopen, printf
#include <stdlib.h> //for malloc, exit
#include <memory.h> //for memcp
#include <string.h> //for strcmp
#include "types.h"
#include "lex.h"
#include "parse.h"
#include "emit.h"
#include "vm.h"
extern struct_token token; //in lex.c
extern char *source; //in lex.c
extern int line_number; //in lex.c
struct_token cur_token;
struct_token upcoming_token;
//let's not distinguish between
//declaration and definition.
//say declaration(dec) = definition(def).
//in programming we have two things.
//data and code.
//data is our variables.
//code is our functions.
//data has type and instance.
// we know them as "datatype" dec/def and "variable" dec/def.
//function also has type and instance.
// we know them as "prototype" dec and "function" def.
//difference is that,
//we have "many" "variables" for "one" "datatype".
// that is why we separated struct_type and struct_symbol
// struct_type is for "datatype" dec/def
// struct_symbol is for "variable" dec/def
//but we have "one" "function" for "one" "prototype".
// that is why we have only struct_func
// struct_func is for "prototype" dec
// and "function" def both
//"datatype" dec/def
struct_type types[MAX_TYPES];
int num_types=2;
//"variable" dec/def
struct_symbol symbols[MAX_SYMBOLS];
// All variables we have declared so far.
// and strings that we have used so far.
int num_symbols=0;
//"prototype" dec and
//"function" def both
//for general funcs
//and mangled must funcs
//and mangled can funcs
struct_func funcs[MAX_FUNCS];
int num_funcs=0;
//"prototype" dec only
//for must funcs
//just for matching purpose
//"function" def
//for must funcs
//are inside funcs
struct_func musts[MAX_MUSTS];
int num_musts=0;
struct_fix fixfaddr[MAX_FIXES];
int num_fixes=0;
int convert_text_to_num(char* text)
{
int len = strlen(text);
if(len > 9)
{
printf("%d: Warning: Numbers more than 9 digits are error codes.\n", line_number);
//exit(1);
}
int num=0;
for (int i=0; i<len; i++)
{
num = num*10 + (text[i]-48); //ascii of char 0 is 48
}
if(num >= 2000000000)
{
printf("%d: Error: Number >= 2000000000. Too big number.\n", line_number);
exit(1);
}
return num;
}
int convert_char_to_num(char* text)
{
return text[0]; //ascii of char literal
}
int members_contains(int rec_ti, char* memname)
{
for(int i=0; i<types[rec_ti].rec_mem_cnt; i++)
{
if(0==strcmp(types[rec_ti].rec_mem_list[i].text, memname))
{
return 1;
}
}
return 0;
}
void members_add(int rec_ti, char* memname, int mem_ti)
{
int member_index = types[rec_ti].rec_mem_cnt; //index for new member
types[rec_ti].rec_mem_cnt += 1; //increase total count
if(types[rec_ti].rec_mem_cnt > MAX_MEMBERS)
{
printf("%d: member table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(types[rec_ti].rec_mem_list[member_index].text, memname);
strcpy(types[rec_ti].rec_mem_list[member_index].type, types[mem_ti].text);
if (member_index == 0)
types[rec_ti].rec_mem_list[member_index].addr=0;
else
{
int prev_mem_addr = types[rec_ti].rec_mem_list[member_index-1].addr;
int prev_mem_ti = get_type_index(types[rec_ti].rec_mem_list[member_index-1].type);
int prev_mem_size = types[prev_mem_ti].sizebyte;
int prev_mem_size_aligned = get_bytes_req(prev_mem_size); //get aligned
types[rec_ti].rec_mem_list[member_index].addr=prev_mem_addr + prev_mem_size_aligned;
}
}
}
int get_member_index(int rec_ti, char* memname)
{
for(int i=0; i<types[rec_ti].rec_mem_cnt; i++)
{
if(0==strcmp(types[rec_ti].rec_mem_list[i].text, memname))
{
return i;
}
}
return -1;
}
int params_contains(int func_index, char* paramname)
{
for(int i=0; i<funcs[func_index].func_param_cnt; i++)
{
if(0==strcmp(funcs[func_index].params[i].text, paramname))
{
return 1;
}
}
return 0;
}
void params_add(int func_index, char* paramname, int param_ti)
{
int param_index = funcs[func_index].func_param_cnt;
//index for new param
funcs[func_index].func_param_cnt += 1;
//increase total count
if(funcs[func_index].func_param_cnt > MAX_PARAMS)
{
printf("%d: param table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(funcs[func_index].params[param_index].text, paramname);
strcpy(funcs[func_index].params[param_index].type, types[param_ti].text);
//addr of params to be taken care after all params added
//high address
//arg_1: new_bp+16
//arg_2: new_bp+12
//arg_3: new_bp+8
//ret addr: new_bp+4
//old_bp: new_bp
//local_1: new_bp-4
//local_2: new_bp-8
//local_3: new_bp-12
//low address
}
}
int get_param_index(int func_index, char* paramname)
{
for(int i=0; i<funcs[func_index].func_param_cnt; i++)
{
if(0==strcmp(funcs[func_index].params[i].text, paramname))
{
return i;
}
}
return -1;
}
int locals_contains(int func_index, char* localname)
{
for(int i=0; i<funcs[func_index].func_local_cnt; i++)
{
if(0==strcmp(funcs[func_index].locals[i].text, localname))
{
return 1;
}
}
return 0;
}
void locals_add(int func_index, char* localname, int local_ti)
{
int local_index = funcs[func_index].func_local_cnt;
//index for new local
funcs[func_index].func_local_cnt += 1;
//increase total count
if(funcs[func_index].func_local_cnt > MAX_PARAMS)
{
printf("%d: local table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(funcs[func_index].locals[local_index].text, localname);
strcpy(funcs[func_index].locals[local_index].type, types[local_ti].text);
//addr of locals to be taken care now
if(local_index == 0)
{
funcs[func_index].locals[local_index].addr = -4;
//offset to bp
}
else
{
int prev_local_addr = funcs[func_index].locals[local_index-1].addr;
funcs[func_index].locals[local_index].addr = prev_local_addr - 4;
}
//high address
//arg_1: new_bp+16
//arg_2: new_bp+12
//arg_3: new_bp+8
//ret addr: new_bp+4
//old_bp: new_bp
//local_1: new_bp-4
//local_2: new_bp-8
//local_3: new_bp-12
//low address
}
}
int get_local_index(int func_index, char* localname)
{
for(int i=0; i<funcs[func_index].func_local_cnt; i++)
{
if(0==strcmp(funcs[func_index].locals[i].text, localname))
{
return i;
}
}
return -1;
}
int mparams_contains(int mfunc_index, char* paramname)
{
for(int i=0; i<musts[mfunc_index].func_param_cnt; i++)
{
if(0==strcmp(musts[mfunc_index].params[i].text, paramname))
{
return 1;
}
}
return 0;
}
void mparams_add(int mfunc_index, char* paramname, int param_ti)
{
int param_index = musts[mfunc_index].func_param_cnt;
//index for new param
musts[mfunc_index].func_param_cnt += 1;
//increase total count
if(musts[mfunc_index].func_param_cnt > MAX_PARAMS)
{
printf("%d: param table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(musts[mfunc_index].params[param_index].text, paramname);
strcpy(musts[mfunc_index].params[param_index].type, types[param_ti].text);
//addr of params to be taken care after all params added
//high address
//arg_1: new_bp+16
//arg_2: new_bp+12
//arg_3: new_bp+8
//ret addr: new_bp+4
//old_bp: new_bp
//local_1: new_bp-4
//local_2: new_bp-8
//local_3: new_bp-12
//low address
}
}
int get_mparam_index(int mfunc_index, char* paramname)
{
for(int i=0; i<musts[mfunc_index].func_param_cnt; i++)
{
if(0==strcmp(musts[mfunc_index].params[i].text, paramname))
{
return i;
}
}
return -1;
}
int symbols_contains(char* symname)
{
for(int i=0; i<num_symbols; i++)
{
if(0==strcmp(symbols[i].text, symname))
{
return 1;
}
}
return 0;
}
void symbols_add(struct_token t, int type_index)
{
int symbol_index = num_symbols; //index for new symbol
num_symbols += 1; //increase total count
if(num_symbols > MAX_SYMBOLS)
{
printf("%d: symbol table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(symbols[symbol_index].text, t.text);
if (t.kind == STRLIT) //if string literal
{
strcpy(symbols[symbol_index].type, "string literal");
symbols[symbol_index].addr=alloc_strlit(symbol_index);
}
else
{
strcpy(symbols[symbol_index].type, types[type_index].text);
symbols[symbol_index].addr=alloc_ident(symbol_index);
}
}
}
int get_symbol_index(char* symname)
{
for(int i=0; i<num_symbols; i++)
{
if(0==strcmp(symbols[i].text, symname))
{
return i;
}
}
return -1;
}
int types_contains(char* typename)
{
for(int i=0; i<num_types; i++)
{
if(0==strcmp(types[i].text, typename))
{
return 1;
}
}
return 0;
}
int get_type_index(char* typename)
{
for(int i=0; i<num_types; i++)
{
if(0==strcmp(types[i].text, typename))
{
return i;
}
}
return -1;
}
//adds only type name
void types_add(char* typename)
{
int type_index = num_types; //index for new type
num_types += 1; //increase total count
if(num_types > MAX_TYPES)
{
printf("%d: type table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(types[type_index].text, typename);
}
}
int funcs_contains(char* funcname)
{
for(int i=0; i<num_funcs; i++)
{
if(0==strcmp(funcs[i].text, funcname))
{
return 1;
}
}
return 0;
}
int get_func_index(char* funcname)
{
for(int i=0; i<num_funcs; i++)
{
if(0==strcmp(funcs[i].text, funcname))
{
return i;
}
}
return -1;
}
void funcs_add(char* funcname)
{
int func_index = num_funcs; //index for new function
num_funcs += 1; //increase total count
if(num_funcs > MAX_FUNCS)
{
printf("%d: function table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(funcs[func_index].text, funcname);
}
funcs[func_index].addr = INVALID_ADDR;
funcs[func_index].func_param_cnt = 0;
funcs[func_index].func_local_cnt = 0;
}
int musts_contains(char* funcname)
{
for(int i=0; i<num_musts; i++)
{
if(0==strcmp(musts[i].text, funcname))
{
return 1;
}
}
return 0;
}
int get_must_index(char* funcname)
{
for(int i=0; i<num_musts; i++)
{
if(0==strcmp(musts[i].text, funcname))
{
return i;
}
}
return -1;
}
void musts_add(char* funcname)
{
int mfunc_index = num_musts; //index for new function
num_musts += 1; //increase total count
if(num_musts > MAX_MUSTS)
{
printf("%d: must function table overflow!\n", line_number);
exit(1);
}
else
{
strcpy(musts[mfunc_index].text, funcname);
}
musts[mfunc_index].addr = INVALID_ADDR;
musts[mfunc_index].func_param_cnt = 0;
musts[mfunc_index].func_local_cnt = 0;
}
int get_addr_of_symbol(struct_token t)
{
int addr = INVALID_ADDR;
for(int i=0; i<num_symbols; i++)
{
if(0==strcmp(symbols[i].text, t.text))
{
return symbols[i].addr;
}
}
return addr;
}
void print_symbols()
{
for(int i=0; i<num_symbols; i++)
{
printf("\n");
printf("symbol text: %s\n", symbols[i].text);
printf("symbol type: %s\n", symbols[i].type);
printf("symbol addr: %d\n", symbols[i].addr);
}
}
void print_types()
{
for(int i=0; i<num_types; i++)
{
printf("\n");
printf("type text: %s\n", types[i].text);
printf("type catg: %d (", types[i].catg);
printf("0=int, 1=char, 2=ptr, 3=arr, 4=rec, 5=func)\n");
printf("type sizebyte: %d\n", types[i].sizebyte);
printf("type ptr_val_type: %s\n", types[i].ptr_val_type);
printf("type arr_mem_type: %s\n", types[i].arr_mem_type);
printf("type arr_mem_cnt: %d\n", types[i].arr_mem_cnt);
printf("type rec_mem_cnt: %d\n", types[i].rec_mem_cnt);
for(int j=0; j<types[i].rec_mem_cnt; j++)
{
if (j!=0) printf(" ----\n");
printf(" member text: %s\n", types[i].rec_mem_list[j].text);
printf(" member type: %s\n", types[i].rec_mem_list[j].type);
printf(" member addr: %d\n", types[i].rec_mem_list[j].addr);
}
}
}
void print_funcs()
{
for(int i=0; i<num_funcs; i++)
{
printf("\n");
printf("func text: %s\n", funcs[i].text);
printf("func addr: %d\n", funcs[i].addr);
printf("func func_param_cnt: %d\n", funcs[i].func_param_cnt);
for(int j=0; j<funcs[i].func_param_cnt; j++)
{
if (j!=0) printf(" ----\n");
printf(" param text: %s\n", funcs[i].params[j].text);
printf(" param type: %s\n", funcs[i].params[j].type);
printf(" param addr: %d\n", funcs[i].params[j].addr);
}
printf("func func_local_cnt: %d\n", funcs[i].func_local_cnt);
for(int j=0; j<funcs[i].func_local_cnt; j++)
{
if (j!=0) printf(" ----\n");
printf(" local text: %s\n", funcs[i].locals[j].text);
printf(" local type: %s\n", funcs[i].locals[j].type);
printf(" local addr: %d\n", funcs[i].locals[j].addr);
}
}
}
void print_musts()
{
for(int i=0; i<num_musts; i++)
{
printf("\n");
printf("must text: %s\n", musts[i].text);
printf("must addr: %d\n", musts[i].addr);
printf("must func_param_cnt: %d\n", musts[i].func_param_cnt);
for(int j=0; j<musts[i].func_param_cnt; j++)
{
if (j!=0) printf(" ----\n");
printf(" param text: %s\n", musts[i].params[j].text);
printf(" param type: %s\n", musts[i].params[j].type);
printf(" param addr: %d\n", musts[i].params[j].addr);
}
printf("must func_local_cnt: %d\n", musts[i].func_local_cnt);
for(int j=0; j<musts[i].func_local_cnt; j++)
{
if (j!=0) printf(" ----\n");
printf(" local text: %s\n", musts[i].locals[j].text);
printf(" local type: %s\n", musts[i].locals[j].type);
printf(" local addr: %d\n", musts[i].locals[j].addr);
}
}
}
char* get_kind_string(int kind)
{
switch(kind)
{
case TBD: return "TBD";
case ENDOFFILE: return "ENDOFFILE";
case NEWLINE: return "NEWLINE";
case INTLIT: return "INTLIT";
case CHARLIT: return "CHARLIT";
case IDENT: return "IDENT";
case STRLIT: return "STRLIT";
case LPAREN: return "LPAREN";
case RPAREN: return "RPAREN";
case DOT: return "DOT";
case LBRKT: return "LBRKT";
case RBRKT: return "RBRKT";
case COMMA: return "COMMA";
case LBRACE: return "LBRACE";
case RBRACE: return "RBRACE";
case DOTDOT: return "DOTDOT";
case TWOCOLON: return "TWOCOLON";
case TYPE: return "TYPE";
case PRINT: return "PRINT";
case INPUT: return "INPUT";
case IF: return "IF";
case THEN: return "THEN";
case ELSE: return "ELSE";
case ENDIF: return "ENDIF";
case WHILE: return "WHILE";
case REPEAT: return "REPEAT";
case ENDWHILE: return "ENDWHILE";
case INT: return "INT";
case CHAR: return "CHAR";
case POINTS: return "POINTS";
case ARRAY: return "ARRAY";
case RECORD: return "RECORD";
case ENDREC: return "ENDREC";
case BIND: return "BIND";
case NEW: return "NEW";
case MOVE: return "MOVE";
case FREE: return "FREE";
case REF: return "REF";
case FUNC: return "FUNC";
case FDEF: return "FDEF";
case RETURN: return "RETURN";
case MUST: return "MUST";
case MDEF: return "MDEF";
case CAN: return "CAN";
case CDEF: return "CDEF";
case POLY: return "POLY";
case EQUAL: return "EQUAL";
case PLUS: return "PLUS";
case MINUS: return "MINUS";
case ASTERISK: return "ASTERISK";
case SLASH: return "SLASH";
case EQEQ: return "EQEQ";
case NOTEQ: return "NOTEQ";
case LESS: return "LESS";
case LTEQ: return "LTEQ";
case GRET: return "GRET";
case GTEQ: return "GTEQ";
case PIPE: return "PIPE";
case AMPERSAND: return "AMPERSAND";
case TILDE: return "TILDE";
default: return "????";
}
}
void print_cur_token_text()
{
if(cur_token.text[0] == '\n')
printf(" cur: newline");
else if (cur_token.text[0] == '\0')
printf(" cur: nullchar");
else
printf(" cur: %s", cur_token.text);
printf("\n");
}
void print_upcoming_token_text()
{
if(upcoming_token.text[0] == '\n')
printf(" upc: newline");
else if (upcoming_token.text[0] == '\0')
printf(" upc: nullchar");
else
printf(" upc: %s", upcoming_token.text);
printf("\n");
}
void make_mandatory(int kind)
{
if(check_cur_token(kind))
consume_cur_token();
else
{
printf("%d: Error! Expected %s, got %s\n", line_number,
get_kind_string(kind), get_kind_string(cur_token.kind));
exit(1);
}
}
int cur_token_is_relop()
{
return check_cur_token(GRET) || check_cur_token(GTEQ)
|| check_cur_token(LESS) || check_cur_token(LTEQ)
|| check_cur_token(EQEQ) || check_cur_token(NOTEQ);
}
void consume_cur_token()
{
cur_token = upcoming_token;
get_token();
upcoming_token = token;
//No need to worry about passing the ENDOFFILE, lexer handles that.
if(DEBUG)
{
print_cur_token_text();
print_upcoming_token_text();
}
}
void parse_init()
{
//to init cur_token and upcoming_token
get_token();
cur_token = upcoming_token;
upcoming_token = token;
get_token();
cur_token = upcoming_token;
upcoming_token = token;
if(DEBUG)
{
print_cur_token_text();
print_upcoming_token_text();
}
//initialize types table
strcpy(types[0].text, "int");
types[0].catg = INT_CATG;
types[0].sizebyte = 4;
strcpy(types[1].text, "char");
types[1].catg = CHAR_CATG;
types[1].sizebyte = 1;
}
int check_cur_token(int kind)
{
if(kind == cur_token.kind)
return 1;
else
return 0;
}
int check_upcoming_token(int kind)
{
if(kind == upcoming_token.kind)
return 1;
else
return 0;
}
void print_all_token()
{
// Parse all the statements in the program.
while (!check_cur_token(ENDOFFILE))
{
print_cur_token_text();
consume_cur_token();
}
}
// Production rules.
// each production-function consumes all tokens of that production
//program ::= {declaration} {procedure}
void program()
{
//Set up things.
ir_emit_code_line("=CALL addr");
put_opcode(CALL); //this opcode goes to addr=0
//keep text section addr to be fixed
//with actual address of main function
int fix_addr = get_text_addr_here();
put_operand(12); //this operand goes to addr=4
//it tells to call main() at addr=12
ir_emit_code_line("=HLT");
put_opcode(HLT); //this opcode goes to addr=8
//main() starts at addr=12
//CALL deposits 8 as return addr where HLT is there
//execution starts at pc=0
// Since some newlines are required in our grammar,
// need to skip the excess.
while (check_cur_token(NEWLINE)) {
consume_cur_token(); // consumes NEWLINE
}
//Parse all the declarations and functions
//in the program.
while (!check_cur_token(ENDOFFILE))
{
if(check_cur_token(TYPE)
|| check_cur_token(REF)
|| check_cur_token(FUNC)
|| check_cur_token(MUST)
|| check_cur_token(INT)
|| check_cur_token(CHAR)
|| (check_cur_token(IDENT) && types_contains(cur_token.text)) )
{
declaration(); // consumes own tokens
}
else if(check_cur_token(FDEF))
{
procedure(); // consumes own tokens
}
else
{
printf("%d: a program has declations and procedures, nothing else: %s\n", line_number, cur_token.text);
exit(1);
}
}
//fix main function address
if(!funcs_contains("main"))
{
printf("%d: main function mandatory: %s\n", line_number, cur_token.text);
exit(1);
}
int func_index = get_func_index("main");
fix_operand(fix_addr, funcs[func_index].addr);
//fix addresses for mutual recusion
for(int i=0; i<num_fixes; i++)
{
int fix_addr = fixfaddr[i].fix_addr;
int fi = fixfaddr[i].func_index;
int func_addr = funcs[fi].addr;
if(func_addr == INVALID_ADDR)
{
printf("%d: calling undefined function: %s\n", line_number, cur_token.text);
exit(1);
}
else
{
fix_operand(fix_addr, func_addr);
}
}
printf("compilation completed!\n");
}
//primtype ::= "int" | "char"
//primvardecl ::= primtype varname nl
void primvardecl(int fi)
{
if(check_cur_token(INT)
|| check_cur_token(CHAR))
{
ir_emit_code_line(cur_token.text);
int local_ti;
//get type index
local_ti = get_type_index(cur_token.text);
consume_cur_token(); // consumes type
ir_emit_code_line(cur_token.text);
//check variable name validity
if(types_contains(cur_token.text))
{
printf("%d: Variable name can't be same as datatype: %s\n", line_number, cur_token.text);
exit(1);
}
//locals can be same as globals
//params and locals hide globals.
if(params_contains(fi, cur_token.text))
{
printf("%d: Variable name can't be same as params: %s\n", line_number, cur_token.text);
exit(1);
}
// If variable doesn't exist, declare it.
if (!locals_contains(fi, cur_token.text))
{
locals_add(fi, cur_token.text, local_ti);
}
else
{
printf("%d: Multiple variable declaration: %s\n", line_number, cur_token.text);
exit(1);
}
make_mandatory(IDENT); // consumes IDENT
nl(); // consumes NEWLINE
}
}
/*
statement ::= "print" (chararr_accessor_lval | accessor | strlit ) nl
| "input" accessor_lval nl
| accessor_lval "=" expression nl
| "poly" must_accessor_lval "=" rec_accessor_lval nl
| "bind" ptr_accessor_lval "=" ("new" typename | accessor_lval) nl
| "move" ptr_accessor_lval "=" ptr_accessor_lval nl
| "free" ptr_accessor_lval nl
| "if" (comparison) "then" nl
{statement}
"else" nl
{statement}
"endif" nl
| "while" (comparison) "repeat" nl
{statement}
"endwhile" nl
*/
void statement(int fi)
{
// Check the first token to see what kind of statement this is.
// "print" (chararr_accessor_lval | accessor | strlit ) nl
if (check_cur_token(PRINT))
{
ir_emit_code_line(cur_token.text);
consume_cur_token(); // consumes PRINT
if (check_cur_token(STRLIT))
{
ir_emit_code_line(cur_token.text);
// If string doesn't already exist,
//add it to symbols, like a declaration.
if (!symbols_contains(cur_token.text))
{
symbols_add(cur_token, -1); //for string literals type_index is not used
}
else
{
//no worries, multiple appearance of string
//is not a multiple declation case.
}
// Simple string, so print it.
ir_emit_code_line("=IMM addr");
ir_emit_code_line("=OUTSTR");
int sym_addr = get_addr_of_symbol(cur_token);
put_opcode(IMM);
put_operand(sym_addr);
put_opcode(OUTSTR);
consume_cur_token(); // consumes STRLIT
}
else
{
int ti = accessor_lval(fi); // consumes own tokens
if(ti == 0) //int
{
//get the int value
ir_emit_code_line("=LI");
put_opcode(LI);
//now print
ir_emit_code_line("=OUTINT");
put_opcode(OUTINT);
}
else if(ti == 1) //char
{
//get the char value
ir_emit_code_line("=LC");
put_opcode(LC);
//now print
ir_emit_code_line("=OUTCHAR");
put_opcode(OUTCHAR);
}
else if(types[ti].catg == ARR_CATG
&& (get_type_index(types[ti].arr_mem_type)==1) ) //char array
{
ir_emit_code_line("=OUTSTR");