-
Notifications
You must be signed in to change notification settings - Fork 40
/
runtime.l
375 lines (307 loc) · 8.96 KB
/
runtime.l
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
(define-global environment (list (obj)))
(define-global target (language))
(define-global nil? (x)
(target
js: (or (= x nil) (= x null))
lua: (= x nil)))
(define-global is? (x) (not (nil? x)))
(define-global no (x) (or (nil? x) (= x false)))
(define-global yes (x) (not (no x)))
(define-global either (x y) (if (is? x) x y))
(define-global has? (l k)
(target js: ((get l 'hasOwnProperty) k)
lua: (is? (get l k))))
(define-global # (x)
(target js: (or (get x 'length) 0) lua: |#x|))
(define-global none? (x) (= (# x) 0))
(define-global some? (x) (> (# x) 0))
(define-global one? (x) (= (# x) 1))
(define-global two? (x) (= (# x) 2))
(define-global hd (l) (at l 0))
(target js: (define-global type (x) (typeof x)))
(define-global string? (x) (= (type x) 'string))
(define-global number? (x) (= (type x) 'number))
(define-global boolean? (x) (= (type x) 'boolean))
(define-global function? (x) (= (type x) 'function))
(define-global obj? (x)
(and (is? x)
(= (type x) (target lua: 'table js: 'object))))
(define-global atom? (x)
(or (nil? x) (string? x) (number? x) (boolean? x)))
(define-global hd? (l x)
(and (obj? l) (= (hd l) x)))
(define-global nan (/ 0 0))
(define-global inf (/ 1 0))
(define-global -inf (- inf))
(define-global nan? (n)
(not (= n n)))
(define-global inf? (n)
(or (= n inf) (= n -inf)))
(define-global clip (s from upto)
(target js: ((get s 'substring) from upto)
lua: ((get string 'sub) s (+ from 1) upto)))
(define-global cut (x from upto)
(with l ()
(let (j 0
i (if (or (nil? from) (< from 0)) 0 from)
n (# x)
upto (if (or (nil? upto) (> upto n)) n upto))
(while (< i upto)
(set (at l j) (at x i))
(inc i)
(inc j))
(each (k v) x
(unless (number? k)
(set (get l k) v))))))
(define-global keys (x)
(with t ()
(each (k v) x
(unless (number? k)
(set (get t k) v)))))
(define-global edge (x)
(- (# x) 1))
(define-global inner (x)
(clip x 1 (edge x)))
(define-global tl (l) (cut l 1))
(define-global char (s n)
(target js: ((get s 'charAt) n) lua: (clip s n (+ n 1))))
(define-global code (s n)
(target
js: ((get s 'charCodeAt) n)
lua: ((get string 'byte) s (if n (+ n 1)))))
(define-global string-literal? (x)
(and (string? x) (= (char x 0) "\"")))
(define-global id-literal? (x)
(and (string? x) (= (char x 0) "|")))
(define-global add (l x)
(target js: (do ((get l 'push) x) nil)
lua: ((get table 'insert) l x)))
(define-global drop (l)
(target js: ((get l 'pop))
lua: ((get table 'remove) l)))
(define-global last (l)
(at l (edge l)))
(define-global almost (l)
(cut l 0 (edge l)))
(define-global reverse (l)
(with l1 (keys l)
(let i (edge l)
(while (>= i 0)
(add l1 (at l i))
(dec i)))))
(define-global reduce (f x)
(if (none? x) nil
(one? x) (hd x)
(f (hd x) (reduce f (tl x)))))
(define-global join ls
(with r ()
(step l ls
(when l
(let n (# r)
(each (k v) l
(if (number? k) (inc k n))
(set (get r k) v)))))))
(define-global find (f t)
(each x t
(let y (f x)
(if y (return y)))))
(define-global first (f l)
(step x l
(let y (f x)
(if y (return y)))))
(define-global in? (x t)
(find (fn (y) (= x y)) t))
(define-global pair (l)
(with l1 ()
(for i (# l)
(add l1 (list (at l i) (at l (+ i 1))))
(inc i))))
(define-global sort (l f)
(target
lua: (do ((get table 'sort) l f) l)
js: ((get l 'sort) (when f (fn (a b) (if (f a b) -1 1))))))
(define-global map (f x)
(with t ()
(step v x
(let y (f v)
(if (is? y)
(add t y))))
(each (k v) x
(unless (number? k)
(let y (f v)
(when (is? y)
(set (get t k) y)))))))
(define-global keep (f x)
(map (fn (v) (when (yes (f v)) v)) x))
(define-global keys? (t)
(each (k v) t
(unless (number? k)
(return true)))
false)
(define-global empty? (t)
(each x t
(return false))
true)
(define-global stash (args)
(when (keys? args)
(let p ()
(each (k v) args
(unless (number? k)
(set (get p k) v)))
(set (get p '_stash) true)
(add args p)))
args)
(define-global unstash (args)
(if (none? args) ()
(let l (last args)
(if (and (obj? l) (get l '_stash))
(with args1 (almost args)
(each (k v) l
(unless (= k '_stash)
(set (get args1 k) v))))
args))))
(define-global destash! (l args1)
(if (and (obj? l) (get l '_stash))
(each (k v) l
(unless (= k '_stash)
(set (get args1 k) v)))
l))
(define-global search (s pattern start)
(target
js: (let i ((get s 'indexOf) pattern start)
(if (>= i 0) i))
lua: (let (start (if start (+ start 1))
i ((get string 'find) s pattern start true))
(and i (- i 1)))))
(define-global split (s sep)
(if (or (= s "") (= sep "")) ()
(with l ()
(let n (# sep)
(while true
(let i (search s sep)
(if (nil? i) (break)
(do (add l (clip s 0 i))
(set s (clip s (+ i n)))))))
(add l s)))))
(define-global cat xs
(either (reduce (fn (a b) (cat a b)) xs) ""))
(define-global + xs
(either (reduce (fn (a b) (+ a b)) xs) 0))
(define-global - xs
(either (reduce (fn (b a) (- a b)) (reverse xs)) 0))
(define-global * xs
(either (reduce (fn (a b) (* a b)) xs) 1))
(define-global / xs
(either (reduce (fn (b a) (/ a b)) (reverse xs)) 1))
(define-global % xs
(either (reduce (fn (b a) (% a b)) (reverse xs)) 0))
(define pairwise (f xs)
(for i (edge xs)
(let (a (at xs i)
b (at xs (+ i 1)))
(unless (f a b)
(return false))))
(return true))
(define-global < xs (pairwise (fn (a b) (< a b)) xs))
(define-global > xs (pairwise (fn (a b) (> a b)) xs))
(define-global = xs (pairwise (fn (a b) (= a b)) xs))
(define-global <= xs (pairwise (fn (a b) (<= a b)) xs))
(define-global >= xs (pairwise (fn (a b) (>= a b)) xs))
(define-global number (s)
(target
js: (let n (parseFloat s)
(unless (isNaN n) n))
lua: (tonumber s)))
(define-global number-code? (n)
(and (> n 47) (< n 58)))
(define-global numeric? (s)
(let n (# s)
(for i n
(unless (number-code? (code s i))
(return false))))
(some? s))
(target js: (define tostring (x) ((get x 'toString))))
(define-global escape (s)
(let s1 "\""
(for i (# s)
(let (c (char s i)
c1 (if (= c "\n") "\\n"
(= c "\r") "\\r"
(= c "\"") "\\\""
(= c "\\") "\\\\"
c))
(cat! s1 c1)))
(cat s1 "\"")))
(define-global str (x stack)
(if (nil? x) "nil"
(nan? x) "nan"
(= x inf) "inf"
(= x -inf) "-inf"
(boolean? x) (if x "true" "false")
(string? x) (escape x)
(atom? x) (tostring x)
(function? x) "function"
(and stack (in? x stack)) "circular"
(target js: false lua: (not (= (type x) 'table)))
(escape (tostring x))
(let (s "(" sp ""
xs () ks ()
l (or stack ()))
(add l x)
(each (k v) x
(if (number? k)
(set (get xs k) (str v l))
(do (target lua:
(unless (string? k)
(set k (str k l))))
(add ks (cat k ":"))
(add ks (str v l)))))
(drop l)
(each v (join xs ks)
(cat! s sp v)
(set sp " "))
(cat s ")"))))
(target lua:
(define values (or unpack (get table 'unpack))))
(define-global apply (f args)
(let args (stash args)
(target js: ((get f 'apply) f args)
lua: (f (values args)))))
(define-global call (f rest: args)
(apply f args))
(define-global setenv (k rest: keys)
(when (string? k)
(let (frame (if (get keys 'toplevel)
(hd environment)
(last environment))
entry (or (get frame k) (obj)))
(each (k v) keys
(set (get entry k) v))
(set (get frame k) entry))))
(target js:
(define-global print (x)
((get console 'log) x)))
(target js:
(define-global error (x)
(throw (new (Error x)))))
(define math (target js: Math lua: math))
(define-global abs (get math 'abs))
(define-global acos (get math 'acos))
(define-global asin (get math 'asin))
(define-global atan (get math 'atan))
(define-global atan2 (get math 'atan2))
(define-global ceil (get math 'ceil))
(define-global cos (get math 'cos))
(define-global floor (get math 'floor))
(define-global log (get math 'log))
(define-global log10 (get math 'log10))
(define-global max (get math 'max))
(define-global min (get math 'min))
(define-global pow (get math 'pow))
(define-global random (get math 'random))
(define-global sin (get math 'sin))
(define-global sinh (get math 'sinh))
(define-global sqrt (get math 'sqrt))
(define-global tan (get math 'tan))
(define-global tanh (get math 'tanh))
(define-global trunc (get math 'floor))