-
Notifications
You must be signed in to change notification settings - Fork 0
/
obcParser.c
1571 lines (1475 loc) · 35.4 KB
/
obcParser.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
/* Oberon-S Parser Implementation */
#include "obcScanner.h"
#include "obcParser.h"
#include "obcCompiler.h"
char progName[MAX_VAR_LEN];
/*inline */ void accept(int expectedSym)
{
PARSER_PRINT("Parsing symbol: %s, expecting symbol: %s\n", symbols[sym], symbols[expectedSym]);
#if ERROR_HANDLING
while (sym == UNKNOWN)
{
PARSER_PRINT("Skipping over UNKNOWN token\n");
nextSym();
}
#endif
sym == expectedSym ? nextSym() : parseError(0);
}
void acceptFinal(int expectedSym)
{
PARSER_PRINT("Parsing EOF symbol: %s, expecting symbol: %s\n", symbols[sym], symbols[expectedSym]);
if (sym != expectedSym)
parseError(2);
PARSER_PRINT("Parse Successful\n");
}
/*1. Module -> MODULE Ident ; Block . */
void Module()
{
accept(MODULEsym);
strcpy(progName, varBuffer);
accept(Ident);
accept(SEMIC);
enterScope();
Block(0);
exitScope();
insertInstr(hlt, 0, 0);
acceptFinal(PER);
}
/*2. Block -> DeclSeq [BEGIN StatSeq] END Ident */
void Block(int procEntry)
{
int displacement = 1;
int saveSymTblPoint = symTblPoint;
/* address of start of block */
int locCounter0 = locCounter;
insertInstr(jmp, 0, 0);
DeclSeq(&displacement);
/* program/proc entry point */
symTbl[procEntry].procRecord.entryAddr = locCounter;
code[locCounter0].addr = locCounter;
insertInstr(isp, 0, displacement - 1); /* allocate stack space for vars */
if (sym == BEGINsym)
{
confirmSym();
int ret = StatSeq();
/* RETURN in void procedure */
if(ret == 1 && symTbl[procEntry].procRecord.resAddr == 0)
compileError(22);
/* no RETURN in non-void procedure */
else if(ret == 0 && symTbl[procEntry].procRecord.resAddr != 0)
compileError(23);
}
accept(ENDsym);
/* checking declaration and end of block name match */
if(procEntry == 0) /* main program block */
{
if(strcmp(progName, varBuffer) != 0)
compileError(20);
}
else if(strcmp(symTbl[procEntry].idName, varBuffer) != 0)
compileError(21);
accept(Ident);
/* restore symTblPoint, removes local vars */
symTblPoint = saveSymTblPoint;
}
/*3. DeclSeq -> [ CONST ConstDecls ][ TYPE TypeDecls ][ VAR VarDecls ]{ ProcDecl } */
void DeclSeq(int *displacement)
{
if (sym == CONSTsym)
{
confirmSym();
ConstDecls();
}
if (sym == TYPEsym)
{
confirmSym();
TypeDecls();
}
if (sym == VARsym)
{
confirmSym();
VarDecls(displacement);
}
while (sym == PROCEDUREsym)
ProcDecl();
#if SYM_TABLE_OUT
printf("\n***End of declarations tables:***");
printTbls();
#endif
}
/*4. ConstDecls -> ConstDecl { ConstDecl } */
void ConstDecls()
{
ConstDecl();
while (sym == Ident)
ConstDecl();
}
/*5. ConstDecl -> Ident = ConstExpr ; */
void ConstDecl()
{
if(sym == Ident)
insertIdent(varBuffer, -1, CONSTREC);
int saveSymTblPoint = symTblPoint;
accept(Ident);
accept(EQUAL);
int value, type;
ConstExpr(&value, &type);
symTbl[saveSymTblPoint].constRecord.constVal = value;
symTbl[saveSymTblPoint].type = type;
accept(SEMIC);
}
/*6. TypeDecls -> TypeDecl { TypeDecl } */
void TypeDecls()
{
TypeDecl();
while (sym == Ident)
TypeDecl();
}
/*7. TypeDecl -> Ident = Type ; */
void TypeDecl()
{
if(sym == Ident)
insertIdent(varBuffer, -1, TYPEREC);
int saveSymTblPoint = symTblPoint;
accept(Ident);
accept(EQUAL);
int type, typeSize;
Type(&type, &typeSize);
symTbl[saveSymTblPoint].type = type;
typeTbl[typeTblPoint].symTblIndex = saveSymTblPoint;
accept(SEMIC);
}
/*8. Type -> Ident | ArrayType | RecordType | EnumType */
void Type(int *type, int *typeSize)
{
*type = NOTYPE;
*typeSize = 1;
if(sym == Ident)
{
int index = lookupIdent(varBuffer);
if(index != 0)
{
if(symTbl[index].recType != TYPEREC)
compileError(9);
*type = symTbl[index].type;
*typeSize = typeTbl[symTbl[index].type].size;
}
confirmSym();
}
else if(sym == ARRAYsym)
ArrayType(type, typeSize);
else if(sym == RECORDsym)
RecordType(type, typeSize);
else if(sym == LPAREN)
EnumType(type, typeSize);
else
parseError(3);
}
/*9. ArrayType -> ARRAY Length { , Length } OF Type */
/*10. Length -> ConstExpr*/
void ArrayType(int *type, int *typeSize)
{
accept(ARRAYsym);
int length, indexType, ofType;
ConstExpr(&length, &indexType);
int length2;
while (sym == COMMA)
{
confirmSym();
ConstExpr(&length2, &indexType);
length *= length2;
}
accept(OFsym);
Type(&ofType, typeSize);
*typeSize = *typeSize * length;
insertType("ARRAY", *typeSize, ARRAYREC);
*type = typeTblPoint;
typeTbl[typeTblPoint].arrayRecord.indexType = indexType;
typeTbl[typeTblPoint].arrayRecord.elementType = ofType;
typeTbl[typeTblPoint].size = length;
}
/*11. RecordType -> RECORD FieldList { ; FieldList } END */
void RecordType(int *type, int *typeSize)
{
enterScope();
accept(RECORDsym);
*typeSize = 0;
int disp = 1;
FieldList(typeSize, &disp);
while (sym == SEMIC)
{
confirmSym();
FieldList(typeSize, &disp);
}
accept(ENDsym);
insertType("RECORD", *typeSize, RECREC);
typeTbl[typeTblPoint].recordRecord.lastField = symTblPoint;
typeTbl[typeTblPoint].size = *typeSize ;
*type = typeTblPoint;
exitRecScope();
}
/*12. EnumType -> '(' Ident { , Ident } ')' */
void EnumType(int *type, int *typeSize)
{
/* new type */
*typeSize = 1;
insertType("ENUM", 1, ENUMREC);
*type = typeTblPoint;
int val = 0;
accept(LPAREN);
if(sym == Ident)
{
insertIdent(varBuffer, 1, CONSTREC);
symTbl[symTblPoint].constRecord.constVal = val;
val++;
accept(Ident);
while(sym == COMMA)
{
confirmSym();
if(sym == Ident)
{
insertIdent(varBuffer, 1, CONSTREC);
symTbl[symTblPoint].constRecord.constVal = val;
val++;
accept(Ident);
}
else
compileError(9);
}
}
else
compileError(9);
accept(RPAREN);
typeTbl[typeTblPoint].enumRecord.lastEnum = symTblPoint;
}
/*13. VarDecls -> VarDecl { VarDecl } */
void VarDecls(int *displacement)
{
VarDecl(displacement);
while (sym == Ident)
VarDecl(displacement);
}
/*14. VarDecl -> IdentList : Type ; */
void VarDecl(int *displacement)
{
int firstEntry, lastEntry;
IdentList(VARREC, &firstEntry, &lastEntry);
accept(COLON);
int type, typeSize;
Type(&type, &typeSize);
if(typeTbl[type].recType == ENUMREC)
type = 1;
do{
symTbl[firstEntry].type = type;
/* if the new entry is based off a declared RECORD TYPE */
if(typeTbl[type].recType == RECREC && symTbl[typeTbl[type].symTblIndex].recType == TYPEREC)
{
int i, fieldDisp = 1;
enterScope();
for(i = typeTbl[type].symTblIndex + 1; i <= typeTbl[type].recordRecord.lastField; i++)
{
insertIdent(symTbl[i].idName, symTbl[i].type, FIELDREC);
symTbl[symTblPoint].fieldRecord.fieldAddr = fieldDisp;
fieldDisp++;
}
exitRecScope();
}
symTbl[firstEntry].varRecord.varAddr = *displacement;
*displacement += typeSize;
firstEntry++;
}while(firstEntry <= lastEntry);
accept(SEMIC);
}
/*15. Identlist -> Ident { , Ident } */
void IdentList(int recType, int *first, int *last)
{
if(sym == Ident)
{
insertIdent(varBuffer, -1, recType);
*first = symTblPoint;
accept(Ident);
}
else
compileError(10);
while (sym == COMMA)
{
confirmSym();
if(sym == Ident)
{
insertIdent(varBuffer, -1, recType);
accept(Ident);
}
else
compileError(10);
}
*last = symTblPoint;
}
/*16. ProcDecl -> PROCEDURE Ident [ FormalPars ] [ : Ident ] ; ProcBody ; */
void ProcDecl()
{
accept(PROCEDUREsym);
int displacement = -2;
int procPoint;
if(sym == Ident)
{
insertIdent(varBuffer, 0, PROCREC);
procPoint = symTblPoint;
accept(Ident);
}
else
compileError(10);
enterScope();
if (sym == LPAREN) /* has params */
{
confirmSym();
FormalPars(procPoint, &displacement);
}
else
symTbl[procPoint].procRecord.lastParam = 0;
if (sym == COLON) /* has return value */
{
confirmSym();
if(sym != Ident)
compileError(10);
else
{
int index = lookupIdent(varBuffer);
accept(Ident);
/* confirm return value is a valid type and set return type */
if(symTbl[index].recType == TYPEREC)
symTbl[procPoint].type = symTbl[index].type;
else
compileError(9);
symTbl[procPoint].procRecord.resAddr = displacement - typeTbl[symTbl[procPoint].type].size;
}
}
accept(SEMIC);
/* ProcBody -> Block */
Block(procPoint);
insertInstr(ret, 0, 0);
/* backpatch store for return value for functions */
if(symTbl[procPoint].procRecord.resAddr != 0)
code[locCounter - 2].addr = symTbl[procPoint].procRecord.resAddr;
exitRecScope();
accept(SEMIC);
}
/*17. FormalPars -> '(' [ FPSection { ; FPSection } ] ')' */
void FormalPars(int procPoint, int *displacementp)
{
if (sym == VARsym || sym == Ident)
{
FPSection();
while (sym == SEMIC)
{
confirmSym();
FPSection();
}
}
symTbl[procPoint].procRecord.lastParam = symTblPoint;
addressParams(procPoint, displacementp);
accept(RPAREN);
}
void addressParams(int procPoint, int *displacementp)
{
int lastParam = symTbl[procPoint].procRecord.lastParam;
int paramType;
while(lastParam > procPoint)
{
paramType = symTbl[lastParam].type;
*displacementp = *displacementp - typeTbl[paramType].size;
symTbl[lastParam].paramRecord.paramAddr = *displacementp;
lastParam--;
}
}
/*18. FPSection -> [ VAR ] IdentList : FormalType
19. FormalType -> Ident
*/
void FPSection()
{
int isVar = 0;
if (sym == VARsym)
{
isVar = 1;
confirmSym();
}
int paramPoint, last;
IdentList(PARAMREC, ¶mPoint, &last);
accept(COLON);
int type;
if(sym == Ident)
{
int index = lookupIdent(varBuffer);
if(index != 0)
{
if(symTbl[index].recType != TYPEREC)
compileError(11);
else
type = symTbl[index].type;
}
}
else
compileError(10);
while(paramPoint <= symTblPoint)
{
symTbl[paramPoint].type = type;
symTbl[paramPoint].paramRecord.variable = isVar;
paramPoint++;
}
accept(Ident);
}
/*21. FieldList -> [ IdentList : Type ] */
void FieldList(int *typeSize, int *disp)
{
if (sym == Ident)
{
int firstEntry, lastEntry;
IdentList(FIELDREC, &firstEntry, &lastEntry);
accept(COLON);
int type, typeSize2;
Type(&type, &typeSize2);
do{
symTbl[firstEntry].type = type;
symTbl[firstEntry].fieldRecord.fieldAddr = *disp;
*typeSize += typeSize2;
*disp += typeSize2;
firstEntry++;
}while(firstEntry <= lastEntry);
}
}
/*22. StatSeq -> Stat { ; Stat } */
int StatSeq()
{
int ret = Stat();
if(ret == 1)
{
if(sym == SEMIC)
nextSym();
if(sym != ENDsym)
compileWarning(2);
while(sym != ENDsym)
nextSym();
}
else if(ret > 1)
return ret;
while (sym == SEMIC)
{
#if SYM_TABLE_OUT
printf("\n***Start of statement tables:***");
printTbls();
#endif
confirmSym();
ret = Stat();
if(ret == 1)
{
if(sym == SEMIC)
nextSym();
if(sym != ENDsym)
compileWarning(2);
while(sym != ENDsym)
nextSym();
}
else if(ret > 1)
return ret;
}
return ret;
}
/*23. Stat -> [ AssignStat | ProcCall | IfStat | WhileStat | RepeatStat */
/* | ForStat | LoopStat | CaseStat | EXIT | RETURN [ Expr ] ] */
int Stat()
{
switch(sym)
{
case Ident: /* AssignStat & ProcCall start with Ident */
procOrAssign();
break;
case IFsym:
return IfStat();
break;
case WHILEsym:
WhileStat();
break;
case REPEATsym:
RepeatStat();
break;
case FORsym:
ForStat();
break;
case LOOPsym:
LoopStat();
break;
case CASEsym:
CaseStat();
break;
case EXITsym:
confirmSym();
insertInstr(jmp, 0, locCounter + 1);
return locCounter - 1;
break;
case RETURNsym:
confirmSym();
int type;
if (exprCheck())
{
Expr(&type);
/* store return value in resAddr on stack - backpatched at end of ProcDecl*/
insertInstr(sto, 0, -1);
return 1;
}
break;
}
return 0;
}
void procOrAssign()
{
int index = lookupIdent(varBuffer);
int recType = symTbl[index].recType;
accept(Ident);
if (sym == ASSGN || sym == PER || sym == LBRAC) /* Regular assignment */
AssignStat(index);
/* ABS and ODD must not be LHS */
else if(recType == STDPROCREC && (symTbl[index].stdProcRecord.stdProcNum == 1 || symTbl[index].stdProcRecord.stdProcNum == 2))
{
compileError(18);
stdProcCall(symTbl[index].stdProcRecord.stdProcNum);
}
else if (sym == LPAREN || (recType == PROCREC || recType == STDPROCREC)) /* ProcCall */
{
if(recType == PROCREC)
ProcCall(index);
else if(recType == STDPROCREC)
stdProcCall(symTbl[index].stdProcRecord.stdProcNum);
}
}
/*24. AssignStat -> Designator := Expr */
/*49. Designator -> Ident { Selector } */
void AssignStat(int indexLHS)
{
int varAddr = symTbl[indexLHS].varRecord.varAddr;
int offset = 1, selType = -1;
int nextIndex = indexLHS;
while(sym == PER || sym == LBRAC)
offset += Selector(&nextIndex, &selType);
accept(ASSGN);
int type;
Expr(&type);
if(typeTbl[symTbl[indexLHS].type].recType == ENUMREC)
checkTypes(1, type);
else if(typeTbl[symTbl[indexLHS].type].recType == ARRAYREC)
checkTypes(typeTbl[symTbl[indexLHS].type].arrayRecord.elementType, type);
else if(typeTbl[symTbl[indexLHS].type].recType == RECREC)
checkTypes(selType, type);
else
checkTypes(symTbl[indexLHS].type, type);
switch(symTbl[indexLHS].recType)
{
case VARREC:
insertInstr(sto, 0, symTbl[indexLHS].varRecord.varAddr + offset - 1);
break;
case FIELDREC:
insertInstr(sto, 0, varAddr + symTbl[indexLHS].fieldRecord.fieldAddr);
break;
case PARAMREC:
if(symTbl[indexLHS].paramRecord.variable == 1)
insertInstr(stoi, 0, symTbl[indexLHS].paramRecord.paramAddr);
else
insertInstr(sto, 0, symTbl[indexLHS].paramRecord.paramAddr);
break;
case CONSTREC:
compileError(15);
break;
default:
compileError(12);
}
}
/*25. ProcCall -> Ident ( [ ActParams ] | ReadParams | [ ReadLnParams ] | WriteParams | [ WriteLnParams ] ) */
void ProcCall(int procPoint)
{
int paramLength = 0;
if(sym == LPAREN)
{
confirmSym();
if(sym == RPAREN) /* no params */
confirmSym();
else
ActParams(procPoint, ¶mLength);
}
if((symTbl[procPoint].procRecord.lastParam != 0) && (paramLength != (symTbl[procPoint].procRecord.lastParam - procPoint)))
compileError(16);
insertInstr(call, currentLevel - symTbl[procPoint].idLevel, symTbl[procPoint].procRecord.entryAddr);
insertInstr(isp, 0, -paramLength);
}
void stdProcCall(int stdProcNum)
{
accept(LPAREN);
int type;
switch(stdProcNum)
{
case 1: /* ABS */
Expr(&type);
if(type != INTTYPE)
compileError(17);
insertInstr(iabs, 0, 0);
accept(RPAREN);
break;
case 2: /* ODD */
Expr(&type);
if(type != INTTYPE)
compileError(19);
insertInstr(lodc, 0, 2);
insertInstr(imod, 0, 0);
insertInstr(lodc, 0, 1);
insertInstr(eq, 0, 0);
accept(RPAREN);
break;
case 3: /* READ */
case 4: /* READLN */
do{
if(sym == Ident)
{
/* lookup parameters */
int index = lookupIdent(varBuffer);
if(index != 0)
{
if(symTbl[index].recType == VARREC)
{
checkTypes(symTbl[index].type, INTTYPE);
insertInstr(loda, currentLevel - symTbl[index].idLevel, symTbl[index].varRecord.varAddr);
insertInstr(rdi, 0, 0);
}
else if(symTbl[index].recType == PARAMREC)
{
checkTypes(symTbl[index].type, INTTYPE);
if(symTbl[index].paramRecord.variable == 1)
insertInstr(lod, currentLevel - symTbl[index].idLevel, symTbl[index].varRecord.varAddr);
else
insertInstr(loda, currentLevel - symTbl[index].idLevel, symTbl[index].varRecord.varAddr);
insertInstr(rdi, 0, 0);
}
else
compileError(13);
}
else
compileError(13);
}
else
compileError(13);
accept(Ident);
if(sym == COMMA)
accept(COMMA);
}while(sym == Ident);
accept(RPAREN);
if(stdProcNum == 4)
insertInstr(rdl, 0, 0);
break;
case 5: /* WRITE */
case 6: /* WRITELN */
do{
int type;
Expr(&type);
insertInstr( wri, 0, 0);
if(sym == COMMA)
accept(COMMA);
}while(exprCheck());
accept(RPAREN);
if(stdProcNum == 6)
insertInstr(wrl, 0, 0);
break;
}
}
/*26. ActParams -> '(' [ Expr { ',' Expr } ] ')' */
void ActParams(int procPoint, int *paramLength)
{
int nextParamPoint = procPoint + 1;
/* function parameter given an identifier */
if(symTbl[nextParamPoint].paramRecord.variable == 1)
{
if(sym == Ident)
{
int index = lookupIdent(varBuffer);
if(index != 0)
{
checkParamTypes(symTbl[nextParamPoint].type, symTbl[index].type);
if(symTbl[index].recType == VARREC)
insertInstr(loda, currentLevel - symTbl[index].idLevel, symTbl[index].varRecord.varAddr);
else if(symTbl[index].recType == PARAMREC)
insertInstr(lod, currentLevel - symTbl[index].idLevel, symTbl[index].paramRecord.paramAddr);
*paramLength = *paramLength + 1;
}
else
compileError(6);
accept(Ident);
}
else
{
confirmSym();
compileError(29);
}
}
/* function parameter given a non-identifier expr */
else
{
int type;
Expr(&type);
checkParamTypes(symTbl[nextParamPoint].type, type);
*paramLength += 1;
}
while(sym == COMMA)
{
confirmSym();
nextParamPoint = nextParamPoint + 1;
/* function parameter given an identifier */
if(symTbl[nextParamPoint].paramRecord.variable == 1)
{
if(sym == Ident)
{
int index = lookupIdent(varBuffer);
if(index != 0)
{
checkParamTypes(symTbl[nextParamPoint].type, symTbl[index].type);
if(symTbl[index].recType == VARREC)
insertInstr(loda, currentLevel - symTbl[index].idLevel, symTbl[index].varRecord.varAddr);
else if(symTbl[index].recType == PARAMREC)
insertInstr(lod, currentLevel - symTbl[index].idLevel, symTbl[index].paramRecord.paramAddr);
*paramLength = *paramLength + 1;
}
else
compileError(6);
accept(Ident);
}
else
{
confirmSym();
compileError(29);
}
}
else
{
int type;
Expr(&type);
checkParamTypes(symTbl[nextParamPoint].type, type);
*paramLength += 1;
}
}
accept(RPAREN);
}
/*33. IfStat -> IF Expr THEN StatSeq { ELSIF Expr THEN StatSeq } [ ELSE StatSeq ] END */
int IfStat()
{
int ret;
accept(IFsym);
int type;
Expr(&type);
checkTypes(type, BOOLTYPE);
accept(THENsym);
/* conditional jump to elseif, else, or end */
int jmpcIF = locCounter;
insertInstr(jmpc, 0, 0);
ret = StatSeq();
/* unconditional jump to end */
int jmpIF = locCounter;
insertInstr(jmp, 0, 0);
int isELSIF = 0;
int endJumps[15];
int index = 0;
if(sym == ELSIFsym)
{
isELSIF = 1;
/* if there is an elseif, if jmpc needs to go here */
code[jmpcIF].addr = locCounter;
int jmpcPrev;
while (sym == ELSIFsym)
{
confirmSym();
Expr(&type);
checkTypes(type, BOOLTYPE);
/* conditional jump to next elseif, else, or end */
jmpcPrev = locCounter;
insertInstr(jmpc, 0, 0);
accept(THENsym);
ret = StatSeq();
/* unconditional jump to end */
endJumps[index] = locCounter;
insertInstr(jmp, 0, 0);
index++;
code[jmpcPrev].addr = locCounter;
}
}
if (sym == ELSEsym)
{
/* if there is no elseif, if jmpc needs to go here */
if(isELSIF == 0)
code[jmpcIF].addr = locCounter;
confirmSym();
ret = StatSeq();
}
/* if no elseif or no else, if jmpc needs to go here */
else if(isELSIF == 0)
code[jmpcIF].addr = locCounter;
/* all unconditional jumps go here */
code[jmpIF].addr = locCounter;
int i;
for(i = 0; i < index; i++)
code[endJumps[i]].addr = locCounter;
accept(ENDsym);
return ret;
}
/*34. WhileStat -> WHILE Expr DO StatSeq { ELSIF expr DO StatSeq } END */
void WhileStat()
{
accept(WHILEsym);
int type;
int whileStartLoc = locCounter; /* location of expr */
Expr(&type);
checkTypes(type, BOOLTYPE);
int jmpcWhile = locCounter;
insertInstr(jmpc, 0, 0);
accept(DOsym);
int exitJmpLoc = StatSeq();
insertInstr(jmp, 0, whileStartLoc);
code[jmpcWhile].addr = locCounter;
int jmpcElsif;
while (sym == ELSIFsym)
{
confirmSym();
Expr(&type);
checkTypes(type, BOOLTYPE);
jmpcElsif = locCounter;
insertInstr(jmpc, 0, 0);
accept(DOsym);
exitJmpLoc = StatSeq();
insertInstr(jmp, 0, whileStartLoc);
code[jmpcElsif].addr = locCounter ;
}
accept(ENDsym);
if(exitJmpLoc > 1)
code[exitJmpLoc].addr = locCounter;
}
/*35. RepeatStat -> REPEAT StatSeq UNTIL Expr */
void RepeatStat()
{
accept(REPEATsym);
int repeatStart = locCounter;
int exitJmpLoc = StatSeq();
accept(UNTILsym);
int type;
Expr(&type);
checkTypes(type, BOOLTYPE);
insertInstr(jmpc, 0, repeatStart);
if(exitJmpLoc > 1)
code[exitJmpLoc].addr = locCounter;
}
/*36. ForStat -> FOR Ident := Expr TO Expr [ BY ConstExpr ] DO StatSeq END */
void ForStat()
{
accept(FORsym);
int index;
if(sym == Ident)
{
index = lookupIdent(varBuffer);
if(symTbl[index].recType == VARREC)
insertInstr(loda, currentLevel - symTbl[index].idLevel, symTbl[index].varRecord.varAddr);
else if(symTbl[index].recType == PARAMREC)
{
if(symTbl[index].paramRecord.variable == 1)
insertInstr(lod, 0, symTbl[index].paramRecord.paramAddr);
else
insertInstr(loda, 0, symTbl[index].paramRecord.paramAddr);
}
accept(Ident);
}
else
compileError(10);
accept(ASSGN);
int type;
Expr(&type);
accept(TOsym);
Expr(&type);
int hasBy = 0;
int byVal;
if (sym == BYsym)
{
hasBy = 1;
confirmSym();
ConstExpr(&byVal, &type);
}
accept(DOsym);
int exitJmpLoc;
if(hasBy == 0)
{
int loc1 = locCounter;
insertInstr(for0, 0, 0);
int loc2 = locCounter;
exitJmpLoc = StatSeq();
insertInstr(for1, 0, loc2);
code[loc1].addr = locCounter;
if(exitJmpLoc > 1)
code[exitJmpLoc].addr = locCounter;
}
/* FOR statement has a BY clause, more complicated looping structure */
else if(hasBy == 1)
{
insertInstr(sto, 0, -1);
int address;
if(symTbl[index].recType == VARREC)
address = symTbl[index].varRecord.varAddr;
else if(symTbl[index].recType == PARAMREC)
{
if(symTbl[index].paramRecord.variable == 1)
address = symTbl[index].paramRecord.paramAddr;
else
address = symTbl[index].paramRecord.paramAddr;
}
insertInstr(sto, 0, address);
insertInstr(isp, 0, -1);
int forCheck = locCounter;
insertInstr(lod, 0, address);
insertInstr(lod, 0, -1);
if(byVal > 0)
insertInstr(le, 0, 0);
else if(byVal < 0)
insertInstr(ge, 0, 0);
else
compileError(28);
int condJmp = locCounter;
insertInstr(jmpc, 0, -1);
exitJmpLoc = StatSeq();
insertInstr(lod, 0, address);
insertInstr(lodc, 0, byVal);