-
Notifications
You must be signed in to change notification settings - Fork 1
/
macros.ps
69 lines (60 loc) · 2.02 KB
/
macros.ps
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
;; -*- mode: lisp -*-
;; use "ps2js --eval '(defconstant +debug+ t)' ..." to allow debug forms in output
(defmacro debug (form)
(when (boundp '+debug+)
form))
(defmacro alambda (args &body body)
`(lambda ,args
(block alambda
,@body)))
(setf (@ *Array prototype remove)
(alambda (thing)
(let ((i 0))
(dolist (x this)
(when (= x thing)
((@ this splice) i 1)
;; TODO: add unit test to ensure remove actually removes from the element
(return-from alambda t))
(incf i)))
nil))
(defmacro defclass (name super args &body constructor)
(let ((inherits '()))
`(progn
(defun ,name ,args
,@constructor
,(when super
`((@ ,(car super) apply) this arguments))
this)
,@(progn
(dolist (parent super)
(push `(setf (@ ,name prototype)
((@ *Object create) (@ ,parent prototype)))
inherits))
inherits)
,(when super
`(setf (@ ,name prototype constructor) ,name))
)))
(defmacro defmethod (class method args &body body)
`(setf (@ ,class prototype ,method)
(lambda ,args
(block ,method
,@body))))
(eval-when (:compile-toplevel)
(defun group (list)
"group elements 2 by 2 from list, reverse order"
(let (l1 l2)
(while list
(push (pop list) l1)
(push (pop list) l2))
(map 'list (lambda (x y) (list x y)) l1 l2))))
(defmacro trigger (self &rest args)
(let (code)
(dolist (event (group args))
(push `((@ ,self trigger) ,@event) code))
(push 'progn code)))
(defmacro export (&rest values)
(let (results)
(dolist (value values)
(push `(unless (= (typeof module) "undefined")
(setf (@ module exports ,value) ,value)) results))
(append '(progn) (nreverse results))))