-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-scheme.rkt
315 lines (266 loc) · 8.26 KB
/
my-scheme.rkt
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
#lang racket
(require racket/hash)
(define (my-and . args)
(if (empty? args)
#t
(my-and-2 args)))
(define (my-and-2 args)
(if (false? (car args))
#f
(if (eq? (cdr args) '())
(car args)
(my-and-2 (cdr args)))))
(define (my-or . args)
(if (empty? args)
#f
(my-or-2 args)))
(define (my-or-2 args)
(if (false? (car args))
(if (empty? (cdr args))
#f
(my-or-2 (cdr args)))
(car args)))
;;;;
;;;; Expression categorizers
;;;;
(define (is-value? expr)
(or (boolean? expr)
(number? expr)
(null? expr)
(string? expr)))
(define (is-variable? expr)
(symbol? expr))
(define (is-quoted? expr)
(eq? 'quote (car expr)))
(define (is-assign? expr)
(eq? (car expr) 'set!))
(define (is-define? expr)
(eq? (car expr) 'define))
(define (is-if? expr)
(eq? (car expr) 'if))
(define (is-lambda? expr)
(eq? (car expr) 'lambda))
(define (is-application? expr)
(pair? expr))
; (and (pair? expr)
; (or (primitive-procedure? (car expr))
; (defined-procedure? (car expr))
; (eq? (car expr) 'and)
; (eq? (car expr) 'or))))
(define primitive-env
(cons (hash
;;
'cons (list 'primitive cons)
'car (list 'primitive car)
'cdr (list 'primitive cdr)
;;
'+ (list 'primitive +)
'- (list 'primitive -)
'* (list 'primitive *)
'/ (list 'primitive /)
'remainder (list 'primitive remainder)
'modulo (list 'primitive modulo)
'and (list 'primitive my-and)
'or (list 'primitive my-or)
;;
'eq? (list 'primitive eq?)
'equal? (list 'primitive equal?)
'= (list 'primitive =)
'> (list 'primitive >)
'< (list 'primitive <)
'>= (list 'primitive >=)
'<= (list 'primitive <=)
'empty? (list 'primitive empty?)
'number? (list 'primitive number?)
'pair? (list 'primitive pair?)
'symbol? (list 'primitive symbol?)
) 0))
(define (defined-procedure? proc env)
(eq? (car proc) 'procedure))
; (hash-has-key? (env-frame env) proc))
(define (primitive-procedure? proc)
(eq? (car proc) 'primitive))
; (hash-has-key? (env-frame primitive-env) proc))
;;;;
;;;; Expression evaluators
;;;;
(define (text-of-quote expr)
(cadr expr))
(define (eval-assignment expr env)
(env-set (cadr expr)
(car (my-eval (caddr expr) env '()))
env))
(define (eval-define expr env)
(if (list? (cadr expr))
(eval-define-procedure expr env)
(env-define (cadr expr)
(car (my-eval (caddr expr) env '()))
env)))
(define (eval-define-procedure expr env)
(env-define (caadr expr) (make-lambda (cdr (cadr expr)) (cddr expr) env) env))
(define (eval-if expr env)
(if (car (my-eval (cadr expr) env '()))
(my-eval (caddr expr) env '())
(my-eval (cadddr expr) env '())))
(define (make-lambda args body env)
(list 'procedure args body))
;;;
;;; EVAL AND APPLY
;;;
(define (my-eval expr env env-map)
(cond
[(is-value? expr) (cons expr env)]
[(is-variable? expr) (cons (env-lookup-variable env expr) env)]
[(is-quoted? expr) (cons (text-of-quote expr) env)]
; NOTE not sure what to return here, since there must be a return value
[(is-assign? expr) (cons #t (eval-assignment expr env))]
[(is-define? expr) (cons #t (eval-define expr env))]
[(is-if? expr) (eval-if expr env)]
[(is-lambda? expr)
(cons (make-lambda (cadr expr) (cddr expr) env)
env)]
[(is-application? expr)
(my-apply (car (my-eval (car expr) env '()))
(eval-args (cdr expr) env)
env)]
[else expr]))
(define (eval-prog exprs)
(eval-prog-2 exprs (get-global-env)))
(define (eval-prog-2 exprs env)
(let ([res (my-eval (car exprs) env '())])
(if (empty? (cdr exprs))
(car res)
(eval-prog-2 (cdr exprs) (cdr res)))))
(define (my-apply proc args env)
(cons
(cond
[(primitive-procedure? proc)
(apply-primitive-procedure proc args env)]
[(defined-procedure? proc env)
(apply-defined-procedure proc args env)]
[else (error "Invalid procedure")])
env))
(define (eval-args args env)
(if (null? args)
'()
(cons (car (my-eval (car args) env '()))
(eval-args (cdr args) env))))
(define (apply-primitive-procedure proc args env)
(apply (cadr proc) args))
(define (apply-defined-procedure proc args env)
(let* (
;[procedure (env-get env proc)]
[params (cadr proc)]
[body (cddr proc)]
[new-env (env-inherit env params args)])
(eval-prog-2 (car body) new-env)))
;;;
;;; ENVIRONMENTS
;;;
(define (list-max xs)
(list-max-2 xs (car xs)))
(define (list-max-2 xs max)
(if (null? xs)
max
(if (> (car xs) max)
(list-max-2 (cdr xs) (car xs))
(list-max-2 (cdr xs) max))))
(define (get-global-env)
(cons (hash
; global default values
'x 133
) 0))
; environment = (cons hash parent-name)
(define (get-global-env-map) '())
; Adds/ sets a value in an environment
(define (env-define ident val env)
; The identifier already exists in this env, update instead
(cons (hash-update (car env) ident (λ _ val)
; Adds a new value if value does not exist
(hash-set (car env) ident val)) '()))
; Update an existing value in an environment
(define (env-set ident val env)
(cons (hash-update (car env) ident (λ x val)
(λ _ (error "Cannot set variable before its definition")))
'()))
(define (env-get env key)
(hash-ref (env-frame env) key))
;; returns the current/ top frame
(define (env-frame env) (car env))
(define (env-parent env) (cdr env))
(define (env-inherit parent-env names values)
(cons (make-immutable-hash (zip names values))
parent-env))
(define (zip xs ys)
(map cons xs ys))
(define (env-lookup-variable env name)
(if (hash-has-key? (env-frame primitive-env) name)
(hash-ref (env-frame primitive-env) name)
(if (hash-has-key? (env-frame env) name)
(hash-ref (env-frame env) name)
(if (eq? (env-parent env) 0)
(error "Undefined variable")
(env-lookup-variable (env-parent env) name)))))
;;;
;;; TEST CASES
;;;
(define test-prog-1
(list
'(define hello (lambda (q) (* 2 (* q q))))
'(hello 3)
))
(define prog-2
(list
'(define (bla x y) (+ x y))
'(bla 1 2)
))
(define higher-order
(list
'(define (ya f x y) (f x y))
'(ya + 9 5)))
(define example-prog
(list
'(define (even x)
(= (remainder x 2) 0))
'(even 10)
'(define (length elems)
(if (empty? elems) 0 (+ 1 (length (cdr elems)))))
'(length (cons 1 (cons 2 (cons 3 (quote ())))))
))
;;; Test "real" programs
(eval-prog test-prog-1)
(eval-prog prog-2)
(eval-prog higher-order)
(eval-prog example-prog)
;;; Test expressions
(my-eval 'x (get-global-env) (get-global-env-map))
(my-eval '"hello" (get-global-env) (get-global-env-map))
(my-eval '123 (get-global-env) (get-global-env-map))
(my-eval '#t (get-global-env) (get-global-env-map))
(my-eval '(define (bla x) x) (get-global-env) '())
(my-eval '(define x 1) (get-global-env) '())
(my-eval '(lambda (x y z) 1) (get-global-env) '())
(my-eval '(set! x 100) (get-global-env) '())
(my-eval '(cons 1 (cons 2 (quote ()))) (get-global-env) '())
(my-eval '(car (cdr (cons 1 (cons 2 (quote '()))))) (get-global-env) '())
(my-eval '(if #t 123 9) (get-global-env) (get-global-env-map))
(my-eval '(+ 3 (+ (* 4 4) (* 3 3))) (get-global-env) '())
(my-eval '(- 4 3) (get-global-env) '())
(my-eval '(/ (* 4 4) (* 2 3)) (get-global-env) '())
(my-eval '(remainder 10 7) (get-global-env) '())
(my-eval '(modulo 10 7) (get-global-env) '())
(my-eval '(and 1 2) (get-global-env) '())
(my-eval '(or #f #t #f) (get-global-env) '())
(my-eval '(eq? 1 2) (get-global-env) '())
(my-eval '(equal? '() '()) (get-global-env) '())
(my-eval '(> 2 1) (get-global-env) '())
(my-eval '(< 2 1) (get-global-env) '())
(my-eval '(= 2 1) (get-global-env) '())
(my-eval '(<= 3 3) (get-global-env) '())
(my-eval '(>= 5 5) (get-global-env) '())
(my-eval '(empty? (quote ())) (get-global-env) '())
(my-eval '(number? 3) (get-global-env) '())
(my-eval '(number? #f) (get-global-env) '())
(my-eval '(pair? (cons 1 2)) (get-global-env) '())
(my-eval '(symbol? (quote hello)) (get-global-env) '())
(my-eval '(symbol? 2) (get-global-env) '())