-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.pas
169 lines (153 loc) · 3.27 KB
/
interpreter.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
unit interpreter;
interface
uses
Classes, SysUtils;
procedure interpret;
implementation
uses defs;
{ lit 0,a : load constant a
opr 0,a : execute operation a
lod l,a : load varible l,a
sto l,a : store varible l,a
cal l,a : call procedure a at level l
int 0,a : increment t-register by a
jmp 0,a : jump to a
jpc 0,a : jump conditional to a }
procedure interpret;
const
stacksize = 500;
var
p, b, t: integer; {program-, base-, topstack-registers}
i: instruction; {instruction register}
s: array [1..stacksize] of integer; {datastore}
function base(l: integer): integer;
var
b1: integer;
begin
b1 := b; {find base l levels down}
while l > 0 do
begin
b1 := s[b1];
l := l - 1;
end;
base := b1;
end {base};
begin
writeln(' start pl/0');
t := 0;
b := 1;
p := 0;
s[1] := 0;
s[2] := 0;
s[3] := 0;
repeat
i := code[p];
p := p + 1;
with i do
case f of
lit:
begin
t := t + 1;
s[t] := a;
end;
opr: case a of {operator}
0:
begin {return}
t := b - 1;
p := s[t + 3];
b := s[t + 2];
end;
1: s[t] := -s[t];
2:
begin
t := t - 1;
s[t] := s[t] + s[t + 1];
end;
3:
begin
t := t - 1;
s[t] := s[t] - s[t + 1];
end;
4:
begin
t := t - 1;
s[t] := s[t] * s[t + 1];
end;
5:
begin
t := t - 1;
s[t] := s[t] div s[t + 1];
end;
6: s[t] := Ord(odd(s[t]));
8:
begin
t := t - 1;
s[t] := Ord(s[t] = s[t + 1]);
end;
9:
begin
t := t - 1;
s[t] := Ord(s[t] <> s[t + 1]);
end;
10:
begin
t := t - 1;
s[t] := Ord(s[t] < s[t + 1]);
end;
11:
begin
t := t - 1;
s[t] := Ord(s[t] >= s[t + 1]);
end;
12:
begin
t := t - 1;
s[t] := Ord(s[t] > s[t + 1]);
end;
13:
begin
t := t - 1;
s[t] := Ord(s[t] <= s[t + 1]);
end;
14:
begin
t := t + 1;
Readln(s[t]);
end;
15:
begin
Writeln(s[t]);
end;
end;
lod:
begin
t := t + 1;
s[t] := s[base(l) + a];
end;
sto:
begin
s[base(l) + a] := s[t];
//writeln(s[t]);
t := t - 1;
end;
cal:
begin {generate new block mark}
s[t + 1] := base(l);
s[t + 2] := b;
s[t + 3] := p;
b := t + 1;
p := a;
end;
int: t := t + a;
jmp: p := a;
jpc:
begin
if s[t] = 0 then
p := a;
t := t - 1;
end
end {with, case}
until p = 0;
Write(' end pl/0');
end {interpret};
end.