-
Notifications
You must be signed in to change notification settings - Fork 17
/
exercise-5.x-letrec-lang-registers.rkt
258 lines (231 loc) · 9.24 KB
/
exercise-5.x-letrec-lang-registers.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
#lang eopl
;; Grammar.
(define the-lexical-spec
'([whitespace (whitespace) skip]
[comment ("%" (arbno (not #\newline))) skip]
[identifier (letter (arbno (or letter digit "_" "-" "?"))) symbol]
[number (digit (arbno digit)) number]
[number ("-" digit (arbno digit)) number]))
(define the-grammar
'([program (expression) a-program]
[expression (number) const-exp]
[expression ("-" "(" expression "," expression ")") diff-exp]
[expression ("zero?" "(" expression ")") zero?-exp]
[expression ("if" expression "then" expression "else" expression) if-exp]
[expression (identifier) var-exp]
[expression ("let" identifier "=" expression "in" expression) let-exp]
[expression ("proc" "(" (separated-list identifier ",") ")" expression) proc-exp]
[expression ("(" expression (arbno expression) ")") call-exp]
[expression ("letrec" identifier "(" (separated-list identifier ",") ")" "=" expression "in" expression)
letrec-exp]))
(sllgen:make-define-datatypes the-lexical-spec the-grammar)
(define scan&parse (sllgen:make-string-parser the-lexical-spec the-grammar))
;; Data structures.
(define-datatype proc proc?
[procedure [bvars (list-of symbol?)]
[body expression?]
[env environment?]])
(define-datatype expval expval?
[num-val [value number?]]
[bool-val [boolean boolean?]]
[proc-val [proc proc?]])
(define expval-extractor-error
(lambda (variant value)
(eopl:error 'expval-extractors "Looking for a ~s, found ~s" variant value)))
(define expval->num
(lambda (v)
(cases expval v
[num-val (num) num]
[else (expval-extractor-error 'num v)])))
(define expval->bool
(lambda (v)
(cases expval v
[bool-val (bool) bool]
[else (expval-extractor-error 'bool v)])))
(define expval->proc
(lambda (v)
(cases expval v
[proc-val (proc) proc]
[else (expval-extractor-error 'proc v)])))
(define-datatype environment environment?
[empty-env]
[extend-env [bvar symbol?]
[bval expval?]
[saved-env environment?]]
[extend-env-rec [p-name symbol?]
[b-vars (list-of symbol?)]
[p-body expression?]
[saved-env environment?]])
(define identifier? symbol?)
(define-datatype continuation continuation?
[end-cont]
[zero1-cont [saved-cont continuation?]]
[let-exp-cont [var identifier?]
[body expression?]
[saved-env environment?]
[saved-cont continuation?]]
[if-test-cont [exp2 expression?]
[exp3 expression?]
[saved-env environment?]
[saved-cont continuation?]]
[diff1-cont [exp2 expression?]
[saved-env environment?]
[saved-cont continuation?]]
[diff2-cont [val1 expval?]
[saved-cont continuation?]]
[rator-cont [rands (list-of expression?)]
[saved-env environment?]
[saved-cont continuation?]]
[rand-cont [val1 expval?]
[vals (list-of expval?)]
[rands (list-of expression?)]
[saved-env environment?]
[saved-cont continuation?]])
;; Interpreter.
(define exp 'uninitialized)
(define env 'uninitialized)
(define cont 'uninitialized)
(define val 'uninitialized)
(define vals 'uninitialized)
(define proc1 'uninitialized)
(define apply-procedure/k
(lambda ()
(cases proc proc1
[procedure (vars body saved-env)
(set! exp body)
(set! env (let loop ([env saved-env]
[vars vars]
[vals vals])
(if (null? vars)
env
(loop (extend-env (car vars)
(car vals)
env)
(cdr vars)
(cdr vals)))))
(value-of/k)])))
(define used-end-conts '())
(define apply-cont
(lambda ()
(cases continuation cont
[end-cont () (if (memq cont used-end-conts)
(eopl:error "Continuation is already used.")
(begin (set! used-end-conts (cons cont used-end-conts))
val))]
[zero1-cont (saved-cont)
(set! cont saved-cont)
(set! val (bool-val (zero? (expval->num val))))
(apply-cont)]
[let-exp-cont (var body saved-env saved-cont)
(set! cont saved-cont)
(set! exp body)
(set! env (extend-env var val saved-env))
(value-of/k)]
[if-test-cont (exp2 exp3 saved-env saved-cont)
(set! cont saved-cont)
(if (expval->bool val)
(set! exp exp2)
(set! exp exp3))
(set! env saved-env)
(value-of/k)]
[diff1-cont (exp2 saved-env saved-cont)
(set! cont (diff2-cont val saved-cont))
(set! exp exp2)
(set! env saved-env)
(value-of/k)]
[diff2-cont (val1 saved-cont)
(let ([num1 (expval->num val1)]
[num2 (expval->num val)])
(set! cont saved-cont)
(set! val (num-val (- num1 num2)))
(apply-cont))]
[rator-cont (rands saved-env saved-cont)
(if (null? rands)
(let ([rator-proc (expval->proc val)])
(set! cont saved-cont)
(set! proc1 rator-proc)
(apply-procedure/k))
(begin (set! cont (rand-cont val
'()
(cdr rands)
saved-env
saved-cont))
(set! exp (car rands))
(set! env saved-env)
(value-of/k)))]
[rand-cont (rator-val rand-vals rand-exps saved-env saved-cont)
(if (null? rand-exps)
(let ([rator-proc (expval->proc rator-val)])
(set! cont saved-cont)
(set! proc1 rator-proc)
(set! vals (reverse (cons val rand-vals)))
(apply-procedure/k))
(begin (set! cont (rand-cont rator-val
(cons val rand-vals)
(cdr rand-exps)
saved-env
saved-cont))
(set! exp (car rand-exps))
(set! env saved-env)
(value-of/k)))])))
(define apply-env
(lambda (env search-sym)
(cases environment env
[empty-env () (eopl:error 'apply-env "No binding for ~s" search-sym)]
[extend-env (var val saved-env) (if (eqv? search-sym var)
val
(apply-env saved-env search-sym))]
[extend-env-rec (p-name b-var p-body saved-env) (if (eqv? search-sym p-name)
(proc-val (procedure b-var p-body env))
(apply-env saved-env search-sym))])))
(define value-of/k
(lambda ()
(cases expression exp
[const-exp (num)
(set! val (num-val num))
(apply-cont)]
[var-exp (var)
(set! val (apply-env env var))
(apply-cont)]
[proc-exp (vars body)
(set! val (proc-val (procedure vars body env)))
(apply-cont)]
[letrec-exp (p-name b-vars p-body letrec-body)
(set! exp letrec-body)
(set! env (extend-env-rec p-name b-vars p-body env))
(value-of/k)]
[zero?-exp (exp1)
(set! cont (zero1-cont cont))
(set! exp exp1)
(value-of/k)]
[let-exp (var exp1 body)
(set! cont (let-exp-cont var body env cont))
(set! exp exp1)
(value-of/k)]
[if-exp (exp1 exp2 exp3)
(set! cont (if-test-cont exp2 exp3 env cont))
(set! exp exp1)
(value-of/k)]
[diff-exp (exp1 exp2)
(set! cont (diff1-cont exp2 env cont))
(set! exp exp1)
(value-of/k)]
[call-exp (rator rands)
(set! cont (rator-cont rands env cont))
(set! exp rator)
(value-of/k)])))
(define (init-env)
(empty-env))
(define value-of-program
(lambda (pgm)
(cases program pgm
[a-program (body)
(set! cont (end-cont))
(set! exp body)
(set! env (init-env))
(value-of/k)])))
;; Interface.
(define run
(lambda (string)
(value-of-program (scan&parse string))))
(provide bool-val num-val run)