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) `',x
) x
)))
31 (symbol (symbol-to-js-string x
))
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 (macrolet ((def-unary-ops (&rest ops
)
38 `(progn ,@(mapcar (lambda (op)
39 (let ((op (if (listp op
) (car op
) op
))
40 (spacep (if (listp op
) (second op
) nil
)))
41 `(define-ps-special-form ,op
(x)
42 (list 'js
:unary-operator
',op
43 (compile-parenscript-form x
:expecting
:expression
)
44 :prefix t
:space
,spacep
))))
46 (def-unary-ops ~
! (new t
) (delete t
) (void t
) (typeof t
)))
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 (define-ps-special-form return
(&optional value
)
51 `(js:return
,(compile-parenscript-form value
:expecting
:expression
)))
53 (define-ps-special-form throw
(value)
54 `(js:throw
,(compile-parenscript-form value
:expecting
:expression
)))
56 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 (define-ps-special-form array
(&rest values
)
59 `(js:array
,@(mapcar (lambda (form) (compile-parenscript-form form
:expecting
:expression
))
62 (define-ps-special-form aref
(array &rest coords
)
63 `(js:aref
,(compile-parenscript-form array
:expecting
:expression
)
64 ,(mapcar (lambda (form)
65 (compile-parenscript-form form
:expecting
:expression
))
68 (defpsmacro list
(&rest values
)
71 (defpsmacro make-array
(&rest initial-values
)
72 `(new (*array
,@initial-values
)))
74 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76 (define-ps-special-form incf
(x &optional
(delta 1))
78 `(js:unary-operator js
:++ ,(compile-parenscript-form x
:expecting
:expression
) :prefix t
)
79 `(js:operator js
:+= ,(compile-parenscript-form x
:expecting
:expression
)
80 ,(compile-parenscript-form delta
:expecting
:expression
))))
82 (define-ps-special-form decf
(x &optional
(delta 1))
84 `(js:unary-operator js
:--
,(compile-parenscript-form x
:expecting
:expression
) :prefix t
)
85 `(js:operator js
:-
= ,(compile-parenscript-form x
:expecting
:expression
)
86 ,(compile-parenscript-form delta
:expecting
:expression
))))
88 (define-ps-special-form -
(first &rest rest
)
90 `(js:operator js
:-
,@(mapcar (lambda (val) (compile-parenscript-form val
:expecting
:expression
))
92 `(js:unary-operator js
:-
,(compile-parenscript-form first
:expecting
:expression
) :prefix t
)))
94 (define-ps-special-form not
(x)
95 (let ((form (compile-parenscript-form x
:expecting
:expression
))
97 (if (and (eq (car form
) 'js
:operator
)
98 (= (length (cddr form
)) 2)
99 (setf inverse-op
(case (cadr form
)
108 `(js:operator
,inverse-op
,@(cddr form
))
109 `(js:unary-operator js
:! ,form
:prefix t
))))
111 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
112 ;;; control structures
113 (defun flatten-blocks (body)
115 (if (and (listp (car body
))
116 (eq 'js
:block
(caar body
)))
117 (append (third (car body
)) (flatten-blocks (cdr body
)))
118 (cons (car body
) (flatten-blocks (cdr body
))))))
120 (defun constant-literal-form-p (form)
124 (eq 'js
:literal
(car form
)))))
126 (define-ps-special-form progn
(&rest body
)
127 (if (and (eq expecting
:expression
) (= 1 (length body
)))
128 (compile-parenscript-form (car body
) :expecting
:expression
)
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:cond
,(mapcar (lambda (clause)
139 (destructuring-bind (test &rest body
)
141 (list (compile-parenscript-form test
:expecting
:expression
)
142 (compile-parenscript-form `(progn ,@body
) :expecting
:statement
))))
144 (:expression
(make-cond-clauses-into-nested-ifs clauses
))))
146 (defun make-cond-clauses-into-nested-ifs (clauses)
148 (destructuring-bind (test &rest body
)
151 (compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
152 `(js:?
,(compile-parenscript-form test
:expecting
:expression
)
153 ,(compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
154 ,(make-cond-clauses-into-nested-ifs (cdr clauses
)))))
155 (compile-parenscript-form nil
:expecting
:expression
))) ;; js:null
157 (define-ps-special-form if
(test then
&optional else
)
159 (:statement
`(js:if
,(compile-parenscript-form test
:expecting
:expression
)
160 ,(compile-parenscript-form `(progn ,then
))
161 ,(when else
(compile-parenscript-form `(progn ,else
)))))
162 (:expression
`(js:?
,(compile-parenscript-form test
:expecting
:expression
)
163 ,(compile-parenscript-form then
:expecting
:expression
)
164 ,(compile-parenscript-form else
:expecting
:expression
)))))
166 (define-ps-special-form switch
(test-expr &rest clauses
)
167 `(js:switch
,(compile-parenscript-form test-expr
:expecting
:expression
)
168 ,(loop for
(val . body
) in clauses collect
169 (cons (if (and (symbolp val
) (eq (ensure-ps-symbol val
) 'default
))
171 (compile-parenscript-form val
:expecting
:expression
))
172 (mapcar (lambda (x) (compile-parenscript-form x
:expecting
:statement
))
175 (defpsmacro case
(value &rest clauses
)
176 (labels ((make-clause (val body more
)
177 (cond ((and (listp val
) (not (eq (car val
) 'quote
)))
178 (append (mapcar #'list
(butlast val
))
179 (make-clause (first (last val
)) body more
)))
180 ((member val
'(t otherwise
))
181 (make-clause 'default body more
))
182 (more `((,val
,@body break
)))
183 (t `((,val
,@body
))))))
184 `(switch ,value
,@(mapcon (lambda (clause)
185 (make-clause (car (first clause
))
190 (defpsmacro when
(test &rest body
)
191 `(if ,test
(progn ,@body
)))
193 (defpsmacro unless
(test &rest body
)
194 `(if (not ,test
) (progn ,@body
)))
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
197 ;;; function definition
198 (defun compile-function-definition (args body
)
199 (list (mapcar (lambda (arg) (compile-parenscript-form arg
:expecting
:symbol
)) args
)
200 (let ((*enclosing-lexical-block-declarations
* ()))
201 ;; the first compilation will produce a list of variables we need to declare in the function body
202 (compile-parenscript-form `(progn ,@body
) :expecting
:statement
)
203 ;; now declare and compile
204 (compile-parenscript-form `(progn
205 ,@(mapcar (lambda (var) `(var ,var
)) *enclosing-lexical-block-declarations
*)
207 :expecting
:statement
))))
209 (define-ps-special-form %js-lambda
(args &rest body
)
210 `(js:lambda
,@(compile-function-definition args body
)))
212 (define-ps-special-form %js-defun
(name args
&rest body
)
213 `(js:defun
,name
,@(compile-function-definition args body
)))
215 (defun parse-function-body (body)
217 (when (stringp (first body
))
219 (body-forms (if docstring
(rest body
) body
)))
220 (values body-forms docstring
)))
222 (defun parse-key-spec (key-spec)
223 "parses an &key parameter. Returns 5 values:
224 var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
227 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
229 (let* ((var (cond ((symbolp key-spec
) key-spec
)
230 ((and (listp key-spec
) (symbolp (first key-spec
))) (first key-spec
))
231 ((and (listp key-spec
) (listp (first key-spec
))) (second key-spec
))))
232 (keyword-name (if (and (listp key-spec
) (listp (first key-spec
)))
233 (first (first key-spec
))
234 (intern (string var
) :keyword
)))
235 (init-form (if (listp key-spec
) (second key-spec
) nil
))
236 (init-form-supplied-p (if (listp key-spec
) t nil
))
237 (supplied-p-var (if (listp key-spec
) (third key-spec
) nil
)))
238 (values var init-form keyword-name supplied-p-var init-form-supplied-p
)))
240 (defun parse-optional-spec (spec)
241 "Parses an &optional parameter. Returns 3 values: var, init-form, supplied-p-var.
242 [&optional {var | (var [init-form [supplied-p-parameter]])}*] "
243 (let* ((var (cond ((symbolp spec
) spec
)
244 ((and (listp spec
) (first spec
)))))
245 (init-form (if (listp spec
) (second spec
)))
246 (supplied-p-var (if (listp spec
) (third spec
))))
247 (values var init-form supplied-p-var
)))
249 (defun parse-aux-spec (spec)
250 "Returns two values: variable and init-form"
251 ;; [&aux {var | (var [init-form])}*])
252 (values (if (symbolp spec
) spec
(first spec
))
253 (when (listp spec
) (second spec
))))
255 (defpsmacro defaultf
(place value
)
256 `(when (=== ,place undefined
)
257 (setf ,place
,value
)))
259 (defun parse-extended-function (lambda-list body
&optional name
)
260 "Returns two values: the effective arguments and body for a function with
261 the given lambda-list and body."
263 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
264 ;; list of variable names, and you have access to the arguments variable inside the function:
265 ;; * standard variables are the mapped directly into the js-lambda list
266 ;; * optional variables' variable names are mapped directly into the lambda list,
267 ;; and for each optional variable with name v and default value d, a form is produced
269 ;; * keyword variables are not included in the js-lambda list, but instead are
270 ;; obtained from the magic js ARGUMENTS pseudo-array. Code assigning values to
271 ;; keyword vars is prepended to the body of the function.
272 (declare (ignore name
))
273 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
274 more? more-context more-count key-object
)
275 (parse-lambda-list lambda-list
)
276 (declare (ignore allow? aux? aux more? more-context more-count key-object
))
277 (let* (;; optionals are of form (var default-value)
282 (mapcar #'parse-optional-spec optionals
))))
284 (mapcar #'(lambda (opt-spec)
285 (multiple-value-bind (var val
) (parse-optional-spec opt-spec
)
286 `(defaultf ,var
,val
)))
291 (let ((decls nil
) (assigns nil
) (defaults nil
))
293 (multiple-value-bind (var init-form keyword
)
295 (push (list 'var var
) decls
)
296 (push `(,keyword
(setf ,var
(aref arguments
(1+ ,n
)))) assigns
)
297 (push (list 'defaultf var init-form
) defaults
)))
300 (loop :for
,n
:from
,(length requireds
)
301 :below
(length arguments
) :by
2 :do
302 (case (aref arguments
,n
) ,@assigns
))
307 `(progn (var ,rest
(array))
308 (dotimes (,i
(- (slot-value arguments
'length
) ,(length effective-args
)))
309 (setf (aref ,rest
,i
) (aref arguments
(+ ,i
,(length effective-args
)))))))
311 (body-paren-forms (parse-function-body body
)) ; remove documentation
312 (effective-body (append opt-forms key-forms
(list rest-form
) body-paren-forms
)))
313 (values effective-args effective-body
))))
315 (defpsmacro defun
(name lambda-list
&body body
)
316 "An extended defun macro that allows cool things like keyword arguments.
319 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
321 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
322 [&aux {var | (var [init-form])}*])"
324 `(defun-function ,name
,lambda-list
,@body
)
325 (progn (assert (and (= (length name
) 2) (eq 'setf
(ensure-ps-symbol (car name
)))) ()
326 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list
)
327 `(defun-setf ,name
,lambda-list
,@body
))))
329 (defpsmacro defun-function
(name lambda-list
&body body
)
330 (multiple-value-bind (effective-args effective-body
)
331 (parse-extended-function lambda-list body name
)
332 `(%js-defun
,name
,effective-args
335 (defvar *defun-setf-name-prefix
* "__setf_")
337 (defpsmacro defun-setf
(setf-name lambda-list
&body body
)
338 (let ((mangled-function-name (intern (concatenate 'string
*defun-setf-name-prefix
* (symbol-name (second setf-name
)))
339 (symbol-package (second setf-name
))))
340 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords
))))
341 `(progn (defsetf ,(second setf-name
) ,(cdr lambda-list
) (store-var)
342 `(,',mangled-function-name
,store-var
,@(list ,@function-args
)))
343 (defun ,mangled-function-name
,lambda-list
,@body
))))
345 (defpsmacro lambda
(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])}*])"
353 (multiple-value-bind (effective-args effective-body
)
354 (parse-extended-function lambda-list body
)
355 `(%js-lambda
,effective-args
358 (defpsmacro flet
(fn-defs &rest body
)
359 (flet ((process-fn-def (def)
360 `(var ,(car def
) (lambda ,@(cdr def
)))))
361 `(progn ,@(mapcar #'process-fn-def fn-defs
) ,@body
)))
363 (defpsmacro labels
(fn-defs &rest body
)
364 (flet ((process-fn-def (def)
365 `(var ,(car def
) (defun ,(car def
) ,@(cdr def
)))))
366 `(progn ,@(mapcar #'process-fn-def fn-defs
) ,@body
)))
368 (defpsmacro defsetf-long
(access-fn lambda-list
(store-var) form
)
369 (setf (get-macro-spec access-fn
*ps-setf-expanders
*)
371 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords
)))
372 `(lambda (access-fn-args store-form
)
373 (destructuring-bind ,lambda-list
375 (let* ((,store-var
(ps-gensym))
376 (gensymed-names (loop repeat
,(length var-bindings
) collecting
(ps-gensym)))
377 (gensymed-arg-bindings (mapcar #'list gensymed-names
(list ,@var-bindings
))))
378 (destructuring-bind ,var-bindings
380 `(let* (,@gensymed-arg-bindings
381 (,,store-var
,store-form
))
385 (defpsmacro defsetf-short
(access-fn update-fn
&optional docstring
)
386 (declare (ignore docstring
))
387 (setf (get-macro-spec access-fn
*ps-setf-expanders
*)
388 (lambda (access-fn-args store-form
)
389 `(,update-fn
,@access-fn-args
,store-form
)))
392 (defpsmacro defsetf
(access-fn &rest args
)
393 `(,(if (= (length args
) 3) 'defsetf-long
'defsetf-short
) ,access-fn
,@args
))
395 (defpsmacro funcall
(&rest arg-form
)
398 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 (defmacro with-temp-macro-environment
((var) &body body
)
401 `(let* ((,var
(make-macro-env-dictionary))
402 (*ps-macro-env
* (cons ,var
*ps-macro-env
*)))
405 (define-ps-special-form macrolet
(macros &body body
)
406 (with-temp-macro-environment (macro-env-dict)
407 (dolist (macro macros
)
408 (destructuring-bind (name arglist
&body body
)
410 (setf (get-macro-spec name macro-env-dict
)
411 (cons nil
(eval (make-ps-macro-function arglist body
))))))
412 (compile-parenscript-form `(progn ,@body
))))
414 (define-ps-special-form symbol-macrolet
(symbol-macros &body body
)
415 (with-temp-macro-environment (macro-env-dict)
416 (dolist (macro symbol-macros
)
417 (destructuring-bind (name expansion
)
419 (setf (get-macro-spec name macro-env-dict
)
420 (cons t
(lambda (x) (declare (ignore x
)) expansion
)))))
421 (compile-parenscript-form `(progn ,@body
))))
423 (define-ps-special-form defmacro
(name args
&body body
) ;; should this be a macro?
424 (eval `(defpsmacro ,name
,args
,@body
))
427 (define-ps-special-form define-symbol-macro
(name expansion
) ;; should this be a macro?
428 (eval `(define-ps-symbol-macro ,name
,expansion
))
431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
434 (define-ps-symbol-macro {} (create))
436 (define-ps-special-form create
(&rest arrows
)
437 `(js:object
,@(loop for
(key-expr val-expr
) on arrows by
#'cddr collecting
438 (let ((key (compile-parenscript-form key-expr
:expecting
:expression
)))
440 (setf key
`(js:variable
,key
)))
441 (assert (or (stringp key
)
444 (or (eq 'js
:variable
(car key
))
445 (eq 'quote
(car key
)))))
447 "Slot key ~s is not one of js-variable, keyword, string or number." key
)
448 (cons key
(compile-parenscript-form val-expr
:expecting
:expression
))))))
450 (define-ps-special-form %js-slot-value
(obj slot
)
451 `(js:slot-value
,(compile-parenscript-form obj
:expecting
:expression
)
452 ,(if (and (listp slot
) (eq 'quote
(car slot
)))
453 (second slot
) ;; assume we're quoting a symbol
454 (compile-parenscript-form slot
))))
456 (define-ps-special-form instanceof
(value type
)
457 `(js:instanceof
,(compile-parenscript-form value
:expecting
:expression
)
458 ,(compile-parenscript-form type
:expecting
:expression
)))
460 (defpsmacro slot-value
(obj &rest slots
)
461 (if (null (rest slots
))
462 `(%js-slot-value
,obj
,(first slots
))
463 `(slot-value (slot-value ,obj
,(first slots
)) ,@(rest slots
))))
465 (defpsmacro with-slots
(slots object
&rest body
)
466 (flet ((slot-var (slot) (if (listp slot
) (first slot
) slot
))
467 (slot-symbol (slot) (if (listp slot
) (second slot
) slot
)))
468 `(symbol-macrolet ,(mapcar #'(lambda (slot)
469 `(,(slot-var slot
) (slot-value ,object
',(slot-symbol slot
))))
473 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
474 ;;; assignment and binding
475 (defun assignment-op (op)
491 (define-ps-special-form setf1%
(lhs rhs
)
492 (let ((lhs (compile-parenscript-form lhs
:expecting
:expression
))
493 (rhs (compile-parenscript-form rhs
:expecting
:expression
)))
495 (eq 'js
:operator
(car rhs
))
496 (member (cadr rhs
) '(+ *))
497 (equalp lhs
(caddr rhs
)))
498 `(js:operator
,(assignment-op (cadr rhs
)) ,lhs
(js:operator
,(cadr rhs
) ,@(cdddr rhs
)))
501 (defpsmacro setf
(&rest args
)
502 (flet ((process-setf-clause (place value-form
)
503 (if (and (listp place
) (get-macro-spec (car place
) *ps-setf-expanders
*))
504 (funcall (get-macro-spec (car place
) *ps-setf-expanders
*) (cdr place
) value-form
)
505 (let ((exp-place (ps-macroexpand place
)))
506 (if (and (listp exp-place
) (get-macro-spec (car exp-place
) *ps-setf-expanders
*))
507 (funcall (get-macro-spec (car exp-place
) *ps-setf-expanders
*) (cdr exp-place
) value-form
)
508 `(setf1%
,exp-place
,value-form
))))))
509 (assert (evenp (length args
)) ()
510 "~s does not have an even number of arguments." (cons 'setf args
))
511 `(progn ,@(loop for
(place value
) on args by
#'cddr collect
(process-setf-clause place value
)))))
513 (defpsmacro psetf
(&rest args
)
514 (let ((vars (loop for x in args by
#'cddr collect x
))
515 (vals (loop for x in
(cdr args
) by
#'cddr collect x
)))
516 (let ((gensyms (mapcar (lambda (x) (declare (ignore x
)) (ps-gensym)) vars
)))
517 `(simple-let* ,(mapcar #'list gensyms vals
)
518 (setf ,@(mapcan #'list vars gensyms
))))))
520 (defun check-setq-args (args)
521 (let ((vars (loop for x in args by
#'cddr collect x
)))
522 (let ((non-var (find-if (complement #'symbolp
) vars
)))
524 (error 'type-error
:datum non-var
:expected-type
'symbol
)))))
526 (defpsmacro setq
(&rest args
)
527 (check-setq-args args
)
530 (defpsmacro psetq
(&rest args
)
531 (check-setq-args args
)
534 (define-ps-special-form var
(name &optional
(value (values) value-provided?
) documentation
)
535 (declare (ignore documentation
))
536 `(js:var
,name
,@(when value-provided?
537 (list (compile-parenscript-form value
:expecting
:expression
)))))
539 (defpsmacro defvar
(name &optional
(value (values) value-provided?
) documentation
)
540 "Note: this must be used as a top-level form, otherwise the result will be undefined behavior."
541 (pushnew name
*ps-special-variables
*)
542 `(var ,name
,@(when value-provided?
(list value
))))
544 (defun make-let-vars (bindings)
545 (mapcar (lambda (x) (if (listp x
) (car x
) x
)) bindings
))
547 (defun make-let-vals (bindings)
548 (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) bindings
))
550 (defpsmacro lexical-let
* (bindings &body body
)
555 (defpsmacro lexical-let
(bindings &body body
)
556 `((lambda ,(make-let-vars bindings
)
558 ,@(make-let-vals bindings
)))
560 (defpsmacro simple-let
* (bindings &body body
)
562 (let ((var (if (listp (car bindings
)) (caar bindings
) (car bindings
))))
563 `(,(if (member var
*ps-special-variables
*) 'let1-dynamic
'let1
) ,(car bindings
)
564 (simple-let* ,(cdr bindings
) ,@body
)))
567 (defpsmacro simple-let
(bindings &body body
)
568 (let ((vars (mapcar (lambda (x) (if (atom x
) x
(first x
))) bindings
))
569 (vals (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) bindings
)))
570 (let ((gensyms (mapcar (lambda (x) (ps-gensym (format nil
"_js_~a" x
))) vars
)))
571 `(simple-let* ,(mapcar #'list gensyms vals
)
572 (simple-let* ,(mapcar #'list vars gensyms
)
573 ,@(mapcar (lambda (x) `(delete ,x
)) gensyms
)
576 (defpsmacro let
* (bindings &body body
)
577 `(simple-let* ,bindings
,@body
))
579 (defpsmacro let
(bindings &body body
)
580 `(,(if (= 1 (length bindings
)) 'simple-let
* 'simple-let
) ,bindings
,@body
))
582 (define-ps-special-form let1
(binding &rest body
)
585 (compile-parenscript-form `(progn ,(if (atom binding
) `(var ,binding
) `(var ,@binding
)) ,@body
) :expecting
:statement
))
587 (let ((var (if (atom binding
) binding
(car binding
)))
588 (variable-assignment (when (listp binding
) (cons 'setf binding
))))
589 (push var
*enclosing-lexical-block-declarations
*)
590 (compile-parenscript-form `(progn ,variable-assignment
,@body
) :expecting
:expression
)))))
592 (defpsmacro let1-dynamic
((var value
) &rest body
)
593 (with-ps-gensyms (temp-stack-var)
594 `(progn (var ,temp-stack-var
)
595 (try (progn (setf ,temp-stack-var
,var
)
599 (setf ,var
,temp-stack-var
)
600 (delete ,temp-stack-var
))))))
602 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
604 (defun make-for-vars/inits
(init-forms)
606 (cons (compile-parenscript-form (if (atom x
) x
(first x
)) :expecting
:symbol
)
607 (compile-parenscript-form (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 x
:expecting
:expression
)) cond-forms
)
614 ,(mapcar (lambda (x) (compile-parenscript-form 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 ,var
) :expecting
:expression
)
675 ,(compile-parenscript-form 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
,(ps1* lisp-form
)))