1 (in-package "PARENSCRIPT")
3 (defmacro with-local-macro-environment
((var env
) &body body
)
4 `(let* ((,var
(make-macro-dictionary))
5 (,env
(cons ,var
,env
)))
8 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 (defmacro defpsliteral
(name string
)
12 (add-ps-reserved-symbol ',name
)
13 (define-ps-special-form ,name
()
14 (list 'js
:literal
,string
))))
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")
24 (macrolet ((def-for-literal (name printer
)
26 (add-ps-reserved-symbol ',name
)
27 (define-ps-special-form ,name
(&optional label
)
28 (list ',printer label
)))))
29 (def-for-literal break js
:break
)
30 (def-for-literal continue js
:continue
))
32 (define-ps-special-form quote
(x)
33 (compile-parenscript-form
35 (cons `(array ,@(mapcar (lambda (x) (when x
`',x
)) x
)))
38 (symbol (symbol-to-js-string x
))
41 :expecting expecting
))
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
)))
49 `(define-ps-special-form ,op
(x)
50 (list 'js
:unary-operator
',op
51 (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
)
52 :prefix t
:space
,spacep
))))
54 (def-unary-ops ~
! (new t
) (delete t
) (void t
) (typeof t
)))
56 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 (define-ps-special-form return
(&optional value
)
59 `(js:return
,(compile-parenscript-form (ps-macroexpand value
) :expecting
:expression
)))
61 (define-ps-special-form throw
(value)
62 `(js:throw
,(compile-parenscript-form (ps-macroexpand value
) :expecting
:expression
)))
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 (define-ps-special-form array
(&rest values
)
67 `(js:array
,@(mapcar (lambda (form) (compile-parenscript-form (ps-macroexpand form
) :expecting
:expression
))
70 (define-ps-special-form aref
(array &rest coords
)
71 `(js:aref
,(compile-parenscript-form (ps-macroexpand array
) :expecting
:expression
)
72 ,(mapcar (lambda (form)
73 (compile-parenscript-form (ps-macroexpand form
) :expecting
:expression
))
76 (defpsmacro list
(&rest values
)
79 (defpsmacro make-array
(&rest initial-values
)
80 `(new (*array
,@initial-values
)))
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 (define-ps-special-form incf
(x &optional
(delta 1))
85 (let ((x (ps-macroexpand x
))
86 (delta (ps-macroexpand delta
)))
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
)))))
92 (define-ps-special-form decf
(x &optional
(delta 1))
93 (let ((x (ps-macroexpand x
))
94 (delta (ps-macroexpand delta
)))
96 `(js:unary-operator js
:--
,(compile-parenscript-form x
:expecting
:expression
) :prefix t
)
97 `(js:operator js
:-
= ,(compile-parenscript-form x
:expecting
:expression
)
98 ,(compile-parenscript-form delta
:expecting
:expression
)))))
100 (define-ps-special-form -
(first &rest rest
)
101 (let ((first (ps-macroexpand first
))
102 (rest (mapcar #'ps-macroexpand rest
)))
104 `(js:operator js
:-
,@(mapcar (lambda (val) (compile-parenscript-form val
:expecting
:expression
))
106 `(js:unary-operator js
:-
,(compile-parenscript-form first
:expecting
:expression
) :prefix t
))))
108 (define-ps-special-form not
(x)
109 (let ((form (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
))
111 (if (and (eq (car form
) 'js
:operator
)
112 (= (length (cddr form
)) 2)
113 (setf inverse-op
(case (cadr form
)
122 `(js:operator
,inverse-op
,@(cddr form
))
123 `(js:unary-operator js
:! ,form
:prefix t
))))
125 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 ;;; control structures
127 (defun flatten-blocks (body)
129 (if (and (listp (car body
))
130 (eq 'js
:block
(caar body
)))
131 (append (cdr (car body
)) (flatten-blocks (cdr body
)))
132 (cons (car body
) (flatten-blocks (cdr body
))))))
134 (defun constant-literal-form-p (form)
138 (eq 'js
:literal
(car form
)))))
140 (define-ps-special-form progn
(&rest body
)
141 (let ((body (mapcar #'ps-macroexpand body
)))
142 (if (and (eq expecting
:expression
) (= 1 (length body
)))
143 (compile-parenscript-form (car body
) :expecting
:expression
)
144 `(,(if (eq expecting
:expression
) 'js
:|
,|
'js
:block
)
145 ,@(let* ((block (flatten-blocks (remove nil
(mapcar (lambda (form)
146 (compile-parenscript-form form
:expecting expecting
))
148 (append (remove-if #'constant-literal-form-p
(butlast block
)) (last block
)))))))
150 (define-ps-special-form cond
(&rest clauses
)
152 (:statement
`(js:if
,(compile-parenscript-form (caar clauses
) :expecting
:expression
)
153 ,(compile-parenscript-form `(progn ,@(cdar clauses
)))
154 ,@(loop for
(test . body
) in
(cdr clauses
) appending
156 `(:else
,(compile-parenscript-form `(progn ,@body
) :expecting
:statement
))
157 `(:else-if
,(compile-parenscript-form test
:expecting
:expression
)
158 ,(compile-parenscript-form `(progn ,@body
) :expecting
:statement
))))))
159 (:expression
(make-cond-clauses-into-nested-ifs clauses
))))
161 (defun make-cond-clauses-into-nested-ifs (clauses)
163 (destructuring-bind (test &rest body
)
166 (compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
167 `(js:?
,(compile-parenscript-form test
:expecting
:expression
)
168 ,(compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
169 ,(make-cond-clauses-into-nested-ifs (cdr clauses
)))))
170 (compile-parenscript-form nil
:expecting
:expression
))) ;; js:null
172 (define-ps-special-form if
(test then
&optional else
)
174 (:statement
`(js:if
,(compile-parenscript-form (ps-macroexpand test
) :expecting
:expression
)
175 ,(compile-parenscript-form `(progn ,then
))
176 ,@(when else
`(:else
,(compile-parenscript-form `(progn ,else
))))))
177 (:expression
`(js:?
,(compile-parenscript-form (ps-macroexpand test
) :expecting
:expression
)
178 ,(compile-parenscript-form (ps-macroexpand then
) :expecting
:expression
)
179 ,(compile-parenscript-form (ps-macroexpand else
) :expecting
:expression
)))))
181 (define-ps-special-form switch
(test-expr &rest clauses
)
182 `(js:switch
,(compile-parenscript-form test-expr
:expecting
:expression
)
183 ,(loop for
(val . body
) in clauses collect
184 (cons (if (eq val
'default
)
186 (compile-parenscript-form val
:expecting
:expression
))
187 (mapcar (lambda (x) (compile-parenscript-form x
:expecting
:statement
))
190 (defpsmacro case
(value &rest clauses
)
191 (labels ((make-clause (val body more
)
192 (cond ((and (listp val
) (not (eq (car val
) 'quote
)))
193 (append (mapcar #'list
(butlast val
))
194 (make-clause (first (last val
)) body more
)))
195 ((member val
'(t otherwise
))
196 (make-clause 'default body more
))
197 (more `((,val
,@body break
)))
198 (t `((,val
,@body
))))))
199 `(switch ,value
,@(mapcon (lambda (clause)
200 (make-clause (car (first clause
))
205 (defpsmacro when
(test &rest body
)
206 `(if ,test
(progn ,@body
)))
208 (defpsmacro unless
(test &rest body
)
209 `(if (not ,test
) (progn ,@body
)))
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
212 ;;; function definition
214 (defvar *vars-bound-in-enclosing-lexical-scopes
* ())
216 (defun compile-function-definition (args body
)
217 (let ((args (mapcar (lambda (arg) (compile-parenscript-form arg
:expecting
:symbol
)) args
)))
219 (let* ((*enclosing-lexical-block-declarations
* ())
220 (*vars-bound-in-enclosing-lexical-scopes
* (append args
221 *vars-bound-in-enclosing-lexical-scopes
*))
222 (body (compile-parenscript-form `(progn ,@body
)))
223 (var-decls (compile-parenscript-form
224 `(progn ,@(mapcar (lambda (var) `(var ,var
)) *enclosing-lexical-block-declarations
*)))))
225 `(js:block
,@(cdr var-decls
) ,@(cdr body
))))))
227 (define-ps-special-form %js-lambda
(args &rest body
)
228 `(js:lambda
,@(compile-function-definition args body
)))
230 (define-ps-special-form %js-defun
(name args
&rest body
)
231 `(js:defun
,name
,@(compile-function-definition args body
)))
233 (defun parse-function-body (body)
235 (when (stringp (first body
))
237 (body-forms (if docstring
(rest body
) body
)))
238 (values body-forms docstring
)))
240 (defun parse-key-spec (key-spec)
241 "parses an &key parameter. Returns 5 values:
242 var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
245 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
247 (let* ((var (cond ((symbolp key-spec
) key-spec
)
248 ((and (listp key-spec
) (symbolp (first key-spec
))) (first key-spec
))
249 ((and (listp key-spec
) (listp (first key-spec
))) (second (first key-spec
)))))
250 (keyword-name (if (and (listp key-spec
) (listp (first key-spec
)))
251 (first (first key-spec
))
252 (intern (string var
) :keyword
)))
253 (init-form (if (listp key-spec
) (second key-spec
) nil
))
254 (init-form-supplied-p (if (listp key-spec
) t nil
))
255 (supplied-p-var (if (listp key-spec
) (third key-spec
) nil
)))
256 (values var init-form keyword-name supplied-p-var init-form-supplied-p
)))
258 (defun parse-optional-spec (spec)
259 "Parses an &optional parameter. Returns 3 values: var, init-form, supplied-p-var.
260 [&optional {var | (var [init-form [supplied-p-parameter]])}*] "
261 (let* ((var (cond ((symbolp spec
) spec
)
262 ((and (listp spec
) (first spec
)))))
263 (init-form (if (listp spec
) (second spec
)))
264 (supplied-p-var (if (listp spec
) (third spec
))))
265 (values var init-form supplied-p-var
)))
267 (defun parse-aux-spec (spec)
268 "Returns two values: variable and init-form"
269 ;; [&aux {var | (var [init-form])}*])
270 (values (if (symbolp spec
) spec
(first spec
))
271 (when (listp spec
) (second spec
))))
273 (defpsmacro defaultf
(name value suppl
)
275 ,@(when suppl
`((var ,suppl t
)))
276 (when (=== ,name undefined
)
277 (setf ,name
,value
,@(when suppl
(list suppl nil
))))))
279 (defun parse-extended-function (lambda-list body
&optional name
)
280 "Returns two values: the effective arguments and body for a function with
281 the given lambda-list and body."
283 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
284 ;; list of variable names, and you have access to the arguments variable inside the function:
285 ;; * standard variables are the mapped directly into the js-lambda list
286 ;; * optional variables' variable names are mapped directly into the lambda list,
287 ;; and for each optional variable with name v, default value d, and
288 ;; supplied-p parameter s, a form is produced (defaultf v d s)
289 ;; * keyword variables are not included in the js-lambda list, but instead are
290 ;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
291 ;; keyword vars is prepended to the body of the function. Defaults and supplied-p
292 ;; are handled using the same mechanism as with optional vars.
293 (declare (ignore name
))
294 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
295 more? more-context more-count key-object
)
296 (parse-lambda-list lambda-list
)
297 (declare (ignore allow? aux? aux more? more-context more-count key-object
))
298 (let* (;; optionals are of form (var default-value)
303 (mapcar #'parse-optional-spec optionals
))))
305 (mapcar #'(lambda (opt-spec)
306 (multiple-value-bind (var val suppl
)
307 (parse-optional-spec opt-spec
)
308 `(defaultf ,var
,val
,suppl
)))
312 (if (< *js-target-version
* 1.6)
314 (let ((decls nil
) (assigns nil
) (defaults nil
))
316 (multiple-value-bind (var init-form keyword-str suppl
)
318 (push `(var ,var
) decls
)
319 (push `(,keyword-str
(setf ,var
(aref arguments
(1+ ,n
)))) assigns
)
320 (push (list 'defaultf var init-form suppl
) defaults
)))
323 (loop :for
,n
:from
,(length requireds
)
324 :below
(length arguments
) :by
2 :do
325 (case (aref arguments
,n
) ,@assigns
))
328 (multiple-value-bind (var init-form keyword-str
)
331 `(let ((,x
((@ *Array prototype index-of call
) arguments
,keyword-str
,(length requireds
))))
332 (var ,var
(if (= -
1 ,x
) ,init-form
(aref arguments
(1+ ,x
))))))))
337 `(progn (var ,rest
(array))
338 (dotimes (,i
(- (slot-value arguments
'length
) ,(length effective-args
)))
339 (setf (aref ,rest
,i
) (aref arguments
(+ ,i
,(length effective-args
)))))))
341 (body-paren-forms (parse-function-body body
)) ; remove documentation
342 (effective-body (append opt-forms key-forms
(list rest-form
) body-paren-forms
)))
343 (values effective-args effective-body
))))
345 (defpsmacro defun
(name lambda-list
&body body
)
346 "An extended defun macro that allows cool things like keyword arguments.
349 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
351 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
352 [&aux {var | (var [init-form])}*])"
354 `(defun-function ,name
,lambda-list
,@body
)
355 (progn (assert (and (listp name
) (= (length name
) 2) (eq 'setf
(car name
))) ()
356 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list
)
357 `(defun-setf ,name
,lambda-list
,@body
))))
359 (defpsmacro defun-function
(name lambda-list
&body body
)
360 (multiple-value-bind (effective-args effective-body
)
361 (parse-extended-function lambda-list body name
)
362 `(%js-defun
,name
,effective-args
365 (defpsmacro lambda
(lambda-list &body body
)
366 "An extended defun macro that allows cool things like keyword arguments.
369 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
371 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
372 [&aux {var | (var [init-form])}*])"
373 (multiple-value-bind (effective-args effective-body
)
374 (parse-extended-function lambda-list body
)
375 `(%js-lambda
,effective-args
378 (define-ps-special-form flet
(fn-defs &rest body
)
379 (let ((fn-renames (make-macro-dictionary)))
380 (loop for
(fn-name . def
) in fn-defs do
381 (setf (gethash fn-name fn-renames
) (ps-gensym fn-name
)))
382 (let ((fn-defs (compile-parenscript-form
383 `(progn ,@(loop for
(fn-name . def
) in fn-defs collect
384 `(var ,(gethash fn-name fn-renames
) (lambda ,@def
))))
385 :expecting expecting
))
386 (*ps-local-function-names
* (cons fn-renames
*ps-local-function-names
*)))
387 (append fn-defs
(cdr (compile-parenscript-form `(progn ,@body
) :expecting expecting
))))))
389 (define-ps-special-form labels
(fn-defs &rest body
)
390 (with-local-macro-environment (local-fn-renames *ps-local-function-names
*)
391 (loop for
(fn-name . def
) in fn-defs do
392 (setf (gethash fn-name local-fn-renames
) (ps-gensym fn-name
)))
393 (compile-parenscript-form
394 `(progn ,@(loop for
(fn-name . def
) in fn-defs collect
395 `(var ,(gethash fn-name local-fn-renames
) (lambda ,@def
)))
397 :expecting expecting
)))
399 (define-ps-special-form function
(fn-name)
400 (compile-parenscript-form (maybe-rename-local-function fn-name
) :expecting expecting
))
402 (defvar *defun-setf-name-prefix
* "__setf_")
404 (defpsmacro defun-setf
(setf-name lambda-list
&body body
)
405 (let ((mangled-function-name (intern (concatenate 'string
*defun-setf-name-prefix
* (symbol-name (second setf-name
)))
406 (symbol-package (second setf-name
))))
407 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords
))))
408 (ps* `(defsetf ,(second setf-name
) ,(cdr lambda-list
) (store-var)
409 `(,',mangled-function-name
,store-var
,@(list ,@function-args
))))
410 `(defun ,mangled-function-name
,lambda-list
,@body
)))
412 (defpsmacro defsetf-long
(access-fn lambda-list
(store-var) form
)
413 (setf (gethash access-fn
*ps-setf-expanders
*)
415 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords
)))
416 `(lambda (access-fn-args store-form
)
417 (destructuring-bind ,lambda-list
419 (let* ((,store-var
(ps-gensym))
420 (gensymed-names (loop repeat
,(length var-bindings
) collecting
(ps-gensym)))
421 (gensymed-arg-bindings (mapcar #'list gensymed-names
(list ,@var-bindings
))))
422 (destructuring-bind ,var-bindings
424 `(let* (,@gensymed-arg-bindings
425 (,,store-var
,store-form
))
429 (defpsmacro defsetf-short
(access-fn update-fn
&optional docstring
)
430 (declare (ignore docstring
))
431 (setf (gethash access-fn
*ps-setf-expanders
*)
432 (lambda (access-fn-args store-form
)
433 `(,update-fn
,@access-fn-args
,store-form
)))
436 (defpsmacro defsetf
(access-fn &rest args
)
437 `(,(if (= (length args
) 3) 'defsetf-long
'defsetf-short
) ,access-fn
,@args
))
439 (defpsmacro funcall
(&rest arg-form
)
442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444 (define-ps-special-form macrolet
(macros &body body
)
445 (with-local-macro-environment (local-macro-dict *ps-macro-env
*)
446 (dolist (macro macros
)
447 (destructuring-bind (name arglist
&body body
)
449 (setf (gethash name local-macro-dict
) (eval (make-ps-macro-function arglist body
)))))
450 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)))
452 (define-ps-special-form symbol-macrolet
(symbol-macros &body body
)
453 (with-local-macro-environment (local-macro-dict *ps-symbol-macro-env
*)
454 (let (local-var-bindings)
455 (dolist (macro symbol-macros
)
456 (destructuring-bind (name expansion
)
458 (setf (gethash name local-macro-dict
) (lambda (x) (declare (ignore x
)) expansion
))
459 (push name local-var-bindings
)))
460 (let ((*vars-bound-in-enclosing-lexical-scopes
* (append local-var-bindings
461 *vars-bound-in-enclosing-lexical-scopes
*)))
462 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)))))
464 (define-ps-special-form defmacro
(name args
&body body
) ;; should this be a macro?
465 (eval `(defpsmacro ,name
,args
,@body
))
468 (define-ps-special-form define-symbol-macro
(name expansion
) ;; should this be a macro?
469 (eval `(define-ps-symbol-macro ,name
,expansion
))
472 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 (add-ps-reserved-symbol '{})
475 (define-ps-symbol-macro {} (create))
477 (define-ps-special-form create
(&rest arrows
)
479 ,@(loop for
(key-expr val-expr
) on arrows by
#'cddr collecting
480 (let ((compiled-key (compile-parenscript-form (ps-macroexpand key-expr
)
481 :expecting
:expression
)))
482 (assert (or (stringp compiled-key
)
483 (numberp compiled-key
)
484 (keywordp compiled-key
)
485 (and (listp compiled-key
)
486 (eq 'js
:variable
(car compiled-key
))))
488 "Slot key ~s is not one of js-variable, keyword, string or number."
490 (let ((key (aif (ps-reserved-symbol-p (if (listp compiled-key
)
491 (second compiled-key
)
495 (cons key
(compile-parenscript-form (ps-macroexpand val-expr
)
496 :expecting
:expression
)))))))
498 (define-ps-special-form instanceof
(value type
)
499 `(js:instanceof
,(compile-parenscript-form value
:expecting
:expression
)
500 ,(compile-parenscript-form type
:expecting
:expression
)))
502 (define-ps-special-form %js-slot-value
(obj slot
)
503 (let ((slot (ps-macroexpand slot
)))
504 `(js:slot-value
,(compile-parenscript-form (ps-macroexpand obj
) :expecting
:expression
)
505 ,(let ((slot (if (and (listp slot
) (eq 'quote
(car slot
)))
506 (second slot
) ;; assume we're quoting a symbol
507 (compile-parenscript-form slot
))))
508 (if (and (symbolp slot
)
509 (ps-reserved-symbol-p slot
))
510 (symbol-name-to-js-string slot
)
513 (defpsmacro slot-value
(obj &rest slots
)
514 (if (null (rest slots
))
515 `(%js-slot-value
,obj
,(first slots
))
516 `(slot-value (slot-value ,obj
,(first slots
)) ,@(rest slots
))))
518 (defpsmacro with-slots
(slots object
&rest body
)
519 (flet ((slot-var (slot) (if (listp slot
) (first slot
) slot
))
520 (slot-symbol (slot) (if (listp slot
) (second slot
) slot
)))
521 `(symbol-macrolet ,(mapcar #'(lambda (slot)
522 `(,(slot-var slot
) (slot-value ,object
',(slot-symbol slot
))))
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
527 ;;; assignment and binding
528 (defun assignment-op (op)
544 (define-ps-special-form setf1%
(lhs rhs
)
545 (let ((lhs (compile-parenscript-form (ps-macroexpand lhs
) :expecting
:expression
))
546 (rhs (compile-parenscript-form (ps-macroexpand rhs
) :expecting
:expression
)))
548 (eq 'js
:operator
(car rhs
))
549 (member (cadr rhs
) '(+ *))
550 (equalp lhs
(caddr rhs
)))
551 `(js:operator
,(assignment-op (cadr rhs
)) ,lhs
(js:operator
,(cadr rhs
) ,@(cdddr rhs
)))
554 (defpsmacro setf
(&rest args
)
555 (assert (evenp (length args
)) ()
556 "~s does not have an even number of arguments." `(setf ,args
))
557 `(progn ,@(loop for
(place value
) on args by
#'cddr collect
558 (let ((place (ps-macroexpand place
)))
559 (aif (and (listp place
) (gethash (car place
) *ps-setf-expanders
*))
560 (funcall it
(cdr place
) value
)
561 `(setf1%
,place
,value
))))))
563 (defpsmacro psetf
(&rest args
)
564 (let ((places (loop for x in args by
#'cddr collect x
))
565 (vals (loop for x in
(cdr args
) by
#'cddr collect x
)))
566 (let ((gensyms (mapcar (lambda (x) (declare (ignore x
)) (ps-gensym)) places
)))
567 `(let ,(mapcar #'list gensyms vals
)
568 (setf ,@(mapcan #'list places gensyms
))))))
570 (defun check-setq-args (args)
571 (let ((vars (loop for x in args by
#'cddr collect x
)))
572 (let ((non-var (find-if (complement #'symbolp
) vars
)))
574 (error 'type-error
:datum non-var
:expected-type
'symbol
)))))
576 (defpsmacro setq
(&rest args
)
577 (check-setq-args args
)
580 (defpsmacro psetq
(&rest args
)
581 (check-setq-args args
)
584 (define-ps-special-form var
(name &optional
(value (values) value-provided?
) documentation
)
585 (declare (ignore documentation
))
586 (let ((name (ps-macroexpand name
)))
589 `(js:var
,name
,@(when value-provided?
590 (list (compile-parenscript-form (ps-macroexpand value
) :expecting
:expression
)))))
592 (push name
*enclosing-lexical-block-declarations
*)
593 (when value-provided?
594 (compile-parenscript-form `(setf ,name
,value
) :expecting
:expression
))))))
596 (defpsmacro defvar
(name &optional
(value (values) value-provided?
) documentation
)
597 ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
598 (declare (ignore documentation
))
599 (pushnew name
*ps-special-variables
*)
600 `(var ,name
,@(when value-provided?
(list value
))))
602 (define-ps-special-form let
(bindings &body body
)
603 (let* (lexical-bindings-introduced-here
604 (normalized-bindings (mapcar (lambda (x)
607 (list (car x
) (ps-macroexpand (cadr x
)))))
609 (free-variables-in-binding-value-expressions (mapcan (lambda (x) (flatten (cadr x
)))
610 normalized-bindings
)))
611 (flet ((maybe-rename-lexical-var (x)
612 (if (or (member x
*vars-bound-in-enclosing-lexical-scopes
*)
613 (member x free-variables-in-binding-value-expressions
))
615 (progn (push x lexical-bindings-introduced-here
) nil
)))
616 (rename (x) (first x
))
619 (let* ((lexical-bindings (loop for x in normalized-bindings
620 unless
(ps-special-variable-p (car x
))
621 collect
(cons (maybe-rename-lexical-var (car x
)) x
)))
622 (dynamic-bindings (loop for x in normalized-bindings
623 when
(ps-special-variable-p (car x
))
624 collect
(cons (ps-gensym (format nil
"~A_~A" (car x
) 'tmp-stack
)) x
)))
625 (renamed-body `(symbol-macrolet ,(loop for x in lexical-bindings
626 when
(rename x
) collect
627 `(,(var x
) ,(rename x
)))
629 (*vars-bound-in-enclosing-lexical-scopes
* (append lexical-bindings-introduced-here
630 *vars-bound-in-enclosing-lexical-scopes
*)))
631 (compile-parenscript-form
633 ,@(mapcar (lambda (x) `(var ,(or (rename x
) (var x
)) ,(val x
))) lexical-bindings
)
634 ,(if dynamic-bindings
635 `(progn ,@(mapcar (lambda (x) `(var ,(rename x
))) dynamic-bindings
)
636 (try (progn (setf ,@(loop for x in dynamic-bindings append
637 `(,(rename x
) ,(var x
)
641 (setf ,@(mapcan (lambda (x) `(,(var x
) ,(rename x
))) dynamic-bindings
)))))
643 :expecting expecting
)))))
645 (defpsmacro let
* (bindings &body body
)
647 `(let (,(car bindings
))
648 (let* ,(cdr bindings
)
652 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
654 (defun make-for-vars/inits
(init-forms)
656 (cons (compile-parenscript-form (ps-macroexpand (if (atom x
) x
(first x
))) :expecting
:symbol
)
657 (compile-parenscript-form (ps-macroexpand (if (atom x
) nil
(second x
))) :expecting
:expression
)))
660 (define-ps-special-form labeled-for
(label init-forms cond-forms step-forms
&rest body
)
662 ,(make-for-vars/inits init-forms
)
663 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
)) cond-forms
)
664 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
)) step-forms
)
665 ,(compile-parenscript-form `(progn ,@body
))))
667 (defpsmacro for
(init-forms cond-forms step-forms
&body body
)
668 `(labeled-for nil
,init-forms
,cond-forms
,step-forms
,@body
))
670 (defun do-make-let-bindings (decls)
673 (if (endp (cdr x
)) (list (car x
))
677 (defun do-make-init-vars (decls)
678 (mapcar (lambda (x) (if (atom x
) x
(first x
))) decls
))
680 (defun do-make-init-vals (decls)
681 (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) decls
))
683 (defun do-make-for-vars/init
(decls)
690 (defun do-make-for-steps (decls)
692 `(setf ,(first x
) ,(third x
)))
693 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
)))
695 (defun do-make-iter-psteps (decls)
697 ,@(mapcan (lambda (x) (list (first x
) (third x
)))
698 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
))))
700 (defpsmacro do
* (decls (termination &optional
(result nil result?
)) &body body
)
703 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
707 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
710 (defpsmacro do
(decls (termination &optional
(result nil result?
)) &body body
)
712 `((lambda ,(do-make-init-vars decls
)
713 (for () ((not ,termination
)) ()
715 ,(do-make-iter-psteps decls
))
717 ,@(do-make-init-vals decls
))
718 `(let ,(do-make-let-bindings decls
)
719 (for () ((not ,termination
)) ()
721 ,(do-make-iter-psteps decls
)))))
723 (define-ps-special-form for-in
((var object
) &rest body
)
724 `(js:for-in
,(compile-parenscript-form var
:expecting
:expression
)
725 ,(compile-parenscript-form (ps-macroexpand object
) :expecting
:expression
)
726 ,(compile-parenscript-form `(progn ,@body
))))
728 (define-ps-special-form while
(test &rest body
)
729 `(js:while
,(compile-parenscript-form test
:expecting
:expression
)
730 ,(compile-parenscript-form `(progn ,@body
))))
732 (defpsmacro dotimes
((var count
&optional
(result nil result?
)) &rest body
)
733 `(do* ((,var
0 (1+ ,var
)))
734 ((>= ,var
,count
) ,@(when result?
(list result
)))
737 (defpsmacro dolist
((var array
&optional
(result nil result?
)) &body body
)
738 (let ((idx (ps-gensym "_js_idx"))
739 (arrvar (ps-gensym "_js_arrvar")))
743 ((>= ,idx
(slot-value ,arrvar
'length
))
744 ,@(when result?
(list result
)))
745 (setq ,var
(aref ,arrvar
,idx
))
748 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
750 (define-ps-special-form with
(expression &rest body
)
751 `(js:with
,(compile-parenscript-form expression
:expecting
:expression
)
752 ,(compile-parenscript-form `(progn ,@body
))))
754 (define-ps-special-form try
(form &rest clauses
)
755 (let ((catch (cdr (assoc :catch clauses
)))
756 (finally (cdr (assoc :finally clauses
))))
757 (assert (not (cdar catch
)) nil
"Sorry, currently only simple catch forms are supported.")
758 (assert (or catch finally
) ()
759 "Try form should have either a catch or a finally clause or both.")
760 `(js:try
,(compile-parenscript-form `(progn ,form
))
761 :catch
,(when catch
(list (compile-parenscript-form (caar catch
) :expecting
:symbol
)
762 (compile-parenscript-form `(progn ,@(cdr catch
)))))
763 :finally
,(when finally
(compile-parenscript-form `(progn ,@finally
))))))
765 (define-ps-special-form cc-if
(test &rest body
)
766 `(js:cc-if
,test
,@(mapcar #'compile-parenscript-form body
)))
768 (define-ps-special-form regex
(regex)
769 `(js:regex
,(string regex
)))
771 (define-ps-special-form lisp
(lisp-form)
772 ;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar))
773 ;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval)
774 `(js:escape
(compiled-form-to-string (compile-parenscript-form ,lisp-form
:expecting
,expecting
))))
776 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
778 (define-ps-special-form eval-when
(situation-list &body body
)
779 "(eval-when (situation*) body-form*)
781 The body forms are evaluated only during the given SITUATION. The accepted SITUATIONS are
782 :load-toplevel, :compile-toplevel, and :execute. The code in BODY-FORM is assumed to be
783 COMMON-LISP code in :compile-toplevel and :load-toplevel sitations, and parenscript code in
785 (when (and (member :compile-toplevel situation-list
)
786 (member *ps-compilation-level
* '(:toplevel
:inside-toplevel-form
)))
787 (eval `(progn ,@body
)))
788 (if (member :execute situation-list
)
789 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)
790 (compile-parenscript-form `(progn) :expecting expecting
)))