-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.ss
391 lines (309 loc) · 12.2 KB
/
13.ss
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
;: Single-file version of the interpreter.
;; Easier to submit to server, probably harder to use in the development process
(load "C:/304project/chez-init.ss")
;-------------------+
; |
; DATATYPES |
; |
;-------------------+
; parsed expression
(define-datatype expression expression?
[var-exp
(id symbol?)]
[tiny-list-exp
(body expression?)]
[quote-exp
(body expression?)]
[lit-exp
(datum (lambda (x) (ormap (lambda (pred) (pred x)) (list number? vector? boolean? symbol? string? pair? null?))))]
[lambda-exp-improper-arg
(ids symbol?)
(body (list-of expression?))]
[lambda-exp
(ids list?)
(body (list-of expression?))]
[let-exp
(vars list?)
(vals (list-of expression?))
(body (list-of expression?))]
[let*-exp
(vars list?)
(vals (list-of expression?))
(body (list-of expression?))]
[letrec-exp
(vars list?)
(vals (list-of expression?))
(body (list-of expression?))]
[vector-exp
(id vector?)]
[set!-exp
(var symbol?)
(val expression?)]
[if-exp
(condition expression?)
(body-true expression?)
(body-false expression?)]
[app-exp
(body (list-of expression?))])
;; environment type definitions
(define scheme-value?
(lambda (x) #t))
; datatype for procedures. At first there is only one
; kind of procedure, but more kinds will be added later.
(define-datatype environment environment?
(empty-env-record)
(extended-env-record
(syms (list-of symbol?))
(vals (list-of scheme-value?))
(env environment?)))
(define-datatype proc-val proc-val?
[prim-proc
(name symbol?)]
[closure
(ids (list-of symbol?))
(body (list-of expression?))
(env environment?)])
;-------------------+
; |
; PARSER |
; |
;-------------------+
; Procedures to make the parser a little bit saner.
(define 1st car)
(define 2nd cadr)
(define 3rd caddr)
(define 4th cadddr)
(define parse-exp
(lambda (datum)
(cond
[(symbol? datum) (var-exp datum)]
[(list? datum)
(cond
[(eqv? (car datum) 'lambda)
(cond
[(null? (cddr datum)) (eopl:error 'parse-exp "lambda-expression: incorrect length ~s" datum)]
[else (if (symbol? (2nd datum))
(lambda-exp-improper-arg (2nd datum) (map parse-exp (cddr datum)))
(if (not (andmap symbol? (2nd datum)))
(eopl:error 'parse-exp "lambda's formal arguments ~s must all be symbols" datum)
(lambda-exp (2nd datum) (map parse-exp (cddr datum)))))])]
[(eqv? (car datum) 'let)
(cond
[(null? (cddr datum)) (eopl:error 'parse-exp "~s-expression has incorrect length ~s" datum)]
[(not (list? (2nd datum))) (eopl:error 'parse-exp "declarations in ~s-expression not a list ~s" datum)]
[(not (andmap list? (2nd datum))) (eopl:error 'parse-exp "declaration in ~s-exp is not a proper list ~s" datum)]
[(not (andmap (lambda (x) (= x 2)) (map length (2nd datum)))) (eopl:error 'parse-exp "declaration in ~s-exp must be a list of length 2 ~s" datum)]
[(not (andmap symbol? (map car (2nd datum)))) (eopl:error 'parse-exp "vars in ~s-exp must be symbols ~s" datum)]
[else (let-exp (map car (2nd datum)) (map parse-exp (map cadr (2nd datum))) (map parse-exp (cddr datum)))])]
[(eqv? (car datum) 'let*)
(cond
[(null? (cddr datum)) (eopl:error 'parse-exp "~s-expression has incorrect length ~s" datum)]
[(not (list? (2nd datum))) (eopl:error 'parse-exp "declarations in ~s-expression not a list ~s" datum)]
[(not (andmap list? (2nd datum))) (eopl:error 'parse-exp "declaration in ~s-exp is not a proper list ~s" datum)]
[(not (andmap (lambda (x) (= x 2)) (map length (2nd datum)))) (eopl:error 'parse-exp "declaration in ~s-exp must be a list of length 2 ~s" datum)]
[(not (andmap symbol? (map car (2nd datum)))) (eopl:error 'parse-exp "vars in ~s-exp must be symbols ~s" datum)]
[else (let*-exp (map car (2nd datum)) (map parse-exp (map cadr (2nd datum))) (map parse-exp (cddr datum)))])]
[(eqv? (car datum) 'letrec)
(cond
[(null? (cddr datum)) (eopl:error 'parse-exp "~s-expression has incorrect length ~s" datum)]
[(not (list? (2nd datum))) (eopl:error 'parse-exp "declarations in ~s-expression not a list ~s" datum)]
[(not (andmap list? (2nd datum))) (eopl:error 'parse-exp "declaration in ~s-exp is not a proper list ~s" datum)]
[(not (andmap (lambda (x) (= x 2)) (map length (2nd datum)))) (eopl:error 'parse-exp "declaration in ~s-exp must be a list of length 2 ~s" datum)]
[(not (andmap symbol? (map car (2nd datum)))) (eopl:error 'parse-exp "vars in ~s-exp must be symbols ~s" datum)]
[else (letrec-exp (map car (2nd datum)) (map parse-exp (map cadr (2nd datum))) (map parse-exp (cddr datum)))])]
[(eqv? (car datum) 'set!)
(cond
[(not (= (length datum) 3)) (eopl:error 'parse-exp "set! expression ~s does not have (only) variable and expression" datum)]
[(not (expression? (3rd datum))) (eopl:error 'parse-exp "set! expression ~s does not have (only) variable and expression" datum)]
[else (set!-exp (2nd datum) (3rd datum))])]
[(eqv? (car datum) 'if)
(cond
[(not (= (length datum) 4)) (eopl:error 'parse-exp "if-expression ~s does not have (only) test, then, and else" datum)]
[else (if-exp (parse-exp (2nd datum)) (parse-exp (3rd datum)) (parse-exp (4th datum)))])]
[(eqv? (car datum) 'quote)
(if (null? (cadr datum))
(lit-exp '())
(quote-exp (lit-exp (cadr datum))))]
[else
(if (null? (cdr datum))
(tiny-list-exp (parse-exp (1st datum)))
(app-exp (map parse-exp datum)))])]
[(lambda (x) (ormap (lambda (pred) (pred x)) (list number? vector? boolean? symbol? string? pair? null?))) (lit-exp datum)]
[(vector? datum) (vector-exp datum)]
[else (eopl:error 'parse-exp "bad expression: ~s" datum)])))
(define unparse-exp
(lambda (exp)
(cases expression exp
[var-exp (id) id]
[tiny-list-exp (body) (list (unparse-exp body))]
[lit-exp (id) id]
[lambda-exp-improper-arg (id body) (append (list 'lambda id) (map unparse-exp body))]
[lambda-exp (id body) (append (list 'lambda id) (map unparse-exp body))]
[let-exp (vars vals body) (append (list 'let (map list vars (map unparse-exp vals))) (map unparse-exp body))]
[let*-exp (vars vals body) (append (list 'let* (map list vars (map unparse-exp vals))) (map unparse-exp body))]
[letrec-exp (vars vals body) (append (list 'letrec (map list vars (map unparse-exp vals))) (map unparse-exp body))]
[vector-exp (id) id]
[set!-exp (var val) (list 'set! var val)]
[if-exp (condition body-true body-false) (list 'if (unparse-exp condition) (unparse-exp body-true) (unparse-exp body-false))]
[app-exp (body) (map unparse-exp body)])))
;-------------------+
; |
; ENVIRONMENTS |
; |
;-------------------+
; Environment definitions for CSSE 304 Scheme interpreter.
; Based on EoPL sections 2.2 and 2.3
(define empty-env
(lambda ()
(empty-env-record)))
(define extend-env
(lambda (syms vals env)
(extended-env-record syms vals env)))
(define list-find-position
(lambda (sym los)
(list-index (lambda (xsym) (eqv? sym xsym)) los)))
(define list-index
(lambda (pred ls)
(cond
[(null? ls) #f]
[(pred (car ls)) 0]
[else
(let ([list-index-r (list-index pred (cdr ls))])
(if (number? list-index-r)
(+ 1 list-index-r)
#f))])))
(define apply-env
(lambda (env sym succeed fail)
(cases environment env
[empty-env-record ()
(fail)]
[extended-env-record (syms vals env)
(let ([pos (list-find-position sym syms)])
(if (number? pos)
(succeed (list-ref vals pos))
(apply-env env sym succeed fail)))])))
;-----------------------+
; |
; SYNTAX EXPANSION |
; |
;-----------------------+
; To be added later
;-------------------+
; |
; INTERPRETER |
; |
;-------------------+
(define *prim-proc-names* '(+ - * / add1 sub1 cons = eq? eqv? equal? length list->vector vector->list >= <= car cdr caar cadr cadar caddr list null? list? pair? vector? number? symbol? procedure? zero? not set-car! set-cdr!))
(define init-env ; for now, our initial global environment only contains
(extend-env
*prim-proc-names*
(map prim-proc *prim-proc-names*)
(empty-env)))
(define global-env init-env)
; top-level-eval evaluates a form in the global environment
(define top-level-eval
(lambda (form)
(eval-exp form (empty-env))))
; eval-exp is the main component of the interpreter
(define eval-exp
(let ([identity-proc (lambda (x) x)])
(lambda (exp env)
(cases expression exp
[lit-exp (datum) datum]
[quote-exp (body) (eval-exp body env)]
[var-exp (id)
(apply-env env
id
identity-proc
(lambda ()
(apply-env global-env
id
identity-proc
(lambda ()
(error 'apply-env "variable ~s is not bound" id)))))]
[if-exp (condition body-true body-false)
(if (eval-exp condition env)
(eval-exp body-true env)
(eval-exp body-false env))]
[app-exp (body)
(let ([proc-value (eval-exp (car body) env)]
[args (eval-rands (cdr body) env)])
(apply-proc proc-value args))]
[let-exp (vars vals body)
(eval-body body
(extend-env vars (eval-rands vals env) env))]
[lambda-exp (ids body)
(closure ids body env)]
[else (eopl:error 'eval-exp "Bad abstract syntax: ~a" exp)]))))
; evaluate the list of operands, putting results into a list
(define eval-rands
(lambda (rands env)
(map (lambda (e) (eval-exp e env) )
rands)))
(define eval-body
(lambda (body env)
(if (null? (cdr body))
(eval-exp (car body) env)
(begin
(eval-exp (car body) env)
(eval-body (cdr body) env)))))
; Apply a procedure to its arguments.
; At this point, we only have primitive procedures.
; User-defined procedures will be added later.
(define apply-proc
(lambda (proc-value args)
(cases proc-val proc-value
[prim-proc (op) (apply-prim-proc op args)]
[closure (ids body env)
(eval-body body (extend-env ids args env))]
[else (eopl:error 'apply-proc "Attempt to apply bad procedure: ~s" proc-value)])))
; Usually an interpreter must define each
; built-in procedure individually. We are "cheating" a little bit.
(define apply-prim-proc
(lambda (prim-proc args)
(case prim-proc
[(+) (apply + args)]
[(-) (apply - args)]
[(*) (apply * args)]
[(/) (apply / args)]
[(add1) (+ (1st args) 1)]
[(sub1) (- (1st args) 1)]
[(cons) (cons (1st args) (2nd args))]
[(=) (= (1st args) (2nd args))]
[(eq?) (eq? (1st args) (2nd args))]
[(eqv?) (eqv? (1st args) (2nd args))]
[(equal?) (equal? (1st args) (2nd args))]
[(length) (length (1st args))]
[(list->vector) (list->vector (1st args))]
[(vector->list) (vector->list (1st args))]
[(>=) (>= (1st args) (2nd args))]
[(<=) (<= (1st args) (2nd args))]
[(car) (car (1st args))]
[(cdr) (cdr (1st args))]
[(caar) (caar (1st args))]
[(cadr) (cadr (1st args))]
[(cadar) (cadar (1st args))]
[(caddr) (caddr (1st args))]
[(list) (apply list args)]
[(null?) (null? (1st args))]
[(list?) (list? (1st args))]
[(pair?) (list? (1st args))]
[(vector?) (vector? (1st args))]
[(number?) (number? (1st args))]
[(symbol?) (symbol? (1st args))]
[(procedure?) (proc-val? (1st args))]
[(zero?) (zero? (1st args))]
[(not) (not (1st args))]
[(set-car!) (set-car! (1st args) (2nd args))]
[(set-cdr!) (set-cdr! (1st args) (2nd args))]
[else (error 'apply-prim-proc "Bad primitive procedure name: ~s" prim-proc)])))
(define rep ; "read-eval-print" loop.
(lambda ()
(display "--> ") ;; notice that we don't save changes to the environment...
(let ([answer (top-level-eval (parse-exp (read)))])
(eopl:pretty-print answer)
(newline)
(rep)))) ; tail-recursive, so stack doesn't grow.
(define eval-one-exp
(lambda (x)
(top-level-eval (parse-exp x))))