1 (in-package "PARENSCRIPT")
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 (defmacro defpsliteral
(name string
)
7 (add-ps-literal ',name
)
8 (define-ps-special-form ,name
()
9 (list 'js
:literal
,string
))))
11 (defpsliteral this
"this")
12 (defpsliteral t
"true")
13 (defpsliteral true
"true")
14 (defpsliteral false
"false")
15 (defpsliteral f
"false")
16 (defpsliteral nil
"null")
17 (defpsliteral undefined
"undefined")
19 (macrolet ((def-for-literal (name printer
)
21 (add-ps-literal ',name
)
22 (define-ps-special-form ,name
(&optional label
)
23 (list ',printer label
)))))
24 (def-for-literal break js
:break
)
25 (def-for-literal continue js
:continue
))
29 (cons (cons 'array
(mapcar (lambda (x) (when x
`',x
)) x
)))
32 (symbol (symbol-to-js-string x
))
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (macrolet ((def-unary-ops (&rest ops
)
39 `(progn ,@(mapcar (lambda (op)
40 (let ((op (if (listp op
) (car op
) op
))
41 (spacep (if (listp op
) (second op
) nil
)))
42 `(define-ps-special-form ,op
(x)
43 (list 'js
:unary-operator
',op
44 (compile-parenscript-form x
:expecting
:expression
)
45 :prefix t
:space
,spacep
))))
47 (def-unary-ops ~
! (new t
) (delete t
) (void t
) (typeof t
)))
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (define-ps-special-form return
(&optional value
)
52 `(js:return
,(compile-parenscript-form value
:expecting
:expression
)))
54 (define-ps-special-form throw
(value)
55 `(js:throw
,(compile-parenscript-form value
:expecting
:expression
)))
57 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 (define-ps-special-form array
(&rest values
)
60 `(js:array
,@(mapcar (lambda (form) (compile-parenscript-form form
:expecting
:expression
))
63 (define-ps-special-form aref
(array &rest coords
)
64 `(js:aref
,(compile-parenscript-form array
:expecting
:expression
)
65 ,(mapcar (lambda (form)
66 (compile-parenscript-form form
:expecting
:expression
))
69 (defpsmacro list
(&rest values
)
72 (defpsmacro make-array
(&rest initial-values
)
73 `(new (*array
,@initial-values
)))
75 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77 (define-ps-special-form incf
(x &optional
(delta 1))
79 `(js:unary-operator js
:++ ,(compile-parenscript-form x
:expecting
:expression
) :prefix t
)
80 `(js:operator js
:+= ,(compile-parenscript-form x
:expecting
:expression
)
81 ,(compile-parenscript-form delta
:expecting
:expression
))))
83 (define-ps-special-form decf
(x &optional
(delta 1))
85 `(js:unary-operator js
:--
,(compile-parenscript-form x
:expecting
:expression
) :prefix t
)
86 `(js:operator js
:-
= ,(compile-parenscript-form x
:expecting
:expression
)
87 ,(compile-parenscript-form delta
:expecting
:expression
))))
89 (define-ps-special-form -
(first &rest rest
)
91 `(js:operator js
:-
,@(mapcar (lambda (val) (compile-parenscript-form val
:expecting
:expression
))
93 `(js:unary-operator js
:-
,(compile-parenscript-form first
:expecting
:expression
) :prefix t
)))
95 (define-ps-special-form not
(x)
96 (let ((form (compile-parenscript-form x
:expecting
:expression
))
98 (if (and (eq (car form
) 'js
:operator
)
99 (= (length (cddr form
)) 2)
100 (setf inverse-op
(case (cadr form
)
109 `(js:operator
,inverse-op
,@(cddr form
))
110 `(js:unary-operator js
:! ,form
:prefix t
))))
112 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
113 ;;; control structures
114 (defun flatten-blocks (body)
116 (if (and (listp (car body
))
117 (eq 'js
:block
(caar body
)))
118 (append (cdr (car body
)) (flatten-blocks (cdr body
)))
119 (cons (car body
) (flatten-blocks (cdr body
))))))
121 (defun constant-literal-form-p (form)
125 (eq 'js
:literal
(car form
)))))
127 (define-ps-special-form progn
(&rest body
)
128 (if (and (eq expecting
:expression
) (= 1 (length body
)))
129 (compile-parenscript-form (car body
) :expecting
:expression
)
130 `(,(if (eq expecting
:expression
) 'js
:|
,|
'js
:block
)
131 ,@(let* ((block (flatten-blocks (remove nil
(mapcar (lambda (form)
132 (compile-parenscript-form form
:expecting expecting
))
134 (append (remove-if #'constant-literal-form-p
(butlast block
)) (last block
))))))
136 (define-ps-special-form cond
(&rest clauses
)
138 (:statement
`(js:if
,(compile-parenscript-form (caar clauses
) :expecting
:expression
)
139 ,(compile-parenscript-form `(progn ,@(cdar clauses
)))
140 ,@(loop for
(test . body
) in
(cdr clauses
) appending
142 `(:else
,(compile-parenscript-form `(progn ,@body
) :expecting
:statement
))
143 `(:else-if
,(compile-parenscript-form test
:expecting
:expression
)
144 ,(compile-parenscript-form `(progn ,@body
) :expecting
:statement
))))))
145 (:expression
(make-cond-clauses-into-nested-ifs clauses
))))
147 (defun make-cond-clauses-into-nested-ifs (clauses)
149 (destructuring-bind (test &rest body
)
152 (compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
153 `(js:?
,(compile-parenscript-form test
:expecting
:expression
)
154 ,(compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
155 ,(make-cond-clauses-into-nested-ifs (cdr clauses
)))))
156 (compile-parenscript-form nil
:expecting
:expression
))) ;; js:null
158 (define-ps-special-form if
(test then
&optional else
)
160 (:statement
`(js:if
,(compile-parenscript-form test
:expecting
:expression
)
161 ,(compile-parenscript-form `(progn ,then
))
162 ,@(when else
`(:else
,(compile-parenscript-form `(progn ,else
))))))
163 (:expression
`(js:?
,(compile-parenscript-form test
:expecting
:expression
)
164 ,(compile-parenscript-form then
:expecting
:expression
)
165 ,(compile-parenscript-form else
:expecting
:expression
)))))
167 (define-ps-special-form switch
(test-expr &rest clauses
)
168 `(js:switch
,(compile-parenscript-form test-expr
:expecting
:expression
)
169 ,(loop for
(val . body
) in clauses collect
170 (cons (if (eq val
'default
)
172 (compile-parenscript-form val
:expecting
:expression
))
173 (mapcar (lambda (x) (compile-parenscript-form x
:expecting
:statement
))
176 (defpsmacro case
(value &rest clauses
)
177 (labels ((make-clause (val body more
)
178 (cond ((and (listp val
) (not (eq (car val
) 'quote
)))
179 (append (mapcar #'list
(butlast val
))
180 (make-clause (first (last val
)) body more
)))
181 ((member val
'(t otherwise
))
182 (make-clause 'default body more
))
183 (more `((,val
,@body break
)))
184 (t `((,val
,@body
))))))
185 `(switch ,value
,@(mapcon (lambda (clause)
186 (make-clause (car (first clause
))
191 (defpsmacro when
(test &rest body
)
192 `(if ,test
(progn ,@body
)))
194 (defpsmacro unless
(test &rest body
)
195 `(if (not ,test
) (progn ,@body
)))
197 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
198 ;;; function definition
199 (defun compile-function-definition (args body
)
200 (list (mapcar (lambda (arg) (compile-parenscript-form arg
:expecting
:symbol
)) args
)
201 (let* ((*enclosing-lexical-block-declarations
* ())
202 (body (compile-parenscript-form `(progn ,@body
)))
203 (var-decls (compile-parenscript-form
204 `(progn ,@(mapcar (lambda (var) `(var ,var
)) *enclosing-lexical-block-declarations
*)))))
205 `(js:block
,@(cdr var-decls
) ,@(cdr body
)))))
207 (define-ps-special-form %js-lambda
(args &rest body
)
208 `(js:lambda
,@(compile-function-definition args body
)))
210 (define-ps-special-form %js-defun
(name args
&rest body
)
211 `(js:defun
,name
,@(compile-function-definition args body
)))
213 (defun parse-function-body (body)
215 (when (stringp (first body
))
217 (body-forms (if docstring
(rest body
) body
)))
218 (values body-forms docstring
)))
220 (defun parse-key-spec (key-spec)
221 "parses an &key parameter. Returns 5 values:
222 var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
225 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
227 (let* ((var (cond ((symbolp key-spec
) key-spec
)
228 ((and (listp key-spec
) (symbolp (first key-spec
))) (first key-spec
))
229 ((and (listp key-spec
) (listp (first key-spec
))) (second (first key-spec
)))))
230 (keyword-name (if (and (listp key-spec
) (listp (first key-spec
)))
231 (first (first key-spec
))
232 (intern (string var
) :keyword
)))
233 (init-form (if (listp key-spec
) (second key-spec
) nil
))
234 (init-form-supplied-p (if (listp key-spec
) t nil
))
235 (supplied-p-var (if (listp key-spec
) (third key-spec
) nil
)))
236 (values var init-form keyword-name supplied-p-var init-form-supplied-p
)))
238 (defun parse-optional-spec (spec)
239 "Parses an &optional parameter. Returns 3 values: var, init-form, supplied-p-var.
240 [&optional {var | (var [init-form [supplied-p-parameter]])}*] "
241 (let* ((var (cond ((symbolp spec
) spec
)
242 ((and (listp spec
) (first spec
)))))
243 (init-form (if (listp spec
) (second spec
)))
244 (supplied-p-var (if (listp spec
) (third spec
))))
245 (values var init-form supplied-p-var
)))
247 (defun parse-aux-spec (spec)
248 "Returns two values: variable and init-form"
249 ;; [&aux {var | (var [init-form])}*])
250 (values (if (symbolp spec
) spec
(first spec
))
251 (when (listp spec
) (second spec
))))
253 (defpsmacro defaultf
(place value
)
254 `(when (=== ,place undefined
)
255 (setf ,place
,value
)))
257 (defun parse-extended-function (lambda-list body
&optional name
)
258 "Returns two values: the effective arguments and body for a function with
259 the given lambda-list and body."
261 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
262 ;; list of variable names, and you have access to the arguments variable inside the function:
263 ;; * standard variables are the mapped directly into the js-lambda list
264 ;; * optional variables' variable names are mapped directly into the lambda list,
265 ;; and for each optional variable with name v and default value d, a form is produced
267 ;; * keyword variables are not included in the js-lambda list, but instead are
268 ;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
269 ;; keyword vars is prepended to the body of the function.
270 (declare (ignore name
))
271 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
272 more? more-context more-count key-object
)
273 (parse-lambda-list lambda-list
)
274 (declare (ignore allow? aux? aux more? more-context more-count key-object
))
275 (let* (;; optionals are of form (var default-value)
280 (mapcar #'parse-optional-spec optionals
))))
282 (mapcar #'(lambda (opt-spec)
283 (multiple-value-bind (var val
) (parse-optional-spec opt-spec
)
284 `(defaultf ,var
,val
)))
288 (if (< *js-target-version
* 1.6)
290 (let ((decls nil
) (assigns nil
) (defaults nil
))
292 (multiple-value-bind (var init-form keyword-str
)
294 (push `(var ,var
) decls
)
295 (push `(,keyword-str
(setf ,var
(aref arguments
(1+ ,n
)))) assigns
)
296 (push (list 'defaultf var init-form
) defaults
)))
299 (loop :for
,n
:from
,(length requireds
)
300 :below
(length arguments
) :by
2 :do
301 (case (aref arguments
,n
) ,@assigns
))
304 (multiple-value-bind (var init-form keyword-str
)
307 `(let ((,x
((@ *Array prototype index-of call
) arguments
,keyword-str
,(length requireds
))))
308 (var ,var
(if (= -
1 ,x
) ,init-form
(aref arguments
(1+ ,x
))))))))
313 `(progn (var ,rest
(array))
314 (dotimes (,i
(- (slot-value arguments
'length
) ,(length effective-args
)))
315 (setf (aref ,rest
,i
) (aref arguments
(+ ,i
,(length effective-args
)))))))
317 (body-paren-forms (parse-function-body body
)) ; remove documentation
318 (effective-body (append opt-forms key-forms
(list rest-form
) body-paren-forms
)))
319 (values effective-args effective-body
))))
321 (defpsmacro defun
(name lambda-list
&body body
)
322 "An extended defun macro that allows cool things like keyword arguments.
325 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
327 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
328 [&aux {var | (var [init-form])}*])"
330 `(defun-function ,name
,lambda-list
,@body
)
331 (progn (assert (and (listp name
) (= (length name
) 2) (eq 'setf
(car name
))) ()
332 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list
)
333 `(defun-setf ,name
,lambda-list
,@body
))))
335 (defpsmacro defun-function
(name lambda-list
&body body
)
336 (multiple-value-bind (effective-args effective-body
)
337 (parse-extended-function lambda-list body name
)
338 `(%js-defun
,name
,effective-args
341 (defvar *defun-setf-name-prefix
* "__setf_")
343 (defpsmacro defun-setf
(setf-name lambda-list
&body body
)
344 (let ((mangled-function-name (intern (concatenate 'string
*defun-setf-name-prefix
* (symbol-name (second setf-name
)))
345 (symbol-package (second setf-name
))))
346 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords
))))
347 `(progn (defsetf ,(second setf-name
) ,(cdr lambda-list
) (store-var)
348 `(,',mangled-function-name
,store-var
,@(list ,@function-args
)))
349 (defun ,mangled-function-name
,lambda-list
,@body
))))
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 (defpsmacro flet
(fn-defs &rest body
)
365 (flet ((process-fn-def (def)
366 `(var ,(car def
) (lambda ,@(cdr def
)))))
367 `(progn ,@(mapcar #'process-fn-def fn-defs
) ,@body
)))
369 (defpsmacro labels
(fn-defs &rest body
)
370 (flet ((process-fn-def (def)
371 `(var ,(car def
) (defun ,(car def
) ,@(cdr def
)))))
372 `(progn ,@(mapcar #'process-fn-def fn-defs
) ,@body
)))
374 (defpsmacro defsetf-long
(access-fn lambda-list
(store-var) form
)
375 (setf (get-macro-spec access-fn
*ps-setf-expanders
*)
377 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords
)))
378 `(lambda (access-fn-args store-form
)
379 (destructuring-bind ,lambda-list
381 (let* ((,store-var
(ps-gensym))
382 (gensymed-names (loop repeat
,(length var-bindings
) collecting
(ps-gensym)))
383 (gensymed-arg-bindings (mapcar #'list gensymed-names
(list ,@var-bindings
))))
384 (destructuring-bind ,var-bindings
386 `(let* (,@gensymed-arg-bindings
387 (,,store-var
,store-form
))
391 (defpsmacro defsetf-short
(access-fn update-fn
&optional docstring
)
392 (declare (ignore docstring
))
393 (setf (get-macro-spec access-fn
*ps-setf-expanders
*)
394 (lambda (access-fn-args store-form
)
395 `(,update-fn
,@access-fn-args
,store-form
)))
398 (defpsmacro defsetf
(access-fn &rest args
)
399 `(,(if (= (length args
) 3) 'defsetf-long
'defsetf-short
) ,access-fn
,@args
))
401 (defpsmacro funcall
(&rest arg-form
)
404 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
406 (defmacro with-temp-macro-environment
((var) &body body
)
407 `(let* ((,var
(make-macro-env-dictionary))
408 (*ps-macro-env
* (cons ,var
*ps-macro-env
*)))
411 (define-ps-special-form macrolet
(macros &body body
)
412 (with-temp-macro-environment (macro-env-dict)
413 (dolist (macro macros
)
414 (destructuring-bind (name arglist
&body body
)
416 (setf (get-macro-spec name macro-env-dict
)
417 (cons nil
(eval (make-ps-macro-function arglist body
))))))
418 (compile-parenscript-form `(progn ,@body
))))
420 (define-ps-special-form symbol-macrolet
(symbol-macros &body body
)
421 (with-temp-macro-environment (macro-env-dict)
422 (dolist (macro symbol-macros
)
423 (destructuring-bind (name expansion
)
425 (setf (get-macro-spec name macro-env-dict
)
426 (cons t
(lambda (x) (declare (ignore x
)) expansion
)))))
427 (compile-parenscript-form `(progn ,@body
))))
429 (define-ps-special-form defmacro
(name args
&body body
) ;; should this be a macro?
430 (eval `(defpsmacro ,name
,args
,@body
))
433 (define-ps-special-form define-symbol-macro
(name expansion
) ;; should this be a macro?
434 (eval `(define-ps-symbol-macro ,name
,expansion
))
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 (define-ps-symbol-macro {} (create))
442 (define-ps-special-form create
(&rest arrows
)
443 `(js:object
,@(loop for
(key-expr val-expr
) on arrows by
#'cddr collecting
444 (let ((key (compile-parenscript-form key-expr
:expecting
:expression
)))
446 (setf key
`(js:variable
,key
)))
447 (assert (or (stringp key
)
450 (or (eq 'js
:variable
(car key
))
451 (eq 'quote
(car key
)))))
453 "Slot key ~s is not one of js-variable, keyword, string or number." key
)
454 (cons key
(compile-parenscript-form val-expr
:expecting
:expression
))))))
456 (define-ps-special-form %js-slot-value
(obj slot
)
457 `(js:slot-value
,(compile-parenscript-form obj
:expecting
:expression
)
458 ,(if (and (listp slot
) (eq 'quote
(car slot
)))
459 (second slot
) ;; assume we're quoting a symbol
460 (compile-parenscript-form slot
))))
462 (define-ps-special-form instanceof
(value type
)
463 `(js:instanceof
,(compile-parenscript-form value
:expecting
:expression
)
464 ,(compile-parenscript-form type
:expecting
:expression
)))
466 (defpsmacro slot-value
(obj &rest slots
)
467 (if (null (rest slots
))
468 `(%js-slot-value
,obj
,(first slots
))
469 `(slot-value (slot-value ,obj
,(first slots
)) ,@(rest slots
))))
471 (defpsmacro with-slots
(slots object
&rest body
)
472 (flet ((slot-var (slot) (if (listp slot
) (first slot
) slot
))
473 (slot-symbol (slot) (if (listp slot
) (second slot
) slot
)))
474 `(symbol-macrolet ,(mapcar #'(lambda (slot)
475 `(,(slot-var slot
) (slot-value ,object
',(slot-symbol slot
))))
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
480 ;;; assignment and binding
481 (defun assignment-op (op)
497 (define-ps-special-form setf1%
(lhs rhs
)
498 (let ((lhs (compile-parenscript-form lhs
:expecting
:expression
))
499 (rhs (compile-parenscript-form rhs
:expecting
:expression
)))
501 (eq 'js
:operator
(car rhs
))
502 (member (cadr rhs
) '(+ *))
503 (equalp lhs
(caddr rhs
)))
504 `(js:operator
,(assignment-op (cadr rhs
)) ,lhs
(js:operator
,(cadr rhs
) ,@(cdddr rhs
)))
507 (defpsmacro setf
(&rest args
)
508 (flet ((process-setf-clause (place value-form
)
509 (if (and (listp place
) (get-macro-spec (car place
) *ps-setf-expanders
*))
510 (funcall (get-macro-spec (car place
) *ps-setf-expanders
*) (cdr place
) value-form
)
511 (let ((exp-place (ps-macroexpand place
)))
512 (if (and (listp exp-place
) (get-macro-spec (car exp-place
) *ps-setf-expanders
*))
513 (funcall (get-macro-spec (car exp-place
) *ps-setf-expanders
*) (cdr exp-place
) value-form
)
514 `(setf1%
,exp-place
,value-form
))))))
515 (assert (evenp (length args
)) ()
516 "~s does not have an even number of arguments." (cons 'setf args
))
517 `(progn ,@(loop for
(place value
) on args by
#'cddr collect
(process-setf-clause place value
)))))
519 (defpsmacro psetf
(&rest args
)
520 (let ((places (loop for x in args by
#'cddr collect x
))
521 (vals (loop for x in
(cdr args
) by
#'cddr collect x
)))
522 (let ((gensyms (mapcar (lambda (x) (declare (ignore x
)) (ps-gensym)) places
)))
523 `(let ,(mapcar #'list gensyms vals
)
524 (setf ,@(mapcan #'list places gensyms
))))))
526 (defun check-setq-args (args)
527 (let ((vars (loop for x in args by
#'cddr collect x
)))
528 (let ((non-var (find-if (complement #'symbolp
) vars
)))
530 (error 'type-error
:datum non-var
:expected-type
'symbol
)))))
532 (defpsmacro setq
(&rest args
)
533 (check-setq-args args
)
536 (defpsmacro psetq
(&rest args
)
537 (check-setq-args args
)
540 (define-ps-special-form var
(name &optional
(value (values) value-provided?
) documentation
)
541 (declare (ignore documentation
))
544 `(js:var
,name
,@(when value-provided?
545 (list (compile-parenscript-form value
:expecting
:expression
)))))
547 (push name
*enclosing-lexical-block-declarations
*)
548 (when value-provided?
549 (compile-parenscript-form `(setf ,name
,value
) :expecting
:expression
)))))
551 (defpsmacro defvar
(name &optional
(value (values) value-provided?
) documentation
)
552 ;; this must be used as a top-level form, otherwise the resulting behavior will be undefined.
553 (declare (ignore documentation
))
554 (pushnew name
*ps-special-variables
*)
555 `(var ,name
,@(when value-provided?
(list value
))))
557 (defpsmacro let
(bindings &body body
)
558 (flet ((add-renamed-vars (bindings predicate
)
559 (mapcar (lambda (x) (append x
(list (ps-gensym (car x
)))))
560 (remove-if predicate bindings
:key
#'car
)))
563 (renamed (x) (third x
)))
564 (let* ((normalized-bindings (mapcar (lambda (x) (if (symbolp x
) `(,x nil
) x
)) bindings
))
565 (lexical-bindings (add-renamed-vars normalized-bindings
#'ps-special-variable-p
))
566 (dynamic-bindings (add-renamed-vars normalized-bindings
(complement #'ps-special-variable-p
)))
567 (renamed-body `(symbol-macrolet ,(mapcar (lambda (x) (list (var x
) (renamed x
)))
571 ,@(mapcar (lambda (x) `(var ,(renamed x
) ,(val x
))) lexical-bindings
)
572 ,(if dynamic-bindings
573 `(progn ,@(mapcar (lambda (x) `(var ,(renamed x
))) dynamic-bindings
)
574 (try (progn (setf ,@(loop for x in dynamic-bindings append
575 `(,(renamed x
) ,(var x
)
579 (setf ,@(mapcan (lambda (x) `(,(var x
) ,(renamed x
))) dynamic-bindings
)))))
582 (defpsmacro let
* (bindings &body body
)
584 `(let (,(car bindings
))
585 (let* ,(cdr bindings
)
589 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
591 (defun make-for-vars/inits
(init-forms)
593 (cons (compile-parenscript-form (if (atom x
) x
(first x
)) :expecting
:symbol
)
594 (compile-parenscript-form (if (atom x
) nil
(second x
)) :expecting
:expression
)))
597 (define-ps-special-form labeled-for
(label init-forms cond-forms step-forms
&rest body
)
599 ,(make-for-vars/inits init-forms
)
600 ,(mapcar (lambda (x) (compile-parenscript-form x
:expecting
:expression
)) cond-forms
)
601 ,(mapcar (lambda (x) (compile-parenscript-form x
:expecting
:expression
)) step-forms
)
602 ,(compile-parenscript-form `(progn ,@body
))))
604 (defpsmacro for
(init-forms cond-forms step-forms
&body body
)
605 `(labeled-for nil
,init-forms
,cond-forms
,step-forms
,@body
))
607 (defun do-make-let-bindings (decls)
610 (if (endp (cdr x
)) (list (car x
))
614 (defun do-make-init-vars (decls)
615 (mapcar (lambda (x) (if (atom x
) x
(first x
))) decls
))
617 (defun do-make-init-vals (decls)
618 (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) decls
))
620 (defun do-make-for-vars/init
(decls)
627 (defun do-make-for-steps (decls)
629 `(setf ,(first x
) ,(third x
)))
630 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
)))
632 (defun do-make-iter-psteps (decls)
634 ,@(mapcan (lambda (x) (list (first x
) (third x
)))
635 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
))))
637 (defpsmacro do
* (decls (termination &optional
(result nil result?
)) &body body
)
640 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
644 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
647 (defpsmacro do
(decls (termination &optional
(result nil result?
)) &body body
)
649 `((lambda ,(do-make-init-vars decls
)
650 (for () ((not ,termination
)) ()
652 ,(do-make-iter-psteps decls
))
654 ,@(do-make-init-vals decls
))
655 `(let ,(do-make-let-bindings decls
)
656 (for () ((not ,termination
)) ()
658 ,(do-make-iter-psteps decls
)))))
660 (define-ps-special-form for-in
((var object
) &rest body
)
661 `(js:for-in
,(compile-parenscript-form var
:expecting
:expression
)
662 ,(compile-parenscript-form object
:expecting
:expression
)
663 ,(compile-parenscript-form `(progn ,@body
))))
665 (define-ps-special-form while
(test &rest body
)
666 `(js:while
,(compile-parenscript-form test
:expecting
:expression
)
667 ,(compile-parenscript-form `(progn ,@body
))))
669 (defpsmacro dotimes
((var count
&optional
(result nil result?
)) &rest body
)
670 `(do* ((,var
0 (1+ ,var
)))
671 ((>= ,var
,count
) ,@(when result?
(list result
)))
674 (defpsmacro dolist
((var array
&optional
(result nil result?
)) &body body
)
675 (let ((idx (ps-gensym "_js_idx"))
676 (arrvar (ps-gensym "_js_arrvar")))
680 ((>= ,idx
(slot-value ,arrvar
'length
))
681 ,@(when result?
(list result
)))
682 (setq ,var
(aref ,arrvar
,idx
))
685 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
687 (define-ps-special-form with
(expression &rest body
)
688 `(js:with
,(compile-parenscript-form expression
:expecting
:expression
)
689 ,(compile-parenscript-form `(progn ,@body
))))
691 (define-ps-special-form try
(form &rest clauses
)
692 (let ((catch (cdr (assoc :catch clauses
)))
693 (finally (cdr (assoc :finally clauses
))))
694 (assert (not (cdar catch
)) nil
"Sorry, currently only simple catch forms are supported.")
695 (assert (or catch finally
) ()
696 "Try form should have either a catch or a finally clause or both.")
697 `(js:try
,(compile-parenscript-form `(progn ,form
))
698 :catch
,(when catch
(list (compile-parenscript-form (caar catch
) :expecting
:symbol
)
699 (compile-parenscript-form `(progn ,@(cdr catch
)))))
700 :finally
,(when finally
(compile-parenscript-form `(progn ,@finally
))))))
702 (define-ps-special-form cc-if
(test &rest body
)
703 `(js:cc-if
,test
,@(mapcar #'compile-parenscript-form body
)))
705 (define-ps-special-form regex
(regex)
706 `(js:regex
,(string regex
)))
708 (define-ps-special-form lisp
(lisp-form)
709 ;; (ps (foo (lisp bar))) is in effect equivalent to (ps* `(foo ,bar))
710 ;; when called from inside of ps*, lisp-form has access only to the dynamic environment (like for eval)
711 `(js:escape
(ps1* ,lisp-form
)))
713 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
715 (define-ps-special-form eval-when
(situation-list &body body
)
716 "(eval-when (situation*) body-form*)
718 The body forms are evaluated only during the given SITUATION. The accepted SITUATIONS are
719 :load-toplevel, :compile-toplevel, and :execute. The code in BODY-FORM is assumed to be
720 COMMON-LISP code in :compile-toplevel and :load-toplevel sitations, and parenscript code in
722 (when (and (member :compile-toplevel situation-list
)
723 (member *toplevel-compilation-level
* '(:toplevel
:inside-toplevel-form
)))
724 (eval `(progn ,@body
)))
725 (if (member :execute situation-list
)
726 (compile-parenscript-form `(progn ,@body
) :expecting expecting
)
727 (compile-parenscript-form `(progn) :expecting expecting
)))