forked from octonion/superpascal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.p
4455 lines (4179 loc) · 90.5 KB
/
parse.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
{ SUPERPASCAL COMPILER
PARSER
28 October 1993
Copyright (c) 1993 Per Brinch Hansen }
procedure parse(
procedure newline(lineno: integer);
procedure get(var value: integer);
procedure put(value: integer);
procedure getreal(var value: real);
procedure putreal(value: real);
procedure getstring(
var length: integer;
var value: string);
procedure putstring(
length: integer;
value: string);
procedure putcase(
lineno, length: integer;
table: casetable);
procedure error(kind: phrase);
procedure halt(kind: phrase));
const
nameless = 0;
type
tokens = set of mintoken..maxtoken;
messagepointer = ^ messagerecord;
pointer = ^ objectrecord;
stringpointer = ^ string;
varset = ^ atom;
class =
(arraytype, channeltype, field,
funktion, ordconst, ordtype,
procedur, realconst, realtype,
recordtype, standardfile,
standardfunc, standardproc,
stringconst, valueparameter,
variable, varparameter,
undefined);
classes = set of class;
atom =
record
next: varset;
item: pointer
end;
context =
record
exprvar, targetvar: varset
end;
messagerecord =
record
previous: messagepointer;
typex: pointer;
end;
objectrecord =
record
id: integer;
previous: pointer;
case kind: class of
ordconst:
(ordvalue: integer;
ordtypex: pointer);
realconst:
(realvalue: real);
stringconst:
(stringptr: stringpointer);
ordtype:
(minvalue, maxvalue:
integer);
realtype:
( );
arraytype:
(lowerbound, upperbound,
arraylength: integer;
indextype, elementtype:
pointer);
recordtype:
(recordlength: integer;
lastfield: pointer);
channeltype:
(messagelist:
messagepointer);
field:
(fielddispl: integer;
fieldtype: pointer);
valueparameter, variable,
varparameter:
(varlevel, vardispl:
integer; vartype:
pointer);
funktion, procedur:
(proclevel, proclabel,
paramlength, varlength:
integer; lastparam,
resulttype: pointer;
recursive: boolean;
implicit: context);
standardfile, standardfunc,
standardproc, undefined:
( )
end;
blockrecord =
record
templength, maxtemp: integer;
heading, lastobject: pointer
end;
blocktable = array [0..maxlevel] of
blockrecord;
var
lineno, token, argument: integer;
realarg: real; stringarg: string;
addtokens, blocktokens,
constanttokens, declarationtokens,
doubletokens, expressiontokens,
factortokens, literaltokens,
longtokens, multiplytokens,
parametertokens, relationtokens,
routinetokens, selectortokens,
signtokens, simpleexprtokens,
statementtokens, termtokens,
unsignedtokens: tokens;
block: blocktable;
blocklevel: integer;
constants, functions, leftparts,
types, variables, parameters,
procedures: classes;
inputfile, outputfile,
typeboolean, typechar,
typeinteger, typereal,
typestring, typeuniversal:
pointer;
labelno, minint: integer;
{ FORWARD DECLARATIONS }
procedure push(length: integer);
forward;
procedure pop(length: integer);
forward;
procedure expression(
var typex: pointer;
var used: context;
restricted: boolean;
stop: tokens);
forward;
procedure statementsequence(
var used: context;
restricted: boolean;
stop: tokens);
forward;
procedure statement(
var used: context;
restricted: boolean;
stop: tokens);
forward;
procedure declarationpart(
var varlength: integer;
stop: tokens);
forward;
procedure statementpart(
var templength: integer;
var used: context;
stop: tokens);
forward;
{ INPUT }
procedure nexttoken;
begin
get(token);
while token = newline1 do
begin
get(lineno); newline(lineno);
get(token)
end;
if token in longtokens then
if token in doubletokens then
get(argument)
else if token = realconst1 then
getreal(realarg)
else { token = stringconst1 }
getstring(
argument {(redundant)},
stringarg)
end;
{ OUTPUT }
procedure emit1(op: integer);
begin put(op) end;
procedure emit2(
op, arg: integer);
begin put(op); put(arg) end;
procedure emit3(
op, arg1, arg2: integer);
begin
put(op); put(arg1);
put(arg2)
end;
procedure emit4(
op, arg1, arg2,
arg3: integer);
begin
put(op); put(arg1);
put(arg2); put(arg3)
end;
procedure emit5(
op, arg1, arg2, arg3,
arg4: integer);
begin
put(op); put(arg1);
put(arg2); put(arg3);
put(arg4)
end;
procedure emit6(
op, arg1, arg2, arg3,
arg4, arg5: integer);
begin
put(op); put(arg1);
put(arg2); put(arg3);
put(arg4); put(arg5)
end;
procedure emitreal(
value: real);
begin
put(realconst2);
putreal(value)
end;
procedure emitstring(
value: string);
begin
put(stringconst2);
putstring(
stringlength(value),
value);
end;
procedure emitcase(
lineno, length: integer;
table: casetable);
begin
put(case2);
putcase(lineno, length,
table);
end;
{ VARIABLE SETS }
function empty(x: varset)
: boolean;
{ empty = (x = []) }
begin
empty := x = nil
end;
function member(x: varset;
y: pointer): boolean;
{ member = y in x }
var correct: boolean;
begin
correct := false;
while not correct
and (x <> nil) do
if x^.item = y then
correct := true
else x := x^.next;
member := correct
end;
function disjoint(
x, y: varset): boolean;
{ disjoint = empty(x*y) }
var correct: boolean;
begin
correct := true;
while correct and
(x <> nil) do
if member(y, x^.item)
then correct := false
else x := x^.next;
disjoint := correct
end;
procedure newset(
var x: varset);
{ x := [] }
begin x := nil end;
procedure include(
var x: varset;
y: pointer);
{ x := x + [y] }
var v: varset;
begin
if not member(x, y) then
begin
new(v);
v^.next := x;
v^.item := y;
x := v
end
end;
procedure addset(
var x: varset;
y: varset);
{ x := x + y }
begin
while y <> nil do
begin
include(x, y^.item);
y := y^.next
end
end;
procedure copyset(
var x: varset;
y: varset);
{ x := y }
begin
newset(x);
addset(x, y)
end;
{ GLOBAL VARIABLES }
function globalvar(
proc, object: pointer)
: boolean;
{ globalvar = the object
is a variable that is
global to a procedure
(or function) }
var kind: class;
begin
kind := object^.kind;
if kind in variables then
globalvar :=
object^.varlevel <=
proc^.proclevel
else if kind = standardfile
then globalvar := true
else globalvar := false
end;
procedure globalset(
proc: pointer;
var left: varset;
right: varset);
{ left := all members of
right which are global
to a procedure (or
function) }
var item: pointer;
begin
newset(left);
while right <> nil do
begin
item := right^.item;
if globalvar(proc, item)
then
include(left, item);
right := right ^.next
end
end;
{ STATEMENT CONTEXTS }
function emptycontext(
used: context): boolean;
{ emptycontext =
(used = []) }
begin
emptycontext :=
empty(used.exprvar) and
empty(used.targetvar)
end;
procedure newcontext(
var used: context);
{ used := [] }
begin
newset(used.targetvar);
newset(used.exprvar)
end;
procedure addcontext(
var left: context;
right: context);
{ left := left + right }
begin
addset(left.targetvar,
right.targetvar);
addset(left.exprvar,
right.exprvar)
end;
procedure copycontext(
var left: context;
right: context);
{ left := right }
begin
newcontext(left);
addcontext(left, right)
end;
procedure globalcontext(
proc: pointer;
var left: context;
right: context);
{ left := all members of
right which are global
to a procedure or
function }
begin
globalset(proc,
left.targetvar,
right.targetvar);
globalset(proc,
left.exprvar,
right.exprvar)
end;
{ SCOPE ANALYSIS }
procedure search(
id, levelno: integer;
var found: boolean;
var object: pointer);
var more: boolean;
begin
more := true;
object :=
block[levelno].lastobject;
while more do
if object = nil then
begin
more := false;
found := false
end
else if object^.id = id then
begin
more := false;
found := true
end
else
object := object^.previous
end;
procedure declare(
id: integer; kind: class;
var object: pointer);
var found: boolean;
other: pointer;
begin
if id <> nameless then
begin
search(id, blocklevel,
found, other);
if found then
error(ambiguous3)
end;
new(object);
object^.id := id;
object^.previous :=
block[blocklevel].lastobject;
object^.kind := kind;
block[blocklevel].lastobject :=
object
end;
procedure find(id: integer;
var object: pointer);
var more, found: boolean;
levelno: integer;
begin
more := true;
levelno := blocklevel;
while more do
begin
search(id, levelno, found,
object);
if found or
(levelno = 0) then
more := false
else levelno := levelno - 1
end;
if not found then
begin
error(undefined3);
declare(id, undefined, object)
end
end;
function within(proc: pointer)
: boolean;
var levelno: integer;
more: boolean;
begin
more := true;
levelno := blocklevel;
while more do
if block[blocklevel].heading
= proc then
begin
more := false;
within := true
end
else if levelno = 0 then
begin
more := false;
within := false
end
else levelno := levelno - 1
end;
procedure checkblock;
var proc: pointer;
begin
proc :=
block[blocklevel].heading;
if proc <> nil then
if proc^.kind = funktion then
error(block3)
end;
procedure newblock(
heading: pointer);
var current: blockrecord;
begin
if blocklevel = maxlevel then
halt(maxlevel5);
blocklevel := blocklevel + 1;
current.templength := 0;
current.maxtemp := 0;
current.heading := heading;
current.lastobject := nil;
block[blocklevel] := current
end;
procedure endblock;
begin
blocklevel := blocklevel - 1
end;
procedure standardblock;
var p: pointer;
begin
blocklevel := -1;
newblock(nil);
{ standard files }
declare(nameless, standardfile,
inputfile);
declare(nameless, standardfile,
outputfile);
{ standard types }
declare(boolean0, ordtype, p);
typeboolean := p;
p^.minvalue := ord(false);
p^.maxvalue := ord(true);
declare(char0, ordtype, p);
typechar := p;
p^.minvalue := ord(chr(null));
p^.maxvalue := ord(chr(del));
declare(integer0, ordtype, p);
typeinteger := p;
p^.minvalue := minint;
p^.maxvalue := maxint;
declare(real0, realtype, p);
typereal := p;
declare(string0, arraytype, p);
typestring := p;
p^.lowerbound := 1;
p^.upperbound := maxstring;
p^.arraylength := maxstring;
p^.indextype := typeinteger;
p^.elementtype := typechar;
declare(nameless, ordtype, p);
typeuniversal := p;
p^.minvalue := 0;
p^.maxvalue := 0;
{ standard constants }
declare(false0, ordconst, p);
p^.ordvalue := ord(false);
p^.ordtypex := typeboolean;
declare(maxint0, ordconst, p);
p^.ordvalue := maxint;
p^.ordtypex := typeinteger;
declare(maxstring0, ordconst, p);
p^.ordvalue := maxstring;
p^.ordtypex := typeinteger;
declare(null0, ordconst, p);
p^.ordvalue := ord(chr(null));
p^.ordtypex := typechar;
declare(true0, ordconst, p);
p^.ordvalue := ord(true);
p^.ordtypex := typeboolean;
{ standard procedures }
declare(open0, standardproc, p);
declare(read0, standardproc, p);
declare(readln0, standardproc, p);
declare(receive0, standardproc, p);
declare(send0, standardproc, p);
declare(write0, standardproc, p);
declare(writeln0, standardproc, p);
{ standard functions }
declare(abs0, standardfunc, p);
declare(arctan0, standardfunc, p);
declare(chr0, standardfunc, p);
declare(cos0, standardfunc, p);
declare(eof0, standardfunc, p);
declare(eoln0, standardfunc, p);
declare(exp0, standardfunc, p);
declare(ln0, standardfunc, p);
declare(odd0, standardfunc, p);
declare(ord0, standardfunc, p);
declare(pred0, standardfunc, p);
declare(round0, standardfunc, p);
declare(sin0, standardfunc, p);
declare(sqr0, standardfunc, p);
declare(sqrt0, standardfunc, p);
declare(succ0, standardfunc, p);
declare(trunc0, standardfunc, p)
end;
{ TYPE ANALYSIS }
procedure checktypes(
var type1: pointer;
type2: pointer);
begin
if type1 <> type2 then
begin
if (type1 <> typeuniversal)
and
(type2 <> typeuniversal)
then error(type3);
type1 := typeuniversal
end
end;
procedure typeerror(
var typex: pointer);
begin
if typex <> typeuniversal then
begin
error(type3);
typex := typeuniversal
end
end;
procedure checktype(
var typex: pointer;
kind: class);
begin
if typex^.kind <> kind then
typeerror(typex)
end;
procedure checkmessage(
var type1, type2: pointer;
var typeno: integer);
{ typeno > 0 }
var mess: messagepointer;
found: boolean;
begin
typeno := 1;
if type1^.kind = channeltype then
begin
mess := type1^.messagelist;
found := false;
while not found and
(mess <> nil) do
if mess^.typex <> type2 then
begin
typeno := typeno + 1;
mess := mess^.previous
end
else found := true;
if not found then
typeerror(type2)
end
else typeerror(type1)
end;
procedure kinderror(
object: pointer);
begin
if object^.kind <> undefined
then error(kind3)
end;
procedure convert1(
var type1: pointer;
type2: pointer);
begin
if (type1 = typeinteger) and
(type2 = typereal) then
begin
emit1(float2);
pop(1); push(2);
type1 := typereal
end
end;
procedure convert2(
var type1, type2: pointer);
begin
if (type1 = typeinteger) and
(type2 = typereal) then
begin
emit1(floatleft2);
pop(1); push(2);
type1 := typereal
end
else convert1(type2, type1)
end;
{ VARIABLE ADDRESSING }
function typelength(
typex: pointer) : integer;
begin
if typex^.kind = ordtype
then typelength := 1
else if typex^.kind = realtype
then typelength := 2
else if typex^.kind = arraytype
then typelength :=
typex^.arraylength
else if typex^.kind = recordtype
then typelength :=
typex^.recordlength
else
{ typex^.kind = channeltype }
typelength := 1
end;
procedure fieldaddressing(
recordlength: integer;
lastfield: pointer);
var displ: integer;
begin
displ := recordlength;
while displ > 0 do
begin
{ skip undefined
object (if any) }
if lastfield^.kind = field
then
begin
displ := displ -
typelength(lastfield^.
fieldtype);
lastfield^.fielddispl
:= displ
end;
lastfield :=
lastfield^.previous
end
end;
procedure variableaddressing(
varlength: integer;
lastvar: pointer);
var displ: integer;
begin
displ := varlength;
while displ > 0 do
begin
{ skip undefined
object (if any) }
if lastvar^.kind = variable
then
begin
displ := displ -
typelength(
lastvar^.vartype);
lastvar^.varlevel :=
blocklevel;
lastvar^.vardispl :=
displ
end;
lastvar := lastvar^.previous
end
end;
procedure parameteraddressing(
paramlength: integer;
lastparam: pointer);
var displ: integer;
begin
displ := 0;
while displ > - paramlength do
begin
{ skip undefined
object (if any) }
if lastparam^.kind =
varparameter then
begin
displ := displ - 1;
lastparam^.varlevel :=
blocklevel;
lastparam^.vardispl :=
displ
end
else if lastparam^.kind =
valueparameter then
begin
displ := displ -
typelength(
lastparam^.vartype);
lastparam^.varlevel :=
blocklevel;
lastparam^.vardispl :=
displ
end;
lastparam :=
lastparam^.previous
end
end;
{ LABELS }
procedure newlabel(
var no: integer);
begin
if labelno = maxlabel then
halt(maxlabel5);
labelno := labelno + 1;
no := labelno
end;
{ TEMPORARIES }
procedure push{length: integer};
begin
with block[blocklevel] do
begin
templength :=
templength + length;
if maxtemp < templength then
maxtemp := templength
end
end;
procedure pop{length: integer};
begin
with block[blocklevel] do
templength :=
templength - length
end;
{ CASE TABLES }
procedure insertcase(
value, index: integer;
var length: integer;
var table: casetable;
var found: boolean);
var x, y: caserecord;
i: integer;
begin
{ 0 <= length < maxcase
and table ordered by
case values }
x.value := value;
x.index := index;
found := false;
for i := 1 to length do
begin
y := table[i];
if x.value < y.value
then
begin
table[i] := x;
x := y
end
else
if x.value = y.value
then found := true
end;
length := length + 1;
table[length] := x
end;
{ INITIALIZATION }
procedure initialize;
begin
addtokens :=
[minus1, or1, plus1];
declarationtokens :=
[const1, function1,
procedure1, type1, var1];
blocktokens := [begin1]
+ declarationtokens;
doubletokens := [charconst1,
intconst1, identifier1];
literaltokens :=
[charconst1, intconst1,
realconst1, stringconst1];
longtokens := [realconst1,
stringconst1] + doubletokens;
multiplytokens :=
[and1, asterisk1, div1,
mod1, slash1];
parametertokens :=
[identifier1, var1];
relationtokens := [equal1,
greater1, less1, notequal1,
notgreater1, notless1];
routinetokens :=
[function1, procedure1];
selectortokens :=
[leftbracket1, period1];
signtokens := [minus1, plus1];
statementtokens :=
[assume1, begin1, case1,
for1, forall1, if1,
identifier1, leftbracket1,
parallel1, repeat1, while1];
unsignedtokens := [identifier1]
+ literaltokens;
constanttokens := signtokens
+ unsignedtokens;
factortokens := [leftparenthesis1,
not1] + unsignedtokens;
termtokens := factortokens;
simpleexprtokens := signtokens
+ termtokens;
expressiontokens :=
simpleexprtokens;
constants := [ordconst, realconst,
stringconst];
functions :=
[funktion, standardfunc];
parameters := [valueparameter,
varparameter];
procedures :=
[procedur, standardproc];
types := [ordtype, realtype,
arraytype, recordtype,
channeltype];
variables :=
[variable] + parameters;
leftparts :=
[funktion] + variables;
labelno := 0;
minint := - maxint - 1
end;
{ SYNTAX ANALYSIS }
procedure syntaxerror(
stop: tokens);
begin
error(syntax3);
while not (token in stop) do
nexttoken
end;
procedure syntaxcheck(
stop: tokens);
begin
if not (token in stop) then
syntaxerror(stop)
end;
procedure expect(t: integer;
stop: tokens);
begin
if token = t then
begin
nexttoken;
syntaxcheck(stop)
end
else syntaxerror(stop)
end;
procedure expectid(
var id: integer;
stop: tokens);
begin
if token = identifier1 then
begin