-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.lisp
More file actions
executable file
·497 lines (407 loc) · 16.4 KB
/
Copy pathjs.lisp
File metadata and controls
executable file
·497 lines (407 loc) · 16.4 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
(load (merge-pathnames "util.lisp" *load-truename*))
(category js-def)
(defun splitstr (str delim &optional acc)
(if (> (length str) 0) (let ((p (position delim str)))
(if p (splitstr (subseq str (+ p 1)) delim (append acc (list (subseq str 0 p))))
(append acc (list str))))
acc))
(defun js-name (name)
(cond ((symbolp name)
(reduce (lambda (a v)
(format nil "~A~A~A" a (char-upcase (char v 0))
(subseq v 1)))
(splitstr (format nil "~A" (string-downcase name)) #\-)))
((stringp name) (format nil "\"~A\"" name))
(t name)))
(defun js-block (&rest forms)
(reduce (sep #\Newline) (loop for x in forms collect (js-eval x :statement? T))))
(defun js-call (form &key statement?)
(format nil "~A(~A)~A"
(if (listp+ (first form))
(reduce (sep #\.)
(loop for x in (first form)
collect (js-eval x)))
(js-name (first form)))
(if (rest form)
(reduce (sep #\,) (mapcar #'js-eval (rest form)))
"")
(if statement? #\; "")))
(define-condition js-error (error)
((form :initarg :in :reader form)
(msg :initarg :msg :reader msg)))
(defmethod print-object ((this js-error) out)
(format out "JS error in ~S.~% ~A~%"
(form this) (msg this)))
(defun js-eval (form &key statement?)
(handler-case
(if (not (listp form)) (return-from js-eval (js-name form))
(handler-case (apply (js-def (first form)) (rest form))
(not-in-category () (js-call form :statement? statement?))))
(js-error (e) (error e))
(error (e) (error 'js-error :in form :msg e))))
(defun js-name! (x)
(if (stringp x) x (js-name x)))
(setf (js-def 'fun)
(lambda (name args &rest body)
(format nil "function ~A(~A){~{~A~}}~%~%"
(if name (js-name! name) "")
(if args
(reduce (lambda (a v) (format nil "~A, ~A" a v))
(mapcar #'js-name! args))
"")
(if body
(mapcar (lambda (x) (js-eval x :statement? T)) body)
""))))
(setf (js-def 'call)
(lambda (&rest form)
(js-call form)))
(setf (js-def '->)
(lambda (&rest args)
(reduce (lambda (a v)
(format nil "~A.~A" a v))
(mapcar #'js-eval args))))
(setf (js-def 'symbol)
(lambda (x)
(format nil "~A" x)))
(setf (js-def 'not)
(lambda (x) (format nil "!(~A)" (js-eval x))))
(setf (js-def 'block)
#'js-block)
(setf (js-def 'if)
(lambda (condition action &optional if-else)
(format nil "if (~a) {~{~a~}}~a~%"
(js-eval condition) (mapcar (lambda (x) (js-eval x :statement? T)) action)
(if if-else (format nil "else {~{~a~}}"
(mapcar (lambda (x) (js-eval x :statement? T)) if-else)) ""))))
(setf (js-def 'if*)
(lambda (condition action else)
(format nil "(~a) ? ~a : ~a"
(js-eval condition)
(js-eval action)
(js-eval else))))
(defun js-stdop (&rest ops)
(loop for op in ops
do (setf (js-def op)
(lambda (&rest args)
(reduce (lambda (a v) (format nil "(~A ~A ~A)" a op v))
(mapcar #'js-eval args))))))
(js-stdop '%)
(setf (js-def '+)
(lambda (&rest args)
(format nil "(~A)"
(reduce (sep #\+) (mapcar #'js-eval args)))))
(setf (js-def 'div)
(lambda (&rest args)
(format nil "(~A)"
(reduce (sep #\/) (mapcar #'js-eval args)))))
(setf (js-def 'div=)
(lambda (a b)
(format nil "~A /= ~A;"
(js-eval a)
(js-eval b))))
(setf (js-def '+=)
(lambda (first &rest args)
(format nil "~A += ~A;" (js-eval first)
(reduce (sep #\+) (mapcar #'js-eval args)))))
(setf (js-def 'and)
(lambda (&rest args)
(format nil "(~A)"
(reduce (lambda (a v) (format nil "~A && ~A" a v))
(mapcar #'js-eval args)))))
(setf (js-def 'or)
(lambda (&rest args)
(format nil "(~A)"
(reduce (lambda (a v) (format nil "~A || ~A" a v))
(mapcar #'js-eval args)))))
(setf (js-def '-)
(lambda (&rest args)
(if (= (length args) 1)
(format nil "(-~A)" (js-eval (first args)))
(format nil "(~A)"
(reduce (sep #\-) (mapcar #'js-eval args))))))
(setf (js-def '^)
(lambda (a b)
(format nil "Math.pow(~A,~A)" (js-eval a) (js-eval b))))
(setf (js-def 'floor)
(lambda (x)
(format nil "Math.floor(~A)" (js-eval x))))
(setf (js-def '++)
(lambda (arg)
(format nil "~A++;" (js-name arg))))
(setf (js-def '--)
(lambda (arg)
(format nil "~A--;" (js-name arg))))
(setf (js-def '--.)
(lambda (arg)
(format nil "~A--" (js-name arg))))
(setf (js-def '*)
(lambda (&rest args)
(format nil "(~A)"
(reduce (sep #\*) (mapcar #'js-eval args)))))
(setf (js-def 'let)
(lambda (name value)
(format nil "var ~A = ~A;" (js-name name) (js-eval value))))
(setf (js-def 'prop)
(lambda (head &rest tail)
(reduce (lambda (a v)
(format nil "~A.~A" a (if (stringp v) v (js-name v))))
(cons (js-eval head) tail))))
(setf (js-def '@)
(lambda (arr idx)
(format nil "~A[~A]" (js-eval arr) (js-eval idx))))
(setf (js-def '=)
(lambda (a b)
(format nil "~A = ~A;" (js-eval a) (js-eval b))))
(setf (js-def '=.)
(lambda (a b)
(format nil "~A = ~A" (js-eval a) (js-eval b))))
(setf (js-def ':=)
(lambda (a b)
(format nil "~A = ~A" (js-eval a) (js-eval b))))
(setf (js-def '==)
(lambda (a b)
(format nil "~A == ~A" (js-eval a) (js-eval b))))
(setf (js-def '!=)
(lambda (a b)
(format nil "~A != ~A" (js-eval a) (js-eval b))))
(setf (js-def '<)
(lambda (a b)
(format nil "~A < ~A" (js-eval a) (js-eval b))))
(setf (js-def '<=)
(lambda (a b)
(format nil "~A <= ~A" (js-eval a) (js-eval b))))
(setf (js-def '>)
(lambda (a b)
(format nil "~A > ~A" (js-eval a) (js-eval b))))
(setf (js-def '>=)
(lambda (a b)
(format nil "~A >= ~A" (js-eval a) (js-eval b))))
(setf (js-def 'return)
(lambda (a)
(format nil "return ~A;" (js-eval a))))
(setf (js-def 'by-id)
(lambda (id)
(format nil "document.getElementById(~A)" (js-eval id))))
(setf (js-def 'for)
(lambda (setup &rest commands)
(format nil "for(var ~A; ~A; ~A) { ~{~A~} }"
(js-eval (first setup)) (js-eval (second setup)) (js-eval (third setup))
(loop for x in commands
collect (js-eval x :statement? T)))))
(setf (js-def 'schedule)
(lambda (function)
(js-eval `(fun "" ()
(set-timeout ,function 0)))))
(setf (js-def 'foreach)
(lambda (setup &rest commands)
(format nil "for(var ~A in ~A) { ~{~A~} }"
(js-eval (first setup)) (js-eval (second setup))
(loop for x in commands
collect (js-eval x :statement? T)))))
(setf (js-def 'while)
(lambda (cond &rest body)
(format nil "while (~A) {~{~A~}}"
(js-eval cond)
(mapcar (lambda (x) (js-eval x :statement? T)) body))))
(setf (js-def 'json)
(lambda (obj)
(format nil "JSON.stringify(~A)" (js-eval obj))))
(setf (js-def 'parse)
(lambda (obj)
(format nil "JSON.parse(~A)" (js-eval obj))))
(setf (js-def 'try)
(lambda (forms backup)
(format nil "try { ~{~A~} } catch(exception) { ~{~A~} }"
(mapcar (lambda (x) (js-eval x :statement? T)) forms)
(mapcar (lambda (x) (js-eval x :statement? T)) backup))))
(setf (js-def 'throw)
(lambda (what)
(format nil "throw ~A;" (js-eval what))))
(setf (js-def 'continue)
(lambda () "continue;"))
(setf (js-def 'break)
(lambda () "break;"))
(setf (js-def '@[])
(lambda (tab &rest indexes)
(format nil "~A~{[~A]~}" (js-eval tab) (mapcar #'js-eval indexes))))
(setf (js-def 'list)
(lambda (&rest args)
(format nil "[~A]"
(reduce (curry #'format nil "~A,~A") (mapcar #'js-eval args)))))
(setf (js-def 'hash)
(lambda (&rest args)
(format nil "{~A}"
(reduce (curry #'format nil "~A,~A")
(mapcar (lambda (x) (format nil "~A: ~A"
(js-eval (first x))
(js-eval (second x)))) args)))))
(setf (js-def 'just) (lambda (x) (format nil "~A;" (js-eval x))))
(setf (js-def 'on-pixels)
(lambda (canvas &rest transforms)
(apply #'js-block
(append `((let canvas ,canvas)
(let ctx ((canvas get-context) "2d"))
(let pixel-data (ctx.get-image-data 0 0 canvas.width canvas.height))
(let pixels (prop pixel-data data)))
(reduce (lambda (acc tfm)
(let ((x (@ tfm '(0 0)))
(y (@ tfm '(0 1)))
(r (@ tfm '(1 0)))
(g (@ tfm '(1 1)))
(b (@ tfm '(1 2)))
(a (or (@ tfm '(1 3)) 255)))
(append acc
`((= (@ pixels (* 4 (+ (* ,y canvas.width) ,x))) ,r)
(= (@ pixels (+ (* 4 (+ (* ,y canvas.width) ,x)) 1)) ,g)
(= (@ pixels (+ (* 4 (+ (* ,y canvas.width) ,x)) 2)) ,b)
(= (@ pixels (+ (* 4 (+ (* ,y canvas.width) ,x)) 3)) ,a)))))
transforms :initial-value nil)
'((ctx.put-image-data pixel-data 0 0))))))
(setf (js-def 'array?)
(lambda (x)
(format nil "Array.isArray(~A)" (js-eval x))))
(setf (js-def 'now)
(lambda ()
"new Date().getTime()"))
(setf (js-def 'typeof)
(lambda (x)
(format nil "typeof ~A" (js-eval x))))
(setf (js-def 'keys)
(lambda (x)
(format nil "Object.keys(~A)" (js-eval x))))
(setf (js-def 'values)
(lambda (x)
(format nil "Object.keys(~A).map(x => ~A[x])"
(js-eval x) (js-eval x))))
(setf (js-def 'undef?)
(lambda (x) (js-eval `(== (typeof ,x) "undefined"))))
(setf (js-def 'number?)
(lambda (x) (js-eval `(== (typeof ,x) "number"))))
(setf (js-def 'function?)
(lambda (x) (js-eval `(== (typeof ,x) "function"))))
(setf (js-def 'object?)
(lambda (x) (js-eval `(== (typeof ,x) "object"))))
(setf (js-def '[])
(lambda (&rest elements)
(format nil "[~A]"
(if elements (reduce (sep #\,) (mapcar #'js-eval elements)) ""))))
(setf (js-def 'draw-line)
(lambda (canvas width &rest points)
(apply #'js-block
(append
`((let ctx ((,canvas get-context) "2d"))
(ctx.begin-path)
(= ctx.line-width ,width)
(ctx.move-to ,(@ points '(0 0)) ,(@ points '(0 1))))
(loop for x in (rest points)
collect `(ctx.line-to ,(first x) ,(second x)))
`((ctx.stroke)
(ctx.begin-path)
(= ctx.fill-style "#000000")
(ctx.arc ,(@ points '(0 0)) ,(@ points '(0 1)) ,(/ width 2)
0 (* 2 (_pi)))
(ctx.fill))))))
(setf (js-def 'dom-node)
(lambda (type &rest props)
(format nil "(~A)()"
(js-eval `(fun nil ()
(let nod ((document create-element) ,type))
,@(loop for prop in props
collect `(= (prop nod ,(first prop)) ,(second prop)))
(return nod))))))
(setf (js-def '_pi)
(lambda ()
"Math.PI"))
(setf (js-def 'sin)
(lambda (arg)
(format nil "Math.sin(~A)" (js-eval arg))))
(setf (js-def 'cos)
(lambda (arg)
(format nil "Math.cos(~A)" (js-eval arg))))
(setf (js-def 'hashcat)
(lambda (&rest hashes)
(format nil "Object.assign({}, ~A)"
(reduce (sep #\,) (mapcar #'js-eval hashes)))))
(let ((counter 0))
(defun js-id (base)
(incf counter)
(format nil "~A_~X" base counter)))
(defun js (&rest definitions)
(!+ 'tag := "script"
:& '("type" "text/javascript")
:< (loop for def in definitions
collect (let ((ans (apply (js-def (car def)) (cdr def))))
(if (eql (car def) 'fun) ans (format nil "~A;" ans))))))
(category js-typetest)
(setf (js-typetest 'list)
(lambda (name &optional len)
`((if (not (array? ,name))
((throw (+ ,(txt 'arg-not-list)
,name))))
,@(if len
`((if (!= (-> ,name length) ,len)
((throw (+ ,(txt 'wrong-list-len) ,(symdc name))))))))))
(setf (js-typetest 'int-list)
(lambda (name &optional len)
`((if (not (array? ,name))
((throw (+ "Argument nie jest listą: "
,name))))
((,name map) (fun nil (x)
(if (not (number? x))
((throw (+ ,(txt 'not-int-list)
x))))))
,@(if len
`((if (!= (-> ,name length) ,len)
((throw (+ ,(txt 'wrong-list-len) ,(symdc name))))))))))
(setf (js-typetest 'number)
(lambda (name)
`((if (not (number? ,name))
((throw (+ ,(txt 'arg-not-number)
,(symdc name))))))))
(setf (js-typetest 'function)
(lambda (name)
`((if (not (function? ,name))
((throw (+ ,(txt 'arg-not-fun) ,(symdc name))))))))
(setf (js-def 'defn)
(lambda (name args &rest body)
(let*((argnames (mapcar (lambda (x)
(if (listp+ x) (second x) x))
(remove-if #'ampsym? args)))
(constr (remove-if-not #'listp args))
(argc (if (find '&rest args)
(length argnames) nil))
(optp (position '&optional args))
(rest (let ((p (position '&rest args)))
(if (and p (not (eql (- (length args) 2) p)))
(error (format nil "wrong position of &rest in: ~S"
(list name args))))
p))
(lencond (cond (optp `(or (< arguments.length ,optp)
(> arguments.length ,(length argnames))))
((not rest)
`(!= arguments.length ,(length argnames)))
(t `(< arguments.length ,rest)))))
(if (and optp rest)
(error "&optional and &rest at the same time in js:defn is not allowed"))
(let ((fundef `(,(if rest (subseq argnames 0 rest)
argnames)
,@(if lencond `((if ,lencond
((throw (+ ,(txt 'wrong-argc)
,(symdc (format nil "~A" name)) ": "
(((values arguments)
join) ", ")))))))
,@(apply #'append
(loop for c in constr
for i from 0
collect (let ((cnd (apply (js-typetest (first c)) (rest c))))
(if (and optp (>= i optp))
`((if (not (undef? ,(second c)))
,cnd))
cnd))))
,@(if rest `((let ,(car (last argnames))
(((values arguments) slice) ,rest))))
,@body)))
(if (listp name)
(js-eval `(= (@[] ,(read-from-string (js-eval (first name))) ,@(mapcar #'js-eval (rest name)))
(fun nil ,@fundef)))
(js-eval `(fun ,(js-eval name) ,@fundef)))))))