-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpretreat.ss
239 lines (208 loc) · 8.03 KB
/
pretreat.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
;; The core 'meaning' procedures that get used from chapter 6 & 7
;; Requires prelude.ss, env.ss to be loaded
;; This actually comes from the previous section §6.2, and isn't
;; changed in §6.3. All the procedures to which it delegates do
;; change, of course.
(define (meaning e r tail?)
(if (pair? e)
(case (car e)
((quote) (meaning-quotation (cadr e) r tail?))
((lambda) (meaning-abstraction (cadr e) (cddr e) r tail?))
((if) (meaning-alternative (cadr e) (caddr e) (cadddr e)
r tail?))
((begin) (meaning-sequence (cdr e) r tail?))
((set!) (meaning-assignment (cadr e) (caddr e) r tail?))
(else (meaning-application (car e) (cdr e) r tail?)))
(if (symbol? e)
(meaning-deref e r tail?)
(meaning-quotation e r tail?))))
;; Literal values and quotations
(define (meaning-quotation v r tail?)
(CONSTANT v))
;; Variable references
(define (meaning-deref n r tail?)
(let ((kind (compute-kind r n)))
(if kind
(case (car kind)
((local)
(let ((level (cadr kind))
(index (cddr kind)))
(if (= level 0)
(SHALLOW-ARGUMENT-REF index)
(DEEP-ARGUMENT-REF level index))))
((global)
(let ((index (cdr kind)))
(CHECKED-GLOBAL-REF index)))
((predefined)
(let ((index (cdr kind)))
(PREDEFINED index))))
(compiler-error "No such variable" n))))
;; Conditional
(define (meaning-alternative ec et ef r tail?)
(let ((mc (meaning ec r #f))
(mt (meaning et r tail?))
(mf (meaning ef r tail?)))
(ALTERNATIVE mc mt mf)))
;; Assignment
(define (meaning-assignment n e r tail?)
(let ((m (meaning e r #f))
(kind (compute-kind r n)))
(if kind
(case (car kind)
((local)
(let ((level (cadr kind))
(index (cddr kind)))
(if (= level 0)
(SHALLOW-ASSIGNMENT! index m)
(DEEP-ASSIGNMENT! level index m))))
((global)
(let ((index (cdr kind)))
(GLOBAL-SET! index m)))
((predefined)
(compiler-error
"Attempted to assign to immutable variable" n)))
(compiler-error "Unknown variable" n))))
;; Begin
(define (meaning-sequence e+ r tail?)
(if (pair? e+)
(if (pair? (cdr e+))
(meaning*-multiple-sequence (car e+) (cdr e+) r tail?)
(meaning*-single-sequence (car e+) r tail?))
(compiler-error "Illegal form (begin)")))
(define (meaning*-single-sequence e r tail?)
(meaning e r tail?))
(define (meaning*-multiple-sequence e e* r tail?)
(let ((m (meaning e r #f))
(m+ (meaning-sequence e* r tail?)))
(SEQUENCE m m+)))
;; OK now the slightly harder bits. Starting with
;; Abstraction
(define (meaning-abstraction nn* e+ r tail?)
(let parse ((n* nn*)
(regular '()))
(cond ((pair? n*) (parse (cdr n*) (cons (car n*) regular)))
;; We ran through them all and no dot!
;; Use nn* to avoid having to reverse `regular`
((null? n*) (meaning-fix-abstraction nn* e+ r tail?))
(else (meaning-dotted-abstraction
(reverse regular) n* e+ r tail?)))))
(define (meaning-fix-abstraction n* e+ r tail?)
(let* ((arity (length n*))
(r2 (r-extend* r n*))
(m+ (meaning-sequence e+ r2 #t)))
(FIX-CLOSURE m+ arity)))
(define (meaning-dotted-abstraction n* n e+ r tail?)
(let* ((arity (length n*))
(r2 (r-extend* r (append n* (list n))))
(m+ (meaning-sequence e+ r2 #t)))
(NARY-CLOSURE m+ arity)))
;; The most fun of all, application
(define (meaning-application e e* r tail?)
(cond ((and (symbol? e)
(let ((kind (compute-kind r e)))
(and (pair? kind)
(eq? 'predefined (car kind))
;; As before I move the checking into
;; meaning-primitive-application; it just gets to
;; messy here.
(meaning-primitive-application e e* r tail?)))))
((and (pair? e)
(eq? 'lambda (car e)))
(meaning-closed-application e e* r tail?))
(else (meaning-regular-application e e* r tail?))))
(define (meaning-regular-application e e* r tail?)
(let ((m (meaning e r #f))
(m* (meaning* e* r (length e*) #f)))
(if tail?
(TR-REGULAR-CALL m m*)
(REGULAR-CALL m m*))))
(define (meaning* e* r size tail?)
(if (pair? e*)
(meaning-some-args (car e*) (cdr e*) r size tail?)
(meaning-no-arg r size tail?)))
(define (meaning-no-arg r size tail?)
(ALLOCATE-FRAME size))
(define (meaning-some-args e e* r size tail?)
(let ((m (meaning e r tail?))
(m* (meaning* e* r size tail?))
(index (- size (+ (length e*) 1))))
(STORE-ARGUMENT m m* index)))
;; left-left-lambda
;; ((lambda (n*...) body) ee*...)
(define (meaning-closed-application e ee* r tail?)
(let parse ((n* (cadr e))
(e* ee*)
(regular '()))
(cond
((pair? n*)
(if (pair? e*)
(parse (cdr n*) (cdr e*) (cons (car n*) regular))
(compiler-error "Too few arguments: need" (cadr e)
"got" ee*)))
((null? n*)
(if (null? e*)
(meaning-fix-closed-application
(cadr e) (cddr e) ee* r tail?)
(compiler-error "Too many arguments: need" (cadr e)
"got" ee*)))
(else
(meaning-dotted-closed-application
(reverse regular) n* (cddr e) ee* r tail?)))))
;; ((lambda (a b) (+ a b)) 1 2)
(define (meaning-fix-closed-application n* body e* r tail?)
(let* ((m* (meaning* e* r (length e*) #f))
(r2 (r-extend* r n*))
(m+ (meaning-sequence body r2 tail?)))
(if tail?
(TR-FIX-LET m* m+)
(FIX-LET m* m+))))
;; ((lambda as (apply + as)) 1 2 3)
(define (meaning-dotted-closed-application n* n body e* r tail?)
(let* ((m* (meaning-dotted* e* r (length e*) (length n*) #f))
(r2 (r-extend* r (append n* (list n))))
(m+ (meaning-sequence body r2 tail?)))
(if tail?
(TR-FIX-LET m* m+)
(FIX-LET m* m+))))
(define (meaning-dotted* e* r size arity tail?)
(if (pair? e*)
(meaning-some-dotted-args (car e*) (cdr e*)
r size arity tail?)
(meaning-no-dotted-arg r size arity tail?)))
(define (meaning-some-dotted-args e e* r size arity tail?)
(let ((m (meaning e r tail?))
(m* (meaning-dotted* e* r size arity tail?))
(index (- size (+ (length e*) 1))))
(if (< index arity)
(STORE-ARGUMENT m m* index)
(CONS-ARGUMENT m m* arity))))
(define (meaning-no-dotted-arg r size arity tail?)
(ALLOCATE-DOTTED-FRAME arity))
(define (meaning-primitive-application e e* r tail?)
(let ((desc (get-description e)))
(and desc ;; I don't know why it wouldn't be there, but anyway
;; desc = (function address . arity)
(or (eq? 'function (car desc))
(compiler-error "Function expected"))
(let ((address (cadr desc))
(size (caddr desc)))
(and
;; I did say I would check arity here
(or (= size (length e*))
(compiler-error "Wrong arity for " e
"expected" size))
;; This time I'll do it the book way; this sets up some of the VM
;; instructions later on.
(case size
((0) (CALL0 address))
((1) (let ((m (meaning (car e*) r #f)))
(CALL1 address m)))
((2) (let ((m1 (meaning (car e*) r #f))
(m2 (meaning (cadr e*) r #f)))
(CALL2 address m1 m2)))
((3) (let ((m1 (meaning (car e*) r #f))
(m2 (meaning (cadr e*) r #f))
(m3 (meaning (caddr e*) r #f)))
(CALL3 address m1 m2 m3)))
(else
(meaning-regular-application e e* r tail?))))))))