-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE-interpreter.lisp
400 lines (315 loc) · 12.6 KB
/
E-interpreter.lisp
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
(in-package #:lang-e)
;; Language as defined in section 3.2.2 in LiSP, ported to CL
(defmethod wrong (msg &rest args)
(let ((output (apply #'format nil (concatenate 'string msg "~&") args)))
(format t "~A" output)
(finish-output)
(error output)))
;; Most generic types.
(defmethod invoke (f v* r k)
(wrong "Not a function: f = ~A, r = ~A, k = ~A" f r k))
(defmethod invoke ((f continuation) v* r k)
;; This is here to allow call/cc to function.
(if (= 1 (length v*))
(resume f (car v*))
(wrong "Not a function: f = ~A, r = ~A, k = ~A" f r k)))
(defmethod resume ((k continuation) v)
(wrong "Unknown continuation: v = ~A k = ~A" v k))
(defmethod lookup ((r environment) n k)
(wrong "Not an environment (in which to lookup): r = ~A, n = ~A, k = ~A"
r n k))
(defmethod update! ((r environment) n k v)
(wrong "Not an environment (in which to update!): r = ~A, n = ~A, v = ~A, k = ~A" r n v k))
(defmethod catch-lookup (k tag kk)
(wrong "Not a catch continuation: k = ~A, tag = ~A, kk = ~A" k tag kk))
(defmethod block-lookup (r n k v)
(wrong "not a block environment: r = ~A, n = ~A, k = ~A, v = ~A" r n k v))
;; The interpreter
(defun evaluate (e r k)
(cond
((or (eq e nil) (eq e t))
;; hard code these symbol's to be CL's true or false, which happens
;; when we pass them on.
(evaluate-quote e r k))
((atom e)
(if (symbolp e)
(evaluate-variable e r k)
;; autoquote atoms I don't understand.
(evaluate-quote e r k)))
((eq (car e) 'quote)
(evaluate-quote (cadr e) r k))
((eq (car e) 'if)
(evaluate-if (cadr e) (caddr e) (cadddr e) r k))
((eq (car e) 'begin)
(evaluate-begin (cdr e) r k))
((eq (car e) 'set!)
(evaluate-set! (cadr e) (caddr e) r k))
((eq (car e) 'lambda)
(evaluate-lambda (cadr e) (cddr e) r k))
;; Implement catch/throw ala Common Lisp
((eq (car e) 'catch)
(evaluate-catch (cadr e) (cddr e) r k))
((eq (car e) 'throw)
(evaluate-throw (cadr e) (caddr e) r k))
;; Implement block/return-from ala Common Lisp
((eq (car e) 'block)
(evaluate-block (cadr e) (cddr e) r k))
((eq (car e) 'return-from)
(evaluate-return-from (cadr e) (caddr e) r k))
;; Unwind-protect
((eq (car e) 'unwind-protect)
(evaluate-unwind-protect (cadr e) (cddr e) r k))
(t
;; everything else is a function application
(evaluate-application (car e) (cdr e) r k))))
;; Quoting
(defun evaluate-quote (v r k)
;; Just pass the literal form as the value to the continuation.
(declare (ignore r))
(resume k v))
;; Alternatives
(defun evaluate-if (ec et ef r k)
(evaluate ec r (make-if-continuation k et ef r)))
(defmethod resume ((k if-continuation) v)
;; If the value is true, we evaluate the true/false espressions
(evaluate (if v (et k) (ef k))
(r k)
(k k)))
;; Sequences
(defun evaluate-begin (e* r k)
(if (consp e*)
(if (consp (cdr e*))
(evaluate (car e*) r (make-begin-continuation k e* r)) ;; cdr in resu.
(evaluate (car e*) r k))
(resume k *empty-begin-value*)))
(defmethod resume ((k begin-continuation) v)
(evaluate-begin (cdr (e* k))
(r k)
(k k)))
;; Variables Environment
(defun evaluate-variable (n r k)
(lookup r n k))
;; Getting a variable's value
(defmethod lookup ((r null-env) n k)
(wrong "Unknown variable: n = ~A, r = ~A, k = ~A" n r k))
(defmethod lookup ((r full-env) n k)
(lookup (others r) n k))
(defmethod lookup ((r variable-env) n k)
(if (eql n (name r))
(resume k (value r))
(lookup (others r) n k)))
;; Setting a variable's value
(defun evaluate-set! (n e r k)
;; We first evaluate the value, then we shove that value to another
;; continuation that assigns it.
(evaluate e r (make-set!-continuation k n r)))
(defmethod resume ((k set!-continuation) v)
(update! (r k) (n k) (k k) v))
(defmethod update! ((r null-env) n k v)
(wrong "Unknown variable to update!: r = ~A, n = ~A, k = ~A, v = ~A" r n k v))
(defmethod update! ((r full-env) n k v)
(update! (others r) n k v))
(defmethod update! ((r variable-env) n k v)
(if (eql n (name r))
(progn
(setf (value r) v)
(resume k v))
(update! (others r) n k v)))
;; Functions
;; Creating a function.
(defun evaluate-lambda (n* e* r k)
(resume k (make-func n* e* r)))
;; Invoking a function.
(defmethod invoke ((f func) v* r k)
(declare (ignorable r))
(let ((env (extend-env (env f) (variables f) v*)))
(evaluate-begin (body f) env k)))
;; Invoking a primitive function
(defun extend-env (env n* v*)
(cond
((and (consp n*) (consp v*))
(make-variable-env
(extend-env env (cdr n*) (cdr v*))
(car n*)
(car v*)))
((and (null n*) (null v*))
;; Link to previous environment of arbitrary size.
env)
(t
(wrong "Arity Mismatch: env = ~A, n* = ~A, v* = ~A" env n* v*))))
;; Applying a function (to a list of evaluated arguments)
(defun evaluate-application (e e* r k)
(evaluate e r (make-evfun-continuation k e* r)))
(defmethod resume ((k evfun-continuation) f)
(evaluate-arguments (e* k) (r k) (make-apply-continuation (k k) f (r k))))
(defun evaluate-arguments (e* r k)
(if (consp e*)
(evaluate (car e*) r (make-argument-continuation k e* r))
(resume k *no-more-arguments*)))
(defmethod resume ((k argument-continuation) v)
(evaluate-arguments (cdr (e* k)) (r k) (make-gather-continuation (k k) v)))
(defmethod resume ((k gather-continuation) v*)
(resume (k k) (cons (v k) v*)))
(defmethod resume ((k apply-continuation) v)
(invoke (f k) v (r k) (k k)))
;; Invoke primitives
(defmethod invoke ((f primitive) v* r k)
;; The address here is the actual function from the underlying lisp system.
(funcall (address f) v* r k))
;; Catch methods
(defmethod evaluate-catch (tag body r k)
(evaluate tag r (make-catch-continuation k body r)))
(defmethod resume ((k catch-continuation) v)
(evaluate-begin (body k) (r k) (make-labeled-continuation (k k) v)))
;; NOTE: This was not in the LiSP book, I had to add it.
(defmethod resume ((k labeled-continuation) v)
(resume (k k) v))
;; Throw methods
(defmethod evaluate-throw (tag form r k)
(evaluate tag r (make-throw-continuation k form r)))
(defmethod resume ((k throw-continuation) tag)
(catch-lookup k tag k))
(defmethod catch-lookup ((k continuation) tag kk)
(format t "catch-lookup ~A~%" k)
(catch-lookup (k k) tag kk))
(defmethod catch-lookup ((k bottom-continuation) tag kk)
(wrong "No associated catch! k = ~A, tag = ~A, kk = ~A" k tag kk))
(defmethod catch-lookup ((k labeled-continuation) tag kk)
(format t "catch-lookup (eql ~A ~A)~%" tag (tag k))
(if (eql tag (tag k))
(evaluate (form kk) (r kk) (make-throwing-continuation kk tag k))
(catch-lookup (k k) tag kk)))
(defmethod resume ((k throwing-continuation) v)
;; Fixed to take into account unwind-protect
;;(resume (cont k) v))
(unwind (k k) v (cont k)))
;; TODO: This is broken when the throw is to an unknown tag
;; So catch/throw can support support unwind-protect
(defmethod unwind ((k unwind-protect-continuation) v target)
(evaluate-begin (cleanup k)
(r k)
(make-unwind-continuation (k k) v target)))
;; To support unwind-protect.
(defmethod resume ((k unwind-continuation) value)
(unwind (k k) (value k) (target k)))
;; Block methods
(defun evaluate-block (label body r k)
(let ((k (make-block-continuation k label)))
;; The block-env object holds the lexical scope of the block. It
;; associates the label with the continuation that represents the
;; future value of the block.
(evaluate-begin body (make-block-env r label k) k)))
(defmethod resume ((k block-continuation) v)
(resume (k k) v))
;; Return-from methods
(defun evaluate-return-from (label form r k)
(evaluate form r (make-return-from-continuation k r label)))
(defmethod resume ((k return-from-continuation) v)
(block-lookup (r k) (label k) (k k) v))
;; TODO: This is suppose to be different according to the book, but both
;; the book and the downloaded source code fail to show a difference. So, I
;; need to check if this is actually correct or if I need to add code here.
(defmethod block-lookup ((r block-env) n k v)
;; Modofied to support unwind-protect
;;(if (eql n (name r))
;; (unwind k v (cont r))
;; (block-lookup (others r) n k v)))
(if (eql n (name r))
(unwind k v (cont r))
(block-lookup (others r) n k v)))
(defmethod block-lookup ((r full-env) n k v)
(block-lookup (others r) n k v))
(defmethod block-lookup ((r null-env) n k v)
(wrong "Unknown block label: r = ~A, n = ~A, k = ~A, v = ~A" r n k v))
(defmethod resume ((k return-from-continuation) v)
(block-lookup (r k) (label k) (k k) v))
(defmethod unwind ((k continuation) v ktarget)
(if (eql k ktarget)
(resume k v)
(unwind (k k) v ktarget)))
(defmethod unwind ((k bottom-continuation) v ktarget)
(wrong "Onselete continuation: k = ~A, v = ~A, ktarget = ~A" k v ktarget))
;; Unwind-protect methods (TODO: Broken, also probably with respect to
;; catch/block too.)
(defun evaluate-unwind-protect (form cleanup r k)
(evaluate form r (make-unwind-protect-continuation k cleanup r)))
(defmethod resume ((k unwind-protect-continuation) v)
(evaluate-begin (cleanup k) (r k) (make-protect-return-continuation (k k) v)))
(defmethod resume ((k protect-return-continuation) v)
(declare (ignore v)) ;; TODO: Figure out if this is true...
(resume (k k) (value k)))
;; Bottom Continuation methods
(defmethod resume ((k bottom-continuation) v)
;; Simply pass the value to the enclosed function, and return.
(funcall (f k) v))
;; Debugging junk TODO: use this to inspect the continuation
;; structures and how they are linked together during the
;; evaluation. (Hint: I can search through the continuations in order
;; to find a particular continuation for which I may seek.
(defmethod dump-cont (k)
(format t "dump-cont[DONE]: k = ~A~%" k))
(defmethod dump-cont ((k continuation))
(format t "dump-cont: k = ~A, k' = ~A~%" k (k k))
(dump-cont (k k)))
;; Interpreter
(defun dump-env (msg r)
(labels ((dump-it (r)
(cond
((variable-env-p r)
(format t "name: ~A, value: ~A~%" (name r) (value r))
(dump-it (others r)))
((null-env-p r)
(format t "End of environment.~%")))))
(format t "~A~%" msg)
(dump-it r)))
;; Return a contination which accepts any single value and prints it
;; out to stdout.
(defun the-bottom ()
(make-bottom-continuation
'void
(lambda (v)
(format t "Bottom continuation got: ~A~%" v))))
(defun test (form)
(flet ((gen-primitive-function (f a)
(lambda (v* r k)
(declare (ignorable r))
;;(inspect k)
(cond
((eq a 'variable-arity)
(resume k (apply f v*)))
((= a (length v*))
(resume k (apply f v*)))
(t
(wrong "Func: ~A wants ~A args, but given ~A args."
f a (length v*)))))))
(evaluate
form
;; a simple environment for testing
(extend-env
(make-null-env)
(list 'cons 'car 'cdr '+ '- '/ '* '< '> '= '/= 'format 'call/cc)
(list (make-primitive 'cons (gen-primitive-function #'cons 2))
(make-primitive 'car (gen-primitive-function #'car 1))
(make-primitive 'cdr (gen-primitive-function #'cdr 1))
(make-primitive '+ (gen-primitive-function #'+ 'variable-arity))
(make-primitive '- (gen-primitive-function #'- 'variable-arity))
(make-primitive '/ (gen-primitive-function #'/ 'variable-arity))
(make-primitive '* (gen-primitive-function #'* 'variable-arity))
(make-primitive '< (gen-primitive-function #'< 'variable-arity))
(make-primitive '> (gen-primitive-function #'> 'variable-arity))
(make-primitive '= (gen-primitive-function #'= 'variable-arity))
(make-primitive '/= (gen-primitive-function #'/= 'variable-arity))
(make-primitive
'format
(gen-primitive-function (lambda (str fmt &rest args)
(apply #'format str fmt args)
(finish-output))
'variable-arity))
(make-primitive
'call/cc
(lambda (v* r k)
(if (= 1 (length v*))
(invoke (car v*) (list k) r k)
(wrong "call/cc: incorrect arity. v* = ~A, r = ~A, k = ~A"
v* r k))))))
(the-bottom))))