-
Notifications
You must be signed in to change notification settings - Fork 4
/
eval.rkt
285 lines (232 loc) · 7.75 KB
/
eval.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
;r5rs
;Frame and environment data structures
(define apply-in-underlying-scheme apply)
(define make-frame cons)
(define frame-variables car)
(define frame-values cdr)
(define (add-binding-to-frame! var val frame)
(set-car! frame (cons var (car frame)))
(set-cdr! frame (cons val (cdr frame))))
(define f (make-frame '(a b c) '(1 2 3)))
(display "f") f
(display "(frame-values f)") (frame-values f)
(define first-frame car)
(define enclosing-environment cdr)
(define the-empty-environment '())
(define (extend-environment var vals base-env)
(cons (make-frame var vals)
base-env))
(define e (extend-environment '(a b) '(1 2) the-empty-environment))
(set! e (extend-environment '(a b c) '(aa bb cc) e))
(display "e") e
(define (lookup-variable-value var env)
(define (scan vars vals)
(cond ((null? vars)
(lookup-variable-value var (enclosing-environment env)))
((eq? var (car vars))
(car vals))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(scan (frame-variables (first-frame env))
(frame-values (first-frame env)))))
(display "(lookup-variable-value b e)") (lookup-variable-value 'b e)
(define (set-variable-value! var val env)
(define (scan vars vals)
(cond ((null? vars)
(set-variable-value! var (enclosing-environment env)))
((eq? var (car vars))
(set-car! vals val))
(else
(scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable -- SET!" var)
(scan (frame-variables (first-frame env))
(frame-values (first-frame env)))))
(set-variable-value! 'b 33 e)
(display "e") e
(define (define-variable! var val env)
(let ((frame (first-frame env)))
(define (scan vars vals)
(cond ((null? vars)
(add-binding-to-frame! var val frame))
((eq? var (car vars))
(set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(scan (frame-variables frame)
(frame-values frame))))
(define-variable! 'd 66 e)
(display "e") e
;Tagged list
(define (tagged-list? exp tag)
(and (pair? exp)
(eq? (car exp) tag)))
;Primitive procedure
(define (primitive-procedure? proc)
(tagged-list? proc 'primitive))
(define primitive-implementation cadr)
(define (apply-primitive-procedure proc args)
(apply-in-underlying-scheme
(primitive-implementation proc) args))
(display "(apply-primitive-procedure (list 'primitive +) '(1 2))") (apply-primitive-procedure (list 'primitive +) '(1 2))
;Procedure data structure
(define (make-procedure parameters body env)
(list 'procedure parameters body env))
(define (compound-procedure? p)
(tagged-list? p 'procedure))
(define (procedure-parameters p) (cadr p))
(define (procedure-body p) (caddr p))
(define (procedure-environment p) (cadddr p))
(define (self-evaluating? exp)
(or (number? exp) (string? exp)))
(define variable? symbol?)
(define (quoted? exp)
(tagged-list? exp 'quote))
(define text-of-quotation cadr)
(define (assignment? exp)
(tagged-list? exp 'set!))
(define assignment-variable cadr)
(define assignment-value caddr)
(define (eval-assignment exp env)
(set-variable-value! (assignment-variable exp)
( (assignment-value exp) env)
env)
'ok)
(define (definition? exp)
(tagged-list? exp 'define))
(define (definition-variable exp)
(if (symbol? (cadr exp))
(cadr exp)
(caadr exp)))
(define (definition-value exp)
(if (symbol? (cadr exp))
(caddr exp)
(make-lambda (cdadr exp) ; formal parameters
(cddr exp)))) ; body
(define (eval-definition exp env)
(define-variable!
(definition-variable exp)
(eval (definition-value exp) env)
env)
'ok)
(define (lambda? exp) (tagged-list? exp 'lambda))
(define lambda-parameters cadr)
(define lambda-body cddr)
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
(define (make-procedure parameters body env)
(list 'procedure parameters body env))
(define (if? exp) (tagged-list? exp 'if))
(define if-predicate cadr)
(define if-consequent caddr)
(define (if-alternative exp)
(if (not (null? (cdddr exp)))
(cadddr exp)
'false))
(define (begin? exp) (tagged-list? exp 'begin))
(define begin-actions cdr)
(define (last-exp? seq) (null? (cdr seq)))
(define first-exp car)
(define rest-exps cdr)
(define (eval-sequence exps env)
(cond ((last-exp? exps) (eval (first-exp exps) env))
(else (eval (first-exp exps) env)
(eval-sequence (rest-exps exps) env))))
(define (application? exp) (pair? exp))
(define operator car)
(define operands cdr)
(define no-operands? null?)
(define first-operand car)
(define rest-operands cdr)
(define (list-of-values exps env)
(if (no-operands? exps)
'()
(cons (eval (first-operand exps) env)
(list-of-values (rest-operands exps) env))))
(define (eval exp env)
(cond ((self-evaluating? exp)
exp)
((variable? exp)
(lookup-variable-value exp env))
((quoted? exp)
(text-of-quotation exp))
((assignment? exp)
(eval-assignment exp env))
((definition? exp)
(eval-definition exp env))
((if? exp)
(eval-if exp env))
((lambda? exp)
(make-procedure (lambda-parameters exp)
(lambda-body exp)
env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((application? exp)
(apply (eval (operator exp) env)
(list-of-values (operands exp) env)))
(else
"error")
))
;Populate primiitive procedures
(define primitive-procedures
(list (list 'car car)
(list 'cdr cdr)
(list 'cons cons)
(list 'null? null?)
(list '+ +)
;<more primitives>
))
(define (primitive-procedure-names)
(map car primitive-procedures))
(define (primitive-procedure-objects)
(map (lambda (proc) (list 'primitive (cadr proc)))
primitive-procedures))
;Apply
(define (apply procedure arguments)
(cond ((primitive-procedure? procedure)
(apply-primitive-procedure procedure arguments))
((compound-procedure? procedure)
(eval-sequence
(procedure-body procedure)
(extend-environment
(procedure-parameters procedure)
arguments
(procedure-environment procedure))))
(else
(error
"Unknown procedure type -- APPLY" procedure))))
(define (setup-environment)
(let ((initial-env
(extend-environment (primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)))
(define-variable! 'true #t initial-env)
(define-variable! 'false #f initial-env)
initial-env))
(define the-global-environment (setup-environment))
;REPL
(define input-prompt ";;; M-Eval input:")
(define output-prompt ";;; M-Eval value:")
(define (driver-loop)
(display the-global-environment)
(prompt-for-input input-prompt)
(let ((input (read)))
(let ((output (eval input the-global-environment)))
(announce-output output-prompt)
(user-print output)))
(driver-loop))
(define (prompt-for-input string)
(newline) (newline) (display string) (newline))
(define (announce-output string)
(newline) (display string) (newline))
(define (user-print object)
(if (compound-procedure? object)
(display (list 'compound-procedure
(procedure-parameters object)
(procedure-body object)
'<procedure-env>))
(display object)))
(define the-global-environment (setup-environment))
(driver-loop)
(apply `(primitive ,+) '(1 2))