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-literal ',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-literal ',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 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))
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
))))
98 (define-ps-special-form -
(first &rest rest
)
100 `(js:operator js
:-
,@(mapcar (lambda (val) (compile-parenscript-form val
:expecting
:expression
))
102 `(js:unary-operator js
:-
,(compile-parenscript-form first
:expecting
:expression
) :prefix t
)))
104 (define-ps-special-form not
(x)
105 (let ((form (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
))
107 (if (and (eq (car form
) 'js
:operator
)
108 (= (length (cddr form
)) 2)
109 (setf inverse-op
(case (cadr form
)
118 `(js:operator
,inverse-op
,@(cddr form
))
119 `(js:unary-operator js
:! ,form
:prefix t
))))
121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
122 ;;; control structures
123 (defun flatten-blocks (body)
125 (if (and (listp (car body
))
126 (eq 'js
:block
(caar body
)))
127 (append (cdr (car body
)) (flatten-blocks (cdr body
)))
128 (cons (car body
) (flatten-blocks (cdr body
))))))
130 (defun constant-literal-form-p (form)
134 (eq 'js
:literal
(car form
)))))
136 (define-ps-special-form progn
(&rest body
)
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
))
144 (append (remove-if #'constant-literal-form-p
(butlast block
)) (last block
)))))))
146 (define-ps-special-form cond
(&rest clauses
)
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
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
))))))
155 (:expression
(make-cond-clauses-into-nested-ifs clauses
))))
157 (defun make-cond-clauses-into-nested-ifs (clauses)
159 (destructuring-bind (test &rest body
)
162 (compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
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
)))))
166 (compile-parenscript-form nil
:expecting
:expression
))) ;; js:null
168 (define-ps-special-form if
(test then
&optional else
)
170 (:statement
`(js:if
,(compile-parenscript-form test
:expecting
:expression
)
171 ,(compile-parenscript-form `(progn ,then
))
172 ,@(when else
`(:else
,(compile-parenscript-form `(progn ,else
))))))
173 (:expression
`(js:?
,(compile-parenscript-form test
:expecting
:expression
)
174 ,(compile-parenscript-form then
:expecting
:expression
)
175 ,(compile-parenscript-form else
:expecting
:expression
)))))
177 (define-ps-special-form switch
(test-expr &rest clauses
)
178 `(js:switch
,(compile-parenscript-form test-expr
:expecting
:expression
)
179 ,(loop for
(val . body
) in clauses collect
180 (cons (if (eq val
'default
)
182 (compile-parenscript-form val
:expecting
:expression
))
183 (mapcar (lambda (x) (compile-parenscript-form x
:expecting
:statement
))
186 (defpsmacro case
(value &rest clauses
)
187 (labels ((make-clause (val body more
)
188 (cond ((and (listp val
) (not (eq (car val
) 'quote
)))
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
))
201 (defpsmacro when
(test &rest body
)
202 `(if ,test
(progn ,@body
)))
204 (defpsmacro unless
(test &rest body
)
205 `(if (not ,test
) (progn ,@body
)))
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 ;;; function definition
209 (defun compile-function-definition (args body
)
210 (list (mapcar (lambda (arg) (compile-parenscript-form arg
:expecting
:symbol
)) args
)
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
)))))
217 (define-ps-special-form %js-lambda
(args &rest body
)
218 `(js:lambda
,@(compile-function-definition args body
)))
220 (define-ps-special-form %js-defun
(name args
&rest body
)
221 `(js:defun
,name
,@(compile-function-definition args body
)))
223 (defun parse-function-body (body)
225 (when (stringp (first body
))
227 (body-forms (if docstring
(rest body
) body
)))
228 (values body-forms docstring
)))
230 (defun parse-key-spec (key-spec)
231 "parses an &key parameter. Returns 5 values:
232 var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
235 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
237 (let* ((var (cond ((symbolp key-spec
) key-spec
)
238 ((and (listp key-spec
) (symbolp (first key-spec
))) (first key-spec
))
239 ((and (listp key-spec
) (listp (first key-spec
))) (second (first key-spec
)))))
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
)))
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
)))
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
))))
263 (defpsmacro defaultf
(place value
)
264 `(when (=== ,place undefined
)
265 (setf ,place
,value
)))
267 (defun parse-extended-function (lambda-list body
&optional name
)
268 "Returns two values: the effective arguments and body for a function with
269 the given lambda-list and body."
271 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
272 ;; list of variable names, and you have access to the arguments variable inside the function:
273 ;; * standard variables are the mapped directly into the js-lambda list
274 ;; * optional variables' variable names are mapped directly into the lambda list,
275 ;; and for each optional variable with name v and default value d, a form is produced
277 ;; * keyword variables are not included in the js-lambda list, but instead are
278 ;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
279 ;; keyword vars is prepended to the body of the function.
280 (declare (ignore name
))
281 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
282 more? more-context more-count key-object
)
283 (parse-lambda-list lambda-list
)
284 (declare (ignore allow? aux? aux more? more-context more-count key-object
))
285 (let* (;; optionals are of form (var default-value)
290 (mapcar #'parse-optional-spec optionals
))))
292 (mapcar #'(lambda (opt-spec)
293 (multiple-value-bind (var val
) (parse-optional-spec opt-spec
)
294 `(defaultf ,var
,val
)))
298 (if (< *js-target-version
* 1.6)
300 (let ((decls nil
) (assigns nil
) (defaults nil
))
302 (multiple-value-bind (var init-form keyword-str
)
304 (push `(var ,var
) decls
)
305 (push `(,keyword-str
(setf ,var
(aref arguments
(1+ ,n
)))) assigns
)
306 (push (list 'defaultf var init-form
) defaults
)))
309 (loop :for
,n
:from
,(length requireds
)
310 :below
(length arguments
) :by
2 :do
311 (case (aref arguments
,n
) ,@assigns
))
314 (multiple-value-bind (var init-form keyword-str
)
317 `(let ((,x
((@ *Array prototype index-of call
) arguments
,keyword-str
,(length requireds
))))
318 (var ,var
(if (= -
1 ,x
) ,init-form
(aref arguments
(1+ ,x
))))))))
323 `(progn (var ,rest
(array))
324 (dotimes (,i
(- (slot-value arguments
'length
) ,(length effective-args
)))
325 (setf (aref ,rest
,i
) (aref arguments
(+ ,i
,(length effective-args
)))))))
327 (body-paren-forms (parse-function-body body
)) ; remove documentation
328 (effective-body (append opt-forms key-forms
(list rest-form
) body-paren-forms
)))
329 (values effective-args effective-body
))))
331 (defpsmacro defun
(name lambda-list
&body body
)
332 "An extended defun macro that allows cool things like keyword arguments.
335 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
337 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
338 [&aux {var | (var [init-form])}*])"
340 `(defun-function ,name
,lambda-list
,@body
)
341 (progn (assert (and (listp name
) (= (length name
) 2) (eq 'setf
(car name
))) ()
342 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list
)
343 `(defun-setf ,name
,lambda-list
,@body
))))
345 (defpsmacro defun-function
(name lambda-list
&body body
)
346 (multiple-value-bind (effective-args effective-body
)
347 (parse-extended-function lambda-list body name
)
348 `(%js-defun
,name
,effective-args
351 (defpsmacro lambda
(lambda-list &body body
)
352 "An extended defun macro that allows cool things like keyword arguments.
355 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
357 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
358 [&aux {var | (var [init-form])}*])"
359 (multiple-value-bind (effective-args effective-body
)
360 (parse-extended-function lambda-list body
)
361 `(%js-lambda
,effective-args
364 (define-ps-special-form flet
(fn-defs &rest body
)
365 (let ((fn-renames (make-macro-dictionary)))
366 (loop for
(fn-name . def
) in fn-defs do
367 (setf (gethash fn-name fn-renames
) (ps-gensym fn-name
)))
368 (let ((fn-defs (compile-parenscript-form
369 `(progn ,@(loop for
(fn-name . def
) in fn-defs collect
370 `(var ,(gethash fn-name fn-renames
) (lambda ,@def
))))
371 :expecting expecting
))
372 (*ps-local-function-names
* (cons fn-renames
*ps-local-function-names
*)))
373 (append fn-defs
(cdr (compile-parenscript-form `(progn ,@body
) :expecting expecting
))))))
375 (define-ps-special-form labels
(fn-defs &rest body
)
376 (with-local-macro-environment (local-fn-renames *ps-local-function-names
*)
377 (loop for
(fn-name . def
) in fn-defs do
378 (setf (gethash fn-name local-fn-renames
) (ps-gensym fn-name
)))
379 (compile-parenscript-form
380 `(progn ,@(loop for
(fn-name . def
) in fn-defs collect
381 `(var ,(gethash fn-name local-fn-renames
) (lambda ,@def
)))
383 :expecting expecting
)))
385 (defvar *defun-setf-name-prefix
* "__setf_")
387 (defpsmacro defun-setf
(setf-name lambda-list
&body body
)
388 (let ((mangled-function-name (intern (concatenate 'string
*defun-setf-name-prefix
* (symbol-name (second setf-name
)))
389 (symbol-package (second setf-name
))))
390 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords
))))
391 (ps* `(defsetf ,(second setf-name
) ,(cdr lambda-list
) (store-var)
392 `(,',mangled-function-name
,store-var
,@(list ,@function-args
))))
393 `(defun ,mangled-function-name
,lambda-list
,@body
)))
395 (defpsmacro defsetf-long
(access-fn lambda-list
(store-var) form
)
396 (setf (gethash access-fn
*ps-setf-expanders
*)
398 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords
)))
399 `(lambda (access-fn-args store-form
)
400 (destructuring-bind ,lambda-list
402 (let* ((,store-var
(ps-gensym))
403 (gensymed-names (loop repeat
,(length var-bindings
) collecting
(ps-gensym)))
404 (gensymed-arg-bindings (mapcar #'list gensymed-names
(list ,@var-bindings
))))
405 (destructuring-bind ,var-bindings
407 `(let* (,@gensymed-arg-bindings
408 (,,store-var
,store-form
))
412 (defpsmacro defsetf-short
(access-fn update-fn
&optional docstring
)
413 (declare (ignore docstring
))
414 (setf (gethash access-fn
*ps-setf-expanders
*)
415 (lambda (access-fn-args store-form
)
416 `(,update-fn
,@access-fn-args
,store-form
)))
419 (defpsmacro defsetf
(access-fn &rest args
)
420 `(,(if (= (length args
) 3) 'defsetf-long
'defsetf-short
) ,access-fn
,@args
))
422 (defpsmacro funcall
(&rest arg-form
)
425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
427 (define-ps-special-form macrolet
(macros &body body
)
428 (with-local-macro-environment (local-macro-dict *ps-macro-env
*)
429 (dolist (macro macros
)
430 (destructuring-bind (name arglist
&body body
)
432 (setf (gethash name local-macro-dict
) (eval (make-ps-macro-function arglist body
)))))
433 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)))
435 (define-ps-special-form symbol-macrolet
(symbol-macros &body body
)
436 (with-local-macro-environment (local-macro-dict *ps-symbol-macro-env
*)
437 (dolist (macro symbol-macros
)
438 (destructuring-bind (name expansion
)
440 (setf (gethash name local-macro-dict
) (lambda (x) (declare (ignore x
)) expansion
))))
441 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)))
443 (define-ps-special-form defmacro
(name args
&body body
) ;; should this be a macro?
444 (eval `(defpsmacro ,name
,args
,@body
))
447 (define-ps-special-form define-symbol-macro
(name expansion
) ;; should this be a macro?
448 (eval `(define-ps-symbol-macro ,name
,expansion
))
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454 (define-ps-symbol-macro {} (create))
456 (define-ps-special-form create
(&rest arrows
)
457 `(js:object
,@(loop for
(key-expr val-expr
) on arrows by
#'cddr collecting
458 (let ((key (compile-parenscript-form key-expr
:expecting
:expression
)))
460 (setf key
`(js:variable
,key
)))
461 (assert (or (stringp key
)
464 (or (eq 'js
:variable
(car key
))
465 (eq 'quote
(car key
)))))
467 "Slot key ~s is not one of js-variable, keyword, string or number." key
)
468 (cons key
(compile-parenscript-form val-expr
:expecting
:expression
))))))
470 (define-ps-special-form %js-slot-value
(obj slot
)
471 (let ((slot (ps-macroexpand slot
)))
472 `(js:slot-value
,(compile-parenscript-form (ps-macroexpand obj
) :expecting
:expression
)
473 ,(if (and (listp slot
) (eq 'quote
(car slot
)))
474 (second slot
) ;; assume we're quoting a symbol
475 (compile-parenscript-form slot
)))))
477 (define-ps-special-form instanceof
(value type
)
478 `(js:instanceof
,(compile-parenscript-form value
:expecting
:expression
)
479 ,(compile-parenscript-form type
:expecting
:expression
)))
481 (defpsmacro slot-value
(obj &rest slots
)
482 (if (null (rest slots
))
483 `(%js-slot-value
,obj
,(first slots
))
484 `(slot-value (slot-value ,obj
,(first slots
)) ,@(rest slots
))))
486 (defpsmacro with-slots
(slots object
&rest body
)
487 (flet ((slot-var (slot) (if (listp slot
) (first slot
) slot
))
488 (slot-symbol (slot) (if (listp slot
) (second slot
) slot
)))
489 `(symbol-macrolet ,(mapcar #'(lambda (slot)
490 `(,(slot-var slot
) (slot-value ,object
',(slot-symbol slot
))))
494 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
495 ;;; assignment and binding
496 (defun assignment-op (op)
512 (define-ps-special-form setf1%
(lhs rhs
)
513 (let ((lhs (compile-parenscript-form (ps-macroexpand lhs
) :expecting
:expression
))
514 (rhs (compile-parenscript-form (ps-macroexpand rhs
) :expecting
:expression
)))
516 (eq 'js
:operator
(car rhs
))
517 (member (cadr rhs
) '(+ *))
518 (equalp lhs
(caddr rhs
)))
519 `(js:operator
,(assignment-op (cadr rhs
)) ,lhs
(js:operator
,(cadr rhs
) ,@(cdddr rhs
)))
522 (defpsmacro setf
(&rest args
)
523 (assert (evenp (length args
)) ()
524 "~s does not have an even number of arguments." `(setf ,args
))
525 `(progn ,@(loop for
(place value
) on args by
#'cddr collect
526 (let ((place (ps-macroexpand place
)))
527 (aif (and (listp place
) (gethash (car place
) *ps-setf-expanders
*))
528 (funcall it
(cdr place
) value
)
529 `(setf1%
,place
,value
))))))
531 (defpsmacro psetf
(&rest args
)
532 (let ((places (loop for x in args by
#'cddr collect x
))
533 (vals (loop for x in
(cdr args
) by
#'cddr collect x
)))
534 (let ((gensyms (mapcar (lambda (x) (declare (ignore x
)) (ps-gensym)) places
)))
535 `(let ,(mapcar #'list gensyms vals
)
536 (setf ,@(mapcan #'list places gensyms
))))))
538 (defun check-setq-args (args)
539 (let ((vars (loop for x in args by
#'cddr collect x
)))
540 (let ((non-var (find-if (complement #'symbolp
) vars
)))
542 (error 'type-error
:datum non-var
:expected-type
'symbol
)))))
544 (defpsmacro setq
(&rest args
)
545 (check-setq-args args
)
548 (defpsmacro psetq
(&rest args
)
549 (check-setq-args args
)
552 (define-ps-special-form var
(name &optional
(value (values) value-provided?
) documentation
)
553 (declare (ignore documentation
))
554 (let ((name (ps-macroexpand name
)))
557 `(js:var
,name
,@(when value-provided?
558 (list (compile-parenscript-form (ps-macroexpand value
) :expecting
:expression
)))))
560 (push name
*enclosing-lexical-block-declarations
*)
561 (when value-provided?
562 (compile-parenscript-form `(setf ,name
,value
) :expecting
:expression
))))))
564 (defpsmacro defvar
(name &optional
(value (values) value-provided?
) documentation
)
565 ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
566 (declare (ignore documentation
))
567 (pushnew name
*ps-special-variables
*)
568 `(var ,name
,@(when value-provided?
(list value
))))
570 (defpsmacro let
(bindings &body body
)
571 (flet ((add-renamed-vars (bindings predicate
)
572 (mapcar (lambda (x) (append x
(list (ps-gensym (car x
)))))
573 (remove-if predicate bindings
:key
#'car
)))
576 (renamed (x) (third x
)))
577 (let* ((normalized-bindings (mapcar (lambda (x) (if (symbolp x
) `(,x nil
) x
)) bindings
))
578 (lexical-bindings (add-renamed-vars normalized-bindings
#'ps-special-variable-p
))
579 (dynamic-bindings (add-renamed-vars normalized-bindings
(complement #'ps-special-variable-p
)))
580 (renamed-body `(symbol-macrolet ,(mapcar (lambda (x) (list (var x
) (renamed x
)))
584 ,@(mapcar (lambda (x) `(var ,(renamed x
) ,(val x
))) lexical-bindings
)
585 ,(if dynamic-bindings
586 `(progn ,@(mapcar (lambda (x) `(var ,(renamed x
))) dynamic-bindings
)
587 (try (progn (setf ,@(loop for x in dynamic-bindings append
588 `(,(renamed x
) ,(var x
)
592 (setf ,@(mapcan (lambda (x) `(,(var x
) ,(renamed x
))) dynamic-bindings
)))))
595 (defpsmacro let
* (bindings &body body
)
597 `(let (,(car bindings
))
598 (let* ,(cdr bindings
)
602 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
604 (defun make-for-vars/inits
(init-forms)
606 (cons (compile-parenscript-form (ps-macroexpand (if (atom x
) x
(first x
))) :expecting
:symbol
)
607 (compile-parenscript-form (ps-macroexpand (if (atom x
) nil
(second x
))) :expecting
:expression
)))
610 (define-ps-special-form labeled-for
(label init-forms cond-forms step-forms
&rest body
)
612 ,(make-for-vars/inits init-forms
)
613 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
)) cond-forms
)
614 ,(mapcar (lambda (x) (compile-parenscript-form (ps-macroexpand x
) :expecting
:expression
)) step-forms
)
615 ,(compile-parenscript-form `(progn ,@body
))))
617 (defpsmacro for
(init-forms cond-forms step-forms
&body body
)
618 `(labeled-for nil
,init-forms
,cond-forms
,step-forms
,@body
))
620 (defun do-make-let-bindings (decls)
623 (if (endp (cdr x
)) (list (car x
))
627 (defun do-make-init-vars (decls)
628 (mapcar (lambda (x) (if (atom x
) x
(first x
))) decls
))
630 (defun do-make-init-vals (decls)
631 (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) decls
))
633 (defun do-make-for-vars/init
(decls)
640 (defun do-make-for-steps (decls)
642 `(setf ,(first x
) ,(third x
)))
643 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
)))
645 (defun do-make-iter-psteps (decls)
647 ,@(mapcan (lambda (x) (list (first x
) (third x
)))
648 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
))))
650 (defpsmacro do
* (decls (termination &optional
(result nil result?
)) &body body
)
653 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
657 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
660 (defpsmacro do
(decls (termination &optional
(result nil result?
)) &body body
)
662 `((lambda ,(do-make-init-vars decls
)
663 (for () ((not ,termination
)) ()
665 ,(do-make-iter-psteps decls
))
667 ,@(do-make-init-vals decls
))
668 `(let ,(do-make-let-bindings decls
)
669 (for () ((not ,termination
)) ()
671 ,(do-make-iter-psteps decls
)))))
673 (define-ps-special-form for-in
((var object
) &rest body
)
674 `(js:for-in
,(compile-parenscript-form var
:expecting
:expression
)
675 ,(compile-parenscript-form (ps-macroexpand object
) :expecting
:expression
)
676 ,(compile-parenscript-form `(progn ,@body
))))
678 (define-ps-special-form while
(test &rest body
)
679 `(js:while
,(compile-parenscript-form test
:expecting
:expression
)
680 ,(compile-parenscript-form `(progn ,@body
))))
682 (defpsmacro dotimes
((var count
&optional
(result nil result?
)) &rest body
)
683 `(do* ((,var
0 (1+ ,var
)))
684 ((>= ,var
,count
) ,@(when result?
(list result
)))
687 (defpsmacro dolist
((var array
&optional
(result nil result?
)) &body body
)
688 (let ((idx (ps-gensym "_js_idx"))
689 (arrvar (ps-gensym "_js_arrvar")))
693 ((>= ,idx
(slot-value ,arrvar
'length
))
694 ,@(when result?
(list result
)))
695 (setq ,var
(aref ,arrvar
,idx
))
698 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
700 (define-ps-special-form with
(expression &rest body
)
701 `(js:with
,(compile-parenscript-form expression
:expecting
:expression
)
702 ,(compile-parenscript-form `(progn ,@body
))))
704 (define-ps-special-form try
(form &rest clauses
)
705 (let ((catch (cdr (assoc :catch clauses
)))
706 (finally (cdr (assoc :finally clauses
))))
707 (assert (not (cdar catch
)) nil
"Sorry, currently only simple catch forms are supported.")
708 (assert (or catch finally
) ()
709 "Try form should have either a catch or a finally clause or both.")
710 `(js:try
,(compile-parenscript-form `(progn ,form
))
711 :catch
,(when catch
(list (compile-parenscript-form (caar catch
) :expecting
:symbol
)
712 (compile-parenscript-form `(progn ,@(cdr catch
)))))
713 :finally
,(when finally
(compile-parenscript-form `(progn ,@finally
))))))
715 (define-ps-special-form cc-if
(test &rest body
)
716 `(js:cc-if
,test
,@(mapcar #'compile-parenscript-form body
)))
718 (define-ps-special-form regex
(regex)
719 `(js:regex
,(string regex
)))
721 (define-ps-special-form lisp
(lisp-form)
722 ;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar))
723 ;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval)
724 `(js:escape
(compiled-form-to-string (compile-parenscript-form ,lisp-form
:expecting
,expecting
))))
726 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
728 (define-ps-special-form eval-when
(situation-list &body body
)
729 "(eval-when (situation*) body-form*)
731 The body forms are evaluated only during the given SITUATION. The accepted SITUATIONS are
732 :load-toplevel, :compile-toplevel, and :execute. The code in BODY-FORM is assumed to be
733 COMMON-LISP code in :compile-toplevel and :load-toplevel sitations, and parenscript code in
735 (when (and (member :compile-toplevel situation-list
)
736 (member *ps-compilation-level
* '(:toplevel
:inside-toplevel-form
)))
737 (eval `(progn ,@body
)))
738 (if (member :execute situation-list
)
739 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)
740 (compile-parenscript-form `(progn) :expecting expecting
)))