1 (in-package :parenscript
)
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 (defmacro defpsliteral
(name string
)
6 `(progn (pushnew ',name
*ps-literals
*)
7 (define-ps-special-form ,name
(expecting)
8 (declare (ignore expecting
))
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 (pushnew ',name
*ps-literals
*)
22 (define-ps-special-form ,name
(expecting &optional label
)
23 (declare (ignore expecting
))
24 (list ',printer label
)))))
25 (def-for-literal break js-break
)
26 (def-for-literal continue js-continue
))
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 (mapcar (lambda (op) (eval `(define-ps-special-form ,op
(expecting value
)
31 (declare (ignore expecting
))
32 (list 'js-named-operator
',op
(compile-parenscript-form value
)))))
33 '(throw delete void typeof new
))
35 (define-ps-special-form return
(expecting &optional value
)
36 (declare (ignore expecting
))
37 (list 'js-return
(compile-parenscript-form value
:expecting
:expression
)))
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 (define-ps-special-form array
(expecting &rest values
)
42 (declare (ignore expecting
))
43 (cons 'array-literal
(mapcar (lambda (form) (compile-parenscript-form form
:expecting
:expression
))
46 (define-ps-special-form aref
(expecting array
&rest coords
)
47 (declare (ignore expecting
))
48 (list 'js-aref
(compile-parenscript-form array
:expecting
:expression
)
49 (mapcar (lambda (form)
50 (compile-parenscript-form form
:expecting
:expression
))
53 (define-ps-special-form {} (expecting &rest arrows
)
54 (declare (ignore expecting
))
55 (cons 'object-literal
(loop for
(key value
) on arrows by
#'cddr
56 collect
(cons key
(compile-parenscript-form value
:expecting
:expression
)))))
58 (defpsmacro list
(&rest values
)
61 (defpsmacro make-array
(&rest inits
)
62 `(new (*array
,@inits
)))
64 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
66 (define-ps-special-form incf
(expecting x
&optional
(delta 1))
67 (declare (ignore expecting
))
69 (list 'unary-operator
"++" (compile-parenscript-form x
:expecting
:expression
) :prefix t
)
70 (list 'operator
'+= (list (compile-parenscript-form x
:expecting
:expression
)
71 (compile-parenscript-form delta
:expecting
:expression
)))))
73 (define-ps-special-form decf
(expecting x
&optional
(delta 1))
74 (declare (ignore expecting
))
76 (list 'unary-operator
"--" (compile-parenscript-form x
:expecting
:expression
) :prefix t
)
77 (list 'operator
'-
= (list (compile-parenscript-form x
:expecting
:expression
)
78 (compile-parenscript-form delta
:expecting
:expression
)))))
80 (define-ps-special-form -
(expecting first
&rest rest
)
81 (declare (ignore expecting
))
83 (list 'unary-operator
"-" (compile-parenscript-form first
:expecting
:expression
) :prefix t
)
84 (list 'operator
'-
(mapcar (lambda (val) (compile-parenscript-form val
:expecting
:expression
))
87 (define-ps-special-form not
(expecting x
)
88 (declare (ignore expecting
))
89 (let ((form (compile-parenscript-form x
:expecting
:expression
))
91 (if (and (eql (first form
) 'operator
)
92 (= (length (third form
)) 2)
93 (setf not-op
(case (second form
)
103 (list 'operator not-op
(third form
))
104 (list 'unary-operator
"!" form
:prefix t
))))
106 (define-ps-special-form ~
(expecting x
)
107 (declare (ignore expecting
))
108 (list 'unary-operator
"~" (compile-parenscript-form x
:expecting
:expression
) :prefix t
))
110 (defpsmacro 1-
(form)
113 (defpsmacro 1+ (form)
116 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
117 ;;; control structures
118 (defun flatten-blocks (body)
120 (if (and (listp (car body
))
121 (eql 'js-block
(caar body
)))
122 (append (third (car body
)) (flatten-blocks (cdr body
)))
123 (cons (car body
) (flatten-blocks (cdr body
))))))
125 (defun constant-literal-form-p (form)
129 (eql 'js-literal
(car form
)))))
131 (define-ps-special-form progn
(expecting &rest body
)
132 (if (and (eql expecting
:expression
) (= 1 (length body
)))
133 (compile-parenscript-form (car body
) :expecting
:expression
)
136 (let* ((block (mapcar (lambda (form)
137 (compile-parenscript-form form
:expecting expecting
))
139 (clean-block (remove nil block
))
140 (flat-block (flatten-blocks clean-block
))
141 (reachable-block (append (remove-if #'constant-literal-form-p
(butlast flat-block
))
145 (define-ps-special-form cond
(expecting &rest clauses
)
147 (:statement
(list 'js-cond-statement
148 (mapcar (lambda (clause)
149 (destructuring-bind (test &rest body
)
151 (list (compile-parenscript-form test
:expecting
:expression
)
152 (compile-parenscript-form `(progn ,@body
) :expecting
:statement
))))
154 (:expression
(make-cond-clauses-into-nested-ifs clauses
))))
156 (defun make-cond-clauses-into-nested-ifs (clauses)
158 (destructuring-bind (test &rest body
)
161 (compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
162 (list 'js-expression-if
(compile-parenscript-form test
:expecting
:expression
)
163 (compile-parenscript-form `(progn ,@body
) :expecting
:expression
)
164 (make-cond-clauses-into-nested-ifs (cdr clauses
)))))
165 (compile-parenscript-form nil
:expecting
:expression
)))
167 (define-ps-special-form if
(expecting test then
&optional else
)
169 (:statement
(list 'js-statement-if
(compile-parenscript-form test
:expecting
:expression
)
170 (compile-parenscript-form `(progn ,then
))
171 (when else
(compile-parenscript-form `(progn ,else
)))))
172 (:expression
(list 'js-expression-if
(compile-parenscript-form test
:expecting
:expression
)
173 (compile-parenscript-form then
:expecting
:expression
)
174 (compile-parenscript-form else
:expecting
:expression
)))))
176 (define-ps-special-form switch
(expecting test-expr
&rest clauses
)
177 (declare (ignore expecting
))
178 (let ((clauses (mapcar (lambda (clause)
179 (let ((val (car clause
))
181 (cons (if (eql val
'default
)
183 (compile-parenscript-form val
:expecting
:expression
))
184 (mapcar (lambda (statement) (compile-parenscript-form statement
:expecting
:statement
))
187 (expr (compile-parenscript-form test-expr
:expecting
:expression
)))
188 (list 'js-switch expr clauses
)))
190 (defpsmacro case
(value &rest clauses
)
191 (labels ((make-clause (val body more
)
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
213 (defun compile-function-definition (args body
)
214 (list (mapcar (lambda (arg) (compile-parenscript-form arg
:expecting
:symbol
)) args
)
215 (let ((*enclosing-lexical-block-declarations
* ()))
216 ;; the first compilation will produce a list of variables we need to declare in the function body
217 (compile-parenscript-form `(progn ,@body
) :expecting
:statement
)
218 ;; now declare and compile
219 (compile-parenscript-form `(progn ,@(loop for var in
*enclosing-lexical-block-declarations
* collect
`(var ,var
))
220 ,@body
) :expecting
:statement
))))
222 (define-ps-special-form %js-lambda
(expecting args
&rest body
)
223 (declare (ignore expecting
))
224 (cons 'js-lambda
(compile-function-definition args body
)))
226 (define-ps-special-form %js-defun
(expecting name args
&rest body
)
227 (declare (ignore expecting
))
228 (append (list 'js-defun
name) (compile-function-definition args body
)))
230 (defun parse-function-body (body)
232 (when (stringp (first body
))
234 (body-forms (if docstring
(rest body
) body
)))
235 (values body-forms docstring
)))
237 (defun parse-key-spec (key-spec)
238 "parses an &key parameter. Returns 4 values:
239 var, init-form, keyword-name, supplied-p-var, init-form-supplied-p.
242 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}*
244 (let* ((var (cond ((symbolp key-spec
) key-spec
)
245 ((and (listp key-spec
) (symbolp (first key-spec
))) (first key-spec
))
246 ((and (listp key-spec
) (listp (first key-spec
))) (second key-spec
))))
247 (keyword-name (if (and (listp key-spec
) (listp (first key-spec
)))
248 (first (first key-spec
))
249 (intern (string var
) :keyword
)))
250 (init-form (if (listp key-spec
) (second key-spec
) nil
))
251 (init-form-supplied-p (if (listp key-spec
) t nil
))
252 (supplied-p-var (if (listp key-spec
) (third key-spec
) nil
)))
253 (values var init-form keyword-name supplied-p-var init-form-supplied-p
)))
255 (defun parse-optional-spec (spec)
256 "Parses an &optional parameter. Returns 3 values: var, init-form, supplied-p-var.
257 [&optional {var | (var [init-form [supplied-p-parameter]])}*] "
258 (let* ((var (cond ((symbolp spec
) spec
)
259 ((and (listp spec
) (first spec
)))))
260 (init-form (if (listp spec
) (second spec
)))
261 (supplied-p-var (if (listp spec
) (third spec
))))
262 (values var init-form supplied-p-var
)))
264 (defun parse-aux-spec (spec)
265 "Returns two values: variable and init-form"
266 ;; [&aux {var | (var [init-form])}*])
267 (values (if (symbolp spec
) spec
(first spec
))
268 (when (listp spec
) (second spec
))))
270 (defpsmacro defaultf
(place value
)
271 `(setf ,place
(or (and (=== undefined
,place
) ,value
)
274 (defun parse-extended-function (lambda-list body
&optional name
)
275 "Returns two values: the effective arguments and body for a function with
276 the given lambda-list and body."
278 ;; The lambda list is transformed as follows, since a javascript lambda list is just a
279 ;; list of variable names, and you have access to the arguments variable inside the function:
280 ;; * standard variables are the mapped directly into the js-lambda list
281 ;; * optional variables' variable names are mapped directly into the lambda list,
282 ;; and for each optional variable with name v and default value d, a form is produced
284 ;; * when any keyword variables are in the lambda list, a single 'optional-args' variable is
285 ;; appended to the js-lambda list as the last argument. WITH-SLOTS is used for all
286 ;; the variables with inside the body of the function,
287 ;; a (with-slots ((var-name key-name)) optional-args ...)
288 (declare (ignore name
))
289 (multiple-value-bind (requireds optionals rest? rest keys? keys allow? aux? aux
290 more? more-context more-count key-object
)
291 (parse-lambda-list lambda-list
)
292 (declare (ignore allow? aux? aux more? more-context more-count
))
293 (let* ((options-var (or key-object
(ps-gensym)))
294 ;; optionals are of form (var default-value)
299 (mapcar #'parse-optional-spec optionals
)
300 (when keys
(list options-var
)))))
301 ;; an alist of arg -> default val
306 ;; optional arguments first
307 (mapcar #'(lambda (opt-spec)
308 (multiple-value-bind (var val
) (parse-optional-spec opt-spec
)
311 (if keys?
(list (cons options-var
'(create))))
312 (mapcar #'(lambda (key-spec)
313 (multiple-value-bind (var val x y specified?
) (parse-key-spec key-spec
)
314 (declare (ignore x y
))
315 (when specified?
(cons var val
))))
317 (body-paren-forms (parse-function-body body
)) ;remove documentation
320 (mapcar #'(lambda (default-pair)
321 `(defaultf ,(car default-pair
) ,(cdr default-pair
)))
326 `(progn (var ,rest
(array))
327 (dotimes (,i
(- arguments.length
,(length effective-args
)))
328 (setf (aref ,rest
,i
) (aref arguments
(+ ,i
,(length effective-args
)))))))
330 (effective-body (append initform-forms
(list rest-form
) body-paren-forms
))
333 (list `(with-slots ,(mapcar #'(lambda (key-spec)
334 (multiple-value-bind (var x key-name
)
335 (parse-key-spec key-spec
)
337 (list var key-name
)))
342 (values effective-args effective-body
))))
344 (defpsmacro defun
(name lambda-list
&body body
)
345 "An extended defun macro that allows cool things like keyword arguments.
348 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
350 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
351 [&aux {var | (var [init-form])}*])"
353 `(defun-function ,name
,lambda-list
,@body
)
354 (progn (assert (and (= (length name
) 2) (eql 'setf
(car name
))) ()
355 "(defun ~s ~s ...) needs to have a symbol or (setf symbol) for a name." name lambda-list
)
356 `(defun-setf ,name
,lambda-list
,@body
))))
358 (defpsmacro defun-function
(name lambda-list
&body body
)
359 (multiple-value-bind (effective-args effective-body
)
360 (parse-extended-function lambda-list body name
)
361 `(%js-defun
,name
,effective-args
364 (defvar *defun-setf-name-prefix
* "__setf_")
366 (defpsmacro defun-setf
(setf-name lambda-list
&body body
)
367 (let ((mangled-function-name (intern (concatenate 'string
*defun-setf-name-prefix
* (symbol-name (second setf-name
)))
368 (symbol-package (second setf-name
))))
369 (function-args (cdr (ordered-set-difference lambda-list lambda-list-keywords
))))
370 `(progn (defsetf ,(second setf-name
) ,(cdr lambda-list
) (store-var)
371 `(,',mangled-function-name
,store-var
,@(list ,@function-args
)))
372 (defun ,mangled-function-name
,lambda-list
,@body
))))
374 (defpsmacro lambda
(lambda-list &body body
)
375 "An extended defun macro that allows cool things like keyword arguments.
378 [&optional {var | (var [init-form [supplied-p-parameter]])}*]
380 [&key {var | ({var | (keyword-name var)} [init-form [supplied-p-parameter]])}* [&allow-other-keys]]
381 [&aux {var | (var [init-form])}*])"
382 (multiple-value-bind (effective-args effective-body
)
383 (parse-extended-function lambda-list body
)
384 `(%js-lambda
,effective-args
387 (defpsmacro defsetf-long
(access-fn lambda-list
(store-var) form
)
388 (setf (get-macro-spec access-fn
*script-setf-expanders
*)
390 (let ((var-bindings (ordered-set-difference lambda-list lambda-list-keywords
)))
391 `(lambda (access-fn-args store-form
)
392 (destructuring-bind ,lambda-list
394 (let* ((,store-var
(ps-gensym))
395 (gensymed-names (loop repeat
,(length var-bindings
) collecting
(ps-gensym)))
396 (gensymed-arg-bindings (mapcar #'list gensymed-names
(list ,@var-bindings
))))
397 (destructuring-bind ,var-bindings
399 `(let* (,@gensymed-arg-bindings
400 (,,store-var
,store-form
))
404 (defpsmacro defsetf-short
(access-fn update-fn
&optional docstring
)
405 (declare (ignore docstring
))
406 (setf (get-macro-spec access-fn
*script-setf-expanders
*)
407 (lambda (access-fn-args store-form
)
408 `(,update-fn
,@access-fn-args
,store-form
)))
411 (defpsmacro defsetf
(access-fn &rest args
)
412 `(,(if (= (length args
) 3) 'defsetf-long
'defsetf-short
) ,access-fn
,@args
))
414 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
416 (defmacro with-temp-macro-environment
((var) &body body
)
417 `(let* ((,var
(make-macro-env-dictionary))
418 (*script-macro-env
* (cons ,var
*script-macro-env
*)))
421 (define-ps-special-form macrolet
(expecting macros
&body body
)
422 (declare (ignore expecting
))
423 (with-temp-macro-environment (macro-env-dict)
424 (dolist (macro macros
)
425 (destructuring-bind (name arglist
&body body
)
427 (setf (get-macro-spec name macro-env-dict
)
428 (cons nil
(make-ps-macro-function arglist body
)))))
429 (compile-parenscript-form `(progn ,@body
))))
431 (define-ps-special-form symbol-macrolet
(expecting symbol-macros
&body body
)
432 (declare (ignore expecting
))
433 (with-temp-macro-environment (macro-env-dict)
434 (dolist (macro symbol-macros
)
435 (destructuring-bind (name expansion
)
437 (setf (get-macro-spec name macro-env-dict
)
438 (cons t
(make-ps-macro-function () (list `',expansion
))))))
439 (compile-parenscript-form `(progn ,@body
))))
441 (define-ps-special-form defmacro
(expecting name args
&body body
)
442 (declare (ignore expecting
))
443 (define-script-macro% name args body
:symbol-macro-p nil
)
446 (define-ps-special-form define-symbol-macro
(expecting name expansion
)
447 (declare (ignore expecting
))
448 (define-script-macro% name
() (list `',expansion
) :symbol-macro-p t
)
451 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
453 (define-ps-special-form create
(expecting &rest args
)
454 (declare (ignore expecting
))
455 (list 'js-object
(loop for
(name val
) on args by
#'cddr collecting
456 (let ((name-expr (compile-parenscript-form name
:expecting
:expression
)))
457 (assert (or (stringp name-expr
)
459 (and (listp name-expr
)
460 (or (eql 'js-variable
(car name-expr
))
461 (eql 'script-quote
(car name-expr
)))))
463 "Slot ~s is not one of js-variable, keyword, string or number." name-expr
)
464 (list name-expr
(compile-parenscript-form val
:expecting
:expression
))))))
466 (define-ps-special-form %js-slot-value
(expecting obj slot
)
467 (declare (ignore expecting
))
468 (if (ps::ps-macroexpand slot
)
469 (list 'js-slot-value
(compile-parenscript-form obj
:expecting
:expression
) (compile-parenscript-form slot
))
470 (compile-parenscript-form obj
:expecting
:expression
)))
472 (define-ps-special-form instanceof
(expecting value type
)
473 (declare (ignore expecting
))
474 (list 'js-instanceof
(compile-parenscript-form value
:expecting
:expression
)
475 (compile-parenscript-form type
:expecting
:expression
)))
477 (defpsmacro slot-value
(obj &rest slots
)
478 (if (null (rest slots
))
479 `(%js-slot-value
,obj
,(first slots
))
480 `(slot-value (slot-value ,obj
,(first slots
)) ,@(rest slots
))))
482 (defpsmacro with-slots
(slots object
&rest body
)
483 (flet ((slot-var (slot) (if (listp slot
) (first slot
) slot
))
484 (slot-symbol (slot) (if (listp slot
) (second slot
) slot
)))
485 `(symbol-macrolet ,(mapcar #'(lambda (slot)
486 `(,(slot-var slot
) (slot-value ,object
',(slot-symbol slot
))))
490 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
491 ;;; assignment and binding
492 (defun assignment-op (op)
508 (defun smart-setf (lhs rhs
)
510 (eql 'operator
(car rhs
))
511 (member lhs
(third rhs
) :test
#'equalp
))
512 (let ((args-without-first (remove lhs
(third rhs
) :count
1 :end
1 :test
#'equalp
)))
513 (cond ((and (assignment-op (second rhs
))
514 (member (second rhs
) '(+ *))
515 (equalp lhs
(first (third rhs
))))
516 (list 'operator
(assignment-op (second rhs
))
517 (list lhs
(list 'operator
(second rhs
) args-without-first
))))
518 (t (list 'js-assign lhs rhs
))))
519 (list 'js-assign lhs rhs
)))
521 (define-ps-special-form setf1%
(expecting lhs rhs
)
522 (declare (ignore expecting
))
523 (smart-setf (compile-parenscript-form lhs
:expecting
:expression
) (compile-parenscript-form rhs
:expecting
:expression
)))
525 (defpsmacro setf
(&rest args
)
526 (flet ((process-setf-clause (place value-form
)
527 (if (and (listp place
) (get-macro-spec (car place
) *script-setf-expanders
*))
528 (funcall (get-macro-spec (car place
) *script-setf-expanders
*) (cdr place
) value-form
)
529 (let ((exp-place (ps-macroexpand place
)))
530 (if (and (listp exp-place
) (get-macro-spec (car exp-place
) *script-setf-expanders
*))
531 (funcall (get-macro-spec (car exp-place
) *script-setf-expanders
*) (cdr exp-place
) value-form
)
532 `(setf1%
,exp-place
,value-form
))))))
533 (assert (evenp (length args
)) ()
534 "~s does not have an even number of arguments." (cons 'setf args
))
535 `(progn ,@(loop for
(place value
) on args by
#'cddr collect
(process-setf-clause place value
)))))
537 (defpsmacro psetf
(&rest args
)
538 (let ((vars (loop for x in args by
#'cddr collect x
))
539 (vals (loop for x in
(cdr args
) by
#'cddr collect x
)))
540 (let ((gensyms (mapcar (lambda (x) (declare (ignore x
)) (ps-gensym)) vars
)))
541 `(simple-let* ,(mapcar #'list gensyms vals
)
542 (setf ,@(mapcan #'list vars gensyms
))))))
544 (defun check-setq-args (args)
545 (let ((vars (loop for x in args by
#'cddr collect x
)))
546 (let ((non-var (find-if (complement #'symbolp
) vars
)))
548 (error 'type-error
:datum non-var
:expected-type
'symbol
)))))
550 (defpsmacro setq
(&rest args
)
551 (check-setq-args args
)
554 (defpsmacro psetq
(&rest args
)
555 (check-setq-args args
)
558 (define-ps-special-form var
(expecting name
&rest value
)
559 (declare (ignore expecting
))
560 (append (list 'js-var name
)
562 (assert (= (length value
) 1) () "Wrong number of arguments to var: ~s" `(var ,name
,@value
))
563 (list (compile-parenscript-form (car value
) :expecting
:expression
)))))
565 (defpsmacro defvar
(name &rest value
)
566 "Note: this must be used as a top-level form, otherwise the result will be undefined behavior."
567 (pushnew name
*ps-special-variables
*)
568 (assert (or (null value
) (= (length value
) 1)) () "Wrong number of arguments to defvar: ~s" `(defvar ,name
,@value
))
569 `(var ,name
,@value
))
571 (defun make-let-vars (bindings)
572 (mapcar (lambda (x) (if (listp x
) (car x
) x
)) bindings
))
574 (defun make-let-vals (bindings)
575 (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) bindings
))
577 (defpsmacro lexical-let
* (bindings &body body
)
582 (defpsmacro lexical-let
(bindings &body body
)
583 `((lambda ,(make-let-vars bindings
)
585 ,@(make-let-vals bindings
)))
587 (defpsmacro simple-let
* (bindings &body body
)
589 (let ((var (if (listp (car bindings
)) (caar bindings
) (car bindings
))))
590 `(,(if (member var
*ps-special-variables
*) 'let1-dynamic
'let1
) ,(car bindings
)
591 (simple-let* ,(cdr bindings
) ,@body
)))
594 (defpsmacro simple-let
(bindings &body body
)
595 (let ((vars (mapcar (lambda (x) (if (atom x
) x
(first x
))) bindings
))
596 (vals (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) bindings
)))
597 (let ((gensyms (mapcar (lambda (x) (ps-gensym (format nil
"_js_~a" x
))) vars
)))
598 `(simple-let* ,(mapcar #'list gensyms vals
)
599 (simple-let* ,(mapcar #'list vars gensyms
)
600 ,@(mapcar (lambda (x) `(delete ,x
)) gensyms
)
603 (defpsmacro let
* (bindings &body body
)
604 `(simple-let* ,bindings
,@body
))
606 (defpsmacro let
(bindings &body body
)
607 `(simple-let ,bindings
,@body
))
609 (define-ps-special-form let1
(expecting binding
&rest body
)
612 (compile-parenscript-form `(progn ,(if (atom binding
) `(var ,binding
) `(var ,@binding
)) ,@body
) :expecting
:statement
))
614 (let ((var (if (atom binding
) binding
(car binding
)))
615 (variable-assignment (when (listp binding
) (cons 'setf binding
))))
616 (push var
*enclosing-lexical-block-declarations
*)
617 (compile-parenscript-form `(progn ,variable-assignment
,@body
) :expecting
:expression
)))))
619 (defpsmacro let1-dynamic
((var value
) &rest body
)
620 (with-ps-gensyms (temp-stack-var)
621 `(progn (var ,temp-stack-var
)
622 (try (progn (setf ,temp-stack-var
,var
)
626 (setf ,var
,temp-stack-var
)
627 (delete ,temp-stack-var
))))))
629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
631 (defun make-for-vars/inits
(init-forms)
633 (cons (compile-parenscript-form (if (atom x
) x
(first x
)) :expecting
:symbol
)
634 (compile-parenscript-form (if (atom x
) nil
(second x
)))))
637 (define-ps-special-form labeled-for
(expecting label init-forms cond-forms step-forms
&rest body
)
638 (declare (ignore expecting
))
639 (let ((vars (make-for-vars/inits init-forms
))
640 (steps (mapcar (lambda (x) (compile-parenscript-form x
:expecting
:expression
)) step-forms
))
641 (tests (mapcar (lambda (x) (compile-parenscript-form x
:expecting
:expression
)) cond-forms
))
642 (body (compile-parenscript-form `(progn ,@body
))))
643 (list 'js-for label vars tests steps body
)))
645 (defpsmacro for
(init-forms cond-forms step-forms
&body body
)
646 `(labeled-for nil
,init-forms
,cond-forms
,step-forms
,@body
))
648 (defun do-make-let-bindings (decls)
651 (if (endp (cdr x
)) (list (car x
))
655 (defun do-make-init-vars (decls)
656 (mapcar (lambda (x) (if (atom x
) x
(first x
))) decls
))
658 (defun do-make-init-vals (decls)
659 (mapcar (lambda (x) (if (or (atom x
) (endp (cdr x
))) nil
(second x
))) decls
))
661 (defun do-make-for-vars/init
(decls)
668 (defun do-make-for-steps (decls)
670 `(setf ,(first x
) ,(third x
)))
671 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
)))
673 (defun do-make-iter-psteps (decls)
675 ,@(mapcan (lambda (x) (list (first x
) (third x
)))
676 (remove-if (lambda (x) (or (atom x
) (< (length x
) 3))) decls
))))
678 (defpsmacro do
* (decls (termination &optional
(result nil result?
)) &body body
)
681 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
685 (for ,(do-make-for-vars/init decls
) ((not ,termination
)) ,(do-make-for-steps decls
)
688 (defpsmacro do
(decls (termination &optional
(result nil result?
)) &body body
)
690 `((lambda ,(do-make-init-vars decls
)
691 (for () ((not ,termination
)) ()
693 ,(do-make-iter-psteps decls
))
695 ,@(do-make-init-vals decls
))
696 `(let ,(do-make-let-bindings decls
)
697 (for () ((not ,termination
)) ()
699 ,(do-make-iter-psteps decls
)))))
701 (define-ps-special-form for-in
(expecting decl
&rest body
)
702 (declare (ignore expecting
))
704 (compile-parenscript-form (first decl
) :expecting
:expression
)
705 (compile-parenscript-form (second decl
) :expecting
:expression
)
706 (compile-parenscript-form `(progn ,@body
))))
708 (defpsmacro doeach
((var array
&optional
(result (values) result?
)) &body body
)
709 "Iterates over `array'. If `var' is a symbol, binds `var' to each
710 element key. If `var' is a list, it must be a list of two
711 symbols, (key value), which will be bound to each successive key/value
715 (destructuring-bind (key val
) var
718 (for-in ((var ,key
) ,array
)
719 (setf ,val
(aref ,array
,key
))
723 (for-in ((var ,var
) ,array
)
727 (destructuring-bind (key val
) var
730 (for-in ((var ,key
) ,array
)
731 (setf ,val
(aref ,array
,key
))
734 (for-in ((var ,var
) ,array
) ,@body
)))))
736 (define-ps-special-form while
(expecting test
&rest body
)
737 (declare (ignore expecting
))
738 (list 'js-while
(compile-parenscript-form test
:expecting
:expression
)
739 (compile-parenscript-form `(progn ,@body
))))
741 (defpsmacro dotimes
((var count
&optional
(result nil result?
)) &rest body
)
742 `(do* ((,var
0 (1+ ,var
)))
743 ((>= ,var
,count
) ,@(when result?
(list result
)))
746 (defpsmacro dolist
((var array
&optional
(result nil result?
)) &body body
)
747 (let ((idx (ps-gensym "_js_idx"))
748 (arrvar (ps-gensym "_js_arrvar")))
752 ((>= ,idx
(slot-value ,arrvar
'length
))
753 ,@(when result?
(list result
)))
754 (setq ,var
(aref ,arrvar
,idx
))
757 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
759 (define-ps-special-form with
(expecting expression
&rest body
)
760 (declare (ignore expecting
))
761 (list 'js-with
(compile-parenscript-form expression
:expecting
:expression
)
762 (compile-parenscript-form `(progn ,@body
))))
764 (define-ps-special-form try
(expecting form
&rest clauses
)
765 (declare (ignore expecting
))
766 (let ((catch (cdr (assoc :catch clauses
)))
767 (finally (cdr (assoc :finally clauses
))))
768 (assert (not (cdar catch
)) nil
"Sorry, currently only simple catch forms are supported.")
769 (assert (or catch finally
) ()
770 "Try form should have either a catch or a finally clause or both.")
771 (list 'js-try
(compile-parenscript-form `(progn ,form
))
772 :catch
(when catch
(list (compile-parenscript-form (caar catch
) :expecting
:symbol
)
773 (compile-parenscript-form `(progn ,@(cdr catch
)))))
774 :finally
(when finally
(compile-parenscript-form `(progn ,@finally
))))))
776 (define-ps-special-form cc-if
(expecting test
&rest body
)
777 (declare (ignore expecting
))
778 (list 'cc-if test
(mapcar #'compile-parenscript-form body
)))
780 (define-ps-special-form regex
(expecting regex
)
781 (declare (ignore expecting
))
782 (list 'js-regex
(string regex
)))
784 (defpsmacro lisp
(&body forms
)
785 "Evaluates the given forms in Common Lisp at ParenScript
786 macro-expansion time. The value of the last form is treated as a
787 ParenScript expression and is inserted into the generated Javascript
788 \(use nil for no-op)."
789 (eval (cons 'progn forms
)))