Added support for supplied-p parameters to optional and keyword arguments.
[clinton/parenscript.git] / src / special-forms.lisp
CommitLineData
0ce67a33 1(in-package "PARENSCRIPT")
18dd299a 2
5a69278c
VS
3(defmacro with-local-macro-environment ((var env) &body body)
4 `(let* ((,var (make-macro-dictionary))
5 (,env (cons ,var ,env)))
6 ,@body))
7
18dd299a
VS
8;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9;;; literals
10(defmacro defpsliteral (name string)
4577df1c
TC
11 `(progn
12 (add-ps-literal ',name)
e8fdcce7 13 (define-ps-special-form ,name ()
0ce67a33 14 (list 'js:literal ,string))))
18dd299a
VS
15
16(defpsliteral this "this")
17(defpsliteral t "true")
18(defpsliteral true "true")
19(defpsliteral false "false")
20(defpsliteral f "false")
21(defpsliteral nil "null")
22(defpsliteral undefined "undefined")
23
c452748e
TC
24(macrolet ((def-for-literal (name printer)
25 `(progn
4577df1c 26 (add-ps-literal ',name)
e8fdcce7 27 (define-ps-special-form ,name (&optional label)
c452748e 28 (list ',printer label)))))
0ce67a33
VS
29 (def-for-literal break js:break)
30 (def-for-literal continue js:continue))
18dd299a 31
5a69278c
VS
32(define-ps-special-form quote (x)
33 (compile-parenscript-form
34 (typecase x
35 (cons `(array ,@(mapcar (lambda (x) (when x `',x)) x)))
36 (null '(array))
37 (keyword x)
38 (symbol (symbol-to-js-string x))
39 (number x)
40 (string x))
41 :expecting expecting))
fb469285 42
18dd299a
VS
43;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44;;; unary operators
6a46e1ef
TC
45(macrolet ((def-unary-ops (&rest ops)
46 `(progn ,@(mapcar (lambda (op)
47 (let ((op (if (listp op) (car op) op))
48 (spacep (if (listp op) (second op) nil)))
e8fdcce7 49 `(define-ps-special-form ,op (x)
0ce67a33 50 (list 'js:unary-operator ',op
5a69278c 51 (compile-parenscript-form (ps-macroexpand x) :expecting :expression)
6a46e1ef
TC
52 :prefix t :space ,spacep))))
53 ops))))
54 (def-unary-ops ~ ! (new t) (delete t) (void t) (typeof t)))
18dd299a 55
6a46e1ef
TC
56;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57;;; statements
e8fdcce7 58(define-ps-special-form return (&optional value)
5a69278c 59 `(js:return ,(compile-parenscript-form (ps-macroexpand value) :expecting :expression)))
18dd299a 60
e8fdcce7 61(define-ps-special-form throw (value)
5a69278c 62 `(js:throw ,(compile-parenscript-form (ps-macroexpand value) :expecting :expression)))
6a46e1ef 63
18dd299a
VS
64;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
65;;; arrays
e8fdcce7 66(define-ps-special-form array (&rest values)
0ce67a33 67 `(js:array ,@(mapcar (lambda (form) (compile-parenscript-form form :expecting :expression))
18dd299a
VS
68 values)))
69
e8fdcce7 70(define-ps-special-form aref (array &rest coords)
5a69278c 71 `(js:aref ,(compile-parenscript-form (ps-macroexpand array) :expecting :expression)
0ce67a33 72 ,(mapcar (lambda (form)
5a69278c 73 (compile-parenscript-form (ps-macroexpand form) :expecting :expression))
0ce67a33 74 coords)))
18dd299a 75
18dd299a
VS
76(defpsmacro list (&rest values)
77 `(array ,@values))
78
79630c82
VS
79(defpsmacro make-array (&rest initial-values)
80 `(new (*array ,@initial-values)))
18dd299a
VS
81
82;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83;;; operators
e8fdcce7 84(define-ps-special-form incf (x &optional (delta 1))
5a69278c
VS
85 (let ((x (ps-macroexpand x))
86 (delta (ps-macroexpand delta)))
87 (if (eql delta 1)
88 `(js:unary-operator js:++ ,(compile-parenscript-form x :expecting :expression) :prefix t)
89 `(js:operator js:+= ,(compile-parenscript-form x :expecting :expression)
90 ,(compile-parenscript-form delta :expecting :expression)))))
18dd299a 91
e8fdcce7 92(define-ps-special-form decf (x &optional (delta 1))
0ce67a33
VS
93 (if (eql delta 1)
94 `(js:unary-operator js:-- ,(compile-parenscript-form x :expecting :expression) :prefix t)
95 `(js:operator js:-= ,(compile-parenscript-form x :expecting :expression)
96 ,(compile-parenscript-form delta :expecting :expression))))
18dd299a 97
e8fdcce7 98(define-ps-special-form - (first &rest rest)
0ce67a33
VS
99 (if rest
100 `(js:operator js:- ,@(mapcar (lambda (val) (compile-parenscript-form val :expecting :expression))
101 (cons first rest)))
102 `(js:unary-operator js:- ,(compile-parenscript-form first :expecting :expression) :prefix t)))
18dd299a 103
e8fdcce7 104(define-ps-special-form not (x)
5a69278c 105 (let ((form (compile-parenscript-form (ps-macroexpand x) :expecting :expression))
0ce67a33
VS
106 inverse-op)
107 (if (and (eq (car form) 'js:operator)
108 (= (length (cddr form)) 2)
109 (setf inverse-op (case (cadr form)
110 (== '!=)
111 (< '>=)
112 (> '<=)
113 (<= '>)
114 (>= '<)
115 (!= '==)
116 (=== '!==)
117 (!== '===))))
118 `(js:operator ,inverse-op ,@(cddr form))
119 `(js:unary-operator js:! ,form :prefix t))))
18dd299a 120
18dd299a
VS
121;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122;;; control structures
123(defun flatten-blocks (body)
124 (when body
125 (if (and (listp (car body))
0ce67a33 126 (eq 'js:block (caar body)))
fdfa77fc 127 (append (cdr (car body)) (flatten-blocks (cdr body)))
18dd299a
VS
128 (cons (car body) (flatten-blocks (cdr body))))))
129
130(defun constant-literal-form-p (form)
131 (or (numberp form)
132 (stringp form)
133 (and (listp form)
0ce67a33 134 (eq 'js:literal (car form)))))
18dd299a 135
e8fdcce7 136(define-ps-special-form progn (&rest body)
5a69278c
VS
137 (let ((body (mapcar #'ps-macroexpand body)))
138 (if (and (eq expecting :expression) (= 1 (length body)))
139 (compile-parenscript-form (car body) :expecting :expression)
140 `(,(if (eq expecting :expression) 'js:|,| 'js:block)
141 ,@(let* ((block (flatten-blocks (remove nil (mapcar (lambda (form)
142 (compile-parenscript-form form :expecting expecting))
143 body)))))
144 (append (remove-if #'constant-literal-form-p (butlast block)) (last block)))))))
18dd299a 145
e8fdcce7 146(define-ps-special-form cond (&rest clauses)
18dd299a 147 (ecase expecting
fdfa77fc
VS
148 (:statement `(js:if ,(compile-parenscript-form (caar clauses) :expecting :expression)
149 ,(compile-parenscript-form `(progn ,@(cdar clauses)))
150 ,@(loop for (test . body) in (cdr clauses) appending
151 (if (eq t test)
152 `(:else ,(compile-parenscript-form `(progn ,@body) :expecting :statement))
153 `(:else-if ,(compile-parenscript-form test :expecting :expression)
154 ,(compile-parenscript-form `(progn ,@body) :expecting :statement))))))
18dd299a
VS
155 (:expression (make-cond-clauses-into-nested-ifs clauses))))
156
157(defun make-cond-clauses-into-nested-ifs (clauses)
158 (if clauses
159 (destructuring-bind (test &rest body)
160 (car clauses)
161 (if (eq t test)
162 (compile-parenscript-form `(progn ,@body) :expecting :expression)
e8fdcce7
VS
163 `(js:? ,(compile-parenscript-form test :expecting :expression)
164 ,(compile-parenscript-form `(progn ,@body) :expecting :expression)
165 ,(make-cond-clauses-into-nested-ifs (cdr clauses)))))
0ce67a33 166 (compile-parenscript-form nil :expecting :expression))) ;; js:null
18dd299a 167
e8fdcce7 168(define-ps-special-form if (test then &optional else)
18dd299a 169 (ecase expecting
e8fdcce7
VS
170 (:statement `(js:if ,(compile-parenscript-form test :expecting :expression)
171 ,(compile-parenscript-form `(progn ,then))
fdfa77fc 172 ,@(when else `(:else ,(compile-parenscript-form `(progn ,else))))))
e8fdcce7
VS
173 (:expression `(js:? ,(compile-parenscript-form test :expecting :expression)
174 ,(compile-parenscript-form then :expecting :expression)
175 ,(compile-parenscript-form else :expecting :expression)))))
176
177(define-ps-special-form switch (test-expr &rest clauses)
0ce67a33
VS
178 `(js:switch ,(compile-parenscript-form test-expr :expecting :expression)
179 ,(loop for (val . body) in clauses collect
b39a6394 180 (cons (if (eq val 'default)
0ce67a33
VS
181 'default
182 (compile-parenscript-form val :expecting :expression))
183 (mapcar (lambda (x) (compile-parenscript-form x :expecting :statement))
184 body)))))
18dd299a
VS
185
186(defpsmacro case (value &rest clauses)
187 (labels ((make-clause (val body more)
587f3aa0 188 (cond ((and (listp val) (not (eq (car val) 'quote)))
18dd299a
VS
189 (append (mapcar #'list (butlast val))
190 (make-clause (first (last val)) body more)))
191 ((member val '(t otherwise))
192 (make-clause 'default body more))
193 (more `((,val ,@body break)))
194 (t `((,val ,@body))))))
195 `(switch ,value ,@(mapcon (lambda (clause)
196 (make-clause (car (first clause))
197 (cdr (first clause))
198 (rest clause)))
199 clauses))))
200
201(defpsmacro when (test &rest body)
202 `(if ,test (progn ,@body)))
203
204(defpsmacro unless (test &rest body)
205 `(if (not ,test) (progn ,@body)))
206
207;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208;;; function definition
209(defun compile-function-definition (args body)
210 (list (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :symbol)) args)
5ffb1eba
VS
211 (let* ((*enclosing-lexical-block-declarations* ())
212 (body (compile-parenscript-form `(progn ,@body)))
213 (var-decls (compile-parenscript-form
214 `(progn ,@(mapcar (lambda (var) `(var ,var)) *enclosing-lexical-block-declarations*)))))
215 `(js:block ,@(cdr var-decls) ,@(cdr body)))))
18dd299a 216
e8fdcce7 217(define-ps-special-form %js-lambda (args &rest body)
0ce67a33 218 `(js:lambda ,@(compile-function-definition args body)))
18dd299a 219
e8fdcce7 220(define-ps-special-form %js-defun (name args &rest body)
0ce67a33 221 `(js:defun ,name ,@(compile-function-definition args body)))
18dd299a
VS
222
223(defun parse-function-body (body)
224 (let* ((docstring
225 (when (stringp (first body))
226 (first body)))
227 (body-forms (if docstring (rest body) body)))
228 (values body-forms docstring)))
229
230(defun parse-key-spec (key-spec)
66acaf33 231 "parses an &key parameter. Returns 5 values:
18dd299a
VS
232var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
233
234Syntax of key spec:
235[&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
236"
237 (let* ((var (cond ((symbolp key-spec) key-spec)
238 ((and (listp key-spec) (symbolp (first key-spec))) (first key-spec))
62ddca23 239 ((and (listp key-spec) (listp (first key-spec))) (second (first key-spec)))))
18dd299a
VS
240 (keyword-name (if (and (listp key-spec) (listp (first key-spec)))
241 (first (first key-spec))
242 (intern (string var) :keyword)))
243 (init-form (if (listp key-spec) (second key-spec) nil))
244 (init-form-supplied-p (if (listp key-spec) t nil))
245 (supplied-p-var (if (listp key-spec) (third key-spec) nil)))
246 (values var init-form keyword-name supplied-p-var init-form-supplied-p)))
247
248(defun parse-optional-spec (spec)
249 "Parses an &optional parameter. Returns 3 values: var, init-form, supplied-p-var.
250[&optional {var | (var [init-form [supplied-p-parameter]])}*] "
251 (let* ((var (cond ((symbolp spec) spec)
252 ((and (listp spec) (first spec)))))
253 (init-form (if (listp spec) (second spec)))
254 (supplied-p-var (if (listp spec) (third spec))))
255 (values var init-form supplied-p-var)))
256
257(defun parse-aux-spec (spec)
258 "Returns two values: variable and init-form"
259 ;; [&aux {var | (var [init-form])}*])
260 (values (if (symbolp spec) spec (first spec))
261 (when (listp spec) (second spec))))
262
a090d1e5
DG
263(defpsmacro defaultf (name value suppl)
264 `(progn
265 ,@(when suppl `((var ,suppl t)))
266 (when (=== ,name undefined)
267 (setf ,name ,value ,@(when suppl (list suppl nil))))))
18dd299a
VS
268
269(defun parse-extended-function (lambda-list body &optional name)
270 "Returns two values: the effective arguments and body for a function with
271the given lambda-list and body."
272
273 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
274 ;; list of variable names, and you have access to the arguments variable inside the function:
275 ;; * standard variables are the mapped directly into the js-lambda list
276 ;; * optional variables' variable names are mapped directly into the lambda list,
a090d1e5
DG
277 ;; and for each optional variable with name v, default value d, and
278 ;; supplied-p parameter s, a form is produced (defaultf v d s)
66acaf33
DG
279 ;; * keyword variables are not included in the js-lambda list, but instead are
280 ;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
a090d1e5
DG
281 ;; keyword vars is prepended to the body of the function. Defaults and supplied-p
282 ;; are handled using the same mechanism as with optional vars.
18dd299a
VS
283 (declare (ignore name))
284 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
285 more? more-context more-count key-object)
286 (parse-lambda-list lambda-list)
66acaf33
DG
287 (declare (ignore allow? aux? aux more? more-context more-count key-object))
288 (let* (;; optionals are of form (var default-value)
18dd299a
VS
289 (effective-args
290 (remove-if
291 #'null
292 (append requireds
66acaf33
DG
293 (mapcar #'parse-optional-spec optionals))))
294 (opt-forms
295 (mapcar #'(lambda (opt-spec)
a090d1e5
DG
296 (multiple-value-bind (var val suppl)
297 (parse-optional-spec opt-spec)
298 `(defaultf ,var ,val ,suppl)))
66acaf33
DG
299 optionals))
300 (key-forms
301 (when keys?
4525e3cd
VS
302 (if (< *js-target-version* 1.6)
303 (with-ps-gensyms (n)
304 (let ((decls nil) (assigns nil) (defaults nil))
305 (mapc (lambda (k)
a090d1e5 306 (multiple-value-bind (var init-form keyword-str suppl)
4525e3cd
VS
307 (parse-key-spec k)
308 (push `(var ,var) decls)
309 (push `(,keyword-str (setf ,var (aref arguments (1+ ,n)))) assigns)
a090d1e5 310 (push (list 'defaultf var init-form suppl) defaults)))
4525e3cd
VS
311 (reverse keys))
312 `(,@decls
313 (loop :for ,n :from ,(length requireds)
314 :below (length arguments) :by 2 :do
315 (case (aref arguments ,n) ,@assigns))
316 ,@defaults)))
317 (mapcar (lambda (k)
318 (multiple-value-bind (var init-form keyword-str)
319 (parse-key-spec k)
320 (with-ps-gensyms (x)
321 `(let ((,x ((@ *Array prototype index-of call) arguments ,keyword-str ,(length requireds))))
322 (var ,var (if (= -1 ,x) ,init-form (aref arguments (1+ ,x))))))))
323 keys))))
18dd299a
VS
324 (rest-form
325 (if rest?
326 (with-ps-gensyms (i)
f326f929 327 `(progn (var ,rest (array))
0ce67a33 328 (dotimes (,i (- (slot-value arguments 'length) ,(length effective-args)))
18dd299a
VS
329 (setf (aref ,rest ,i) (aref arguments (+ ,i ,(length effective-args)))))))
330 `(progn)))
66acaf33
DG
331 (body-paren-forms (parse-function-body body)) ; remove documentation
332 (effective-body (append opt-forms key-forms (list rest-form) body-paren-forms)))
18dd299a
VS
333 (values effective-args effective-body))))
334
335(defpsmacro defun (name lambda-list &body body)
336 "An extended defun macro that allows cool things like keyword arguments.
337lambda-list::=
338 (var*
339 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
340 [&rest var]
341 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
342 [&aux {var | (var [init-form])}*])"
343 (if (symbolp name)
344 `(defun-function ,name ,lambda-list ,@body)
b39a6394 345 (progn (assert (and (listp name) (= (length name) 2) (eq 'setf (car name))) ()
18dd299a
VS
346 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list)
347 `(defun-setf ,name ,lambda-list ,@body))))
348
349(defpsmacro defun-function (name lambda-list &body body)
350 (multiple-value-bind (effective-args effective-body)
351 (parse-extended-function lambda-list body name)
352 `(%js-defun ,name ,effective-args
353 ,@effective-body)))
354
18dd299a
VS
355(defpsmacro lambda (lambda-list &body body)
356 "An extended defun macro that allows cool things like keyword arguments.
357lambda-list::=
358 (var*
359 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
360 [&rest var]
361 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
362 [&aux {var | (var [init-form])}*])"
363 (multiple-value-bind (effective-args effective-body)
364 (parse-extended-function lambda-list body)
365 `(%js-lambda ,effective-args
366 ,@effective-body)))
367
5a69278c
VS
368(define-ps-special-form flet (fn-defs &rest body)
369 (let ((fn-renames (make-macro-dictionary)))
370 (loop for (fn-name . def) in fn-defs do
371 (setf (gethash fn-name fn-renames) (ps-gensym fn-name)))
372 (let ((fn-defs (compile-parenscript-form
373 `(progn ,@(loop for (fn-name . def) in fn-defs collect
374 `(var ,(gethash fn-name fn-renames) (lambda ,@def))))
375 :expecting expecting))
376 (*ps-local-function-names* (cons fn-renames *ps-local-function-names*)))
377 (append fn-defs (cdr (compile-parenscript-form `(progn ,@body) :expecting expecting))))))
378
379(define-ps-special-form labels (fn-defs &rest body)
380 (with-local-macro-environment (local-fn-renames *ps-local-function-names*)
381 (loop for (fn-name . def) in fn-defs do
382 (setf (gethash fn-name local-fn-renames) (ps-gensym fn-name)))
383 (compile-parenscript-form
384 `(progn ,@(loop for (fn-name . def) in fn-defs collect
385 `(var ,(gethash fn-name local-fn-renames) (lambda ,@def)))
386 ,@body)
387 :expecting expecting)))
ef3be63e 388
5a69278c
VS
389(defvar *defun-setf-name-prefix* "__setf_")
390
391(defpsmacro defun-setf (setf-name lambda-list &body body)
392 (let ((mangled-function-name (intern (concatenate 'string *defun-setf-name-prefix* (symbol-name (second setf-name)))
393 (symbol-package (second setf-name))))
394 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords))))
395 (ps* `(defsetf ,(second setf-name) ,(cdr lambda-list) (store-var)
396 `(,',mangled-function-name ,store-var ,@(list ,@function-args))))
397 `(defun ,mangled-function-name ,lambda-list ,@body)))
ef3be63e 398
18dd299a 399(defpsmacro defsetf-long (access-fn lambda-list (store-var) form)
5a69278c 400 (setf (gethash access-fn *ps-setf-expanders*)
18dd299a
VS
401 (compile nil
402 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords)))
403 `(lambda (access-fn-args store-form)
404 (destructuring-bind ,lambda-list
405 access-fn-args
406 (let* ((,store-var (ps-gensym))
407 (gensymed-names (loop repeat ,(length var-bindings) collecting (ps-gensym)))
408 (gensymed-arg-bindings (mapcar #'list gensymed-names (list ,@var-bindings))))
409 (destructuring-bind ,var-bindings
410 gensymed-names
58c4ef4f
VS
411 `(let* (,@gensymed-arg-bindings
412 (,,store-var ,store-form))
18dd299a
VS
413 ,,form))))))))
414 nil)
415
416(defpsmacro defsetf-short (access-fn update-fn &optional docstring)
417 (declare (ignore docstring))
5a69278c 418 (setf (gethash access-fn *ps-setf-expanders*)
18dd299a
VS
419 (lambda (access-fn-args store-form)
420 `(,update-fn ,@access-fn-args ,store-form)))
421 nil)
422
423(defpsmacro defsetf (access-fn &rest args)
424 `(,(if (= (length args) 3) 'defsetf-long 'defsetf-short) ,access-fn ,@args))
425
59217e4c
VS
426(defpsmacro funcall (&rest arg-form)
427 arg-form)
428
18dd299a
VS
429;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
430;;; macros
e8fdcce7 431(define-ps-special-form macrolet (macros &body body)
5a69278c 432 (with-local-macro-environment (local-macro-dict *ps-macro-env*)
18dd299a
VS
433 (dolist (macro macros)
434 (destructuring-bind (name arglist &body body)
435 macro
5a69278c 436 (setf (gethash name local-macro-dict) (eval (make-ps-macro-function arglist body)))))
3bfb836a 437 (compile-parenscript-form `(progn ,@body) :expecting expecting)))
18dd299a 438
e8fdcce7 439(define-ps-special-form symbol-macrolet (symbol-macros &body body)
5a69278c 440 (with-local-macro-environment (local-macro-dict *ps-symbol-macro-env*)
18dd299a
VS
441 (dolist (macro symbol-macros)
442 (destructuring-bind (name expansion)
443 macro
5a69278c 444 (setf (gethash name local-macro-dict) (lambda (x) (declare (ignore x)) expansion))))
3bfb836a 445 (compile-parenscript-form `(progn ,@body) :expecting expecting)))
18dd299a 446
0ce67a33 447(define-ps-special-form defmacro (name args &body body) ;; should this be a macro?
8cfc6fe9 448 (eval `(defpsmacro ,name ,args ,@body))
18dd299a
VS
449 nil)
450
0ce67a33 451(define-ps-special-form define-symbol-macro (name expansion) ;; should this be a macro?
8cfc6fe9 452 (eval `(define-ps-symbol-macro ,name ,expansion))
18dd299a
VS
453 nil)
454
455;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456;;; objects
79630c82
VS
457(add-ps-literal '{})
458(define-ps-symbol-macro {} (create))
459
e8fdcce7 460(define-ps-special-form create (&rest arrows)
0ce67a33
VS
461 `(js:object ,@(loop for (key-expr val-expr) on arrows by #'cddr collecting
462 (let ((key (compile-parenscript-form key-expr :expecting :expression)))
463 (when (keywordp key)
464 (setf key `(js:variable ,key)))
465 (assert (or (stringp key)
466 (numberp key)
467 (and (listp key)
468 (or (eq 'js:variable (car key))
469 (eq 'quote (car key)))))
470 ()
471 "Slot key ~s is not one of js-variable, keyword, string or number." key)
472 (cons key (compile-parenscript-form val-expr :expecting :expression))))))
18dd299a 473
e8fdcce7 474(define-ps-special-form %js-slot-value (obj slot)
5a69278c
VS
475 (let ((slot (ps-macroexpand slot)))
476 `(js:slot-value ,(compile-parenscript-form (ps-macroexpand obj) :expecting :expression)
477 ,(if (and (listp slot) (eq 'quote (car slot)))
478 (second slot) ;; assume we're quoting a symbol
479 (compile-parenscript-form slot)))))
18dd299a 480
e8fdcce7 481(define-ps-special-form instanceof (value type)
0ce67a33
VS
482 `(js:instanceof ,(compile-parenscript-form value :expecting :expression)
483 ,(compile-parenscript-form type :expecting :expression)))
18dd299a
VS
484
485(defpsmacro slot-value (obj &rest slots)
486 (if (null (rest slots))
487 `(%js-slot-value ,obj ,(first slots))
488 `(slot-value (slot-value ,obj ,(first slots)) ,@(rest slots))))
489
490(defpsmacro with-slots (slots object &rest body)
491 (flet ((slot-var (slot) (if (listp slot) (first slot) slot))
b508414b 492 (slot-symbol (slot) (if (listp slot) (second slot) slot)))
18dd299a 493 `(symbol-macrolet ,(mapcar #'(lambda (slot)
b508414b
TC
494 `(,(slot-var slot) (slot-value ,object ',(slot-symbol slot))))
495 slots)
18dd299a
VS
496 ,@body)))
497
498;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499;;; assignment and binding
500(defun assignment-op (op)
501 (case op
502 (+ '+=)
503 (~ '~=)
504 (\& '\&=)
505 (\| '\|=)
506 (- '-=)
507 (* '*=)
508 (% '%=)
509 (>> '>>=)
510 (^ '^=)
511 (<< '<<=)
512 (>>> '>>>=)
513 (/ '/=)
514 (t nil)))
515
e8fdcce7 516(define-ps-special-form setf1% (lhs rhs)
5a69278c
VS
517 (let ((lhs (compile-parenscript-form (ps-macroexpand lhs) :expecting :expression))
518 (rhs (compile-parenscript-form (ps-macroexpand rhs) :expecting :expression)))
0ce67a33
VS
519 (if (and (listp rhs)
520 (eq 'js:operator (car rhs))
521 (member (cadr rhs) '(+ *))
522 (equalp lhs (caddr rhs)))
523 `(js:operator ,(assignment-op (cadr rhs)) ,lhs (js:operator ,(cadr rhs) ,@(cdddr rhs)))
524 `(js:= ,lhs ,rhs))))
18dd299a
VS
525
526(defpsmacro setf (&rest args)
5a69278c
VS
527 (assert (evenp (length args)) ()
528 "~s does not have an even number of arguments." `(setf ,args))
529 `(progn ,@(loop for (place value) on args by #'cddr collect
530 (let ((place (ps-macroexpand place)))
531 (aif (and (listp place) (gethash (car place) *ps-setf-expanders*))
532 (funcall it (cdr place) value)
533 `(setf1% ,place ,value))))))
18dd299a 534
1fe28ee1 535(defpsmacro psetf (&rest args)
5ffb1eba 536 (let ((places (loop for x in args by #'cddr collect x))
1fe28ee1 537 (vals (loop for x in (cdr args) by #'cddr collect x)))
5ffb1eba
VS
538 (let ((gensyms (mapcar (lambda (x) (declare (ignore x)) (ps-gensym)) places)))
539 `(let ,(mapcar #'list gensyms vals)
540 (setf ,@(mapcan #'list places gensyms))))))
1fe28ee1 541
ec227186
TC
542(defun check-setq-args (args)
543 (let ((vars (loop for x in args by #'cddr collect x)))
544 (let ((non-var (find-if (complement #'symbolp) vars)))
545 (when non-var
546 (error 'type-error :datum non-var :expected-type 'symbol)))))
547
548(defpsmacro setq (&rest args)
549 (check-setq-args args)
550 `(setf ,@args))
551
552(defpsmacro psetq (&rest args)
553 (check-setq-args args)
554 `(psetf ,@args))
555
0ce67a33
VS
556(define-ps-special-form var (name &optional (value (values) value-provided?) documentation)
557 (declare (ignore documentation))
058f137f
VS
558 (let ((name (ps-macroexpand name)))
559 (ecase expecting
560 (:statement
561 `(js:var ,name ,@(when value-provided?
5a69278c 562 (list (compile-parenscript-form (ps-macroexpand value) :expecting :expression)))))
058f137f
VS
563 (:expression
564 (push name *enclosing-lexical-block-declarations*)
565 (when value-provided?
566 (compile-parenscript-form `(setf ,name ,value) :expecting :expression))))))
18dd299a 567
0ce67a33 568(defpsmacro defvar (name &optional (value (values) value-provided?) documentation)
5ffb1eba
VS
569 ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
570 (declare (ignore documentation))
58c4ef4f 571 (pushnew name *ps-special-variables*)
0ce67a33 572 `(var ,name ,@(when value-provided? (list value))))
58c4ef4f 573
5ffb1eba
VS
574(defpsmacro let (bindings &body body)
575 (flet ((add-renamed-vars (bindings predicate)
576 (mapcar (lambda (x) (append x (list (ps-gensym (car x)))))
577 (remove-if predicate bindings :key #'car)))
578 (var (x) (first x))
579 (val (x) (second x))
580 (renamed (x) (third x)))
581 (let* ((normalized-bindings (mapcar (lambda (x) (if (symbolp x) `(,x nil) x)) bindings))
582 (lexical-bindings (add-renamed-vars normalized-bindings #'ps-special-variable-p))
583 (dynamic-bindings (add-renamed-vars normalized-bindings (complement #'ps-special-variable-p)))
584 (renamed-body `(symbol-macrolet ,(mapcar (lambda (x) (list (var x) (renamed x)))
585 lexical-bindings)
586 ,@body)))
587 `(progn
588 ,@(mapcar (lambda (x) `(var ,(renamed x) ,(val x))) lexical-bindings)
589 ,(if dynamic-bindings
590 `(progn ,@(mapcar (lambda (x) `(var ,(renamed x))) dynamic-bindings)
591 (try (progn (setf ,@(loop for x in dynamic-bindings append
592 `(,(renamed x) ,(var x)
593 ,(var x) ,(val x))))
594 ,renamed-body)
595 (:finally
596 (setf ,@(mapcan (lambda (x) `(,(var x) ,(renamed x))) dynamic-bindings)))))
597 renamed-body)))))
3530f5e1 598
5ffb1eba 599(defpsmacro let* (bindings &body body)
58c4ef4f 600 (if bindings
5ffb1eba
VS
601 `(let (,(car bindings))
602 (let* ,(cdr bindings)
603 ,@body))
58c4ef4f
VS
604 `(progn ,@body)))
605
18dd299a
VS
606;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
607;;; iteration
6a2ce72d
TC
608(defun make-for-vars/inits (init-forms)
609 (mapcar (lambda (x)
5a69278c
VS
610 (cons (compile-parenscript-form (ps-macroexpand (if (atom x) x (first x))) :expecting :symbol)
611 (compile-parenscript-form (ps-macroexpand (if (atom x) nil (second x))) :expecting :expression)))
6a2ce72d 612 init-forms))
18dd299a 613
e8fdcce7 614(define-ps-special-form labeled-for (label init-forms cond-forms step-forms &rest body)
0ce67a33
VS
615 `(js:for ,label
616 ,(make-for-vars/inits init-forms)
5a69278c
VS
617 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x) :expecting :expression)) cond-forms)
618 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x) :expecting :expression)) step-forms)
0ce67a33 619 ,(compile-parenscript-form `(progn ,@body))))
6a2ce72d
TC
620
621(defpsmacro for (init-forms cond-forms step-forms &body body)
622 `(labeled-for nil ,init-forms ,cond-forms ,step-forms ,@body))
623
624(defun do-make-let-bindings (decls)
625 (mapcar (lambda (x)
626 (if (atom x) x
627 (if (endp (cdr x)) (list (car x))
628 (subseq x 0 2))))
629 decls))
630
631(defun do-make-init-vars (decls)
632 (mapcar (lambda (x) (if (atom x) x (first x))) decls))
633
634(defun do-make-init-vals (decls)
635 (mapcar (lambda (x) (if (or (atom x) (endp (cdr x))) nil (second x))) decls))
636
637(defun do-make-for-vars/init (decls)
638 (mapcar (lambda (x)
639 (if (atom x) x
640 (if (endp (cdr x)) x
641 (subseq x 0 2))))
642 decls))
643
644(defun do-make-for-steps (decls)
645 (mapcar (lambda (x)
646 `(setf ,(first x) ,(third x)))
647 (remove-if (lambda (x) (or (atom x) (< (length x) 3))) decls)))
648
649(defun do-make-iter-psteps (decls)
650 `(psetq
651 ,@(mapcan (lambda (x) (list (first x) (third x)))
652 (remove-if (lambda (x) (or (atom x) (< (length x) 3))) decls))))
653
654(defpsmacro do* (decls (termination &optional (result nil result?)) &body body)
655 (if result?
656 `((lambda ()
657 (for ,(do-make-for-vars/init decls) ((not ,termination)) ,(do-make-for-steps decls)
658 ,@body)
659 (return ,result)))
660 `(progn
661 (for ,(do-make-for-vars/init decls) ((not ,termination)) ,(do-make-for-steps decls)
662 ,@body))))
663
664(defpsmacro do (decls (termination &optional (result nil result?)) &body body)
665 (if result?
666 `((lambda ,(do-make-init-vars decls)
667 (for () ((not ,termination)) ()
668 ,@body
669 ,(do-make-iter-psteps decls))
670 (return ,result))
671 ,@(do-make-init-vals decls))
672 `(let ,(do-make-let-bindings decls)
673 (for () ((not ,termination)) ()
674 ,@body
675 ,(do-make-iter-psteps decls)))))
676
0ce67a33 677(define-ps-special-form for-in ((var object) &rest body)
5ffb1eba 678 `(js:for-in ,(compile-parenscript-form var :expecting :expression)
5a69278c 679 ,(compile-parenscript-form (ps-macroexpand object) :expecting :expression)
0ce67a33 680 ,(compile-parenscript-form `(progn ,@body))))
6a2ce72d 681
e8fdcce7 682(define-ps-special-form while (test &rest body)
0ce67a33
VS
683 `(js:while ,(compile-parenscript-form test :expecting :expression)
684 ,(compile-parenscript-form `(progn ,@body))))
18dd299a 685
6a2ce72d
TC
686(defpsmacro dotimes ((var count &optional (result nil result?)) &rest body)
687 `(do* ((,var 0 (1+ ,var)))
688 ((>= ,var ,count) ,@(when result? (list result)))
689 ,@body))
690
691(defpsmacro dolist ((var array &optional (result nil result?)) &body body)
692 (let ((idx (ps-gensym "_js_idx"))
693 (arrvar (ps-gensym "_js_arrvar")))
694 `(do* (,var
695 (,arrvar ,array)
696 (,idx 0 (1+ ,idx)))
697 ((>= ,idx (slot-value ,arrvar 'length))
698 ,@(when result? (list result)))
699 (setq ,var (aref ,arrvar ,idx))
700 ,@body)))
18dd299a
VS
701
702;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
703;;; misc
e8fdcce7 704(define-ps-special-form with (expression &rest body)
0ce67a33
VS
705 `(js:with ,(compile-parenscript-form expression :expecting :expression)
706 ,(compile-parenscript-form `(progn ,@body))))
18dd299a 707
e8fdcce7 708(define-ps-special-form try (form &rest clauses)
18dd299a
VS
709 (let ((catch (cdr (assoc :catch clauses)))
710 (finally (cdr (assoc :finally clauses))))
711 (assert (not (cdar catch)) nil "Sorry, currently only simple catch forms are supported.")
712 (assert (or catch finally) ()
713 "Try form should have either a catch or a finally clause or both.")
0ce67a33
VS
714 `(js:try ,(compile-parenscript-form `(progn ,form))
715 :catch ,(when catch (list (compile-parenscript-form (caar catch) :expecting :symbol)
18dd299a 716 (compile-parenscript-form `(progn ,@(cdr catch)))))
0ce67a33 717 :finally ,(when finally (compile-parenscript-form `(progn ,@finally))))))
18dd299a 718
e8fdcce7 719(define-ps-special-form cc-if (test &rest body)
0ce67a33 720 `(js:cc-if ,test ,@(mapcar #'compile-parenscript-form body)))
18dd299a 721
e8fdcce7 722(define-ps-special-form regex (regex)
0ce67a33 723 `(js:regex ,(string regex)))
18dd299a 724
ceb1f277
VS
725(define-ps-special-form lisp (lisp-form)
726 ;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar))
727 ;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval)
5a69278c 728 `(js:escape (compiled-form-to-string (compile-parenscript-form ,lisp-form :expecting ,expecting))))
8877a380
VS
729
730;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
731;;; eval-when
732(define-ps-special-form eval-when (situation-list &body body)
733 "(eval-when (situation*) body-form*)
734
735The body forms are evaluated only during the given SITUATION. The accepted SITUATIONS are
736:load-toplevel, :compile-toplevel, and :execute. The code in BODY-FORM is assumed to be
737COMMON-LISP code in :compile-toplevel and :load-toplevel sitations, and parenscript code in
738:execute. "
739 (when (and (member :compile-toplevel situation-list)
5a69278c 740 (member *ps-compilation-level* '(:toplevel :inside-toplevel-form)))
8877a380
VS
741 (eval `(progn ,@body)))
742 (if (member :execute situation-list)
743 (compile-parenscript-form `(progn ,@body) :expecting expecting)
744 (compile-parenscript-form `(progn) :expecting expecting)))