forked from menghuanlater/BUAA_Complier_Design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascal_S.pas
2862 lines (2775 loc) · 87.4 KB
/
pascal_S.pas
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
program PASCALS(INPUT,OUTPUT,PRD,PRR);
{ author:N.Wirth, E.T.H. CH-8092 Zurich,1.3.76 }
{ modified by R.E.Berry
Department of computer studies
University of Lancaster
Variants of this program are used on
Data General Nova,Apple,and
Western Digital Microengine machines. }
{ further modified by M.Z.Jin
Department of Computer Science&Engineering BUAA,0ct.1989
}
const nkw = 27; { no. of key words }
alng = 10; { no. of significant chars in identifiers }
llng = 121; { input line length }
emax = 322; { max exponent of real numbers }
emin = -292; { min exponent }
kmax = 15; { max no. of significant digits }
tmax = 100; { size of table }
bmax = 20; { size of block-talbe }
amax = 30; { size of array-table }
c2max = 20; { size of real constant table }
csmax = 30; { max no. of cases }
cmax = 800; { size of code }
lmax = 7; { maximum level }
smax = 600; { size of string-table }
ermax = 58; { max error no. }
omax = 63; { highest order code }
xmax = 32767; { 2**15-1 }
nmax = 32767; { 2**15-1 }
lineleng = 132; { output line length }
linelimit = 200;
stacksize = 1450;
type symbol = ( intcon, realcon, charcon, stringcon,
notsy, plus, minus, times, idiv, rdiv, imod, andsy, orsy,
eql, neq, gtr, geq, lss, leq,
lparent, rparent, lbrack, rbrack, comma, semicolon, period,
colon, becomes, constsy, typesy, varsy, funcsy,
procsy, arraysy, recordsy, programsy, ident,
beginsy, ifsy, casesy, repeatsy, whilesy, forsy,
endsy, elsesy, untilsy, ofsy, dosy, tosy, downtosy, thensy);
index = -xmax..+xmax;
alfa = packed array[1..alng]of char;
objecttyp = (konstant, vvariable, typel, prozedure, funktion );
types = (notyp, ints, reals, bools, chars, arrays, records );
symset = set of symbol;
typset = set of types;
item = record
typ: types;
ref: index;
end;
order = packed record
f: -omax..+omax;
x: -lmax..+lmax;
y: -nmax..+nmax
end;
var ch: char; { last character read from source program }
rnum: real; { real number from insymbol }
inum: integer; { integer from insymbol }
sleng: integer; { string length }
cc: integer; { character counter }
lc: integer; { program location counter }
ll: integer; { length of current line }
errpos: integer;
t,a,b,sx,c1,c2:integer; { indices to tables }
iflag, oflag, skipflag, stackdump, prtables: boolean;
sy: symbol; { last symbol read by insymbol }
errs: set of 0..ermax;
id: alfa; { identifier from insymbol }
progname: alfa;
stantyps: typset;
constbegsys, typebegsys, blockbegsys, facbegsys, statbegsys: symset;
line: array[1..llng] of char;
key: array[1..nkw] of alfa;
ksy: array[1..nkw] of symbol;
sps: array[char]of symbol; { special symbols }
display: array[0..lmax] of integer;
tab: array[0..tmax] of { indentifier lable }
packed record
name: alfa;
link: index;
obj: objecttyp;
typ: types;
ref: index;
normal: boolean;
lev: 0..lmax;
adr: integer
end;
atab: array[1..amax] of { array-table }
packed record
inxtyp,eltyp: types;
elref,low,high,elsize,size: index
end;
btab: array[1..bmax] of { block-table }
packed record
last, lastpar, psize, vsize: index
end;
stab: packed array[0..smax] of char; { string table }
rconst: array[1..c2max] of real;
code: array[0..cmax] of order;
psin,psout,prr,prd:text; { default in pascal p }
inf, outf, fprr: string;
procedure errormsg;
var k : integer;
msg: array[0..ermax] of alfa;
begin
msg[0] := 'undef id '; msg[1] := 'multi def ';
msg[2] := 'identifier'; msg[3] := 'program ';
msg[4] := ') '; msg[5] := ': ';
msg[6] := 'syntax '; msg[7] := 'ident,var ';
msg[8] := 'of '; msg[9] := '( ';
msg[10] := 'id,array '; msg[11] := '( ';
msg[12] := '] '; msg[13] := '.. ';
msg[14] := '; '; msg[15] := 'func. type';
msg[16] := '= '; msg[17] := 'boolean ';
msg[18] := 'convar typ'; msg[19] := 'type ';
msg[20] := 'prog.param'; msg[21] := 'too big ';
msg[22] := '. '; msg[23] := 'type(case)';
msg[24] := 'character '; msg[25] := 'const id ';
msg[26] := 'index type'; msg[27] := 'indexbound';
msg[28] := 'no array '; msg[29] := 'type id ';
msg[30] := 'undef type'; msg[31] := 'no record ';
msg[32] := 'boole type'; msg[33] := 'arith type';
msg[34] := 'integer '; msg[35] := 'types ';
msg[36] := 'param type'; msg[37] := 'variab id ';
msg[38] := 'string '; msg[39] := 'no.of pars';
msg[40] := 'real numbr'; msg[41] := 'type ';
msg[42] := 'real type '; msg[43] := 'integer ';
msg[44] := 'var,const '; msg[45] := 'var,proc ';
msg[46] := 'types(:=) '; msg[47] := 'typ(case) ';
msg[48] := 'type '; msg[49] := 'store ovfl';
msg[50] := 'constant '; msg[51] := ':= ';
msg[52] := 'then '; msg[53] := 'until ';
msg[54] := 'do '; msg[55] := 'to downto ';
msg[56] := 'begin '; msg[57] := 'end ';
msg[58] := 'factor';
writeln(psout);
writeln(psout,'key words');
k := 0;
while errs <> [] do
begin
while not( k in errs )do k := k + 1;
writeln(psout, k, ' ', msg[k] );
errs := errs - [k]
end { while errs }
end { errormsg } ;
procedure endskip;
begin { underline skipped part of input }
while errpos < cc do
begin
write( psout, '-');
errpos := errpos + 1
end;
skipflag := false
end { endskip };
procedure nextch; { read next character; process line end }
begin
if cc = ll
then begin
if eof( psin )
then begin
writeln( psout );
writeln( psout, 'program incomplete' );
errormsg;
exit;
end;
if errpos <> 0
then begin
if skipflag then endskip;
writeln( psout );
errpos := 0
end;
write( psout, lc: 5, ' ');
ll := 0;
cc := 0;
while not eoln( psin ) do
begin
ll := ll + 1;
read( psin, ch );
write( psout, ch );
line[ll] := ch
end;
ll := ll + 1;
readln( psin );
line[ll] := ' ';
writeln( psout );
end;
cc := cc + 1;
ch := line[cc];
end { nextch };
procedure error( n: integer );
begin
if errpos = 0
then write ( psout, '****' );
if cc > errpos
then begin
write( psout, ' ': cc-errpos, '^', n:2);
errpos := cc + 3;
errs := errs +[n]
end
end { error };
procedure fatal( n: integer );
var msg : array[1..7] of alfa;
begin
writeln( psout );
errormsg;
msg[1] := 'identifier'; msg[2] := 'procedures';
msg[3] := 'reals '; msg[4] := 'arrays ';
msg[5] := 'levels '; msg[6] := 'code ';
msg[7] := 'strings ';
writeln( psout, 'compiler table for ', msg[n], ' is too small');
exit; {terminate compilation }
end { fatal };
procedure insymbol; {reads next symbol}
label 1,2,3;
var i,j,k,e: integer;
procedure readscale;
var s,sign: integer;
begin
nextch;
sign := 1;
s := 0;
if ch = '+'
then nextch
else if ch = '-'
then begin
nextch;
sign := -1
end;
if not(( ch >= '0' )and (ch <= '9' ))
then error( 40 )
else repeat
s := 10*s + ord( ord(ch)-ord('0'));
nextch;
until not(( ch >= '0' ) and ( ch <= '9' ));
e := s*sign + e
end { readscale };
procedure adjustscale;
var s : integer;
d, t : real;
begin
if k + e > emax
then error(21)
else if k + e < emin
then rnum := 0
else begin
s := abs(e);
t := 1.0;
d := 10.0;
repeat
while not odd(s) do
begin
s := s div 2;
d := sqr(d)
end;
s := s - 1;
t := d * t
until s = 0;
if e >= 0
then rnum := rnum * t
else rnum := rnum / t
end
end { adjustscale };
procedure options;
procedure switch( var b: boolean );
begin
b := ch = '+';
if not b
then if not( ch = '-' )
then begin { print error message }
while( ch <> '*' ) and ( ch <> ',' ) do
nextch;
end
else nextch
else nextch
end { switch };
begin { options }
repeat
nextch;
if ch <> '*'
then begin
if ch = 't'
then begin
nextch;
switch( prtables )
end
else if ch = 's'
then begin
nextch;
switch( stackdump )
end;
end
until ch <> ','
end { options };
begin { insymbol }
1: while( ch = ' ' ) or ( ch = chr(9) ) do
nextch; { space & htab }
case ch of
'a','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z':
begin { identifier of wordsymbol }
k := 0;
id := ' ';
repeat
if k < alng
then begin
k := k + 1;
id[k] := ch
end;
nextch
until not((( ch >= 'a' ) and ( ch <= 'z' )) or (( ch >= '0') and (ch <= '9' )));
i := 1;
j := nkw; { binary search }
repeat
k := ( i + j ) div 2;
if id <= key[k]
then j := k - 1;
if id >= key[k]
then i := k + 1;
until i > j;
if i - 1 > j
then sy := ksy[k]
else sy := ident
end;
'0','1','2','3','4','5','6','7','8','9':
begin { number }
k := 0;
inum := 0;
sy := intcon;
repeat
inum := inum * 10 + ord(ch) - ord('0');
k := k + 1;
nextch
until not (( ch >= '0' ) and ( ch <= '9' ));
if( k > kmax ) or ( inum > nmax )
then begin
error(21);
inum := 0;
k := 0
end;
if ch = '.'
then begin
nextch;
if ch = '.'
then ch := ':'
else begin
sy := realcon;
rnum := inum;
e := 0;
while ( ch >= '0' ) and ( ch <= '9' ) do
begin
e := e - 1;
rnum := 10.0 * rnum + (ord(ch) - ord('0'));
nextch
end;
if e = 0
then error(40);
if ch = 'e'
then readscale;
if e <> 0 then adjustscale
end
end
else if ch = 'e'
then begin
sy := realcon;
rnum := inum;
e := 0;
readscale;
if e <> 0
then adjustscale
end;
end;
':':
begin
nextch;
if ch = '='
then begin
sy := becomes;
nextch
end
else sy := colon
end;
'<':
begin
nextch;
if ch = '='
then begin
sy := leq;
nextch
end
else
if ch = '>'
then begin
sy := neq;
nextch
end
else sy := lss
end;
'>':
begin
nextch;
if ch = '='
then begin
sy := geq;
nextch
end
else sy := gtr
end;
'.':
begin
nextch;
if ch = '.'
then begin
sy := colon;
nextch
end
else sy := period
end;
'''':
begin
k := 0;
2: nextch;
if ch = ''''
then begin
nextch;
if ch <> ''''
then goto 3
end;
if sx + k = smax
then fatal(7);
stab[sx+k] := ch;
k := k + 1;
if cc = 1
then begin { end of line }
k := 0;
end
else goto 2;
3: if k = 1
then begin
sy := charcon;
inum := ord( stab[sx] )
end
else if k = 0
then begin
error(38);
sy := charcon;
inum := 0
end
else begin
sy := stringcon;
inum := sx;
sleng := k;
sx := sx + k
end
end;
'(':
begin
nextch;
if ch <> '*'
then sy := lparent
else begin { comment }
nextch;
if ch = '$'
then options;
repeat
while ch <> '*' do nextch;
nextch
until ch = ')';
nextch;
goto 1
end
end;
'{':
begin
nextch;
if ch = '$'
then options;
while ch <> '}' do
nextch;
nextch;
goto 1
end;
'+', '-', '*', '/', ')', '=', ',', '[', ']', ';':
begin
sy := sps[ch];
nextch
end;
'$','"' ,'@', '?', '&', '^', '!':
begin
error(24);
nextch;
goto 1
end
end { case }
end { insymbol };
procedure enter(x0:alfa; x1:objecttyp; x2:types; x3:integer );
begin
t := t + 1; { enter standard identifier }
with tab[t] do
begin
name := x0;
link := t - 1;
obj := x1;
typ := x2;
ref := 0;
normal := true;
lev := 0;
adr := x3;
end
end; { enter }
procedure enterarray( tp: types; l,h: integer );
begin
if l > h
then error(27);
if( abs(l) > xmax ) or ( abs(h) > xmax )
then begin
error(27);
l := 0;
h := 0;
end;
if a = amax
then fatal(4)
else begin
a := a + 1;
with atab[a] do
begin
inxtyp := tp;
low := l;
high := h
end
end
end { enterarray };
procedure enterblock;
begin
if b = bmax
then fatal(2)
else begin
b := b + 1;
btab[b].last := 0;
btab[b].lastpar := 0;
end
end { enterblock };
procedure enterreal( x: real );
begin
if c2 = c2max - 1
then fatal(3)
else begin
rconst[c2+1] := x;
c1 := 1;
while rconst[c1] <> x do
c1 := c1 + 1;
if c1 > c2
then c2 := c1
end
end { enterreal };
procedure emit( fct: integer );
begin
if lc = cmax
then fatal(6);
code[lc].f := fct;
lc := lc + 1
end { emit };
procedure emit1( fct, b: integer );
begin
if lc = cmax
then fatal(6);
with code[lc] do
begin
f := fct;
y := b;
end;
lc := lc + 1
end { emit1 };
procedure emit2( fct, a, b: integer );
begin
if lc = cmax then fatal(6);
with code[lc] do
begin
f := fct;
x := a;
y := b
end;
lc := lc + 1;
end { emit2 };
procedure printtables;
var i: integer;
o: order;
mne: array[0..omax] of
packed array[1..5] of char;
begin
mne[0] := 'LDA '; mne[1] := 'LOD '; mne[2] := 'LDI ';
mne[3] := 'DIS '; mne[8] := 'FCT '; mne[9] := 'INT ';
mne[10] := 'JMP '; mne[11] := 'JPC '; mne[12] := 'SWT ';
mne[13] := 'CAS '; mne[14] := 'F1U '; mne[15] := 'F2U ';
mne[16] := 'F1D '; mne[17] := 'F2D '; mne[18] := 'MKS ';
mne[19] := 'CAL '; mne[20] := 'IDX '; mne[21] := 'IXX ';
mne[22] := 'LDB '; mne[23] := 'CPB '; mne[24] := 'LDC ';
mne[25] := 'LDR '; mne[26] := 'FLT '; mne[27] := 'RED ';
mne[28] := 'WRS '; mne[29] := 'WRW '; mne[30] := 'WRU ';
mne[31] := 'HLT '; mne[32] := 'EXP '; mne[33] := 'EXF ';
mne[34] := 'LDT '; mne[35] := 'NOT '; mne[36] := 'MUS ';
mne[37] := 'WRR '; mne[38] := 'STO '; mne[39] := 'EQR ';
mne[40] := 'NER '; mne[41] := 'LSR '; mne[42] := 'LER ';
mne[43] := 'GTR '; mne[44] := 'GER '; mne[45] := 'EQL ';
mne[46] := 'NEQ '; mne[47] := 'LSS '; mne[48] := 'LEQ ';
mne[49] := 'GRT '; mne[50] := 'GEQ '; mne[51] := 'ORR ';
mne[52] := 'ADD '; mne[53] := 'SUB '; mne[54] := 'ADR ';
mne[55] := 'SUR '; mne[56] := 'AND '; mne[57] := 'MUL ';
mne[58] := 'DIV '; mne[59] := 'MOD '; mne[60] := 'MUR ';
mne[61] := 'DIR '; mne[62] := 'RDL '; mne[63] := 'WRL ';
writeln(psout);
writeln(psout);
writeln(psout);
writeln(psout,' identifiers link obj typ ref nrm lev adr');
writeln(psout);
for i := btab[1].last to t do
with tab[i] do
writeln( psout, i,' ', name, link:5, ord(obj):5, ord(typ):5,ref:5, ord(normal):5,lev:5,adr:5);
writeln( psout );
writeln( psout );
writeln( psout );
writeln( psout, 'blocks last lpar psze vsze' );
writeln( psout );
for i := 1 to b do
with btab[i] do
writeln( psout, i:4, last:9, lastpar:5, psize:5, vsize:5 );
writeln( psout );
writeln( psout );
writeln( psout );
writeln( psout, 'arrays xtyp etyp eref low high elsz size');
writeln( psout );
for i := 1 to a do
with atab[i] do
writeln( psout, i:4, ord(inxtyp):9, ord(eltyp):5, elref:5, low:5, high:5, elsize:5, size:5);
writeln( psout );
writeln( psout );
writeln( psout );
writeln( psout, 'code:');
writeln( psout );
for i := 0 to lc-1 do
begin
write( psout, i:5 );
o := code[i];
write( psout, mne[o.f]:8, o.f:5 );
if o.f < 31
then if o.f < 4
then write( psout, o.x:5, o.y:5 )
else write( psout, o.y:10 )
else write( psout, ' ' );
writeln( psout, ',' )
end;
writeln( psout );
writeln( psout, 'Starting address is ', tab[btab[1].last].adr:5 )
end { printtables };
procedure block( fsys: symset; isfun: boolean; level: integer );
type conrec = record
case tp: types of
ints, chars, bools : ( i:integer );
reals :( r:real )
end;
var dx : integer ; { data allocation index }
prt: integer ; { t-index of this procedure }
prb: integer ; { b-index of this procedure }
x : integer ;
procedure skip( fsys:symset; n:integer);
begin
error(n);
skipflag := true;
while not ( sy in fsys ) do
insymbol;
if skipflag then endskip
end { skip };
procedure test( s1,s2: symset; n:integer );
begin
if not( sy in s1 )
then skip( s1 + s2, n )
end { test };
procedure testsemicolon;
begin
if sy = semicolon
then insymbol
else begin
error(14);
if sy in [comma, colon]
then insymbol
end;
test( [ident] + blockbegsys, fsys, 6 )
end { testsemicolon };
procedure enter( id: alfa; k:objecttyp );
var j,l : integer;
begin
if t = tmax
then fatal(1)
else begin
tab[0].name := id;
j := btab[display[level]].last;
l := j;
while tab[j].name <> id do
j := tab[j].link;
if j <> 0
then error(1)
else begin
t := t + 1;
with tab[t] do
begin
name := id;
link := l;
obj := k;
typ := notyp;
ref := 0;
lev := level;
adr := 0;
normal := false { initial value }
end;
btab[display[level]].last := t
end
end
end { enter };
function loc( id: alfa ):integer;
var i,j : integer; { locate if in table }
begin
i := level;
tab[0].name := id; { sentinel }
repeat
j := btab[display[i]].last;
while tab[j].name <> id do
j := tab[j].link;
i := i - 1;
until ( i < 0 ) or ( j <> 0 );
if j = 0
then error(0);
loc := j
end { loc } ;
procedure entervariable;
begin
if sy = ident
then begin
enter( id, vvariable );
insymbol
end
else error(2)
end { entervariable };
procedure constant( fsys: symset; var c: conrec );
var x, sign : integer;
begin
c.tp := notyp;
c.i := 0;
test( constbegsys, fsys, 50 );
if sy in constbegsys
then begin
if sy = charcon
then begin
c.tp := chars;
c.i := inum;
insymbol
end
else begin
sign := 1;
if sy in [plus, minus]
then begin
if sy = minus
then sign := -1;
insymbol
end;
if sy = ident
then begin
x := loc(id);
if x <> 0
then
if tab[x].obj <> konstant
then error(25)
else begin
c.tp := tab[x].typ;
if c.tp = reals
then c.r := sign*rconst[tab[x].adr]
else c.i := sign*tab[x].adr
end;
insymbol
end
else if sy = intcon
then begin
c.tp := ints;
c.i := sign*inum;
insymbol
end
else if sy = realcon
then begin
c.tp := reals;
c.r := sign*rnum;
insymbol
end
else skip(fsys,50)
end;
test(fsys,[],6)
end
end { constant };
procedure typ( fsys: symset; var tp: types; var rf,sz:integer );
var eltp : types;
elrf, x : integer;
elsz, offset, t0, t1 : integer;
procedure arraytyp( var aref, arsz: integer );
var eltp : types;
low, high : conrec;
elrf, elsz: integer;
begin
constant( [colon, rbrack, rparent, ofsy] + fsys, low );
if low.tp = reals
then begin
error(27);
low.tp := ints;
low.i := 0
end;
if sy = colon
then insymbol
else error(13);
constant( [rbrack, comma, rparent, ofsy ] + fsys, high );
if high.tp <> low.tp
then begin
error(27);
high.i := low.i
end;
enterarray( low.tp, low.i, high.i );
aref := a;
if sy = comma
then begin
insymbol;
eltp := arrays;
arraytyp( elrf, elsz )
end
else begin
if sy = rbrack
then insymbol
else begin
error(12);
if sy = rparent
then insymbol
end;
if sy = ofsy
then insymbol
else error(8);
typ( fsys, eltp, elrf, elsz )
end;
with atab[aref] do
begin
arsz := (high-low+1) * elsz;
size := arsz;
eltyp := eltp;
elref := elrf;
elsize := elsz
end
end { arraytyp };
begin { typ }
tp := notyp;
rf := 0;
sz := 0;
test( typebegsys, fsys, 10 );
if sy in typebegsys
then begin
if sy = ident
then begin
x := loc(id);
if x <> 0
then with tab[x] do
if obj <> typel
then error(29)
else begin
tp := typ;
rf := ref;
sz := adr;
if tp = notyp
then error(30)
end;
insymbol
end
else if sy = arraysy
then begin
insymbol;
if sy = lbrack
then insymbol
else begin
error(11);
if sy = lparent
then insymbol
end;
tp := arrays;
arraytyp(rf,sz)
end
else begin { records }
insymbol;
enterblock;
tp := records;
rf := b;
if level = lmax
then fatal(5);
level := level + 1;
display[level] := b;
offset := 0;
while not ( sy in fsys - [semicolon,comma,ident]+ [endsy] ) do
begin { field section }
if sy = ident
then begin
t0 := t;
entervariable;
while sy = comma do
begin
insymbol;
entervariable
end;
if sy = colon
then insymbol
else error(5);
t1 := t;
typ( fsys + [semicolon, endsy, comma,ident], eltp, elrf, elsz );
while t0 < t1 do
begin
t0 := t0 + 1;
with tab[t0] do
begin
typ := eltp;
ref := elrf;
normal := true;
adr := offset;
offset := offset + elsz
end
end
end; { sy = ident }
if sy <> endsy
then begin
if sy = semicolon
then insymbol
else begin
error(14);
if sy = comma
then insymbol
end;
test( [ident,endsy, semicolon],fsys,6 )
end
end; { field section }
btab[rf].vsize := offset;
sz := offset;
btab[rf].psize := 0;
insymbol;
level := level - 1
end; { record }
test( fsys, [],6 )
end;
end { typ };
procedure parameterlist; { formal parameter list }
var tp : types;
valpar : boolean;
rf, sz, x, t0 : integer;
begin
insymbol;
tp := notyp;
rf := 0;
sz := 0;
test( [ident, varsy], fsys+[rparent], 7 );
while sy in [ident, varsy] do
begin
if sy <> varsy
then valpar := true
else begin
insymbol;
valpar := false