-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp.p
4018 lines (3823 loc) · 115 KB
/
comp.p
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
(*$c+,t-,d-,l+*)
(**********************************************
* *
* *
* portable pascal compiler *
* ************************ *
* *
* pascal p4 *
* *
* *
* authors: *
* urs ammann *
* kesav nori *
* christian jacobi *
* *
* address: *
* *
* institut fuer informatik *
* eidg. technische hochschule *
* ch-8096 zuerich *
* *
* *
* last changes completed in may 76 *
* *
* *
**********************************************)
program pascalcompiler(input,output,prr);
const displimit = 20; maxlevel = 10;
intsize = 1;
intal = 1;
realsize = 2;
realal = 1;
charsize = 1;
charal = 1;
charmax = 1;
boolsize = 1;
boolal = 1;
ptrsize = 2;
adral = 1;
setsize = 4;
setal = 1;
stackelsize = 1;
stackal = 1;
strglgth = 24;
sethigh = 63; setlow = 0;
ordmaxchar = 127; ordminchar = 0;
lcaftermarkstack = 10;
maxint = 32767;
fileal = charal;
(* stackelsize = minimum size for 1 stackelement
= k*stackal
stackal = scm(all other al-constants)
charmax = scm(charsize,charal)
scm = smallest common multiple
lcaftermarkstack >= 4*ptrsize+max(x-size)
= k1*stackelsize *)
maxstack = 1;
parmal = stackal;
parmsize = stackelsize;
recal = stackal;
filebuffer = 4;
maxaddr = maxint;
type (*describing:*)
(*************)
(*basic symbols*)
(***************)
symbol = (ident,intconst,realconst,stringconst,notsy,mulop,addop,relop,
lparent,rparent,lbrack,rbrack,comma,semicolon,period,arrow,
colon,becomes,labelsy,constsy,typesy,varsy,funcsy,progsy,
procsy,setsy,packedsy,arraysy,recordsy,filesy,forwardsy,
beginsy,ifsy,casesy,repeatsy,whilesy,forsy,withsy,
gotosy,endsy,elsesy,untilsy,ofsy,dosy,tosy,downtosy,
thensy,othersy);
operator = (mul,rdiv,andop,idiv,imod,plus,minus,orop,ltop,leop,geop,gtop,
neop,eqop,inop,noop);
setofsys = set of symbol;
setlohi = set of setlow..sethigh;
chtp = (letter,number,special,illegal);
(*constants*)
(***********)
cstclass = (reel,pset,strg);
csp = ^ constant;
constant = record case cclass: cstclass of
reel: (rval: packed array [1..strglgth] of char);
pset: (pval: setlohi);
strg: (slgth: 0..strglgth;
sval: packed array [1..strglgth] of char)
end;
valu = record case intval: boolean of (*intval never set nore tested*)
true: (ival: integer);
false: (valp: csp)
end;
(*data structures*)
(*****************)
levrange = 0..maxlevel; addrrange = 0..maxaddr;
structform = (scalar,subrange,pointer,power,arrays,records,files,
tagfld,variant);
declkind = (standard,declared);
stp = ^ structure; ctp = ^ identifier;
structure = packed record
marked: boolean; (*for test phase only*)
size: addrrange;
case form: structform of
scalar: (case scalkind: declkind of
standard, (* to make compiler happy *)
declared: (fconst: ctp));
subrange: (rangetype: stp; min,max: valu);
pointer: (eltype: stp);
power: (elset: stp);
arrays: (aeltype,inxtype: stp);
records: (fstfld: ctp; recvar: stp);
files: (filtype: stp);
tagfld: (tagfieldp: ctp; fstvar: stp);
variant: (nxtvar,subvar: stp; varval: valu)
end;
(*names*)
(*******)
idclass = (types,konst,vars,field,proc,func);
setofids = set of idclass;
idkind = (actual,formal);
alpha = packed array [1..8] of char;
identifier = packed record
name: alpha; llink, rlink: ctp;
idtype: stp; next: ctp;
case klass: idclass of
types, (* to make compiler happy *)
konst: (values: valu);
vars: (vkind: idkind; vlev: levrange; vaddr: addrrange);
field: (fldaddr: addrrange);
proc,
func: (case pfdeckind: declkind of
standard: (key: 1..15);
declared: (pflev: levrange; pfname: integer;
case pfkind: idkind of
formal, (* to make compiler happy *)
actual: (forwdecl, extern:
boolean)))
end;
disprange = 0..displimit;
where = (blck,crec,vrec,rec);
(*expressions*)
(*************)
attrkind = (cst,varbl,expr);
vaccess = (drct,indrct,inxd);
attr = record typtr: stp;
case kind: attrkind of
cst: (cval: valu);
varbl: (case access: vaccess of
drct: (vlevel: levrange; dplmt: addrrange);
indrct: (idplmt: addrrange))
end;
testp = ^ testpointer;
testpointer = packed record
elt1,elt2 : stp;
lasttestp : testp
end;
(*labels*)
(********)
lbp = ^ labl;
labl = record nextlab: lbp; defined: boolean;
labval, labname: integer
end;
extfilep = ^filerec;
filerec = record filename:alpha; nextfile:extfilep end;
(*-------------------------------------------------------------------------*)
var
(*returned by source program scanner
insymbol:
**********)
sy: symbol; (*last symbol*)
op: operator; (*classification of last symbol*)
val: valu; (*value of last constant*)
lgth: integer; (*length of last string constant*)
id: alpha; (*last identifier (possibly truncated)*)
kk: 1..8; (*nr of chars in last identifier*)
ch: char; (*last character*)
eol: boolean; (*end of line flag*)
(*counters:*)
(***********)
chcnt: integer; (*character counter*)
lc,ic: addrrange; (*data location and instruction counter*)
linecount: integer;
(*switches:*)
(***********)
dp, (*declaration part*)
prterr, (*to allow forward references in pointer type
declaration by suppressing error message*)
list,prcode,prtables: boolean; (*output options for
-- source program listing
-- printing symbolic code
-- displaying ident and struct tables
--> procedure option*)
debug: boolean;
(*pointers:*)
(***********)
parmptr,
intptr,realptr,charptr,
boolptr,nilptr,textptr: stp; (*pointers to entries of standard ids*)
utypptr,ucstptr,uvarptr,
ufldptr,uprcptr,ufctptr, (*pointers to entries for undeclared ids*)
fwptr: ctp; (*head of chain of forw decl type ids*)
fextfilep: extfilep; (*head of chain of external files*)
globtestp: testp; (*last testpointer*)
(*bookkeeping of declaration levels:*)
(************************************)
level: levrange; (*current static level*)
disx, (*level of last id searched by searchid*)
top: disprange; (*top of display*)
display: (*where: means:*)
array [disprange] of
packed record (*=blck: id is variable id*)
fname: ctp; flabel: lbp; (*=crec: id is field id in record with*)
case occur: where of (* constant address*)
crec: (clev: levrange; (*=vrec: id is field id in record with*)
cdspl: addrrange);(* variable address*)
vrec: (vdspl: addrrange)
end; (* --> procedure withstatement*)
(*error messages:*)
(*****************)
errinx: 0..10; (*nr of errors in current source line*)
errlist:
array [1..10] of
packed record pos: integer;
nmr: 1..400
end;
(*expression compilation:*)
(*************************)
gattr: attr; (*describes the expr currently compiled*)
(*structured constants:*)
(***********************)
constbegsys,simptypebegsys,typebegsys,blockbegsys,selectsys,facbegsys,
statbegsys,typedels: setofsys;
chartp : array[char] of chtp;
rw: array [1..35(*nr. of res. words*)] of alpha;
frw: array [1..9] of 1..36(*nr. of res. words + 1*);
rsy: array [1..35(*nr. of res. words*)] of symbol;
ssy: array [char] of symbol;
rop: array [1..35(*nr. of res. words*)] of operator;
sop: array [char] of operator;
na: array [1..35] of alpha;
mn: array[0..60] of packed array[1..4] of char;
sna: array [1..23] of packed array [1..4] of char;
cdx: array[0..60] of -4..+4;
pdx: array[1..23] of -7..+7;
ordint: array[char] of integer;
intlabel,mxint10,digmax: integer;
(*-------------------------------------------------------------------------*)
procedure endofline;
var lastpos,freepos,currpos,currnmr,f,k: integer;
begin
if errinx > 0 then (*output error messages*)
begin write(output,' **** ':15);
lastpos := 0; freepos := 1;
for k := 1 to errinx do
begin
with errlist[k] do
begin currpos := pos; currnmr := nmr end;
if currpos = lastpos then write(output,',')
else
begin
while freepos < currpos do
begin write(output,' '); freepos := freepos + 1 end;
write(output,'^');
lastpos := currpos
end;
if currnmr < 10 then f := 1
else if currnmr < 100 then f := 2
else f := 3;
write(output,currnmr:f);
freepos := freepos + f + 1
end;
writeln(output); errinx := 0
end;
if list and (not eof(input)) then
begin linecount := linecount + 1; write(output,linecount:6,' ':2);
if dp then write(output,lc:7) else write(output,ic:7);
write(output,' ')
end;
chcnt := 0
end (*endofline*) ;
procedure error(ferrnr: integer);
begin
if errinx >= 9 then
begin errlist[10].nmr := 255; errinx := 10 end
else
begin errinx := errinx + 1;
errlist[errinx].nmr := ferrnr
end;
errlist[errinx].pos := chcnt
end (*error*) ;
procedure insymbol;
(*read next basic symbol of source program and return its
description in the global variables sy, op, id, val and lgth*)
label 1,2,3;
var i,k: integer;
digit: packed array [1..strglgth] of char;
string: packed array [1..strglgth] of char;
lvp: csp;test: boolean;
procedure nextch;
begin if eol then
begin if list then writeln(output); endofline
end;
if not eof(input) then
begin eol := eoln(input); read(input,ch);
if list then write(output,ch);
chcnt := chcnt + 1
end
else
begin writeln(output,' *** eof ','encountered');
test := false
end
end;
procedure options;
begin
repeat nextch;
if ch <> '*' then
begin
if ch = 't' then
begin nextch; prtables := ch = '+' end
else
if ch = 'l' then
begin nextch; list := ch = '+';
if not list then writeln(output)
end
else
if ch = 'd' then
begin nextch; debug := ch = '+' end
else
if ch = 'c' then
begin nextch; prcode := ch = '+' end;
nextch
end
until ch <> ','
end (*options*) ;
begin (*insymbol*)
1:
repeat while (ch = ' ') and not eol do nextch;
test := eol;
if test then nextch
until not test;
if chartp[ch] = illegal then
begin sy := othersy; op := noop;
error(399); nextch
end
else
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 k := 0;
repeat
if k < 8 then
begin k := k + 1; id[k] := ch end ;
nextch
until chartp[ch] in [special,illegal];
if k >= kk then kk := k
else
repeat id[kk] := ' '; kk := kk - 1
until kk = k;
for i := frw[k] to frw[k+1] - 1 do
if rw[i] = id then
begin sy := rsy[i]; op := rop[i]; goto 2 end;
sy := ident; op := noop;
2: end;
'0','1','2','3','4','5','6','7','8','9':
begin op := noop; i := 0;
repeat i := i+1; if i<= digmax then digit[i] := ch; nextch
until chartp[ch] <> number;
if (ch = '.') or (ch = 'e') then
begin
k := i;
if ch = '.' then
begin k := k+1; if k <= digmax then digit[k] := ch;
nextch; if ch = '.' then begin ch := ':'; goto 3 end;
if chartp[ch] <> number then error(201)
else
repeat k := k + 1;
if k <= digmax then digit[k] := ch; nextch
until chartp[ch] <> number
end;
if ch = 'e' then
begin k := k+1; if k <= digmax then digit[k] := ch;
nextch;
if (ch = '+') or (ch ='-') then
begin k := k+1; if k <= digmax then digit[k] := ch;
nextch
end;
if chartp[ch] <> number then error(201)
else
repeat k := k+1;
if k <= digmax then digit[k] := ch; nextch
until chartp[ch] <> number
end;
new(lvp,reel); sy:= realconst; lvp^.cclass := reel;
with lvp^ do
begin for i := 1 to strglgth do rval[i] := ' ';
if k <= digmax then
for i := 2 to k + 1 do rval[i] := digit[i-1]
else begin error(203); rval[2] := '0';
rval[3] := '.'; rval[4] := '0'
end
end;
val.valp := lvp
end
else
3: begin
if i > digmax then begin error(203); val.ival := 0 end
else
with val do
begin ival := 0;
for k := 1 to i do
begin
if ival <= mxint10 then
ival := ival*10+ordint[digit[k]]
else begin error(203); ival := 0 end
end;
sy := intconst
end
end
end;
'''':
begin lgth := 0; sy := stringconst; op := noop;
repeat
repeat nextch; lgth := lgth + 1;
if lgth <= strglgth then string[lgth] := ch
until (eol) or (ch = '''');
if eol then error(202) else nextch
until ch <> '''';
lgth := lgth - 1; (*now lgth = nr of chars in string*)
if lgth = 0 then error(205)
else
if lgth = 1 then val.ival := ord(string[1])
else
begin new(lvp,strg); lvp^.cclass:=strg;
if lgth > strglgth then
begin error(399); lgth := strglgth end;
with lvp^ do
begin slgth := lgth;
for i := 1 to lgth do sval[i] := string[i]
end;
val.valp := lvp
end
end;
':':
begin op := noop; nextch;
if ch = '=' then
begin sy := becomes; nextch end
else sy := colon
end;
'.':
begin op := noop; nextch;
if ch = '.' then
begin sy := colon; nextch end
else sy := period
end;
'<':
begin nextch; sy := relop;
if ch = '=' then
begin op := leop; nextch end
else
if ch = '>' then
begin op := neop; nextch end
else op := ltop
end;
'>':
begin nextch; sy := relop;
if ch = '=' then
begin op := geop; nextch end
else op := gtop
end;
'(':
begin nextch;
if ch = '*' then
begin nextch;
if ch = '$' then options;
repeat
while (ch <> '*') and not eof(input) do nextch;
nextch
until (ch = ')') or (eof(input));
nextch; goto 1
end;
sy := lparent; op := noop
end;
'*','+','-',
'=','/',')',
'[',']',',',';','^','$':
begin sy := ssy[ch]; op := sop[ch];
nextch
end;
' ': sy := othersy
end (*case*)
end (*insymbol*) ;
procedure enterid(fcp: ctp);
(*enter id pointed at by fcp into the name-table,
which on each declaration level is organised as
an unbalanced binary tree*)
var nam: alpha; lcp, lcp1: ctp; lleft: boolean;
begin nam := fcp^.name;
lcp := display[top].fname;
if lcp = nil then
display[top].fname := fcp
else
begin
repeat lcp1 := lcp;
if lcp^.name = nam then (*name conflict, follow right link*)
begin error(101); lcp := lcp^.rlink; lleft := false end
else
if lcp^.name < nam then
begin lcp := lcp^.rlink; lleft := false end
else begin lcp := lcp^.llink; lleft := true end
until lcp = nil;
if lleft then lcp1^.llink := fcp else lcp1^.rlink := fcp
end;
fcp^.llink := nil; fcp^.rlink := nil
end (*enterid*) ;
procedure searchsection(fcp: ctp; var fcp1: ctp);
(*to find record fields and forward declared procedure id's
--> procedure proceduredeclaration
--> procedure selector*)
label 1;
begin
while fcp <> nil do
if fcp^.name = id then goto 1
else if fcp^.name < id then fcp := fcp^.rlink
else fcp := fcp^.llink;
1: fcp1 := fcp
end (*searchsection*) ;
procedure searchid(fidcls: setofids; var fcp: ctp);
label 1;
var lcp: ctp;
begin
for disx := top downto 0 do
begin lcp := display[disx].fname;
while lcp <> nil do
if lcp^.name = id then
if lcp^.klass in fidcls then goto 1
else
begin if prterr then error(103);
lcp := lcp^.rlink
end
else
if lcp^.name < id then
lcp := lcp^.rlink
else lcp := lcp^.llink
end;
(*search not succsessful; suppress error message in case
of forward referenced type id in pointer type definition
--> procedure simpletype*)
if prterr then
begin error(104);
(*to avoid returning nil, reference an entry
for an undeclared id of appropriate class
--> procedure enterundecl*)
if types in fidcls then lcp := utypptr
else
if vars in fidcls then lcp := uvarptr
else
if field in fidcls then lcp := ufldptr
else
if konst in fidcls then lcp := ucstptr
else
if proc in fidcls then lcp := uprcptr
else lcp := ufctptr;
end;
1: fcp := lcp
end (*searchid*) ;
procedure getbounds(fsp: stp; var fmin,fmax: integer);
(*get internal bounds of subrange or scalar type*)
(*assume fsp<>intptr and fsp<>realptr*)
begin
fmin := 0; fmax := 0;
if fsp <> nil then
with fsp^ do
if form = subrange then
begin fmin := min.ival; fmax := max.ival end
else
if fsp = charptr then
begin fmin := ordminchar; fmax := ordmaxchar
end
else
if fconst <> nil then
fmax := fconst^.values.ival
end (*getbounds*) ;
function alignquot(fsp: stp): integer;
begin
alignquot := 1;
if fsp <> nil then
with fsp^ do
case form of
scalar: if fsp=intptr then alignquot := intal
else if fsp=boolptr then alignquot := boolal
else if scalkind=declared then alignquot := intal
else if fsp=charptr then alignquot := charal
else if fsp=realptr then alignquot := realal
else (*parmptr*) alignquot := parmal;
subrange: alignquot := alignquot(rangetype);
pointer: alignquot := adral;
power: alignquot := setal;
files: alignquot := fileal;
arrays: alignquot := alignquot(aeltype);
records: alignquot := recal;
variant,tagfld: error(501)
end
end (*alignquot*);
procedure align(fsp: stp; var flc: addrrange);
var k,l: integer;
begin
k := alignquot(fsp);
l := flc-1;
flc := l + k -(k + l) mod k
end (*align*);
procedure printtables(fb: boolean);
(*print data structure and name table*)
var i, lim: disprange;
procedure marker;
(*mark data structure entries to avoid multiple printout*)
var i: integer;
procedure markctp(fp: ctp); forward;
procedure markstp(fp: stp);
(*mark data structures, prevent cycles*)
begin
if fp <> nil then
with fp^ do
begin marked := true;
case form of
scalar: ;
subrange: markstp(rangetype);
pointer: (*don't mark eltype: cycle possible; will be marked
anyway, if fp = true*) ;
power: markstp(elset) ;
arrays: begin markstp(aeltype); markstp(inxtype) end;
records: begin markctp(fstfld); markstp(recvar) end;
files: markstp(filtype);
tagfld: markstp(fstvar);
variant: begin markstp(nxtvar); markstp(subvar) end
end (*case*)
end (*with*)
end (*markstp*);
procedure markctp;
begin
if fp <> nil then
with fp^ do
begin markctp(llink); markctp(rlink);
markstp(idtype)
end
end (*markctp*);
begin (*mark*)
for i := top downto lim do
markctp(display[i].fname)
end (*mark*);
procedure followctp(fp: ctp); forward;
procedure followstp(fp: stp);
begin
if fp <> nil then
with fp^ do
if marked then
begin marked := false; write(output,' ':4,ord(fp):6,size:10);
case form of
scalar: begin write(output,'scalar':10);
if scalkind = standard then
write(output,'standard':10)
else write(output,'declared':10,' ':4,ord(fconst):6);
writeln(output)
end;
subrange:begin
write(output,'subrange':10,' ':4,ord(rangetype):6);
if rangetype <> realptr then
write(output,min.ival,max.ival)
else
if (min.valp <> nil) and (max.valp <> nil) then
write(output,' ',min.valp^.rval:9,
' ',max.valp^.rval:9);
writeln(output); followstp(rangetype);
end;
pointer: writeln(output,'pointer':10,' ':4,ord(eltype):6);
power: begin writeln(output,'set':10,' ':4,ord(elset):6);
followstp(elset)
end;
arrays: begin
writeln(output,'array':10,' ':4,ord(aeltype):6,' ':4,
ord(inxtype):6);
followstp(aeltype); followstp(inxtype)
end;
records: begin
writeln(output,'record':10,' ':4,ord(fstfld):6,' ':4,
ord(recvar):6); followctp(fstfld);
followstp(recvar)
end;
files: begin write(output,'file':10,' ':4,ord(filtype):6);
followstp(filtype)
end;
tagfld: begin writeln(output,'tagfld':10,' ':4,ord(tagfieldp):6,
' ':4,ord(fstvar):6);
followstp(fstvar)
end;
variant: begin writeln(output,'variant':10,' ':4,ord(nxtvar):6,
' ':4,ord(subvar):6,varval.ival);
followstp(nxtvar); followstp(subvar)
end
end (*case*)
end (*if marked*)
end (*followstp*);
procedure followctp;
var i: integer;
begin
if fp <> nil then
with fp^ do
begin write(output,' ':4,ord(fp):6,' ',name:9,' ':4,ord(llink):6,
' ':4,ord(rlink):6,' ':4,ord(idtype):6);
case klass of
types: write(output,'type':10);
konst: begin write(output,'constant':10,' ':4,ord(next):6);
if idtype <> nil then
if idtype = realptr then
begin
if values.valp <> nil then
write(output,' ',values.valp^.rval:9)
end
else
if idtype^.form = arrays then (*stringconst*)
begin
if values.valp <> nil then
begin write(output,' ');
with values.valp^ do
for i := 1 to slgth do
write(output,sval[i])
end
end
else write(output,values.ival)
end;
vars: begin write(output,'variable':10);
if vkind = actual then write(output,'actual':10)
else write(output,'formal':10);
write(output,' ':4,ord(next):6,vlev,' ':4,vaddr:6 );
end;
field: write(output,'field':10,' ':4,ord(next):6,' ':4,fldaddr:6);
proc,
func: begin
if klass = proc then write(output,'procedure':10)
else write(output,'function':10);
if pfdeckind = standard then
write(output,'standard':10,
key:10)
else
begin write(output,'declared':10,' ':4,ord(next):6);
write(output,pflev,' ':4,pfname:6);
if pfkind = actual then
begin write(output,'actual':10);
if forwdecl then write(output,'forward':10)
else write(output,'notforward':10);
if extern then write(output,'extern':10)
else write(output,'not extern':10);
end
else write(output,'formal':10)
end
end
end (*case*);
writeln(output); followctp(llink); followctp(rlink);
followstp(idtype)
end (*with*)
end (*followctp*);
begin (*printtables*)
writeln(output); writeln(output); writeln(output);
if fb then lim := 0
else begin lim := top; write(output,' local') end;
writeln(output,' tables '); writeln(output);
marker;
for i := top downto lim do
followctp(display[i].fname);
writeln(output);
if not eol then write(output,' ':chcnt+16)
end (*printtables*);
procedure genlabel(var nxtlab: integer);
begin intlabel := intlabel + 1;
nxtlab := intlabel
end (*genlabel*);
procedure block(fsys: setofsys; fsy: symbol; fprocp: ctp);
var lsy: symbol; test: boolean;
procedure skip(fsys: setofsys);
(*skip input string until relevant symbol found*)
begin
if not eof(input) then
begin while not(sy in fsys) and (not eof(input)) do insymbol;
if not (sy in fsys) then insymbol
end
end (*skip*) ;
procedure constant(fsys: setofsys; var fsp: stp; var fvalu: valu);
var lsp: stp; lcp: ctp; sign: (none,pos,neg);
lvp: csp; i: 2..strglgth;
begin lsp := nil; fvalu.ival := 0;
if not(sy in constbegsys) then
begin error(50); skip(fsys+constbegsys) end;
if sy in constbegsys then
begin
if sy = stringconst then
begin
if lgth = 1 then lsp := charptr
else
begin
new(lsp,arrays);
with lsp^ do
begin aeltype := charptr; inxtype := nil;
size := lgth*charsize; form := arrays
end
end;
fvalu := val; insymbol
end
else
begin
sign := none;
if (sy = addop) and (op in [plus,minus]) then
begin if op = plus then sign := pos else sign := neg;
insymbol
end;
if sy = ident then
begin searchid([konst],lcp);
with lcp^ do
begin lsp := idtype; fvalu := values end;
if sign <> none then
if lsp = intptr then
begin if sign = neg then fvalu.ival := -fvalu.ival end
else
if lsp = realptr then
begin
if sign = neg then
begin new(lvp,reel);
if fvalu.valp^.rval[1] = '-' then
lvp^.rval[1] := '+'
else lvp^.rval[1] := '-';
for i := 2 to strglgth do
lvp^.rval[i] := fvalu.valp^.rval[i];
fvalu.valp := lvp;
end
end
else error(105);
insymbol;
end
else
if sy = intconst then
begin if sign = neg then val.ival := -val.ival;
lsp := intptr; fvalu := val; insymbol
end
else
if sy = realconst then
begin if sign = neg then val.valp^.rval[1] := '-';
lsp := realptr; fvalu := val; insymbol
end
else
begin error(106); skip(fsys) end
end;
if not (sy in fsys) then
begin error(6); skip(fsys) end
end;
fsp := lsp
end (*constant*) ;
function equalbounds(fsp1,fsp2: stp): boolean;
var lmin1,lmin2,lmax1,lmax2: integer;
begin
if (fsp1=nil) or (fsp2=nil) then equalbounds := true
else
begin
getbounds(fsp1,lmin1,lmax1);
getbounds(fsp2,lmin2,lmax2);
equalbounds := (lmin1=lmin2) and (lmax1=lmax2)
end
end (*equalbounds*) ;
function comptypes(fsp1,fsp2: stp) : boolean;
(*decide whether structures pointed at by fsp1 and fsp2 are compatible*)
var nxt1,nxt2: ctp; comp: boolean;
ltestp1,ltestp2 : testp;
begin
if fsp1 = fsp2 then comptypes := true
else
if (fsp1 <> nil) and (fsp2 <> nil) then
if fsp1^.form = fsp2^.form then
case fsp1^.form of
scalar:
comptypes := false;
(* identical scalars declared on different levels are
not recognized to be compatible*)
subrange:
comptypes := comptypes(fsp1^.rangetype,fsp2^.rangetype);
pointer:
begin
comp := false; ltestp1 := globtestp;
ltestp2 := globtestp;
while ltestp1 <> nil do
with ltestp1^ do
begin
if (elt1 = fsp1^.eltype) and
(elt2 = fsp2^.eltype) then comp := true;
ltestp1 := lasttestp
end;
if not comp then
begin new(ltestp1);
with ltestp1^ do
begin elt1 := fsp1^.eltype;
elt2 := fsp2^.eltype;
lasttestp := globtestp
end;
globtestp := ltestp1;
comp := comptypes(fsp1^.eltype,fsp2^.eltype)
end;
comptypes := comp; globtestp := ltestp2
end;
power:
comptypes := comptypes(fsp1^.elset,fsp2^.elset);
arrays:
begin
comp := comptypes(fsp1^.aeltype,fsp2^.aeltype)
and comptypes(fsp1^.inxtype,fsp2^.inxtype);
comptypes := comp and
equalbounds(fsp1^.inxtype,fsp2^.inxtype)
and (fsp1^.size = fsp2^.size)
end;