-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcps.ol
292 lines (275 loc) · 6.3 KB
/
cps.ol
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
(define (atom? exp)
(or (number? exp)
(string? exp)
(boolean? exp)
(null? exp)
(symbol? exp)))
(define (inspect-short exp)
(vector-slice (inspect exp) 0 100))
(define (cps-quote data)
(lambda (k)
(k `(quote ,data))))
(define (cps-set! var form)
(lambda (k)
((cps form)
(lambda (a)
`(begin
(cps-jump
#f
,(inspect-short `(set! ,var ,a))
(lambda ()
(set! ,var ,a)
,(k ''void))))))))
(define (cps-define var/func body)
(if (list? var/func)
(lambda (k)
((cps `(lambda ,(cdr var/func) ,@body))
(lambda (a)
`(begin
(define ,(car var/func) ,a)
,(k ''void)))))
(lambda (k)
((cps (car body))
(lambda (a)
`(begin
(define ,var/func ,a)
,(k ''void)))))))
(define (cps-if bool form1 form2)
(lambda (k)
((cps bool)
(lambda (b)
`(if ,b
,((cps form1) k)
,(if (== form2 #f)
(k ''void)
((cps form2) k)))))))
(define (cps-begin e)
(if (list? e)
(if (list? (cdr e))
(let ((v (gensym)))
(lambda (k)
((cps (car e))
(lambda (a)
;; force the order of evaluation so that set!'s
;; happen appropriately by wrapping the next cps in a
;; lambda
`((lambda (,v)
,((cps-begin (cdr e))
(lambda (v) (k v))))
,a)))))
(cps (car e)))
(cps '())))
(define (cps-application e)
(lambda (k)
(if (list-find primitives (car e))
((cps-terms (cdr e))
(lambda (t)
`(cps-jump
#f
,(inspect-short `(,(car e) ,@t))
(lambda () ,(k `(,(car e) ,@t))))))
((cps-terms e)
(lambda (t)
(let ((d (gensym)))
`(cps-jump
#f
,(inspect-short `(,(car t) ,@(cdr t)))
(lambda () (,(car t) (lambda (,d) ,(k d)) ,@(cdr t))))))))))
(define (cps-terms e)
(if (list? e)
(lambda (k)
((cps (car e))
(lambda (a)
((cps-terms (cdr e))
(lambda (as)
(k (cons a as)))))))
(lambda (k) (k '()))))
(define (cps-abstraction vars body)
(lambda (k)
(k (let ((c (gensym)))
`(lambda (,c ,@vars)
,((cps (cons 'begin body))
(lambda (a) `(,c ,a))))))))
(define (cps-tilde func args)
(lambda (k)
((cps-terms args)
(lambda (t)
`(,func ,@t
(lambda (err msg)
(cps-trampoline
(cps-jump
#f
"<callback>"
(lambda ()
,(k 'msg)
#f)))))))))
;; (define (cps-call/cc catcher)
;; (lambda (k)
;; (let ((c (k )))
;; )
;; (let ((c (car (cadr catcher)))
;; (body (cddr catcher)))
;; `(lambda (,c)
;; ,((cps 'begin body)
;; (lambda (a) `(cps-jump (lambda () (k r)))))
;; ))))
(define primitives
'(and or
+ - * / % > < <= >= >> <<
bitwise-or bitwise-and
type
number?
string?
symbol?
key?
boolean?
null?
list?
vector?
dict?
function?
literal?
str
symbol->key
key->symbol
string->key
key->string
string->symbol
symbol->string
_emptylst
list
cons
car
cdr
cadr
cddr
cdar
caddr
cdddr
cadar
cddar
caadr
cdadr
list-ref
length
list-append
_list-append
list-find
map
for-each
fold
reverse
vector->list
make-vector
vector
vector-ref
vector-put!
vector-concat
vector-slice
vector-push!
vector-find
vector-length
list->vector
vector-map
vector-for-each
vector-fold
dict
dict-put!
dict-ref
dict-map
dict-merge
dict->vector
dict->list
keys
vals
zip
not
==
=
eq?
equal?
print
println
pp
%inspect-non-sequence
%recur-protect
%space
inspect
apply
trampoline-result?
trampoline
%gensym-base
gensym-fresh
gensym
cps-trampoline
cps-jump
cps-halt
disable-breakpoints
enable-breakpoints
alert
RegExp
s.match
fs.readFileSync
throw
parseInt
parseFloat
setTimeout
ctx.fillRect
document.addEventListener
document.getElementById
canvas.getContext
Math.random
Math.floor
process.stdin.on
process.stdin.pause
process.stdin.resume
client.set
client.get
client.end
console.log
redis.createClient))
(define (cps e)
(if (or (atom? e)
(dict? e)
(vector? e))
(lambda (k) (k e))
(case (car e)
((require) (lambda (k) `(begin ,e ,(k ''void))))
((throw) (lambda (k) `(begin ,e ,(k ''void))))
((debug)
(lambda (k)
(let ((res (gensym)))
`(begin
(cps-jump
#t
,(inspect-short (cadr e))
(lambda ()
,((cps (cadr e))
(lambda (r)
`((lambda (,res)
(println (str "result: " ,res))
,(k res))
,r)))))))))
((callback)
(lambda (k)
(k `(lambda ,(cadr e)
(cps-trampoline
(cps-jump
#f
,(inspect-short `(lambda ,(cadr e) ,@(cddr e)))
(lambda () ,((cps (cons 'begin (cddr e)))
(lambda (r) #f)))))))))
((~) (cps-tilde (cadr e) (cddr e)))
((quote) (cps-quote (cadr e)))
;;((call/cc (cps-call/cc (cadr e))))
((if) (cps-if (cadr e)
(caddr e)
(if (null? (cdddr e))
#f
(car (cdddr e)))))
((begin) (cps-begin (cdr e)))
((set!) (cps-set! (cadr e) (caddr e)))
((define) (cps-define (cadr e) (cddr e)))
((lambda) (cps-abstraction (cadr e) (cddr e)))
(else (cps-application e)))))
(set! module.exports {:cps cps})