-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.scm
505 lines (386 loc) · 13.5 KB
/
interpreter.scm
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
;; new-env.scm
;; A binding is a name value pair
;; A frame is a list of bindings
(define (println data)
(newline)
(display data)
(newline)
'done)
(define empty-env '())
(define (new-frame vars vals)
(if (and (null? vars) (null? vals))
'()
(cons (cons (car vars) (car vals))
(new-frame (cdr vars) (cdr vals)))))
(define (build-frame vars vals)
(cons 'frame (new-frame vars vals)))
(define (binds frame) (cdr frame))
(define (frame-vars frame) (map car (binds frame)))
(define (frame-vals frame) (map cdr (binds frame)))
(define (curr-frame env) (car env))
(define (prev-frame env) (cdr env))
(define (add-binding-to-frame! var val frame)
(set-cdr! frame (cons (cons var val) (binds frame)))
'done)
(define (extend-env vars vals base-env)
(cons (build-frame vars vals) base-env))
(define (traverse-env proc var env #!optional val)
(if (null? env)
(error "Unbound variable" var)
(let* ((frame (curr-frame env))
(bind-list (binds frame)))
(proc bind-list))))
(define (lookup-variable var env)
(define (lookup bind-list)
(cond ((null? bind-list) ;FIXME: Find better way to recurse
(lookup-variable var (prev-frame env)))
((eq? var (caar bind-list)) (cdar bind-list))
(else (lookup (cdr bind-list)))))
(traverse-env lookup var env))
(define (set-var-value! var val env)
(define (set-var bind-list)
(cond ((null? bind-list) (set-var-value! var val (prev-frame env)))
((eq? var (caar bind-list))
(set-cdr! (car bind-list) val))
(else (set-var (cdr bind-list)))))
(traverse-env set-var var env val))
(define (define-variable! var val env)
(let ((frame (curr-frame env)))
(define (helper bind-list)
(cond ((null? bind-list) (add-binding-to-frame! var val frame))
((eq? var (caar bind-list))
(set-cdr! (car bind-list) val))
(else (helper (cdr bind-list)))))
(traverse-env helper var env val)))
;; syntax.scm
;; extract pieces of information from entered code
;; essentially defines the syntax of the language
(println "syntax.scm")
(define (self-eval? exp) (or (number? exp) (string? exp)))
;; (define (name? exp) (symbol? exp)) ;; variable name == symbol
(define name? symbol?)
(define (oper=? exp name) (and (pair? exp) (eq? (car exp) name)))
(define (quote? exp) (oper=? exp 'quote))
(define (quote-content exp) (car (cdr exp)))
(define (assign? exp) (oper=? exp 'set!))
;; (set! x 2) -> x
(define (assign-var exp)
(if (and (assign? exp) (name? (car (cdr exp))))
(car (cdr exp))
(error "Wrong expression for assign-var:" exp)))
;; (set! x 2) -> 2
(define (assign-value exp)
(if (assign? exp)
(car (cdr (cdr exp)))
(error "Wrong expression for assign-value:" exp)))
(define (func? exp)
(oper=? exp 'func)) ; func instead of define
(define (func-name exp)
(if (and (func? exp) (name? (car (car (cdr exp)))))
(car (car (cdr exp))) ; (func (square n) (* n n)) -> square
(error "Wrong expression for def-name:" exp)))
(define (func-value exp)
(let ((f-params (cdr (car (cdr exp))))
(body (cdr (cdr exp)))) ; func body can be multiple expressions
(if (func? exp) ;(func (fact n) (if (= n 0) 1 (* n (fact (- n 1))))
(build-make-proc f-params body)
(error "Wrong expression for func-value:" exp))))
(define (var? exp) (oper=? exp 'var))
(define (var-name exp)
(if (and (var? exp) (name? (car (cdr exp)))) ; (var x (+ 1 (+ 2 3))) -> x
(car (cdr exp))
(error "Wrong expression for var-name:" exp)))
(define (var-value exp)
(if (var? exp)
(car (cdr (cdr exp)))
(error "Wrong expression for var-value:" exp)))
;; make-proc is new lambda
(define (make-proc? exp) (oper=? exp 'make-proc))
(define (make-proc-params exp)
(if (make-proc? exp)
(car (cdr exp)) ; (lambda (num) (= num 5))
(error "Wrong expression for make-proc-params:" exp)))
(define (make-proc-body exp)
(if (make-proc? exp)
(cdr (cdr exp))
(error "Wrong expression for make-proc-body:" exp)))
(define (build-make-proc params body)
(cons 'make-proc (cons params body)))
(define (if? exp) (oper=? exp 'if))
(define (if-pred exp)
(if (if? exp)
(car (cdr exp))
(error "Wrong expression for if-pred:" exp)))
(define (if-conseq exp)
(if (if? exp) ; (if (null? lis) '() (cons 1 2))
(car (cdr (cdr exp)))
(error "Wrong expression for if-conseq:" exp)))
(define (if-altern exp) ; proc for else clause
(if (if? exp)
(if (not (null? (cdr (cdr (cdr exp)))))
(car (cdr (cdr (cdr exp))))
'false)
(error "Wrong expression for if-altern:" exp)))
(define (build-if pred conseq altern)
(list 'if pred conseq altern))
(define (begin? exp) (oper=? exp 'begin))
(define (begin-exps exp)
(if (begin? exp)
(cdr exp)
(error "Wrong expression for begin-exps:" exp)))
(define (build-begin seq)
(cons 'begin seq))
(define (application? exp) (pair? exp))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operand ops) (cdr ops))
(define (cond? exp) (oper=? exp 'cond))
(define (cond-clauses exp)
(if (cond? exp)
(cdr exp)
(error "Wrong expression for cond-clauses:" exp)))
(define (cond-predicate clause) (car clause))
(define (cond-actions clause) (cdr clause))
(define (cond-else-clause? clause) (eq? (cond-predicate clause) 'else))
(define (cond->if exp) (expand-clauses (cond-clauses exp)))
(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
(define (sequence->exp seq)
(cond ((null? seq) '())
((last-exp? seq) (first-exp seq))
(else (build-begin seq))))
(define (expand-clauses clauses)
(if (null? clauses)
'false
(let ((first (car clauses)) (rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause should be last" clauses))
(build-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))
(define (let? exp) (oper=? exp 'let))
(define (let-binds exp)
(if (let? exp)
(car (cdr exp))
(error "Wrong expression for let-binds:" exp)))
(define (let-body exp)
(if (let? exp)
(car (cdr (cdr exp)))
(error "Wrong expression for let-body:" exp)))
(define (bind-vars binds) (map car binds))
(define (bind-exps binds) (map car (map cdr binds)))
(define (build-combination params body exps)
(cons (list 'lambda params body) exps))
(define (let->combination exps)
(let ((binds (let-binds exps)))
(build-combination (bind-vars binds)
(let-body exps)
(bind-exps binds))))
(define (false? exp) (eq? exp false))
(define (true? exp) (not (false? exp)))
(define (build-procedure params body env)
(list 'proc params body env))
(define (compound-proc? p) (oper=? p 'proc))
(define (proc-params p)
(if (compound-procedure? p)
(car (cdr p))
(error "Wrong expression for proc-params:" p)))
(define (proc-body p)
(if (compound-procedure? p)
(car (cdr (cdr p)))
(error "Wrong expression for proc-body:" p)))
(define (proc-env p)
(if (compound-procedure? p)
(car (cdr (cdr (cdr p))))
(error "Wrong expression for proc-env:" p)))
;; primitives.scm
(define (foldl oper init lis)
(if (null? lis)
init
(foldl oper (oper init (car lis)) (cdr lis))))
(define (foldr oper init lis)
(if (null? lis)
init
(oper (car lis) (foldr oper init (cdr lis)))))
(define (v-map proc lis)
(foldr (lambda (first result) (cons (proc first) result)) '() lis))
(define (v-filter pred lis)
(define (filter-helper first result)
(if (pred first)
(cons first result)
result))
(foldr filter-helper '() lis))
(define (v-reverse lis)
(foldl (lambda (init first) (cons first init)) '() lis))
(define (v-length lis)
(foldl (lambda (init first) (+ init 1)) 0 lis))
(define (v-append list-a list-b)
(foldr cons list-b list-a))
(define (v-list-ref lis index)
(if (and (< index (v-length lis)) (>= index 0))
(if (= index 0)
(car lis)
(v-list-ref (cdr lis) (- index 1)))
(error "Index out of range" index)))
(define (v-for-each proc lis)
(if (null? lis)
'done
(begin (proc (car lis))
(v-for-each proc (cdr lis)))))
(define (v-remove ele lis)
(define (remove-helper first init)
(if (equal? first ele)
init
(cons first init)))
(foldr remove-helper '() lis))
(define (v-remove-first ele lis)
(if (not (null? lis))
(if (equal? (car lis) ele)
(cdr lis)
(cons (car lis) (v-remove-first ele (cdr lis))))
(error "Not in list ele:" ele)))
(define (v-and exps)
(cond ((false? (car exps)) 'false)
((null? exps) 'true)
(else (v-and (cdr exps)))))
(define (v-or exps)
(cond ((null? exps) 'false)
((not (false? (car exps))) 'true)
(else (v-or (cdr exps)))))
(define (range num)
(define (range-helper start)
(if (= start (- num 1))
(cons start '())
(cons start (range-helper (+ start 1)))))
(range-helper 0))
(define (my-abs num)
(if (< num 0)
(* -1 num)
num))
(define (v-even? num) (= (remainder num 2) 0))
(define (v-odd? num) (not (v-even? num)))
(define (println data)
(newline)
(display data)
(newline)
'done)
;TODO: Rename keywords to make fancier names
;; 1. cons -> pair
;; 2. car -> head
;; 3. cdr -> tail
(define (primitive-proc? proc) (oper=? proc 'primitive))
(define primitive-procs
(list (list 'foldl foldl)
(list 'foldr foldr)
(list 'map v-map)
(list 'filter v-filter)
(list 'reverse v-reverse)
(list 'length v-length)
(list 'append v-append)
(list 'list-ref v-list-ref)
(list 'for-each v-for-each)
(list 'remove 'v-remove)
(list 'remove-first 'v-remove-first)
(list 'head car)
(list 'tail cdr)
(list 'pair cons)
(list 'null? null?)
(list '+ +)
(list '* *)
(list 'square square)))
(define (primitive-procs-names) (map car primitive-procs))
(define (primitive-proc-vals)
(map (lambda (proc) (list 'primitive (cadr proc))) primitive-procs))
(define (name->primitive proc) (cadr proc))
(define (apply-primitive-proc proc args)
(apply-in-scheme (name->primitive proc) args))
;; eval.scm
(define (first-exp seq) (car seq))
(define (println data)
(newline)
(display data)
(newline))
(define (eval exp env)
(cond ((self-eval? exp) exp)
((name? exp) (lookup-variable exp env))
((quote? exp) (quote-content exp))
((assign? exp) (eval-assign exp env))
((func? exp) (eval-func exp env)) ; TODO
((if? exp) (eval-if exp env))
((make-proc? exp) (build-procedure (make-proc-params exp)
(make-proc-body exp)
env))
((begin? exp) (eval-sequence (begin-exps exp) env)) ; TODO
((cond? exp) (eval (cond->if exp) env))
((application? exp)
(eval-apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
(else (error "Unknown expression -- EVAL" exp))))
(define (list-of-values exps env)
(if (no-operands? exps)
'()
(cons (eval (first-operand exps) env)
(list-of-values (rest-operand exps) env))))
(define (eval-if exp env)
(if (true? (eval (if-pred exp) env))
(eval (if-conseq exp) env)
(eval (if-altern exp) env)))
(define (eval-sequence exps env)
(cond ((last-exp? exps) (eval (first-exp exps) env))
(else (eval (first-exp exps) env)
(eval-sequence (rest-exps exp) env))))
(define (eval-assign exp env)
(set-var-value! (assign-var exp)
(eval (assign-value exp) env)
env)
'done)
(define (eval-func exp env)
(define-variable! (func-name exp) (eval (func-value exp) env) env)
'done)
(define (eval-apply proc args)
(println "apply")
(println proc)
(println args)
(cond ((primitive-proc? proc) (apply-primitive-proc proc args))
((compound-proc? proc)
(eval-sequence (proc-body proc)
(extend-env (proc-params proc)
args
(proc-env proc))))
(else (error "Unknown proc type -- APPLY" proc))))
;; main.scm
(define (println data)
(newline)
(display data)
(newline)
'done)
(define (setup-env)
(let ((init-env (extend-env (primitive-procs-names)
(primitive-proc-vals)
empty-env)))
(define-variable! 'true true init-env)
(define-variable! 'false false init-env)
init-env))
(define global-env (setup-env))
(define apply-in-scheme apply)
(define input-prompt ">> ")
(define output-prompt "Value:")
(define (user-print obj)
(if (compound-procedure? obj)
(display (list 'compound-procedure
(proc-params obj)
(proc-body obj)
'<procedure-env>))
(display obj)))
(define (driver-loop)
(println input-prompt)
(let ((input (read)))
(let ((output (eval input global-env)))
(println output-prompt)
(user-print output)))
(driver-loop))