1 (in-package "PARENSCRIPT")
3 ;;; Handy utilities for doing common tasks found in many web browser
4 ;;; JavaScript implementations
6 (defpsmacro do-set-timeout
((timeout) &body body
)
7 `(set-timeout (lambda () ,@body
) ,timeout
))
11 (defmacro def-js-maths
(&rest mathdefs
)
12 `(progn ,@(mapcar (lambda (def) (cons 'defpsmacro def
)) mathdefs
)))
15 (max (&rest nums
) `((@ *math max
) ,@nums
))
16 (min (&rest nums
) `((@ *math min
) ,@nums
))
17 (floor (n &optional divisor
) `((@ *math floor
) ,(if divisor
`(/ ,n
,divisor
) n
)))
18 (ceiling (n &optional divisor
) `((@ *math ceil
) ,(if divisor
`(/ ,n
,divisor
) n
)))
19 (round (n &optional divisor
) `((@ *math round
) ,(if divisor
`(/ ,n
,divisor
) n
)))
20 (sin (n) `((@ *math sin
) ,n
))
21 (cos (n) `((@ *math cos
) ,n
))
22 (tan (n) `((@ *math tan
) ,n
))
23 (asin (n) `((@ *math asin
) ,n
))
24 (acos (n) `((@ *math acos
) ,n
))
25 (atan (y &optional x
) (if x
`((@ *math atan2
) ,y
,x
) `((@ *math atan
) ,y
)))
26 (sinh (n) `((lambda (x) (return (/ (- (exp x
) (exp (- x
))) 2))) ,n
))
27 (cosh (n) `((lambda (x) (return (/ (+ (exp x
) (exp (- x
))) 2))) ,n
))
28 (tanh (n) `((lambda (x) (return (/ (- (exp x
) (exp (- x
))) (+ (exp x
) (exp (- x
)))))) ,n
))
29 (asinh (n) `((lambda (x) (return (log (+ x
(sqrt (1+ (* x x
))))))) ,n
))
30 (acosh (n) `((lambda (x) (return (* 2 (log (+ (sqrt (/ (1+ x
) 2)) (sqrt (/ (1- x
) 2))))))) ,n
))
31 (atanh (n) `((lambda (x) (return (/ (- (log (+ 1 x
)) (log (- 1 x
))) 2))) ,n
))
34 (abs (n) `((@ *math abs
) ,n
))
35 (evenp (n) `(not (oddp ,n
)))
37 (exp (n) `((@ *math exp
) ,n
))
38 (expt (base power
) `((@ *math pow
) ,base
,power
))
39 (log (n &optional base
)
40 (or (and (null base
) `((@ *math log
) ,n
))
41 (and (numberp base
) (= base
10) `(* (log ,n
) (@ *math
*log10e
*)))
42 `(/ (log ,n
) (log ,base
))))
43 (sqrt (n) `((@ *math sqrt
) ,n
))
44 (random (&optional upto
) (if upto
45 `(floor (* ,upto
((@ *math random
))))
46 '((@ *math random
)))))
48 (define-ps-symbol-macro pi
(@ *math
*pi
*))
50 ;;; Exception handling
52 (defpsmacro ignore-errors
(&body body
)
53 `(try (progn ,@body
) (:catch
(e))))
57 (defpsmacro [] (&rest args
)
58 `(array ,@(mapcar (lambda (arg)
59 (if (and (consp arg
) (not (equal '[] (car arg
))))
64 (defpsmacro length
(a)
72 (defpsmacro undefined
(x)
75 (defpsmacro defined
(x)
76 `(not (undefined ,x
)))
78 (defpsmacro @ (obj &rest props
)
79 "Handy getprop/aref composition macro."
81 `(@ (getprop ,obj
,(if (symbolp (car props
))
87 (defpsmacro chain
(&rest method-calls
)
88 (labels ((do-chain (method-calls)
89 (if (cdr method-calls
)
90 (if (listp (car method-calls
))
91 `((@ ,(do-chain (cdr method-calls
)) ,(caar method-calls
)) ,@(cdar method-calls
))
92 `(@ ,(do-chain (cdr method-calls
)) ,(car method-calls
)))
94 (do-chain (reverse method-calls
))))
97 (defpsmacro concatenate
(result-type &rest sequences
)
98 (assert (equal result-type
''string
) () "Right now Parenscript 'concatenate' only support strings.")
101 (defmacro concat-string
(&rest things
)
102 "Like concatenate but prints all of its arguments."
103 `(format nil
"~@{~A~}" ,@things
))
105 (defpsmacro concat-string
(&rest things
)
108 (defpsmacro elt
(array index
)
109 `(aref ,array
,index
))
111 (defpsmacro with-lambda
(() &body body
)
112 "Wraps BODY in a lambda so that it can be treated as an expression."
113 `((lambda () ,@body
)))
115 (defpsmacro stringp
(x)
116 `(= (typeof ,x
) "string"))
118 (defpsmacro numberp
(x)
119 `(= (typeof ,x
) "number"))
121 (defpsmacro functionp
(x)
122 `(= (typeof ,x
) "function"))
124 (defpsmacro objectp
(x)
125 `(= (typeof ,x
) "object"))
127 (defpsmacro memoize
(fn-expr)
128 (destructuring-bind (defun fn-name (arg) &rest fn-body
)
130 (declare (ignore defun
))
131 (with-ps-gensyms (table value compute-fn
)
133 (defun ,compute-fn
(,arg
) ,@fn-body
)
134 (defun ,fn-name
(,arg
)
135 (let ((,value
(aref ,table
,arg
)))
137 (setf ,value
(,compute-fn
,arg
))
138 (setf (aref ,table
,arg
) ,value
))
139 (return ,value
)))))))
141 (defpsmacro append
(arr1 &rest arrs
)
143 `((@ ,arr1 concat
) ,@arrs
)
146 (defpsmacro apply
(fn &rest args
)
147 (let ((arglist (if (> (length args
) 1)
148 `(append (list ,@(butlast args
)) ,(car (last args
)))
150 `((@ ,fn apply
) this
,arglist
)))
152 (defun destructuring-wrap (arr n bindings body
&key setf?
)
153 (labels ((bind-expr (var expr inner-body
)
155 `(progn (setf ,var
,expr
) ,inner-body
)
156 `(let ((,var
,expr
)) ,inner-body
)))
158 (bind-expr sym
`(when (> (length ,arr
) ,n
)
161 (cond ((null bindings
)
163 ((atom bindings
) ;; dotted destructuring list
164 (bind-rest bindings
))
165 ((eq (car bindings
) '&rest
)
166 (if (and (= (length bindings
) 2)
167 (atom (second bindings
)))
168 (bind-rest (second bindings
))
169 (error "~a is invalid in destructuring list." bindings
)))
170 ((eq (car bindings
) '&optional
)
171 (destructuring-wrap arr n
(cdr bindings
) body
:setf? setf?
))
172 (t (let ((var (car bindings
))
173 (inner-body (destructuring-wrap arr
(1+ n
) (cdr bindings
) body
:setf? setf?
)))
174 (cond ((null var
) inner-body
)
175 ((atom var
) (bind-expr var
`(aref ,arr
,n
) inner-body
))
176 (t `(,(if setf?
'dset
'destructuring-bind
)
180 (defpsmacro dset
(bindings expr
&body body
)
181 (let ((arr (if (complex-js-expr? expr
) (ps-gensym) expr
)))
183 ,@(unless (eq arr expr
) `((setf ,arr
,expr
)))
184 ,(destructuring-wrap arr
0 bindings
(cons 'progn body
) :setf? t
))))
186 (defpsmacro destructuring-bind
(bindings expr
&body body
)
187 (let* ((arr (if (complex-js-expr? expr
) (ps-gensym) expr
))
188 (bound (destructuring-wrap arr
0 bindings
(cons 'progn body
))))
191 `(let ((,arr
,expr
)) ,bound
))))