Fixed some special forms that weren't macro-expanding their 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)
6dce8f77 67 `(js:array ,@(mapcar (lambda (form) (compile-parenscript-form (ps-macroexpand 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
6dce8f77 170 (:statement `(js:if ,(compile-parenscript-form (ps-macroexpand test) :expecting :expression)
e8fdcce7 171 ,(compile-parenscript-form `(progn ,then))
fdfa77fc 172 ,@(when else `(:else ,(compile-parenscript-form `(progn ,else))))))
6dce8f77
VS
173 (:expression `(js:? ,(compile-parenscript-form (ps-macroexpand test) :expecting :expression)
174 ,(compile-parenscript-form (ps-macroexpand then) :expecting :expression)
175 ,(compile-parenscript-form (ps-macroexpand else) :expecting :expression)))))
e8fdcce7
VS
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
16151f19
VS
209
210(defvar *vars-bound-in-enclosing-lexical-scopes* ())
211
18dd299a 212(defun compile-function-definition (args body)
16151f19
VS
213 (let ((args (mapcar (lambda (arg) (compile-parenscript-form arg :expecting :symbol)) args)))
214 (list args
215 (let* ((*enclosing-lexical-block-declarations* ())
216 (*vars-bound-in-enclosing-lexical-scopes* (append args
217 *vars-bound-in-enclosing-lexical-scopes*))
218 (body (compile-parenscript-form `(progn ,@body)))
219 (var-decls (compile-parenscript-form
220 `(progn ,@(mapcar (lambda (var) `(var ,var)) *enclosing-lexical-block-declarations*)))))
221 `(js:block ,@(cdr var-decls) ,@(cdr body))))))
18dd299a 222
e8fdcce7 223(define-ps-special-form %js-lambda (args &rest body)
0ce67a33 224 `(js:lambda ,@(compile-function-definition args body)))
18dd299a 225
e8fdcce7 226(define-ps-special-form %js-defun (name args &rest body)
0ce67a33 227 `(js:defun ,name ,@(compile-function-definition args body)))
18dd299a
VS
228
229(defun parse-function-body (body)
230 (let* ((docstring
231 (when (stringp (first body))
232 (first body)))
233 (body-forms (if docstring (rest body) body)))
234 (values body-forms docstring)))
235
236(defun parse-key-spec (key-spec)
66acaf33 237 "parses an &key parameter. Returns 5 values:
18dd299a
VS
238var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
239
240Syntax of key spec:
241[&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
242"
243 (let* ((var (cond ((symbolp key-spec) key-spec)
244 ((and (listp key-spec) (symbolp (first key-spec))) (first key-spec))
62ddca23 245 ((and (listp key-spec) (listp (first key-spec))) (second (first key-spec)))))
18dd299a
VS
246 (keyword-name (if (and (listp key-spec) (listp (first key-spec)))
247 (first (first key-spec))
248 (intern (string var) :keyword)))
249 (init-form (if (listp key-spec) (second key-spec) nil))
250 (init-form-supplied-p (if (listp key-spec) t nil))
251 (supplied-p-var (if (listp key-spec) (third key-spec) nil)))
252 (values var init-form keyword-name supplied-p-var init-form-supplied-p)))
253
254(defun parse-optional-spec (spec)
255 "Parses an &optional parameter. Returns 3 values: var, init-form, supplied-p-var.
256[&optional {var | (var [init-form [supplied-p-parameter]])}*] "
257 (let* ((var (cond ((symbolp spec) spec)
258 ((and (listp spec) (first spec)))))
259 (init-form (if (listp spec) (second spec)))
260 (supplied-p-var (if (listp spec) (third spec))))
261 (values var init-form supplied-p-var)))
262
263(defun parse-aux-spec (spec)
264 "Returns two values: variable and init-form"
265 ;; [&aux {var | (var [init-form])}*])
266 (values (if (symbolp spec) spec (first spec))
267 (when (listp spec) (second spec))))
268
a090d1e5
DG
269(defpsmacro defaultf (name value suppl)
270 `(progn
271 ,@(when suppl `((var ,suppl t)))
272 (when (=== ,name undefined)
273 (setf ,name ,value ,@(when suppl (list suppl nil))))))
18dd299a
VS
274
275(defun parse-extended-function (lambda-list body &optional name)
276 "Returns two values: the effective arguments and body for a function with
277the given lambda-list and body."
278
279 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
280 ;; list of variable names, and you have access to the arguments variable inside the function:
281 ;; * standard variables are the mapped directly into the js-lambda list
282 ;; * optional variables' variable names are mapped directly into the lambda list,
a090d1e5
DG
283 ;; and for each optional variable with name v, default value d, and
284 ;; supplied-p parameter s, a form is produced (defaultf v d s)
66acaf33
DG
285 ;; * keyword variables are not included in the js-lambda list, but instead are
286 ;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
a090d1e5
DG
287 ;; keyword vars is prepended to the body of the function. Defaults and supplied-p
288 ;; are handled using the same mechanism as with optional vars.
18dd299a
VS
289 (declare (ignore name))
290 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
291 more? more-context more-count key-object)
292 (parse-lambda-list lambda-list)
66acaf33
DG
293 (declare (ignore allow? aux? aux more? more-context more-count key-object))
294 (let* (;; optionals are of form (var default-value)
18dd299a
VS
295 (effective-args
296 (remove-if
297 #'null
298 (append requireds
66acaf33
DG
299 (mapcar #'parse-optional-spec optionals))))
300 (opt-forms
301 (mapcar #'(lambda (opt-spec)
a090d1e5
DG
302 (multiple-value-bind (var val suppl)
303 (parse-optional-spec opt-spec)
304 `(defaultf ,var ,val ,suppl)))
66acaf33
DG
305 optionals))
306 (key-forms
307 (when keys?
4525e3cd
VS
308 (if (< *js-target-version* 1.6)
309 (with-ps-gensyms (n)
310 (let ((decls nil) (assigns nil) (defaults nil))
311 (mapc (lambda (k)
a090d1e5 312 (multiple-value-bind (var init-form keyword-str suppl)
4525e3cd
VS
313 (parse-key-spec k)
314 (push `(var ,var) decls)
315 (push `(,keyword-str (setf ,var (aref arguments (1+ ,n)))) assigns)
a090d1e5 316 (push (list 'defaultf var init-form suppl) defaults)))
4525e3cd
VS
317 (reverse keys))
318 `(,@decls
319 (loop :for ,n :from ,(length requireds)
320 :below (length arguments) :by 2 :do
321 (case (aref arguments ,n) ,@assigns))
322 ,@defaults)))
323 (mapcar (lambda (k)
324 (multiple-value-bind (var init-form keyword-str)
325 (parse-key-spec k)
326 (with-ps-gensyms (x)
327 `(let ((,x ((@ *Array prototype index-of call) arguments ,keyword-str ,(length requireds))))
328 (var ,var (if (= -1 ,x) ,init-form (aref arguments (1+ ,x))))))))
329 keys))))
18dd299a
VS
330 (rest-form
331 (if rest?
332 (with-ps-gensyms (i)
f326f929 333 `(progn (var ,rest (array))
0ce67a33 334 (dotimes (,i (- (slot-value arguments 'length) ,(length effective-args)))
18dd299a
VS
335 (setf (aref ,rest ,i) (aref arguments (+ ,i ,(length effective-args)))))))
336 `(progn)))
66acaf33
DG
337 (body-paren-forms (parse-function-body body)) ; remove documentation
338 (effective-body (append opt-forms key-forms (list rest-form) body-paren-forms)))
18dd299a
VS
339 (values effective-args effective-body))))
340
341(defpsmacro defun (name lambda-list &body body)
342 "An extended defun macro that allows cool things like keyword arguments.
343lambda-list::=
344 (var*
345 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
346 [&rest var]
347 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
348 [&aux {var | (var [init-form])}*])"
349 (if (symbolp name)
350 `(defun-function ,name ,lambda-list ,@body)
b39a6394 351 (progn (assert (and (listp name) (= (length name) 2) (eq 'setf (car name))) ()
18dd299a
VS
352 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list)
353 `(defun-setf ,name ,lambda-list ,@body))))
354
355(defpsmacro defun-function (name lambda-list &body body)
356 (multiple-value-bind (effective-args effective-body)
357 (parse-extended-function lambda-list body name)
358 `(%js-defun ,name ,effective-args
359 ,@effective-body)))
360
18dd299a
VS
361(defpsmacro lambda (lambda-list &body body)
362 "An extended defun macro that allows cool things like keyword arguments.
363lambda-list::=
364 (var*
365 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
366 [&rest var]
367 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
368 [&aux {var | (var [init-form])}*])"
369 (multiple-value-bind (effective-args effective-body)
370 (parse-extended-function lambda-list body)
371 `(%js-lambda ,effective-args
372 ,@effective-body)))
373
5a69278c
VS
374(define-ps-special-form flet (fn-defs &rest body)
375 (let ((fn-renames (make-macro-dictionary)))
376 (loop for (fn-name . def) in fn-defs do
377 (setf (gethash fn-name fn-renames) (ps-gensym fn-name)))
378 (let ((fn-defs (compile-parenscript-form
379 `(progn ,@(loop for (fn-name . def) in fn-defs collect
380 `(var ,(gethash fn-name fn-renames) (lambda ,@def))))
381 :expecting expecting))
382 (*ps-local-function-names* (cons fn-renames *ps-local-function-names*)))
383 (append fn-defs (cdr (compile-parenscript-form `(progn ,@body) :expecting expecting))))))
384
385(define-ps-special-form labels (fn-defs &rest body)
386 (with-local-macro-environment (local-fn-renames *ps-local-function-names*)
387 (loop for (fn-name . def) in fn-defs do
388 (setf (gethash fn-name local-fn-renames) (ps-gensym fn-name)))
389 (compile-parenscript-form
390 `(progn ,@(loop for (fn-name . def) in fn-defs collect
391 `(var ,(gethash fn-name local-fn-renames) (lambda ,@def)))
392 ,@body)
393 :expecting expecting)))
ef3be63e 394
5a69278c
VS
395(defvar *defun-setf-name-prefix* "__setf_")
396
397(defpsmacro defun-setf (setf-name lambda-list &body body)
398 (let ((mangled-function-name (intern (concatenate 'string *defun-setf-name-prefix* (symbol-name (second setf-name)))
399 (symbol-package (second setf-name))))
400 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords))))
401 (ps* `(defsetf ,(second setf-name) ,(cdr lambda-list) (store-var)
402 `(,',mangled-function-name ,store-var ,@(list ,@function-args))))
403 `(defun ,mangled-function-name ,lambda-list ,@body)))
ef3be63e 404
18dd299a 405(defpsmacro defsetf-long (access-fn lambda-list (store-var) form)
5a69278c 406 (setf (gethash access-fn *ps-setf-expanders*)
18dd299a
VS
407 (compile nil
408 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords)))
409 `(lambda (access-fn-args store-form)
410 (destructuring-bind ,lambda-list
411 access-fn-args
412 (let* ((,store-var (ps-gensym))
413 (gensymed-names (loop repeat ,(length var-bindings) collecting (ps-gensym)))
414 (gensymed-arg-bindings (mapcar #'list gensymed-names (list ,@var-bindings))))
415 (destructuring-bind ,var-bindings
416 gensymed-names
58c4ef4f
VS
417 `(let* (,@gensymed-arg-bindings
418 (,,store-var ,store-form))
18dd299a
VS
419 ,,form))))))))
420 nil)
421
422(defpsmacro defsetf-short (access-fn update-fn &optional docstring)
423 (declare (ignore docstring))
5a69278c 424 (setf (gethash access-fn *ps-setf-expanders*)
18dd299a
VS
425 (lambda (access-fn-args store-form)
426 `(,update-fn ,@access-fn-args ,store-form)))
427 nil)
428
429(defpsmacro defsetf (access-fn &rest args)
430 `(,(if (= (length args) 3) 'defsetf-long 'defsetf-short) ,access-fn ,@args))
431
59217e4c
VS
432(defpsmacro funcall (&rest arg-form)
433 arg-form)
434
18dd299a
VS
435;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
436;;; macros
e8fdcce7 437(define-ps-special-form macrolet (macros &body body)
5a69278c 438 (with-local-macro-environment (local-macro-dict *ps-macro-env*)
18dd299a
VS
439 (dolist (macro macros)
440 (destructuring-bind (name arglist &body body)
441 macro
5a69278c 442 (setf (gethash name local-macro-dict) (eval (make-ps-macro-function arglist body)))))
3bfb836a 443 (compile-parenscript-form `(progn ,@body) :expecting expecting)))
18dd299a 444
e8fdcce7 445(define-ps-special-form symbol-macrolet (symbol-macros &body body)
5a69278c 446 (with-local-macro-environment (local-macro-dict *ps-symbol-macro-env*)
16151f19
VS
447 (let (local-var-bindings)
448 (dolist (macro symbol-macros)
449 (destructuring-bind (name expansion)
450 macro
451 (setf (gethash name local-macro-dict) (lambda (x) (declare (ignore x)) expansion))
452 (push name local-var-bindings)))
453 (let ((*vars-bound-in-enclosing-lexical-scopes* (append local-var-bindings
454 *vars-bound-in-enclosing-lexical-scopes*)))
455 (compile-parenscript-form `(progn ,@body) :expecting expecting)))))
18dd299a 456
0ce67a33 457(define-ps-special-form defmacro (name args &body body) ;; should this be a macro?
8cfc6fe9 458 (eval `(defpsmacro ,name ,args ,@body))
18dd299a
VS
459 nil)
460
0ce67a33 461(define-ps-special-form define-symbol-macro (name expansion) ;; should this be a macro?
8cfc6fe9 462 (eval `(define-ps-symbol-macro ,name ,expansion))
18dd299a
VS
463 nil)
464
465;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
466;;; objects
79630c82
VS
467(add-ps-literal '{})
468(define-ps-symbol-macro {} (create))
469
e8fdcce7 470(define-ps-special-form create (&rest arrows)
0ce67a33 471 `(js:object ,@(loop for (key-expr val-expr) on arrows by #'cddr collecting
6dce8f77 472 (let ((key (compile-parenscript-form (ps-macroexpand key-expr) :expecting :expression)))
0ce67a33
VS
473 (when (keywordp key)
474 (setf key `(js:variable ,key)))
475 (assert (or (stringp key)
476 (numberp key)
477 (and (listp key)
478 (or (eq 'js:variable (car key))
479 (eq 'quote (car key)))))
480 ()
481 "Slot key ~s is not one of js-variable, keyword, string or number." key)
6dce8f77 482 (cons key (compile-parenscript-form (ps-macroexpand val-expr) :expecting :expression))))))
18dd299a 483
e8fdcce7 484(define-ps-special-form %js-slot-value (obj slot)
5a69278c
VS
485 (let ((slot (ps-macroexpand slot)))
486 `(js:slot-value ,(compile-parenscript-form (ps-macroexpand obj) :expecting :expression)
487 ,(if (and (listp slot) (eq 'quote (car slot)))
488 (second slot) ;; assume we're quoting a symbol
489 (compile-parenscript-form slot)))))
18dd299a 490
e8fdcce7 491(define-ps-special-form instanceof (value type)
0ce67a33
VS
492 `(js:instanceof ,(compile-parenscript-form value :expecting :expression)
493 ,(compile-parenscript-form type :expecting :expression)))
18dd299a
VS
494
495(defpsmacro slot-value (obj &rest slots)
496 (if (null (rest slots))
497 `(%js-slot-value ,obj ,(first slots))
498 `(slot-value (slot-value ,obj ,(first slots)) ,@(rest slots))))
499
500(defpsmacro with-slots (slots object &rest body)
501 (flet ((slot-var (slot) (if (listp slot) (first slot) slot))
b508414b 502 (slot-symbol (slot) (if (listp slot) (second slot) slot)))
18dd299a 503 `(symbol-macrolet ,(mapcar #'(lambda (slot)
b508414b
TC
504 `(,(slot-var slot) (slot-value ,object ',(slot-symbol slot))))
505 slots)
18dd299a
VS
506 ,@body)))
507
508;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
509;;; assignment and binding
510(defun assignment-op (op)
511 (case op
512 (+ '+=)
513 (~ '~=)
514 (\& '\&=)
515 (\| '\|=)
516 (- '-=)
517 (* '*=)
518 (% '%=)
519 (>> '>>=)
520 (^ '^=)
521 (<< '<<=)
522 (>>> '>>>=)
523 (/ '/=)
524 (t nil)))
525
e8fdcce7 526(define-ps-special-form setf1% (lhs rhs)
5a69278c
VS
527 (let ((lhs (compile-parenscript-form (ps-macroexpand lhs) :expecting :expression))
528 (rhs (compile-parenscript-form (ps-macroexpand rhs) :expecting :expression)))
0ce67a33
VS
529 (if (and (listp rhs)
530 (eq 'js:operator (car rhs))
531 (member (cadr rhs) '(+ *))
532 (equalp lhs (caddr rhs)))
533 `(js:operator ,(assignment-op (cadr rhs)) ,lhs (js:operator ,(cadr rhs) ,@(cdddr rhs)))
534 `(js:= ,lhs ,rhs))))
18dd299a
VS
535
536(defpsmacro setf (&rest args)
5a69278c
VS
537 (assert (evenp (length args)) ()
538 "~s does not have an even number of arguments." `(setf ,args))
539 `(progn ,@(loop for (place value) on args by #'cddr collect
540 (let ((place (ps-macroexpand place)))
541 (aif (and (listp place) (gethash (car place) *ps-setf-expanders*))
542 (funcall it (cdr place) value)
543 `(setf1% ,place ,value))))))
18dd299a 544
1fe28ee1 545(defpsmacro psetf (&rest args)
5ffb1eba 546 (let ((places (loop for x in args by #'cddr collect x))
1fe28ee1 547 (vals (loop for x in (cdr args) by #'cddr collect x)))
5ffb1eba
VS
548 (let ((gensyms (mapcar (lambda (x) (declare (ignore x)) (ps-gensym)) places)))
549 `(let ,(mapcar #'list gensyms vals)
550 (setf ,@(mapcan #'list places gensyms))))))
1fe28ee1 551
ec227186
TC
552(defun check-setq-args (args)
553 (let ((vars (loop for x in args by #'cddr collect x)))
554 (let ((non-var (find-if (complement #'symbolp) vars)))
555 (when non-var
556 (error 'type-error :datum non-var :expected-type 'symbol)))))
557
558(defpsmacro setq (&rest args)
559 (check-setq-args args)
560 `(setf ,@args))
561
562(defpsmacro psetq (&rest args)
563 (check-setq-args args)
564 `(psetf ,@args))
565
0ce67a33
VS
566(define-ps-special-form var (name &optional (value (values) value-provided?) documentation)
567 (declare (ignore documentation))
058f137f
VS
568 (let ((name (ps-macroexpand name)))
569 (ecase expecting
570 (:statement
571 `(js:var ,name ,@(when value-provided?
5a69278c 572 (list (compile-parenscript-form (ps-macroexpand value) :expecting :expression)))))
058f137f
VS
573 (:expression
574 (push name *enclosing-lexical-block-declarations*)
575 (when value-provided?
576 (compile-parenscript-form `(setf ,name ,value) :expecting :expression))))))
18dd299a 577
0ce67a33 578(defpsmacro defvar (name &optional (value (values) value-provided?) documentation)
5ffb1eba
VS
579 ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
580 (declare (ignore documentation))
58c4ef4f 581 (pushnew name *ps-special-variables*)
0ce67a33 582 `(var ,name ,@(when value-provided? (list value))))
58c4ef4f 583
16151f19
VS
584(define-ps-special-form let (bindings &body body)
585 (let* (lexical-bindings-introduced-here
586 (normalized-bindings (mapcar (lambda (x) (if (symbolp x) `(,x nil) x)) bindings))
587 (free-variables-in-binding-value-expressions (mapcan (lambda (x) (flatten (cadr x)))
588 normalized-bindings)))
589 (flet ((maybe-rename-lexical-var (x)
590 (if (or (member x *vars-bound-in-enclosing-lexical-scopes*)
591 (member x free-variables-in-binding-value-expressions))
592 (ps-gensym x)
593 (progn (push x lexical-bindings-introduced-here) nil)))
594 (rename (x) (first x))
595 (var (x) (second x))
596 (val (x) (third x)))
597 (let* ((lexical-bindings (loop for x in normalized-bindings
598 unless (ps-special-variable-p (car x))
599 collect (cons (maybe-rename-lexical-var (car x)) x)))
600 (dynamic-bindings (loop for x in normalized-bindings
601 when (ps-special-variable-p (car x))
602 collect (cons (ps-gensym (format nil "~A_~A" (car x) 'tmp-stack)) x)))
603 (renamed-body `(symbol-macrolet ,(loop for x in lexical-bindings
604 when (rename x) collect
605 `(,(var x) ,(rename x)))
606 ,@body))
607 (*vars-bound-in-enclosing-lexical-scopes* (append lexical-bindings-introduced-here
608 *vars-bound-in-enclosing-lexical-scopes*)))
609 (compile-parenscript-form
610 `(progn
611 ,@(mapcar (lambda (x) `(var ,(or (rename x) (var x)) ,(val x))) lexical-bindings)
612 ,(if dynamic-bindings
613 `(progn ,@(mapcar (lambda (x) `(var ,(rename x))) dynamic-bindings)
614 (try (progn (setf ,@(loop for x in dynamic-bindings append
615 `(,(rename x) ,(var x)
616 ,(var x) ,(val x))))
617 ,renamed-body)
618 (:finally
619 (setf ,@(mapcan (lambda (x) `(,(var x) ,(rename x))) dynamic-bindings)))))
620 renamed-body))
621 :expecting expecting)))))
3530f5e1 622
5ffb1eba 623(defpsmacro let* (bindings &body body)
58c4ef4f 624 (if bindings
5ffb1eba
VS
625 `(let (,(car bindings))
626 (let* ,(cdr bindings)
627 ,@body))
58c4ef4f
VS
628 `(progn ,@body)))
629
18dd299a
VS
630;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631;;; iteration
6a2ce72d
TC
632(defun make-for-vars/inits (init-forms)
633 (mapcar (lambda (x)
5a69278c
VS
634 (cons (compile-parenscript-form (ps-macroexpand (if (atom x) x (first x))) :expecting :symbol)
635 (compile-parenscript-form (ps-macroexpand (if (atom x) nil (second x))) :expecting :expression)))
6a2ce72d 636 init-forms))
18dd299a 637
e8fdcce7 638(define-ps-special-form labeled-for (label init-forms cond-forms step-forms &rest body)
0ce67a33
VS
639 `(js:for ,label
640 ,(make-for-vars/inits init-forms)
5a69278c
VS
641 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x) :expecting :expression)) cond-forms)
642 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x) :expecting :expression)) step-forms)
0ce67a33 643 ,(compile-parenscript-form `(progn ,@body))))
6a2ce72d
TC
644
645(defpsmacro for (init-forms cond-forms step-forms &body body)
646 `(labeled-for nil ,init-forms ,cond-forms ,step-forms ,@body))
647
648(defun do-make-let-bindings (decls)
649 (mapcar (lambda (x)
650 (if (atom x) x
651 (if (endp (cdr x)) (list (car x))
652 (subseq x 0 2))))
653 decls))
654
655(defun do-make-init-vars (decls)
656 (mapcar (lambda (x) (if (atom x) x (first x))) decls))
657
658(defun do-make-init-vals (decls)
659 (mapcar (lambda (x) (if (or (atom x) (endp (cdr x))) nil (second x))) decls))
660
661(defun do-make-for-vars/init (decls)
662 (mapcar (lambda (x)
663 (if (atom x) x
664 (if (endp (cdr x)) x
665 (subseq x 0 2))))
666 decls))
667
668(defun do-make-for-steps (decls)
669 (mapcar (lambda (x)
670 `(setf ,(first x) ,(third x)))
671 (remove-if (lambda (x) (or (atom x) (< (length x) 3))) decls)))
672
673(defun do-make-iter-psteps (decls)
674 `(psetq
675 ,@(mapcan (lambda (x) (list (first x) (third x)))
676 (remove-if (lambda (x) (or (atom x) (< (length x) 3))) decls))))
677
678(defpsmacro do* (decls (termination &optional (result nil result?)) &body body)
679 (if result?
680 `((lambda ()
681 (for ,(do-make-for-vars/init decls) ((not ,termination)) ,(do-make-for-steps decls)
682 ,@body)
683 (return ,result)))
684 `(progn
685 (for ,(do-make-for-vars/init decls) ((not ,termination)) ,(do-make-for-steps decls)
686 ,@body))))
687
688(defpsmacro do (decls (termination &optional (result nil result?)) &body body)
689 (if result?
690 `((lambda ,(do-make-init-vars decls)
691 (for () ((not ,termination)) ()
692 ,@body
693 ,(do-make-iter-psteps decls))
694 (return ,result))
695 ,@(do-make-init-vals decls))
696 `(let ,(do-make-let-bindings decls)
697 (for () ((not ,termination)) ()
698 ,@body
699 ,(do-make-iter-psteps decls)))))
700
0ce67a33 701(define-ps-special-form for-in ((var object) &rest body)
5ffb1eba 702 `(js:for-in ,(compile-parenscript-form var :expecting :expression)
5a69278c 703 ,(compile-parenscript-form (ps-macroexpand object) :expecting :expression)
0ce67a33 704 ,(compile-parenscript-form `(progn ,@body))))
6a2ce72d 705
e8fdcce7 706(define-ps-special-form while (test &rest body)
0ce67a33
VS
707 `(js:while ,(compile-parenscript-form test :expecting :expression)
708 ,(compile-parenscript-form `(progn ,@body))))
18dd299a 709
6a2ce72d
TC
710(defpsmacro dotimes ((var count &optional (result nil result?)) &rest body)
711 `(do* ((,var 0 (1+ ,var)))
712 ((>= ,var ,count) ,@(when result? (list result)))
713 ,@body))
714
715(defpsmacro dolist ((var array &optional (result nil result?)) &body body)
716 (let ((idx (ps-gensym "_js_idx"))
717 (arrvar (ps-gensym "_js_arrvar")))
718 `(do* (,var
719 (,arrvar ,array)
720 (,idx 0 (1+ ,idx)))
721 ((>= ,idx (slot-value ,arrvar 'length))
722 ,@(when result? (list result)))
723 (setq ,var (aref ,arrvar ,idx))
724 ,@body)))
18dd299a
VS
725
726;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727;;; misc
e8fdcce7 728(define-ps-special-form with (expression &rest body)
0ce67a33
VS
729 `(js:with ,(compile-parenscript-form expression :expecting :expression)
730 ,(compile-parenscript-form `(progn ,@body))))
18dd299a 731
e8fdcce7 732(define-ps-special-form try (form &rest clauses)
18dd299a
VS
733 (let ((catch (cdr (assoc :catch clauses)))
734 (finally (cdr (assoc :finally clauses))))
735 (assert (not (cdar catch)) nil "Sorry, currently only simple catch forms are supported.")
736 (assert (or catch finally) ()
737 "Try form should have either a catch or a finally clause or both.")
0ce67a33
VS
738 `(js:try ,(compile-parenscript-form `(progn ,form))
739 :catch ,(when catch (list (compile-parenscript-form (caar catch) :expecting :symbol)
18dd299a 740 (compile-parenscript-form `(progn ,@(cdr catch)))))
0ce67a33 741 :finally ,(when finally (compile-parenscript-form `(progn ,@finally))))))
18dd299a 742
e8fdcce7 743(define-ps-special-form cc-if (test &rest body)
0ce67a33 744 `(js:cc-if ,test ,@(mapcar #'compile-parenscript-form body)))
18dd299a 745
e8fdcce7 746(define-ps-special-form regex (regex)
0ce67a33 747 `(js:regex ,(string regex)))
18dd299a 748
ceb1f277
VS
749(define-ps-special-form lisp (lisp-form)
750 ;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar))
751 ;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval)
5a69278c 752 `(js:escape (compiled-form-to-string (compile-parenscript-form ,lisp-form :expecting ,expecting))))
8877a380
VS
753
754;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
755;;; eval-when
756(define-ps-special-form eval-when (situation-list &body body)
757 "(eval-when (situation*) body-form*)
758
759The body forms are evaluated only during the given SITUATION. The accepted SITUATIONS are
760:load-toplevel, :compile-toplevel, and :execute. The code in BODY-FORM is assumed to be
761COMMON-LISP code in :compile-toplevel and :load-toplevel sitations, and parenscript code in
762:execute. "
763 (when (and (member :compile-toplevel situation-list)
5a69278c 764 (member *ps-compilation-level* '(:toplevel :inside-toplevel-form)))
8877a380
VS
765 (eval `(progn ,@body)))
766 (if (member :execute situation-list)
767 (compile-parenscript-form `(progn ,@body) :expecting expecting)
768 (compile-parenscript-form `(progn) :expecting expecting)))