-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypechecker.c
850 lines (741 loc) · 27.5 KB
/
typechecker.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
/*
Batch No: 47
Gyanendra Mishra 2013A7PS126P
Prabhjyot Singh Sodhi 2013A7PS151P
Filename: typechecker.c
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include "typechecker.h"
/*
Handles typechecking of multiple type of statments. Also takes care of several
semantic rules. The check_stmt function is invoked by semantic.c for semantic
analysis.
*/
// Handles boolean expressions
void handle_boolean_exp(parseTree stmt, hashtable *ht, char* scope, list_var **head)
{
char *to_check_here;
to_check_here = (char*) malloc(20*sizeof(char));
traverse_all_boolean(stmt, ht, scope, to_check_here, head);
}
// Adds idList to a linked list called list_var
// Used to keep track of variables changing in itterations
void add_to_list_var(list_var **head, char* name)
{
if (head != NULL)
{
list_var *it = *head;
list_var *prev = NULL;
while(it != NULL)
{
// found variable already
if (strcmp(it->name, name) == 0)
return;
prev = it;
it = it->next;
}
if (prev == NULL)
{
list_var * new = (list_var *) malloc(sizeof(list_var));
new->next = NULL;
new->name = (char*) malloc(20*sizeof(char));
strcpy(new->name, name);
*head = new;
}
else
{
list_var * new = (list_var *) malloc(sizeof(list_var));
new->next = NULL;
new->name = (char*) malloc(20*sizeof(char));
strcpy(new->name, name);
prev->next = new;
}
}
else
{
list_var * new = (list_var *) malloc(sizeof(list_var));
new->next = NULL;
new->name = (char*) malloc(20*sizeof(char));
strcpy(new->name, name);
*head = new;
}
}
// Traverses all boolean expressions.
void traverse_all_boolean(parseTree curr, hashtable *ht, char *scope, char *check, list_var **head)
{
if (curr == NULL)
return;
if(curr->firstKid != NULL)
{
traverse_all_boolean(curr->firstKid, ht, scope, check, head);
// do you thing again!
}
else
{
// do your thing
// TK_ID detected
if (curr->id == 4)
{
entry *found = get(ht, curr->lexeme, scope);
if (found == NULL)
found = get(ht, curr->lexeme, "global");
if (found != NULL)
{
if (strlen(check) > 0)
{ // made change here changed tk_num to int in strcmp(chec, "int") talk to sodhi @Todo
if (!(strcmp(found->type, "int") == 0 && strcmp(check, "TK_NUM") == 0) && !(strcmp(found->type, "real") == 0 && strcmp(check, "TK_RNUM") == 0))
{
printf("Error: Variable <%s> is not of expected type: <%s> at line <%d>\n", curr->lexeme, check, curr->lineNo);
symbolerror = 1;
}
else
{
add_to_list_var(head, curr->lexeme);
}
}
else
{
// char answer[10];
if (strcmp("int", found->type) == 0 )
{
// answer = "int";
strcpy(check, "TK_NUM");
}
else
{
// answer = "real";
strcpy(check, "TK_RNUM");
}
add_to_list_var(head, curr->lexeme);
}
}
else
{
printf("Error: <%s> not declared in scope: <%s> or in global scope at line <%d>\n", curr->lexeme, scope, curr->lineNo);
symbolerror = 1;
}
}
else if (curr->id == 5 || curr->id == 6)
{
if (strlen(check) == 0)
{
strcpy(check, getCorrespondingToken(curr->id));
}
else
if (strcmp(check, getCorrespondingToken(curr->id)) != 0)
{
printf("Error: type <%s> expected got <%s> at line no: <%d>\n", check, getCorrespondingToken(curr->id), curr->lineNo);
symbolerror = 1;
}
}
}
parseTree prev = curr;
curr = curr->siblings;
if(curr != NULL)
{
return traverse_all_boolean(curr, ht, scope, check, head);
}
}
// Traverses all write statements
// Also used for read statements.
void traverse_all_write(parseTree curr, hashtable *ht, char *scope)
{
if (curr == NULL)
return;
if(curr->firstKid != NULL)
{
traverse_all(curr->firstKid, ht, scope);
// do you thing again!
}
else
{
// TK_ID detected
if (curr->id == 4)
{
entry *found = get(ht, curr->lexeme, scope);
if (found == NULL)
found = get(ht, curr->lexeme, "global");
if (found == NULL)
{
printf("Error: Variable: <%s> undeclared in <%s> or global scope at line <%d>\n", curr->lexeme, scope, curr->lineNo);
symbolerror = 1;
return;
}
// record type
if (found->isRecordInstance == 1)
{
if (curr->siblings->firstKid == NULL)
{ // @Change not an error any more.
// printf("Error: Cannot print a record, must specify member at line <%d>\n", found->lineNo);
// symbolerror = 1;
// return;
}
else
{
char *field = curr->siblings->firstKid->lexeme;
// to get the members for a record
found = get(ht, found->type, "global");
record_dec *starter = found->record;
int flag = 0;
while (starter != NULL)
{
if (strcmp(starter->name, field) == 0)
{
flag = 1;
// printf("sab equal hai\n");
break;
}
starter = starter->next;
}
if (flag == 0)
{
printf("Error: There is no field <%s> in record type <%s> at line <%d>\n", field, found->key, found->lineNo);
symbolerror = 1;
return;
}
}
}
return;
}
}
parseTree prev = curr;
curr = curr->siblings;
if(curr != NULL)
{
return traverse_all(curr->firstKid, ht, scope);
}
}
// Tells if a certain value exists in a linked list.
int var_in_list_var(list_var **head, char* name)
{
if (head == NULL || *head == NULL)
{
printf("HEAD NULL SUPER ERROR :(\n");
return 0;
}
list_var *iter;
iter = *head;
while(iter != NULL)
{
if (strcmp(iter->name, name) == 0)
return 1;
iter = iter->next;
}
return 0;
}
// Checks if given variable is changed
int check_var_changed(parseTree curr, list_var **head)
{
parseTree iter = curr;
while(iter != NULL)
{
if (iter->firstKid->id == 123) // check for assignment statements
{
parseTree tk_id = iter->firstKid->firstKid;
// boolean expressions don't allow record type stuff
if (tk_id->siblings->firstKid == NULL)
{
if (var_in_list_var(head, tk_id->lexeme) == 1)
return 1;
}
}
iter = iter->siblings;
}
return 0;
}
// things to check here
// 1) expression in if statment is boolean, Done => by the parser
// 2) call handle_stmts recursively on this, for otherstmts, Done
// Semantic and Type Checking of conditional statements
void check_conditional_stmt(parseTree curr, hashtable *st, char* scope)
{
curr = curr->firstKid;
list_var **head;
list_var *lis = NULL;
head = &lis;
while (curr != NULL)
{
// boolean expression
if (curr->id == 141)
{
handle_boolean_exp(curr->firstKid, st, scope, head);
}
else if (curr->id == 120)
{
handle_oth_stmts(curr->firstKid, st, scope);
}
if (curr->id == 129)
curr = curr->firstKid;
else
curr = curr->siblings;
}
}
// Finds out if a record is dividable or multiplicable
// A record with all same types is both divisable and multiplicable
// Records containing all ints can only be divided by integer
// Records containing all real can only be divided by real
int recordIsDivMulable(hashtable *st, char * record, char * inpType){
entry *temp = get(st, record, "global");
record_dec * rec = temp->record;
char * type = (char*) malloc(sizeof(char)*30);
int first = 1;
while(rec != NULL)
{ if (first){
first = 0;
strcpy(type, rec->type);
}
else if(strcmp(type, rec->type) != 0){
return 1;
}
rec = rec->next;
}
if (strcmp(inpType, type) == 0)
return 1;
else
return 0;
}
// Type checking of factor
void check_factor(parseTree factor, hashtable *st, char *scope, char *type){
parseTree all = factor->firstKid;
int highPrec = 0;
if(factor->siblings != NULL && factor->siblings->id == 137){
highPrec = 1;
}
if(all->id == 5){
if(strcmp(type, "int") != 0){
printf("Error: <%s> expected got <%s> at line <%d>\n", "int", type, all->lineNo);
symbolerror = 1;
}
}
// a wild 5.55 was encountered but type expected was different
else if(all->id == 6){
if(strcmp(type, "real") != 0){
printf("Error: <%s> expected got <%s> at line <%d>\n", "real", type, all->lineNo);
symbolerror = 1;
}
}
else {
parseTree temp = all->siblings;
// Normal variable (not record)
if(temp == NULL || temp->firstKid == NULL){
entry *var = get(st, all->lexeme, scope);
if (var == NULL){
var = get(st, all->lexeme, "global");
// doesnt exist globally
if (var == NULL){
printf("Error: Variable <%s> not even declared globally at line <%d>\n", all->lexeme, all->lineNo);
symbolerror = 1;
}
// exists globally
else {
if(strcmp(type, "int") != 0 && strcmp(type, "real") != 0){
if( strcmp("int", var->type) == 0 || strcmp("real", var->type) == 0){
if (recordIsDivMulable(st, type, var->type) == 1){
if( highPrec != 1){
printf("Error: cant add/subtract scalar from record at line <%d>\n", var->lineNo);
symbolerror = 1;
}
}
else {
printf("Error: Given record <%s> isnt good for multiplication/divsion with type <%s> as it has fields of different types at line <%d>", type, var->type, var->lineNo);
symbolerror = 1;
}
}
else if (strcmp(type, var->type) != 0){
printf("Error: expected record of type <%s> got of type <%s> at line <%d>\n", type, var->type, var->lineNo);
symbolerror = 1;
}
else if (strcmp(type, var->type) == 0){
if(highPrec == 1){
printf("Error: Cant divide record at by record at line <%d>\n", var->lineNo);
symbolerror = 1;
}
}
}
else if(strcmp(type, var->type) != 0){
printf("Error: <%s> expected got <%s> in <%s> at line <%d>\n", type, var->type, all->lexeme, var->lineNo);
symbolerror = 1;
}
}
}
else {
if(strcmp(type, "int") != 0 && strcmp(type, "real") != 0){
if( strcmp("int", var->type) == 0 || strcmp("real", var->type) == 0){
if (recordIsDivMulable(st, type, var->type) == 1){
if( highPrec != 1){
printf("Error: cant add/subtract scalar from record at line <%d>\n", var->lineNo);
symbolerror = 1;
}
}
else {
printf("Error: Given record <%s> isnt good for arithmetic with type <%s> as it has fields of different types at line <%d>\n", type, var->type, var->lineNo);
symbolerror = 1;
}
}
else if (strcmp(type, var->type) != 0){
printf("Error: expected record of type <%s> got of type <%s> at line <%d>\n", type, var->type, var->lineNo);
symbolerror = 1;
}
else if (strcmp(type, var->type) == 0){
if(highPrec == 1){
printf("Error: Cant divide record by record at line <%d>\n", var->lineNo);
symbolerror = 1;
}
}
}
else if(strcmp(type, var->type) != 0){
printf("Error: <%s> expected got <%s> in <%s> at line <%d>\n", type, var->type, all->lexeme, var->lineNo);
symbolerror = 1;
}
}
}
else {
entry * record = get(st, all->lexeme, scope);
if (record == NULL){
record = get(st, all->lexeme, "global");
if (record == NULL){
printf("Error: Record Variable <%s> not even declared globally at line <%d>\n", all->firstKid->lexeme, all->lineNo);
symbolerror = 1;
}
else {
// record declaration found, we go through linkedlsit to find type
record = get(st, record->type, "global");
record_dec * rec = record-> record;
while(rec!= NULL && strcmp(rec->name, temp->firstKid->lexeme) != 0){
rec = rec->next;
}
if(rec == NULL){
printf("Error: Record <%s> has no field <%s> at line <%d>\n",record->key, temp->firstKid->lexeme, record->lineNo);
symbolerror = 1;
}
else if (strcmp(rec->type, type)){
printf("Error: <%s> expected got <%s> in <%s.%s> at line <%d>\n", type, rec->type, all->firstKid->lexeme,rec->name, record->lineNo);
symbolerror = 1;
}
}
}
else {
record = get(st, record->type, "global");
record_dec * rec = record-> record;
while(rec!= NULL && strcmp(rec->name, temp->firstKid->lexeme) != 0){
rec = rec->next;
}
if(rec == NULL){
printf("Error: Record <%s> has no field <%s> at line <%d>\n",record->key, temp->firstKid->lexeme, temp->firstKid->lineNo);
symbolerror = 1;
}
else if (strcmp(rec->type, type)){
printf("Error: <%s> expected got <%s> in <%s.%s> at line <%d>\n", type, rec->type, all->lexeme,rec->name, temp->firstKid->lineNo);
symbolerror = 1;
}
}
}
}
}
// Checks termPrime part of arithmetic expression
void check_termprime(parseTree termprime, hashtable *st, char*scope, char* type){
parseTree hpo = termprime->firstKid;
parseTree factor = hpo->siblings;
parseTree tpp = factor->siblings;
if (factor->firstKid->id == 132){
check_arith(factor->firstKid, st, scope, type);
}
else {
check_factor(factor, st, scope, type);
}
if(tpp != NULL && tpp->firstKid!=NULL){
check_termprime(tpp, st, scope, type);
}
}
// Checks expprime part of arithmetic expression
void check_expprime(parseTree expprime, hashtable *st, char * scope, char * type){
parseTree term = expprime->firstKid->siblings;
parseTree expp = term->siblings;
if(expp->firstKid!=NULL)
check_expprime(expp, st, scope, type);
parseTree factor = term->firstKid;
parseTree termprime = factor->siblings;
if (factor->firstKid->id == 132){
check_arith(factor->firstKid, st, scope, type);
}
else {
check_factor(factor, st, scope, type);
}
if(termprime!= NULL && termprime->firstKid != NULL){
check_termprime(termprime, st, scope, type);
}
}
void check_arith(parseTree expr, hashtable *st, char * scope, char * type){
parseTree term = expr->firstKid;
parseTree expprime = term->siblings;
parseTree factor = term->firstKid;
parseTree termprime = factor->siblings;
if (factor->firstKid->id == 132){
check_arith(factor->firstKid, st, scope, type);
}
else {
check_factor(factor, st, scope, type);
}
if(termprime!= NULL && termprime->firstKid != NULL){
check_termprime(termprime, st, scope, type);
}
if(expprime != NULL && expprime->firstKid != NULL){
check_expprime(expprime, st, scope, type);
}
}
void check_assignment_stmt(parseTree curr, hashtable *st, char* scope){
parseTree sorrec = curr->firstKid;
char *type = (char*) malloc(sizeof(char)*20);
// non record variable
if(sorrec->firstKid->siblings->firstKid == NULL){
entry * var = get(st, sorrec->firstKid->lexeme, scope);
// not in current scope maybe declared globally
if(var == NULL){
entry * var = get(st, sorrec->firstKid->lexeme, "global");
if(var == NULL){
printf("Error: variable <%s> is not declared at line <%d>\n", sorrec->firstKid->lexeme, sorrec->firstKid->lineNo);
symbolerror = 1;
return;
}
else{
strcpy(type, var->type);
var->assigned = 1;
}
}
else {
strcpy(type, var->type);
var->assigned = 1;
}
}
else {
entry * record = get(st, sorrec->firstKid->lexeme, scope);
entry * var = get(st, sorrec->firstKid->lexeme, scope);
if (record == NULL){
record = get(st, sorrec->firstKid->lexeme, "global");
var = get(st, sorrec->firstKid->lexeme, "global");
if (record == NULL){
printf("Error: Record Variable <%s> not even declared globally at line <%d>\n", sorrec->firstKid->lexeme, sorrec->firstKid->lineNo);
symbolerror = 1;
}
else {
// record declaration found, we go through linkedlsit to find type
record = get(st, record->type, "global");
record_dec * rec = record-> record;
while(rec!= NULL && strcmp(rec->name, sorrec->firstKid->siblings->firstKid->lexeme) != 0){
rec = rec->next;
}
if(rec != NULL){
strcpy(type, rec->type);
var->assigned = 1;
}
else {
printf("Error: Record <%s> has no field <%s> at line <%d>\n", record->key, sorrec->firstKid->siblings->firstKid->lexeme, sorrec->firstKid->siblings->firstKid->lineNo);
symbolerror = 1;
}
}
}
else {
record = get(st, record->type, "global");
record_dec * rec = record-> record;
while(rec!= NULL && strcmp(rec->name, sorrec->firstKid->siblings->firstKid->lexeme) != 0){
rec = rec->next;
}
if(rec != NULL){
strcpy(type, rec->type);
var->assigned = 1;
}
else{
printf("Error: Record <%s> has no field <%s> at line <%d>\n", record->key, sorrec->firstKid->siblings->firstKid->lexeme, sorrec->firstKid->siblings->firstKid->lineNo);
symbolerror = 1;
}
}
}
parseTree arithmeticex = sorrec->siblings;
if (arithmeticex == NULL){
;
}
else {
check_arith(arithmeticex, st, scope, type);
}
}
// Checks iterative statements for type expressions
void check_iterative_stmt(parseTree curr, hashtable *st, char* scope)
{
curr = curr->firstKid;
list_var **head;
list_var *lis = NULL;
head = &lis;
while (curr != NULL)
{
// boolean expression
if (curr->id == 141)
{
handle_boolean_exp(curr->firstKid, st, scope, head);
}
else if (curr->id == 120)
{
// by this time, list_var would have been populated!
int any_var_changed = check_var_changed(curr->firstKid, head);
if (any_var_changed == 0)
{
printf("Error: None of variables involved in the iterations are redefined\n");
symbolerror = 1;
}
// call handle_oth_stmts here
handle_oth_stmts(curr->firstKid, st, scope);
}
curr = curr->siblings;
}
}
void check_io_stmt(parseTree curr, hashtable *st, char* scope)
{
// read statement
if (curr->firstKid->id == 35)
{
parseTree sor = curr->firstKid->siblings->firstKid;
entry *temp = get(st, sor->lexeme, scope);
if(temp != NULL){
temp->assigned = 1;
}
parseTree srid = curr->firstKid->siblings->firstKid;
traverse_all_write(srid, st, scope);
}
else
{
traverse_all_write(curr->firstKid->siblings->firstKid, st, scope);
}
}
// check id list for matches
void check_idlist(hashtable *st, parseTree idList, char * funid, int io, char * scope){
if (io == 0)
{
int i = 0;
int line;
while(idList!=NULL && idList->firstKid != NULL){
entry * temp = getOutputParameter(st, funid, i);
if(temp == NULL){
printf("Error: Function <%s> has only <%d> valid output parameters\n", funid, i + 1);
symbolerror = 1;
}
else {
entry * var = get(st, idList->firstKid->lexeme, scope);
line = idList->firstKid->lineNo;
if(var == NULL){
var = get(st, idList->firstKid->lexeme, "global");
}
if(var == NULL){
printf("Error: variable <%s> undeclared at line <%d>\n", idList->firstKid->lexeme, idList->firstKid->lineNo);
symbolerror = 1;
}
else{
var->assigned = 1;
if(strcmp(temp->type, var->type)!=0){
printf("Error: output paramter <%d> types <%s> and <%s> dont match at line <%d>\n", i, temp->type, var->type, line);
symbolerror = 1;
}
}
}
i++;
idList = idList->firstKid->siblings->firstKid;
}
entry * temp = getOutputParameter(st, funid, i);
if(temp!=NULL){
printf("Error: The number of output parameters for function <%s> at line <%d> is incorrect", funid, lineNo);
symbolerror = 1;
}
}
else{
int i = 0;
int line;
while(idList!=NULL && idList->firstKid != NULL){
entry * temp = getInputParameter(st, funid, i);
if(temp == NULL){
printf("Error: Function <%s> has only <%d> valid input parameters\n", funid, i + 1);
symbolerror = 1;
}
else {
entry * var = get(st, idList->firstKid->lexeme, scope);
line = idList->firstKid->lineNo;
if(var == NULL){
var = get(st, idList->firstKid->lexeme, "global");
}
if(var == NULL){
printf("Error: variable <%s> undeclared at line <%d>\n", idList->firstKid->lexeme, idList->firstKid->lineNo);
symbolerror = 1;
}
else{
if(strcmp(temp->type, var->type)!=0){
printf("Error: input paramter <%d> types <%s> and <%s> dont match at line <%d>\n", i, var->type, temp->type, idList->firstKid->lineNo);
symbolerror = 1;
}
}
}
i++;
idList = idList->firstKid->siblings->firstKid;
}
entry * temp = getInputParameter(st, funid, i);
if(temp!=NULL){
printf("Error: The number of input parameters for function <%s> at line <%d> is incorrect\n", funid, line);
symbolerror = 1;
}
}
}
// checks functional semantic errors
void check_fun(parseTree funcall, hashtable *st, char * scope){
parseTree outputparameters = funcall->firstKid;
parseTree funid = outputparameters->siblings;
parseTree inputParameters = funid->siblings;
if(strcmp(funid->lexeme, scope) == 0){
printf("Error: function <%s> being recursively called at line <%d>\n", scope, funid->lineNo);
symbolerror = 1;
return;
}
entry * function = get(st, funid->lexeme, "global");
entry * current = get(st, scope, "global");
// undeclared function or function declared after current function
if (function == NULL || function->lineNo > current->lineNo){
printf("Error: Undefined function <%s> being called at line <%d>\n", funid->lexeme, funid->lineNo);
symbolerror = 1;
return;
}
// no return parameters
if (outputparameters == NULL || outputparameters->firstKid == NULL){
entry *temp = getOutputParameter(st, funid->lexeme, 0);
if(temp != NULL){
symbolerror = 1;
printf("Error: Function has return items but there is no return associated with this function <%s>\n", funid->lexeme);
}
}
else {
parseTree idList = outputparameters->firstKid;
check_idlist(st, idList, funid->lexeme, 0, scope);
}
parseTree inputParametersList = inputParameters->firstKid;
check_idlist(st, inputParametersList, funid->lexeme, 1, scope);
}
// checks return stmt
void check_return(parseTree curr, hashtable *st, char *scope){
if(curr!=NULL && curr->firstKid !=NULL){
parseTree idList = curr->firstKid;
check_idlist(st, idList, scope, 0, scope);
}
}
// invoked by semantic.c
void check_stmt(parseTree curr, hashtable *st, int type, char* scope)
{
if (type == 1) // assignment statement
check_assignment_stmt(curr, st, scope);
else if (type == 3) // conditional statement
check_conditional_stmt(curr, st, scope);
else if (type == 2) // iterative statment
check_iterative_stmt(curr, st, scope);
else if (type == 10) // IO statement
check_io_stmt(curr, st, scope);
else if (type == 4)
check_fun(curr, st, scope);
else if(type ==5)
check_return(curr, st, scope);
}