-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINT.WHL
More file actions
203 lines (146 loc) · 4.67 KB
/
INT.WHL
File metadata and controls
203 lines (146 loc) · 4.67 KB
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
(*
WHILE Interpreter
(C) 1998, Mike Wiering
*)
(*
INT.WHL
Input must have the following format:
(P.d)
where:
P = WHILE program
d = input data
*)
read Pd;
(* initialize variables here for debugging *)
VarList := nil;
Code := nil;
Stack := nil;
CodeStack := nil;
(* Pd = (Program.Data) *)
Program := hd Pd;
Data := tl Pd;
(* Program = ((var InputVar) Code (var OutputVar)) *)
InputVar := tl (hd Program);
Code := hd (tl Program);
OutputVar := tl (hd (tl (tl Program)));
(* not needed anymore *)
Pd := nil;
Program := nil;
(* insert Data into VarList, at position InputVar (usually 1) *)
VarList := cons Data nil;
while tl InputVar do
VarList := cons nil VarList;
InputVar := tl InputVar;
(* main loop *)
while Code do
(* (; C1 C2) -> Push (C2); Code := C1 *)
while =? (hd Code) (quote ;) do
CodeStack := cons (hd (tl (tl Code))) CodeStack;
Code := hd (tl Code);
(* (:= (var V) E) -> VarName := V; Code := E; Push (assignment) *)
while =? (hd Code) (quote :=) do
VarName := tl (hd (tl Code));
Code := hd (tl (tl Code));
CodeStack := cons assignment CodeStack;
(* Code = (while E C) -> Code := E; Push (runwhile, Code) *)
while =? (hd Code) (quote while) do
CodeStack := cons (quote runwhile) (cons Code CodeStack);
Code := hd (tl Code);
(* (=? E1 E2) -> Code := E1; Push (E2, compare) *)
while =? (hd Code) (quote =?) do
CodeStack := cons (hd (tl (tl Code))) (cons compare CodeStack);
Code := hd (tl Code);
(* (cons E1 E2) -> Code := E1; Push (E2, runcons) *)
while =? (hd Code) (quote cons) do
CodeStack := cons (hd (tl (tl Code))) (cons runcons CodeStack);
Code := hd (tl Code);
(* (hd E) -> Code := E; Push (head) *)
while =? (hd Code) (quote hd) do
CodeStack := cons head CodeStack;
Code := hd (tl Code);
(* (tl E) -> Code := E; Push (tail) *)
while =? (hd Code) (quote tl) do
CodeStack := cons tail CodeStack;
Code := hd (tl Code);
(* (var V) -> Push (var V) *)
while =? (hd Code) (quote var) do
Var := (tl Code);
V := VarList;
while tl Var do
V := tl V;
Var := tl Var;
Stack := cons (hd V) Stack;
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* (quote X) -> Push (X) *)
while =? (hd Code) (quote quote) do
Quote := hd (tl Code);
Stack := cons Quote Stack;
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* runwhile -> Pop (E); if E then Pop (C); Code := C else skip *)
while =? Code (quote runwhile) do
Expr := hd Stack;
ExitLoop := true;
while Expr do
ExitLoop := false;
(* CodeStack contains (while E C), Code := C *)
Code := hd (tl (tl (hd CodeStack)));
Expr := false;
while ExitLoop do
(* remove (while E C) from CodeStack *)
CodeStack := tl CodeStack;
Code := hd CodeStack;
CodeStack := tl CodeStack;
ExitLoop := false;
Stack := tl Stack;
(* assignment -> Pop (E); var VarName := E *)
while =? Code (quote assignment) do
(* insert value into VarList at position VarName *)
V := nil;
while VarName do
VarName := tl VarName;
V := cons (hd VarList) V;
VarList := tl VarList;
V := cons (hd Stack) (tl V);
W := nil;
while V do
W := cons (hd V) W;
V := tl V;
Tmp := nil;
while W do
Tmp := cons (hd W) Tmp;
W := tl W;
while Tmp do
VarList := cons (hd Tmp) VarList;
Tmp := tl Tmp;
Stack := tl Stack;
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* head -> Pop (E); Push (hd E) *)
while =? Code (quote head) do
Stack := cons (hd (hd Stack)) (tl Stack);
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* tail -> Pop (E); Push (tl E) *)
while =? Code (quote tail) do
Stack := cons (tl (hd Stack)) (tl Stack);
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* compare -> Pop (E1, E2); Push (=? E1 E2) *)
while =? Code (quote compare) do
Stack := cons (=? (hd (tl Stack)) (hd Stack)) (tl (tl Stack));
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* runcons -> Pop (E1, E2); Push (cons E1 E2) *)
while =? Code (quote runcons) do
Stack := cons (cons (hd (tl Stack)) (hd Stack)) (tl (tl Stack));
Code := hd CodeStack;
CodeStack := tl CodeStack;
(* finally, write value of (var OutputVar) *)
V := VarList;
while tl OutputVar do
V := tl V;
OutputVar := tl OutputVar;
Output := hd V;
write Output;