-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp.clj
348 lines (316 loc) · 10.2 KB
/
lisp.clj
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
; Implementation of the Metacircular Evaluator
; (a.k.a. Lisp interpreter in Clojure).
(require '[clojure.test :refer [deftest is run-tests]])
(import 'clojure.lang.IFn)
(declare $eval)
;;---------------------------------------------------------
;; Defines a Closure type that implements the IFn
;; interface which provides instances of the type the
;; ability to be called using the apply function:
;;
;; (def c (->Closure (atom {'a 5, '+ +}) '(x) '(+ a x)))
;; => #'user/c
;;
;; (apply c '(10))
;; => 15
;;
(deftype Closure
[env params body]
IFn
(applyTo [self args]
($eval body
(merge @env (zipmap params args)))))
;;; Helper functions.
(defn third
"Return the third element of lst."
[lst]
(nth lst 2))
(defn fourth
"Return the fourth element of lst."
[lst]
(nth lst 3))
(defn $eval
"Evaluates expr using the bindings in the env map."
[expr env]
(cond
; Variable references
(symbol? expr)
(if (contains? env expr)
(env expr)
(throw (RuntimeException.
(str "Unbound variable: " expr))))
; Check for special forms
(list? expr)
(case (first expr)
nil
()
quote
(second expr)
if
(if ($eval (second expr) env)
($eval (third expr) env)
($eval (fourth expr) env))
lambda
(->Closure (atom env) (second expr) (third expr))
label
(let [closure ($eval (third expr) env)]
(swap! (.env closure)
#(assoc % (second expr) closure))
closure)
; Ordinary function application
(apply ($eval (first expr) env)
(map #($eval % env) (rest expr))))
; Anything that is not a symbol or a list evals to itself
:else
expr))
;==========================================================
(deftest test-var-ref
(is (= 15 ($eval 'c
{'a 4, 'b 8, 'c 15})))
(is (thrown? RuntimeException
($eval 'x
{'a 4, 'b 8, 'c 15}))))
;==========================================================
(deftest test-itself
(is (= 42 ($eval 42 {})))
(is (= true ($eval true {})))
(is (= false ($eval false {})))
(is (= nil ($eval nil {})))
(is (= "hello" ($eval "hello" {}))))
;==========================================================
(deftest test-empty-list
(is (= () ($eval () {}))))
;==========================================================
(deftest test-quote
(is (= 'a
($eval '(quote a) {})))
(is (= '(1 2 3)
($eval '(quote (1 2 3)) {})))
(is (= '42 ($eval '(quote 42) {}))))
;==========================================================
(deftest test-if
(is (= 1 ($eval '(if true 1 2) {})))
(is (= 2 ($eval '(if false 1 2) {}))))
;==========================================================
(deftest test-function-invocation
(is (= 3
($eval '(f 1 2)
{'f +})))
(is (= 'a
($eval '(g (quote (a b c d e)))
{'g first})))
(is (= '(a b c)
($eval '(cons x y)
{'cons cons, 'x 'a, 'y '(b c)})))
(is (= 55
($eval '(+ 1 2 3 4 5 6 7 8 9 10)
{'+ +})))
(is (= '(a b c)
($eval '(apply cons (quote (a (b c))))
{'apply apply, 'cons cons}))))
;==========================================================
(deftest test-lambda
(let [c ($eval '(lambda (x)
(* x 2))
{'* *})]
(is (instance? Closure c))
(is (= @(.env c) {'* *}))
(is (= (.params c) '(x)))
(is (= (.body c) '(* x 2)))
(is (= 42 (apply c '(21)))))
(is (= 8
($eval '((lambda (f x) (f (f (f x))))
(lambda (x) (* x 2))
1)
{'* *}))))
;==========================================================
(deftest test-label
(is (= '(a a b b c c)
($eval
'((label dup (lambda (lst)
(if (eq lst ())
()
(cons (car lst)
(cons (car lst)
(dup (cdr lst)))))))
(quote (a b c)))
{'eq =
'cons cons
'car first
'cdr rest})))
(is (= '(1 4 9 16)
($eval
'((label mapcar (lambda (fun lst)
(if (eq lst ())
()
(cons (fun (car lst))
(mapcar fun (cdr lst))))))
(lambda (x) (* x x))
(quote (1 2 3 4)))
{'eq =
'cons cons
'car first
'cdr rest
'* *}))))
;==========================================================
;;; Remove comments to carry out the following tests.
;(deftest test-do
; (is (nil? ($eval '(do)
; {})))
; (is (= 3
; ($eval '(do (+ 1 2))
; {'+ +})))
; (is (= 10
; ($eval '(do (+ 2 5)
; (- 2 5)
; (/ 2 5)
; (* 2 5))
; {'+ +
; '- -
; '/ /
; '* *})))
; (is (= "7-32/510"
; (with-out-str
; ($eval '(do (pr (+ 2 5))
; (pr (- 2 5))
; (pr (/ 2 5))
; (pr (* 2 5)))
; {'pr pr
; '+ +
; '- -
; '/ /
; '* *}))))
; (is (= (let [nl (System/lineSeparator)]
; (str "1" nl "2" nl "3" nl))
; (with-out-str
; ($eval '(do (prn (+ -2 (+ 1 2)))
; (prn (+ 1 1))
; (prn 3)
; (+ 2 2))
; {'+ +
; 'prn prn}))))
; (is (= (let [nl (System/lineSeparator)]
; (str "One potato," nl
; "two potatoes," nl
; "three potatoes," nl
; "four." nl))
; (with-out-str
; ($eval '(do (println "One potato,")
; (println "two potatoes,")
; (println "three potatoes,")
; (println "four."))
; {'println println})))))
;==========================================================
;;; Remove comments to carry out the following tests.
;(deftest test-dotimes
; (is (= ""
; (with-out-str ($eval '(dotimes (x 0)
; (println x))
; {'println println}))))
; (is (= "0123456789"
; (with-out-str ($eval '(dotimes (x 10)
; (pr x))
; {'pr pr}))))
; (is (= (let [nl (System/lineSeparator)]
; (str "Line 0" nl "Line 1" nl "Line 2" nl "Line 3" nl))
; (with-out-str ($eval '(dotimes (i (+ 2 2))
; (println "Line" i))
; {'println println, '+ +}))))
; (is (= "1-4-9-16-25-36-49-64-81-100-"
; (with-out-str ($eval '(dotimes (some-var (* 2 5))
; (printf "%d-"
; ((lambda (x) (* x x))
; (inc some-var))))
; {'printf printf, '* *, 'inc inc}))))
; (is (= (str "**************************************************"
; "**************************************************")
; (with-out-str ($eval '(dotimes (mxyzptlk (* 2 2 5 5))
; (print "*"))
; {'print print, '* *})))))
;==========================================================
;;; Remove comments to carry out the following tests.
;(deftest test-let
; (is (= 7
; ($eval '(let (a 7) a) {})))
; (is (= 42
; ($eval '(let (x 6)
; (* 7 x))
; {'* *})))
; (is (= 111
; ($eval '(let (x (* 2 5))
; (let (y (+ 1 x))
; (+ 1 (* y x))))
; {'+ +
; '* *})))
; (is (= 60
; ($eval '((lambda (x y)
; (let (y (+ 1 y))
; (* x y)))
; 10
; 5)
; {'* *
; '+ +})))
; (is (= '(a b c d)
; ($eval '(let (one (quote (c d)))
; (let (two (cons (quote b) one))
; (let (three (cons (quote a) two))
; three)))
; {'cons cons})))
; (is (= 8
; ($eval '(((lambda (x y)
; (let (z (let (r) (* x y)) r)
; (lambda (w)
; (let (t (+ x (let (a y) a) z w))
; (let (t t) t))))) 1 2) 3)
; {'* *
; '+ +}))))
;==========================================================
;;; Remove comments to carry out the following tests.
;(deftest test-cond
; (is (nil? ($eval '(cond)
; {})))
; (is (= 2
; ($eval '(cond
; 1 2)
; {})))
; (is (nil? ($eval '(cond
; false 2)
; {})))
; (is (= 'three
; ($eval '(cond
; (= x 1) (quote one)
; (= x 2) (quote two)
; (= x 3) (quote three)
; (= x 4) (quote four)
; true (quote other))
; {'x 3
; '= =})))
; (is (= 'other
; ($eval '(cond
; (= x 1) (quote one)
; (= x 2) (quote two)
; (= x 3) (quote three)
; (= x 4) (quote four)
; true (quote other))
; {'x 5
; '= =})))
; (is (nil? ($eval '(cond
; (= x 1) (quote one)
; (= x 2) (quote two)
; (= x 3) (quote three)
; (= x 4) (quote four))
; {'x 5
; '= =})))
; (is (= "Lannister"
; (with-out-str
; ($eval '(cond
; (< 4 got) (print "Targaryen")
; (< 3 got) (print "Stark")
; (< 2 got) (print "Tully")
; (< 1 got) (print "Lannister")
; true (print "Tyrell"))
; {'got 2
; '< <
; 'print print})))))
;==========================================================
(run-tests)