-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpascals.txt
1758 lines (1687 loc) · 59.6 KB
/
pascals.txt
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); (*1.6.75*)
(* N. Wirth, E.T.H.
Clausiusstr.55 CH-8006 Zurich *)
label 99;
const nkw = 27; (*no. of key words*)
alng = 10; (*no. of significant chars in identifiers*)
llng = 120; (*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-table*)
amax = 38; (*size of array-table*)
c2max = 20; (*size of real constant table*)
csmax = 30; (*max no. of cases*)
cmax = 850; (*size of code*)
lmax = 7; (*maximum level*)
smax = 600; (*size of string-table*)
ermax = 58; (*max error no.*)
omax = 63; (*highest order code*)
xmax = 131071; (*2**17 - 1*)
nmax = 281474976710655; (*2**48-1*)
lineleng = 136; (*output line length*)
linelimit = 200;
stacksize = 1500;
type symbol = (intcon,realcon,charcon,string,
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,functionsy,
proceduresy,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;
object = (konstant,variable,type1,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 sy: symbol; (*last symbol read by insymbol*)
id: alfa; (*identifier from insymbol*)
inum: integer; (*integer from insymbol*)
rnum: real; (*real number from insymbol*)
sleng: integer; (*string length*)
ch: char; (*last character read from source program*)
line: array [1..llng] of char;
cc: integer; (*character counter*)
lc: integer; (*program location counter*)
ll: integer; (*length of current line*)
errs: set of 0..ermax;
errpos: integer;
progname: alfa;
iflag, oflag: boolean;
constbegsys,typebegsys,blockbegsys,facbegsys,statbegsys: symset;
key: array [1..nkw] of alfa;
ksy: array [1..nkw] of symbol;
spa: array [char] of symbol; (*special symbols*)
t,a,b,sx,c1,c2: integer; (*indices b tables*)
stantyps: typset;
display: array [0 .. lmax] of integer;
tab: array [0 .. tmax] of (*identifier table*)
packed record
name: alfa; link: index;
obj: object; typ: types;
ref: index; normal: boolean;
lev: 0 .. lmax; adr: integer;
end ;
atab: array [0 .. amax] of (*array-table*)
packed record
indtyp, eltyp: types;
elref, low, high, elsize, size: index;
end ;
btab: array [0 .. 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;
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] := 'typ (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] := 'type '; 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 ovf1';
msg[50] := 'constant '; msg[51] := ':= ';
msg[52] := 'then '; msg[53] := 'until ';
msg[54] := 'do '; msg[55] := 'to downto ';
msg[56] := 'begin '; msg[57] := 'end ';
k := 0; writeln; writeln(' key words');
while errs <> [] do
begin while not (k in errs) do k := k+1;
writeln(k,' ',msg[k]); errs := errs - [k]
end
end (*errormsg*) ;
procedure nextch; (*read next character; process line end*)
begin if cc = ll then
begin if eos(input) then
begin writeln;
writeln(' program incomplete');
errormsg; goto 99
end ;
if errpos <> 0 then
begin writeln; errpos := 0
end ;
write(lc:5, ' ');
ll := 0; cc := 0;
while not eo1n(input) do
begin ll := ll+1; read(ch); write(ch); line[ll] := ch
end ;
writeln; ll := ll+1: read(1ine[ll])
end ;
cc := cc+1; ch := 1ine[cc];
end (*nextch*) ;
procedure error(n: integer);
begin if errpos = 0 then write(' ****');
if cc > errpos then
begin write(' ': cc-errpos, '^', n:2);
errpos := cc+3; errs := errs + [n]
end
end (*error*) ;
procedure fatal(n: integer);
var msg: array [1..7] of a1fa;
begin writeln; errormsg;
msg[ 1] := 'identifier'; msg[ 2] := 'procedures';
msg[ 3] := 'reals '; msg[ 4] := 'arrays ';
msg[ 5] := 'levels '; msg[ 6] := 'code ';
msg[ 7] := 'strings ';
write1n(' compiler table for ', msg[n], ' is too small');
goto 99 (* 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; siqn := -1 end ;
while ch in ['0'..'9'] do
begin s := 10*s + ord(ch) - ord('0'); nextch
end ;
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*) ;
begin (*insymbol*)
1: while ch = ' ' do nextch;
if ch in ['a'..'z'] then
begin (*word*) k := 0; id := ' ';
repeat if k < alng then
begin k := k+1; id[k] := ch
end ;
nextch
until not (ch in ['a'..'z','0'..'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 else
if ch in ['0'..'9'] then
begin (*number*) k := 0; inum := 0; sy := intcon;
repeat inum := inum*10 + ord(ch) - ord('0');
k := k+1; nextch
until not (ch in ['0'..'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 in ['0'..'9'] do
begin e := e-1;
rnum := 10.0*rnum + (ord(ch)-ord('0')); nextch
end ;
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 else
case ch of
':' : 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
'''': 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 := string; inum := sx; sleng := k; sx := sx+k
end
end ;
'(' : begin nextch;
if ch <> '*' then sy := lparent else
begin (*comment*) nextch;
repeat
while ch <> '*' do nextch;
nextch
until ch = ')';
nextch; goto 1
end
end ;
'+', '-', '*', '/', ')', '=', ',', '[', ']', '#', '&', ':' :
begin sy := sps[ch]; nextch
'$', '%', '@', '\', '~', '{', '}', '^' :
begin error(24); nextch; goto 1
end
end
end (*insymbol*) ;
procedure enter(x0: alfa; x1: object;
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 fata1(4) else
begin a := a+1;
with atab[a] do
begin inxtyp := tp; low := 1; 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;
begin
writeln('0identifiers link obj typ ref nrm lev adr');
for i := btab[1].last +1 to t do
with tab[i] do
writeln(i,' ',name,link:5, ord(obj):5, ord(typ):5, ref:5,
ord(normal):5, lev:5, adr:5);
writeln('0blocks last lpar psze vsze');
for i := 1 to b do
with btab[i] do
writeln(i, last:5, lastpar:5, psize:5, vsize:5);
writeln('0arrays xtyp etyp eref low high elsz size');
for i := 1 to a do
with atab[i] do
writeln(i, ord(inxtyp):5, ord(eltyp):5,
elref:5, low:5, high:5, elsize:5, size:5);
writeln('0code:');
for i := 0 to lc-1 do
begin if i mod 5 = 0 then
begin writeln; write(i:5)
end ;
o := code[i]; write(o.f:5);
if o.f < 31 then
if o.f < 4 then write(o.x:2, o.y:5)
else write(o.y:7)
else write(' ');
write(',')
end ;
writeln
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);
while not (sy in fsys) do insymbol
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: object);
var j,l: integer;
begin if t = tmax then fatal(1) else
begin tab[0].name := id;
j := btab[display[level]].last; 1 := 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
end ;
btab[display[level]].last := t
end
end
end (*enter*) ;
function loc(id: alfa): integer;
var i,j: integer; (*locate id 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,variable); 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 x: integer;
eltp: types; elrf: intager;
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 <> type1 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 sy <> 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 ;
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 ;
btab[rf].vsize := offset; sz := offset;
btab[rf].psize := 0; insymbol; level := level-1
end ;
test(fsys, [], 6)
end
end (*typ*) ;
procedure parameterlist; (*formal parameter list*)
var tp: types;
rf, sz, x, t0: integer;
valpar: boolean;
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
end ;
t0 := t; entervariable;
while sy = comma do
begin insymbol; entervariable;
end ;
if sy = colon then
begin insymbol;
if sy <> ident then error(2) else
begin x := loc(id); insymbol;
if x <> 0 then
with tab[x] do
if obj <> type1 then error(29) else
begin tp := typ; rf := ref;
if valpar then sz := adr else sz := 1
end ;
end ;
test([semicolon,rparent], [comma,ident]+fsys, 14)
end
else error(5);
while t0 < t do
begin t0 := t0+1;
with tab[t0] do
begin typ := tp; ref := rf;
normal := valpar; adr := dx; lev := level;
dx := dx + sz
end
end ;
if sy <> rparent then
begin if sy = semicolon then insymbol else
begin error(14);
if sy = comma then insymbol
end ;
test([ident,varsy], [rparent]+fsys, 6)
end
end (*while*) ;
if sy = rparent then
begin insymbol;
test([semicolon,colon], fsys, 6)
end
else error(4)
end (*parameterlist*) ;
procedure constantdeclaration;
var c: conrec;
begin insymbol;
test([ident], blockbegsys, 2);
while sy = ident do
begin enter(id,konstant); insymbol;
if sy = eql then insymbol else
begin error(16);
if sy = becomes then insymbol
end ;
constant([semicolon,comma,ident]+fsys,c);
tab[t].typ := c.tp; tab[t].ref := 0;
if c.tp = reals then
begin enterreal(c.r); tab[t].adr := c1 end
else tab[t].adr := c.i;
testsemicolon
end
end (*constantdeclaration*) ;
procedure typedeclaration;
var tp: types; rf, sz, t1: integer;
begin insymbol;
test([ident], blockbegsys, 2);
while sy = ident do
begin enter(id,type1); t1 := t; insymbol;
if sy = eql then insymbol else
begin error(16);
if sy = becomes then insymbol
end ;
typ([semicolon,comma,ident]+fsys, tp, rf, sz);
with tab[t1] do
begin typ := tp; ref := rf; adr := sz
end ;
testsemicolon
end
end (*typedeclaration*) ;
procedure variabledeclaration;
var t0, t1, rf, sz: integer;
tp: types;
begin insymbol;
while sy = ident do
begin t0 := t; entervariable;
while sy = comma do
begin insymbol; entervariable;
end ;
if sy = colon then insymbol else error(5);
t1 := t;
typ([semicolon, comma, ident]+fsys, tp, rf, sz);
while t0 < t1 do
begin t0 := t0+1;
with tab[t0] do
begin typ := tp; ref := rf;
lev := level; adr := dx; normal := true;
dx := dx + sz
end
end ;
testsemicolon
end
end (*variabledeclaration*) ;
procedure procdeclaration;
var isfun: boolean;
begin isfun := sy = functionsy; insymbol;
if sy <> ident then
begin error(2); id := ' '
end ;
if isfun then enter(id,funktion) else enter(id,prozedure);
tab[t].normal := true;
insymbol; block([semicolon]+fsys, isfun, level+1);
if sy = semicolon then insymbol else error(14);
emit(32+ord(isfun)) (*exit*)
end (*proceduredeclaration*) ;
procedure statement(fsys: symset);
var i: integer; x: item;
procedure expression(fsys: symset; var x: item); forward;
procedure selector(fsys: symset; var v:item);
var x: item; a,j: integer;
begin (*sy in [lparent, lbrack, period]*)
repeat
if sy = period then
begin insymbol; (*field selector*)
if sy <> ident then error(2) else
begin
if v.typ <> records then error(31) else
begin (*search field identifier*)
j := btab[v.ref] .last; tab[0].name := id;
while tab[j].name <> id do j := tab[j].link;
if j = 0 then error(0);
v.typ := tab[j].typ; v.ref := tab[j].ref;
a := tab[j].adr; if a <> 0 then emit1(9,a)
end ;
insymbol
end
end else
begin (*array selector*)
if sy <> lbrack then error(11);
repeat insymbol;
expression(sys+[comma,rbrack], x);
if v.typ <> arrays then error(28) else
begin a := v.ref;
if atab[a].inxtyp <> x.typ then error(26) else
if atab[a].elsize = 1 then emit1(20,a)
else emit1(21,a);
v.typ := atab[a].eltyp: v.ref := atab[a].elref
end
until sy <> comma;
if sy = rbrack then insymbol else
begin error(12); if sy = rparent then insymbol
end
end
until not (sy in [lbrack,lparent,period]);
test(fsys, [], 6)
end (*selector*) ;
procedure call(fsys: symset; i: integer);
var x: item;
lastp, cp, k: integer;
begin emit1(18,i); (*mark stack*)
lastp := btab[tab[i].ref].lastpar; cp := i;
if sy = lparent then
begin (*actual parameter list*)
repeat insymbol;
if cp >= lastp then error(39) else
begin cp := cp+1;
if tab[cp].normal then
begin (*value parameter*)
expression(fsys+[comma,colon,rparent], x);
if x.typ=tab[cp].typ then
begin
if x.ref <> tab[cp].ref then error(36) else
if x.typ = arrays then emit1(22,atab[x.ref].size) else
if x.typ = records then emit1(22,btab[x.ref].vsize)
end else
if (x.typ=ints) and (tab[cp].typ=reals) then
emit1(26,0) else
if x.typ<>notyp then error(36);
end else
begin (*variable parameter*)
if sy <> ident then error(2) else
begin k := loc(id); insymbol;
if k <> 0 then
begin if tab[k].obj <> variable then error(37);
x.typ := tab[k].typ; x.ref := tab[k].ref;
if tab[k].normal
then emit2(0,tab[k].lev,tab[k].adr)
else emit2(1,tab[k].lev,tab[k].adr);
if sy in [lbrack,lparent,period] then
selector(fsys+[comma,colon,rparent], x);
if (x.typ<>tab[cp].typ) or (x.ref<>tab[cp].ref)
then error(36)
end
end
end
end ;
test([comma,rparent], fsys, 6)
until sy <> comma;
if sy = rparent then insymbol else error(4)
end ;
if cp < lastp then error(39); (*too few actual parameters*)
emit1(19, btab[tab[i].ref].psize-1);
if tab[i].lev < level then emit2(3, tab[i].lev, level)
end (*call*) ;
function resulttype(a,b: types): types;
begin
if (a>reals) or (b>reals) then
begin error(33); resulttype := notyp
end else
if (a=notyp) or (b=notyp) then resulttype := notyp else
if a=ints then
if b=ints then resulttype := ints else
begin resulttype := reals; emit1(26,1)
end
else
begin resulttype := reals;
if b=ints then emit1(26,0)
end
end (*resulttype*) ;
procedure expression;
var y:item; op:symbol;
procedure simpleexpression(fsys:symset; var x:item);
var y:item; op:symbol;
procedure term(fsys:symset; var x:item);
var y:item; op:symbol; ts:typset;
procedure factor(fsys:symset; var x:item);
var i,f: integer;
procedure standfct(n: integer);
var ts: typset;
begin (*standard function no. n*)
if sy = lparent then insymbol else error(9);
if n < 17 then
begin expression(fsys+[rparent],x);
case n of
(*abs,sqr*) 0,2: begin ts := [ints,reals];
tab[i].typ := x.typ;
if x.typ = reals then n := n+1
end ;
(*odd,chr*) 4,5: ts := [ints];
(*ord*) 6: ts := [ints,bools,chars];
(*succ,pred*) 7,8: ts := [chars];
(*round,trunc*) 9,10,11,12,13,14,15,16:
(*sin,cos,...*) begin ts := [ints,reals];
if x.typ = ints then emit1(26,0)
end ;
end ;
if x.typ in ts then emit1(8,n) else
if x.typ <> notyp then error(48);
end else
(*eof,eoln*) begin (*n in [17,18]*)
if sy <> ident then error(2) else
if id <> 'input ' then error(0) else insymbol;
emit1(8,n);
end ;
x.typ := tab[i].typ;
if sy = rparent then insymbol else error(4)
end (*standfct*) ;
begin (*factor*) x.typ := notyp; x.ref := 0;
test(facbegsys, fsys, 58);
while sy in facbegsys do
begin
if sy = ident then
begin i := loc(id); insymbol;
with tab[i] do
case obj of
konstant: begin x.typ := typ; x.ref := 0;
if x.typ = reals then
emit1(25,adr) else
emit1(24,adr)
end ;
variable: begin x.typ := typ; x.ref := ref;
if sy [lbrack,lparent,period] then
begin if normal then f := 0 else f := 1;
emit2(f, lev, adr);
selector(fsys,x);
if x.typ in stantyps then emit(34)
end else
begin
if x.typ in stantyps then
if normal then f := 1 else f := 2
else
if normal then f := 0 else f := 1;
emit2(f, lev, adr)
end
end ;
type1, prozedure: error(44);
funktion :begin x.typ := typ;
if lev <> 0 then call(fsys, i)
else standfct(adr)
end
end (*case,with*)
end else
if sy in [charcon,intcon,realcon] then
begin
if sy = realcon then
begin x.typ := reals; enterreal(rnum);
emit1(25, c1)
end else
begin if sy = charcon then x.typ := chars
else x.typ := ints;
emit1(24, inum)
end ;
x.ref := 0; insymbol
end else
if sy = lparent then
begin insymbol; expression(fsys+[rparent], x);
if sy = rparent then insymbol else error(4)
end else
if sy = notsy then
begin insymbol; factor(fsys,x);
if x.typ=bools then emit(35) else
if x.typ<>notyp then error(32)
end ;
test(fsys, facbegsys, 6)
end (*while*)
end (*factor*) ;
begin (*term*)
factor(fsys+[times,rdiv,idiv,imod,andsy], x);
while sy in [times,rdiv,idiv.imod,andsy] do
begin op := sy; insymbol;
factor(fsys+[times,rdiv,idiv,imod,andsy), y);
if op = times then
begin x.typ := resulttype(x.typ, y.typ);
case x.typ of
notyp: ;
ints : emit(57);
reals: emit(60);
end
end else
if op = rdiv then
begin
if x.typ = ints then
begin emit1(26,1); x.typ := reals
end ;
if y.typ = ints then
begin emit1(26,0); y.typ := reals
end ;
if (x.typ=reals) and (y.typ=reals) then
emit(61) else
begin if (x.typ<>notyp) and (y.typ<>notyp) then
error(33);
x.typ := notyp
end
end else
if op = andsy then
begin if (x.typ=bools) and (y.typ=bools) then
emit(56) else
begin if (x.typ<>notyp) and (y.typ<>notyp)
then error(32);
x.typ := notyp
end
end else
begin (*op in [idiv, imod]*)
if (x.typ=ints) and (y.typ=ints) then
if op=idiv then emit(58)
else emit(59) else
begin if (x.typ<>notyp) and (y.typ<>notyp) then
error(34);
x.typ := notyp
end
end
end
end (*term*) ;
begin (*simpleexpression*)
if sy in [plus,minus] then
begin op := sy; insymbol;
term(fsys+[plus,minus], x);
if x.typ > reals then error(33) else
if op = minus then emit(36)
end else
term(fsys+[plus,minus,orsy], x);
while sy in [plus,minus,orsy] do
begin op := sy; insymbol;
term(fsys+[plus,minus,orsy], y);
if op = orsy then
begin
if (x.typ=bools) and (y.typ=bools) then emit(51) else
begin if (x.typ<>notyp) and (y.typ<>notyp) then
error(32);
x.typ := notyp
end
end else
begin typ := resulttype(x.typ, y.typ);
case x.typ of
notyp: ;
ints : if op = plus then emit(52)
else emit(53);
reals: if op = plus then emit(54)
else emit(55)
end
end
end