-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter-meaning.scm
381 lines (342 loc) · 9.72 KB
/
interpreter-meaning.scm
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
(load "match.scm")
(load "sexp-file-io.scm")
(load "derived-form.scm")
(load "set-collection.scm")
(load "mutables.scm")
(define STACK (make-vector (expt 2 17)))
(define STACK-INDEX 0)
(define (stack-push! obj)
(vector-set! STACK STACK-INDEX obj)
(set! STACK-INDEX (add1 STACK-INDEX)))
(define (stack-pop!)
(set! STACK-INDEX (sub1 STACK-INDEX))
(vector-ref STACK STACK-INDEX))
(define ENV '())
(define VAL '())
(define PROC '())
;;; meaning
(define (meaning-quotation c tail?)
`(CONSTANT ,c))
(define (meaning-assignment level rank e tail?)
`(DEEP-SET! ,level ,rank ,(meaning e #f)))
(define (meaning-reference level rank tail?)
`(DEEP-FETCH ,level ,rank))
(define (meaning-alternative pred conseq alter tail?)
(let ([pred (meaning pred #f)]
[conseq (meaning conseq tail?)]
[alter (meaning alter tail?)])
`(ALTERNATIVE ,pred ,conseq ,alter)))
(define (meaning-sequence es tail?)
(if (pair? es)
(if (pair? (cdr es))
(meaning-multi-sequence (car es) (cdr es) tail?)
(meaning (car es) tail?))
(error meaning-sequence 'no-empty-sequence es)))
(define (meaning-multi-sequence e es tail?)
(let* ([m (meaning e #f)]
[ms (meaning-sequence es tail?)])
`(SEQUENCE ,m ,ms)))
(define (meaning-trim-abs free-vars params body tail?)
(let ([body (meaning-sequence body #t)])
`(MAKE-FUNCTION ,free-vars ,(get-arity params) ,body)))
(define (meaning-load e tail?)
`(LOAD ,(meaning e tail?)))
(define (meaning-application f-exp es tail?)
(let* ([m (meaning f-exp #f)]
[m* (meaning-operands es #f)])
(if tail?
`(TAIL-CALL ,m ,m*)
`(CALL ,m ,m*))))
(define (meaning-operands es tail?)
(if (pair? es)
(meaning-some-args (car es) (cdr es) tail?)
(meaning-no-arg tail?)))
(define (meaning-some-args e es tail?)
(let* ([m (meaning e tail?)]
[m* (meaning-operands es tail?)])
`(STORE-ARG ,m ,m*)))
(define (meaning-no-arg tail?)
`(INIT-EMPTY-LIST))
(define (meaning e tail?)
(match e
[,() (guard (or (number? e) (boolean? e) (char? e) (string? e)))
(meaning-quotation e tail?)]
[(svar ,level ,rank) (meaning-reference level rank tail?)]
[(quote ,e) (meaning-quotation e tail?)]
[(if ,pred ,conseq ,alter)
(meaning-alternative pred conseq alter tail?)]
[(begin . ,es) (meaning-sequence es tail?)]
[(set! (,level ,rank) ,e) (meaning-assignment level rank e tail?)]
[(lambda* ,free-vars ,params . ,body) (meaning-trim-abs free-vars params body tail?)]
[(load ,file) (meaning-load file tail?)]
[(,op . ,es) (meaning-application op es tail?)]))
;;; helper
(define (get-arity params)
(let loop ([params params]
[arity 0])
(if (pair? params)
(loop (cdr params) (+ 1 arity))
(list arity (not (null? params))))))
(define (zip-by-arity vs arity)
(let ([improper (cadr arity)]
[k (car arity)])
(if improper
(append (list-head vs k) (list (list-tail vs k)))
vs)))
;;; instruction
(define (CONSTANT v)
(lambda () (set! VAL v)))
(define (ALTERNATIVE pred conseq alter)
(lambda ()
(pred)
(if VAL
(conseq)
(alter))))
(define (LOCAL-SET! rank m)
(lambda ()
(m)
(box-set! (frame-ref ENV rank) VAL)))
(define (LOCAL-FETCH rank)
(lambda ()
(set! VAL (box-val (frame-ref ENV rank)))))
(define (SEQUENCE m m+)
(lambda () (m) (m+)))
(define (TAIL-CALL m m*)
(lambda ()
(m)
(stack-push! VAL)
(m*)
(set! PROC (stack-pop!))
(PROC)))
(define (CALL m m*)
(lambda ()
(m)
(stack-push! VAL)
(m*)
(set! PROC (stack-pop!))
(stack-push! ENV)
(PROC)
(set! ENV (stack-pop!))))
(define (STORE-ARG m m*)
(lambda ()
(m)
(stack-push! VAL)
(m*)
(set! PROC (stack-pop!))
(set! VAL (cons PROC VAL))))
(define (INIT-EMPTY-LIST)
(lambda () (set! VAL '())))
(define (MAKE-FUNCTION free-vars arity body)
(lambda ()
(set! VAL (MAKE-CLOSURE ENV free-vars arity body))))
(define (MAKE-CLOSURE env free-vars arity body)
(let ([bs (map (lambda (r) (frame-ref env r)) (map cadr free-vars))])
(lambda _
(set! ENV (make-frame (append bs (map make-box (zip-by-arity VAL arity)))))
(body))))
(define (LOAD m)
(lambda ()
(m)
(interp-top (cons 'begin (read-sexps VAL)))))
(define (meaning->lambda e)
(match e
[(CONSTANT ,v) (CONSTANT v)]
[(ALTERNATIVE ,pred ,conseq ,alter)
(ALTERNATIVE
(meaning->lambda pred)
(meaning->lambda conseq)
(meaning->lambda alter))]
[(DEEP-SET! global ,rank ,m)
(LOCAL-SET! rank (meaning->lambda m))]
[(DEEP-FETCH global ,rank)
(LOCAL-FETCH rank)]
[(DEEP-SET! 0 ,rank ,m)
(LOCAL-SET! rank (meaning->lambda m))]
[(DEEP-FETCH 0 ,rank)
(LOCAL-FETCH rank)]
[(SEQUENCE ,m ,m+)
(SEQUENCE (meaning->lambda m) (meaning->lambda m+))]
[(TAIL-CALL ,m ,m*)
(TAIL-CALL (meaning->lambda m) (meaning->lambda m*))]
[(CALL ,m ,m*)
(CALL (meaning->lambda m) (meaning->lambda m*))]
[(STORE-ARG ,m ,m*)
(STORE-ARG (meaning->lambda m) (meaning->lambda m*))]
[(INIT-EMPTY-LIST)
(INIT-EMPTY-LIST)]
[(MAKE-FUNCTION ,free-vars ,arity ,body)
(MAKE-FUNCTION free-vars arity (meaning->lambda body))]
[(LOAD ,m)
(LOAD (meaning->lambda m))]
[,()
(error meaning->lambda 'unknown e)]))
;;; interp
(define (interp-top e)
(let*
([e* (desugar e)]
[exports (collect-definition e*)]
[e* (convert-definition-top e*)]
[imports (free-vars e*)]
[syms (append imports exports)]
[e* (trim-proc e*)]
[_ (GLOBAL.VALS.EXTEND (map (lambda (s) (list s s)) syms))]
[id-env (map (lambda (ps) (list (caddr ps) (caddr ps))) (car GLOBAL.SENV))]
[e* (translate e* GLOBAL.SENV)]
[m (meaning e* #t)]
[m* (meaning->lambda m)]
)
(set! ENV GLOBAL.FRAME)
(m*)
VAL
))
(define (interp* e)
(GLOBAL.VALS.INIT)
(GLOBAL.VALS.EXTEND
(map (lambda (ps) (list (car ps) (convert-primitive (cadr ps))))
(primitives.impl)))
(interp-top e))
;;; globals
(define GLOBAL.SENV (list '()))
(define GLOBAL.FRAME (make-frame '()))
(define (GLOBAL.VALS.INIT)
(set! GLOBAL.SENV (list '()))
(set! GLOBAL.FRAME (make-frame '())))
(define (GLOBAL.VALS.EXTEND pairs)
(let* ([syms (set-diff (map car pairs)
(map car (car GLOBAL.SENV)))]
[pairs
(filter (lambda (p) (member (car p) syms)) pairs)])
(append-to-curr-global! (map car pairs) GLOBAL.SENV)
(frame-append! GLOBAL.FRAME (map (lambda (p) (make-box (cadr p))) pairs))))
(define (convert-primitive f)
(lambda _
(set! VAL (apply f VAL))
))
(define (primtive.apply . _)
(let ([proc (car VAL)])
(set! VAL (cadr VAL))
(apply proc VAL)
VAL))
(define (primitive-call-with-port . _)
(let ([port (car VAL)]
[proc (cadr VAL)])
(set! VAL (list port))
(proc)
VAL))
(define (primitives.impl)
(list
(list 'apply primtive.apply)
(list 'add1 add1)
(list 'sub1 sub1)
(list '+ +)
(list '- -)
(list '* *)
(list 'div div)
(list '<= <=)
(list '>= >=)
(list '= =)
(list '< <)
(list 'expt expt)
(list 'ash ash)
(list 'lognot lognot)
(list 'logor logor)
(list 'logand logand)
;;; predicate
(list 'equal? equal?)
(list 'eq? eq?)
(list 'char? char?)
(list 'vector? vector?)
(list 'number? number?)
(list 'boolean? boolean?)
(list 'string? string?)
(list 'pair? pair?)
(list 'null? null?)
(list 'symbol? symbol?)
(list 'procedure? procedure?)
;;; others
(list 'void void)
(list 'gensym gensym)
(list 'read read)
(list 'cons cons)
(list 'car car)
(list 'cdr cdr)
(list 'set-car! set-car!)
(list 'set-cdr! set-cdr!)
;;; symbol,string
(list 'string->symbol string->symbol)
(list 'symbol->string symbol->string)
(list 'string-length string-length)
(list 'native-transcoder native-transcoder)
(list 'string->bytevector string->bytevector)
(list 'bytevector->u8-list bytevector->u8-list)
(list 'integer->char integer->char)
(list 'list->string list->string)
(list 'string-append string-append)
;;; vector
(list 'vector? vector?)
(list 'make-vector make-vector)
(list 'list->vector list->vector)
(list 'vector-ref vector-ref)
(list 'vector-set! vector-set!)
(list 'vector-append vector-append)
(list 'vector-length vector-length)
;;; script
(list 'command-line command-line)
(list 'error error)
;;; file-io
(list 'pretty-print pretty-print)
(list 'display display)
(list 'newline newline)
(list 'call-with-port primitive-call-with-port)
(list 'eof-object? eof-object?)
(list 'open-input-file open-input-file)
(list 'open-output-file open-output-file)
(list 'open-file-output-port open-file-output-port)
(list 'open-file-input-port open-file-input-port)
(list 'read-sexps read-sexps)
;;; derivable
(list 'list list)
(list 'append! append!)
(list 'iota iota)
(list 'list-ref list-ref)
(list 'list-head list-head)
(list 'list-tail list-tail)
(list 'length length)
(list 'list? list?)
(list 'list* list*)
(list 'reverse reverse)
(list 'append append)
(list 'member member)
(list 'assoc assoc)
(list 'map map)
(list 'filter filter)
(list 'fold-left fold-left)
(list 'caar caar)
(list 'cdar cdar)
(list 'cadr cadr)
(list 'cddr cddr)
(list 'caaar caaar)
(list 'cadar cadar)
(list 'caadr caadr)
(list 'caddr caddr)
(list 'cdaar cdaar)
(list 'cddar cddar)
(list 'cdadr cdadr)
(list 'cdddr cdddr)
(list 'caaaar caaaar)
(list 'caadar caadar)
(list 'caaadr caaadr)
(list 'caaddr caaddr)
(list 'cadaar cadaar)
(list 'caddar caddar)
(list 'cadadr cadadr)
(list 'cadddr cadddr)
(list 'cdaaar cdaaar)
(list 'cdadar cdadar)
(list 'cdaadr cdaadr)
(list 'cdaddr cdaddr)
(list 'cddaar cddaar)
(list 'cdddar cdddar)
(list 'cddadr cddadr)
(list 'cddddr cddddr)
))