-
Notifications
You must be signed in to change notification settings - Fork 0
/
jasper.jpr
279 lines (223 loc) · 5.21 KB
/
jasper.jpr
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
(assign slice (fun slice (xs start end)
(().slice.call xs start end)))
(assign list (fun list ('xs) xs))
(assign-mac mac (fun mac (name 'argsnbody)
(list 'assign-mac name ((list 'fun name).concat argsnbody))))
(mac def (name 'argsnbody)
(list 'assign name ((list 'fun name).concat argsnbody)))
(def idfn (x) x)
(def cat ()
(().concat.apply () arguments))
(def join (xs x)
(().join.call xs x))
(def str ('xs)
(join xs ""))
(def alist (x)
(rawif x (is x.constructor Array)))
(def astr (x)
(is (typeof x) 'string))
(def anumber (x)
(is (typeof x) 'number))
(def afunction (x)
(is (typeof x) 'function))
(def apply (f 'argsa argsb)
(f.apply null (cat argsa argsb)))
(def apply-method (f o 'argsa argsb)
(f.apply o (cat argsa argsb)))
(mac |[| ('body)
(list 'fun "" (list '_) body))
(def map1 (f xs)
(().map.call xs [f _]))
(def map (f 'xss)
(if (is xss.length 1)
(map1 f (get xss 0))
(let length (apply Math.min (map1 [. _ length] xss))
(let ys ()
(let i 0
(while (< i length)
(ys.push (apply f (map1 [get _ i] xss)))
(++ i)))
ys))))
(def sfy (x)
(if (is x null)
'null
(is x undefined)
'undefined
(alist x)
(str "(" (join (map sfy x) " ") ")")
(str x)))
(def prn (a 'as)
(apply-method console.log console (sfy a) (map sfy as))
a)
(def mapcat (f 'xss)
(apply cat (apply map f xss)))
(def quote (a)
(JSON.stringify a))
(def catmac (xs)
(if (xs.every [and (alist _) (is (get _ 0) 'list)])
(cat '(list) (mapcat (fun || ((_ 'ys))
ys)
xs))
(cat '(cat) xs)))
(def runqq (a)
(if (alist a)
(if (is (get a 0) ',@)
(get a 1)
(is (get a 0) ',)
(list 'list (get a 1))
(list 'list (catmac (map runqq a))))
(cat '(list) (quote a))))
(mac ` (a)
(let xs (runqq a)
(if (and (alist xs) (is (get xs 0) 'list))
(get xs 1)
(list 'get xs 0))))
(mac fn (args 'body)
`(fun || ,args ,@body))
(mac when (cond 'body)
`(if ,cond (do ,@body)))
(mac land ('xs)
(if (is xs.length 0)
true
(is xs.length 1)
(get xs 0)
`(if ,(get xs 0)
(land ,@(slice xs 1)))))
(def subsname (name sub code)
(if (alist code)
(if (is (get code 0) '.)
`(. ,(subsname name sub (get code 1)) ,@(slice code 2))
(map (fn (x) (subsname name sub x)) code))
(is code name)
sub
code))
(def first ((a _)) a)
(def second ((_ a)) a)
(mac with (bindings 'body)
(if (is bindings.length 0)
`(do ,@body)
`(let ,(first bindings) ,(second bindings)
(with ,(slice bindings 2)
,@body))))
(mac aif (expr then else)
`(let it ,expr
(if it ,then ,else)))
(mac laif (expr then else)
`(let it ,expr
(if it ,then ,else)))
(mac afn (args 'body)
`(fun self ,args ,@body))
(mac awhile (cond 'body)
`(let it undefined
(while (= it ,cond)
,@body)))
(def listify (x)
(if (alist x)
x
(list x)))
(mac w/uniq (names 'body)
`(with ,(mapcat [list _ `(gensym ',_)]
(listify names))
,@body))
(mac zap (f place 'args)
`(= ,place (,f ,place ,@args)))
(def pair (xs)
(with (ys ()
i 0)
(while (< i xs.length)
(if (< (+ i 1) xs.length)
(ys.push (list (get xs i) (get xs (+ i 1))))
(ys.push (list (get xs i))))
(zap + i 2))
ys))
(mac case (expr 'args)
(w/uniq g
`(let ,g ,expr
(if ,@(mapcat [if (is _.length 2)
`((is ,g ',(get _ 0))
,(get _ 1))
_]
(pair args))))))
(mac dbg ('xs)
(w/uniq g
`(let ,g ,xs
(prn ',xs ":" ,g)
,g)))
(mac lor ('xs)
(if xs
(w/uniq g
`(let ,g ,(get xs 0)
(if ,g ,g (lor ,@(slice xs 1)))))))
(mac do1 (x 'xs)
(w/uniq g
`(let ,g ,x
,@xs
,g)))
(def list2obj (args)
(let res (Object.create null)
(map (fn ((k v)) (= (get res k) v))
args)
res))
(mac obj ('args)
`(list2obj (list ,@(map (fn ((k v))
`(list ',k ,v))
(pair args)))))
(mac each-from-while (starti iv xs cond 'body)
(with (i null v null gxs (gensym xs))
(if (alist iv)
(do
(= i (first iv))
(= v (second iv)))
(do
(= i (gensym i))
(= v iv)))
`(with (,i ,starti
,gxs ,xs)
,(subsname v `(get ,gxs ,i)
`(while (and (< ,i (, gxs).length) ,cond)
,@body
(++ ,i)))
,i)))
(mac each-while (iv xs cond 'body)
`(each-from-while 0 ,iv ,xs ,cond ,@body))
(mac each (iv xs 'body)
`(each-while ,iv ,xs true ,@body))
(def testify (test)
(if (afunction test)
test
[is test _]))
(mac no (x)
`(if ,x
false
true))
(def index-by (test xs)
(with (f (testify test)
index false)
(each-while (i v) xs (no index)
(if (f v)
(= index i)))
index))
(def find (test xs)
(laif (index-by test xs)
(get xs it)))
(def split-by (f xs)
(let i (index-by f xs)
(if (is i false)
(list xs ())
(list (slice xs 0 i) (slice xs i)))))
(def some (test xs)
(with (f (testify test)
res false)
(each-while x xs (no res)
(laif (f x)
(= res it)))
res))
(def all (test xs)
(let f (testify test)
(! (some [! (f _)] xs))))
(def iso (x y)
(or (is x y)
(and (alist x) (alist y) (is x.length y.length)
(all true (map iso x y)))))
(mac callback (to from 'body)
`(,@to (fn ,from ,@body)))