3 ;;; ecmascript standard:
4 ;;; http://www.ecma-international.org/publications/standards/Ecma-262.htm
6 ;;; javascript name conversion
8 (defparameter *special-chars
*
18 (defun string-chars (string)
19 (coerce string
'list
))
21 (defun constant-string-p (string)
22 (let ((len (length string
))
23 (constant-chars '(#\
+ #\
*)))
25 (member (char string
0) constant-chars
)
26 (member (char string
(1- len
)) constant-chars
))))
28 (defun first-uppercase-p (string)
29 (and (> (length string
) 1)
30 (member (char string
0) '(#\
+ #\
*))))
32 (defun untouchable-string-p (string)
33 (and (> (length string
) 1)
34 (char= #\
: (char string
0))))
36 (defun symbol-to-js (symbol)
37 (when (symbolp symbol
)
38 (setf symbol
(symbol-name symbol
)))
39 (let ((symbols (string-split symbol
'(#\.
))))
40 (cond ((null symbols
) "")
41 ((= (length symbols
) 1)
46 (cond ((constant-string-p symbol
)
48 symbol
(subseq symbol
1 (1- (length symbol
)))))
49 ((first-uppercase-p symbol
)
51 symbol
(subseq symbol
1)))
52 ((untouchable-string-p symbol
)
54 symbol
(subseq symbol
1))))
58 ((and lowercase
(not all-uppercase
))
63 (dotimes (i (length symbol
))
64 (let ((c (char symbol i
)))
67 (setf lowercase
(not lowercase
)))
68 ((assoc c
*special-chars
*)
69 (dolist (i (coerce (cdr (assoc c
*special-chars
*)) 'list
))
72 (coerce (nreverse res
) 'string
)))
73 (t (string-join (mapcar #'symbol-to-js symbols
) ".")))))
77 (defmethod js-equal ((obj1 list
) (obj2 list
))
78 (and (= (length obj1
) (length obj2
))
79 (every #'js-equal obj1 obj2
)))
80 (defmethod js-equal ((obj1 t
) (obj2 t
))
83 (defmacro defjsclass
(name superclasses slots
&rest class-options
)
84 (let ((slot-names (mapcar #'(lambda (slot) (if (atom slot
) slot
(first slot
))) slots
)))
86 (defclass ,name
,superclasses
87 ,slots
,@class-options
)
88 (defmethod js-equal ((obj1 ,name
) (obj2 ,name
))
89 (every #'(lambda (slot)
90 (js-equal (slot-value obj1 slot
)
91 (slot-value obj2 slot
)))
94 (defjsclass statement
()
95 ((value :initarg
:value
:accessor value
:initform nil
)))
97 (defjsclass expression
(statement)
102 (defun special-append-to-last (form elt
)
103 (flet ((special-append (form elt
)
104 (let ((len (length form
)))
106 (member (char form
(1- len
))
109 (concatenate 'string form elt
)))))
110 (cond ((stringp form
)
111 (special-append form elt
))
113 (let ((last (last form
)))
114 (if (stringp (car last
))
115 (rplaca last
(special-append (car last
) elt
))
116 (append-to-last (car last
) elt
))
118 (t (error "unsupported form ~S" form
)))))
120 (defun dwim-join (value-string-lists max-length
125 (white-space (make-string (length start
) :initial-element
#\Space
))
127 (append-to-last #'append-to-last
)
130 (format t
"value-string-lists: ~S~%" value-string-lists
)
132 ;;; collect single value-string-lists until line full
134 (do* ((string-lists value-string-lists
(cdr string-lists
))
135 (string-list (car string-lists
) (car string-lists
))
144 (list (concatenate 'string start end
))
148 (funcall append-to-last
(first res
) end
)))
151 (format t
"string-list: ~S~%" string-list
)
154 (unless (null (cdr string-lists
))
155 (funcall append-to-last string-list join-after
)))
157 (if (and collect
(= (length string-list
) 1))
160 (format t
"cur-elt: ~S line-length ~D, max-length ~D, string: ~S~%"
162 (+ (length (first string-list
))
167 (< (+ (length (first string-list
))
168 (length cur-elt
)) max-length
))
170 (concatenate 'string cur-elt
171 (if (or is-first
(and cur-empty
(string= join-before
"")))
172 "" (concatenate 'string separator join-before
))
177 (setf cur-elt
(concatenate 'string white-space
178 join-before
(first string-list
))
184 (setf cur-elt white-space
186 (setf res
(nconc (nreverse
187 (cons (concatenate 'string
192 (mapcar #'(lambda (x) (concatenate 'string white-space x
))
195 (setf cur-elt white-space cur-empty t
)))))
197 (defmethod js-to-strings ((expression expression
) start-pos
)
198 (declare (ignore start-pos
))
199 (list (princ-to-string (value expression
))))
201 (defmethod js-to-statement-strings ((expression expression
) start-pos
)
202 (js-to-strings expression start-pos
))
204 (defmethod js-to-statement-strings ((statement statement
) start-pos
)
205 (declare (ignore start-pos
))
206 (list (princ-to-string (value statement
))))
210 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
211 (defvar *js-compiler-macros
* (make-hash-table :test
'equal
)
212 "*JS-COMPILER-MACROS* is a hash-table containing the functions corresponding
213 to javascript special forms, indexed by their name. Javascript special
214 forms are compiler macros for JS expressions.")
216 (defun undefine-js-compiler-macro (name)
217 (declare (type symbol name
))
218 (when (gethash (symbol-name name
) *js-compiler-macros
*)
219 (warn "Redefining compiler macro ~S" name
)
220 (remhash (symbol-name name
) *js-compiler-macros
*))))
222 (defmacro define-js-compiler-macro
(name lambda-list
&rest body
)
223 "Define a javascript compiler macro NAME. Arguments are destructured
224 according to LAMBDA-LIST. The resulting JS language types are appended
225 to the ongoing javascript compilation."
226 (let ((js-name (intern (concatenate 'string
"JS-" (symbol-name name
)) #.
*package
*)))
227 `(eval-when (:compile-toplevel
:load-toplevel
:execute
)
228 (defun ,js-name
,lambda-list
,@body
)
229 (setf (gethash ,(symbol-name name
) *js-compiler-macros
*) #',js-name
))))
231 (defun js-compiler-macro-form-p (form)
232 (when (and (symbolp (car form
))
233 (gethash (symbol-name (car form
)) *js-compiler-macros
*))
236 (defun js-get-compiler-macro (name)
238 (gethash (symbol-name name
) *js-compiler-macros
*)))
242 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
243 (defvar *js-macro-toplevel
* (make-hash-table :test
'equal
)
244 "Toplevel of macro expansion, holds all the toplevel javascript macros.")
245 (defvar *js-macro-env
* (list *js-macro-toplevel
*)
246 "Current macro environment."))
248 (defun lookup-macro (name)
249 "Lookup the macro NAME in the current macro expansion
250 environment. Returns the macro and the parent macro environment of
252 (unless (symbolp name
)
253 (return-from lookup-macro nil
))
254 (do ((env *js-macro-env
* (cdr env
)))
256 (let ((val (gethash (symbol-name name
) (car env
))))
258 (return-from lookup-macro
259 (values val
(or (cdr env
)
260 (list *js-macro-toplevel
*))))))))
262 (defmacro defjsmacro
(name args
&rest body
)
263 "Define a javascript macro, and store it in the toplevel macro environment."
264 (let ((lambda-list (gensym)))
265 (undefine-js-compiler-macro name
)
266 `(setf (gethash ,(symbol-name name
) *js-macro-toplevel
*)
267 #'(lambda (&rest
,lambda-list
)
268 (destructuring-bind ,args
,lambda-list
,@body
)))))
270 (defun import-macros-from-lisp (&rest names
)
271 "Import the named lisp macros into the js macro expansion"
274 (undefine-js-compiler-macro name
)
275 (setf (gethash (symbol-name name
) *js-macro-toplevel
*)
277 (macroexpand `(,name
,@args
)))))))
279 (defun js-expand-form (expr)
280 "Expand a javascript form."
282 (multiple-value-bind (js-macro macro-env
)
285 (js-expand-form (let ((*js-macro-env
* macro-env
))
289 ((js-compiler-macro-form-p expr
) expr
)
291 ((equal (first expr
) 'quote
) expr
)
293 (t (let ((js-macro (lookup-macro (car expr
))))
295 (js-expand-form (apply js-macro
(cdr expr
)))
298 (defvar *gen-js-name-counter
* 0)
300 (defun gen-js-name-string (&key
(prefix "_ps_"))
301 "Generates a unique valid javascript identifier ()"
303 prefix
(princ-to-string (incf *gen-js-name-counter
*))))
305 (defun gen-js-name (&key
(prefix "_ps_"))
306 "Generate a new javascript identifier."
307 (intern (gen-js-name-string :prefix prefix
)
310 (defmacro with-unique-js-names
(symbols &body body
)
311 "Evaluate BODY with the variables on SYMBOLS bound to new javascript identifiers.
313 Each element of SYMBOLS is either a symbol or a list of (symbol
315 `(let* ,(mapcar (lambda (symbol)
316 (destructuring-bind (symbol &optional prefix
)
321 `(,symbol
(gen-js-name :prefix
,prefix
))
322 `(,symbol
(gen-js-name)))))
326 (defjsmacro rebind
(variables expression
)
327 ;; Creates a new js lexical environment and copies the given
328 ;; variable(s) there. Executes the body in the new environment. This
329 ;; has the same effect as a new (let () ...) form in lisp but works on
330 ;; the js side for js closures."
332 (unless (listp variables
)
333 (setf variables
(list variables
)))
335 (let ((new-context (new *object
)))
336 ,@(loop for variable in variables
337 do
(setf variable
(symbol-to-js variable
))
338 collect
`(setf (slot-value new-context
,variable
) (slot-value this
,variable
)))
340 (return ,expression
))))))
342 (defvar *var-counter
* 0)
344 (defun js-gensym (&optional
(name "js"))
345 (intern (format nil
"tmp-~A-~A" name
(incf *var-counter
*)) #.
*package
*))
349 (defmacro defjsliteral
(name string
)
350 "Define a Javascript literal that will expand to STRING."
351 `(define-js-compiler-macro ,name
() (make-instance 'expression
:value
,string
)))
353 (defjsliteral this
"this")
354 (defjsliteral t
"true")
355 (defjsliteral nil
"null")
356 (defjsliteral false
"false")
357 (defjsliteral undefined
"undefined")
359 (defmacro defjskeyword
(name string
)
360 "Define a Javascript keyword that will expand to STRING."
361 `(define-js-compiler-macro ,name
() (make-instance 'statement
:value
,string
)))
363 (defjskeyword break
"break")
364 (defjskeyword continue
"continue")
368 (defjsclass array-literal
(expression)
369 ((values :initarg
:values
:accessor array-values
)))
371 (define-js-compiler-macro array
(&rest values
)
372 (make-instance 'array-literal
373 :values
(mapcar #'js-compile-to-expression values
)))
375 (defjsmacro list
(&rest values
)
378 (defmethod js-to-strings ((array array-literal
) start-pos
)
379 (let ((value-string-lists
380 (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
381 (array-values array
)))
382 (max-length (- 80 start-pos
2)))
383 (dwim-join value-string-lists max-length
384 :start
"[ " :end
" ]"
387 (defjsclass js-aref
(expression)
388 ((array :initarg
:array
389 :accessor aref-array
)
390 (index :initarg
:index
391 :accessor aref-index
)))
393 (define-js-compiler-macro aref
(array &rest coords
)
394 (make-instance 'js-aref
395 :array
(js-compile-to-expression array
)
396 :index
(mapcar #'js-compile-to-expression coords
)))
398 (defmethod js-to-strings ((aref js-aref
) start-pos
)
399 (dwim-join (cons (js-to-strings (aref-array aref
) start-pos
)
400 (mapcar #'(lambda (x) (dwim-join (list (js-to-strings x
(+ start-pos
2)))
402 :start
"[" :end
"]"))
404 (- 80 start-pos
2) :separator
""
407 (defjsmacro make-array
(&rest inits
)
408 `(new (*array
,@inits
)))
410 ;;; object literals (maps and hash-tables)
412 (defjsclass object-literal
(expression)
413 ((values :initarg
:values
:accessor object-values
)))
415 (define-js-compiler-macro {} (&rest values
)
416 (make-instance 'object-literal
418 for
(key value
) on values by
#'cddr
419 collect
(cons key
(js-compile-to-expression value
)))))
421 (defmethod js-to-strings ((obj object-literal
) start-pos
)
423 for
(key . value
) in
(object-values obj
)
425 (dwim-join (list (list (format nil
"~A:" (symbol-to-js key
)))
426 (js-to-strings value
(+ start-pos
2)))
428 :start
"" :end
"" :join-after
"")))
430 :start
"{ " :end
" }"
435 (defjsclass string-literal
(expression)
438 (defvar *js-quote-char
* #\'
439 "Specifies which character JS sholud use for delimiting strings.
441 This variable is usefull when have to embed some javascript code
442 in an html attribute delimited by #\\\" as opposed to #\\', or
445 (defmethod js-to-strings ((string string-literal
) start-pos
)
446 (declare (ignore start-pos
)
447 (inline lisp-special-char-to-js
))
448 (list (with-output-to-string (escaped)
449 (write-char *js-quote-char
* escaped
)
451 for char across
(value string
)
452 for code
= (char-code char
)
453 for special
= (lisp-special-char-to-js char
)
457 (write-char #\\ escaped
)
458 (write-char special escaped
))
459 ((or (<= code
#x1f
) (>= code
#x80
))
460 (format escaped
"\\u~4,'0x" code
))
461 (t (write-char char escaped
)))
462 finally
(write-char *js-quote-char
* escaped
)))))
464 (defparameter *js-lisp-escaped-chars
*
468 (#\f .
#.
(code-char 12))
473 (defun lisp-special-char-to-js(lisp-char)
474 (car (rassoc lisp-char
*js-lisp-escaped-chars
*)))
478 (defjsclass number-literal
(expression)
483 (defjsclass js-variable
(expression)
486 (defmethod js-to-strings ((v js-variable
) start-form
)
487 (declare (ignore start-form
))
488 (list (symbol-to-js (value v
))))
490 ;;; arithmetic operators
492 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
494 (defparameter *op-precedence-hash
* (make-hash-table))
496 (defparameter *op-precedences
*
513 (setf *= /= %
= += -
= <<= >>= >>>= \
&= ^
= \|
=)
516 ;;; generate the operator precedences from *OP-PRECEDENCES*
517 (let ((precedence 1))
518 (dolist (ops *op-precedences
*)
520 (setf (gethash op
*op-precedence-hash
*) precedence
))
523 (defun js-convert-op-name (op)
532 (defjsclass op-form
(expression)
533 ((operator :initarg
:operator
:accessor operator
)
534 (args :initarg
:args
:accessor op-args
)))
536 (defun op-form-p (form)
538 (not (js-compiler-macro-form-p form
))
539 (not (null (gethash (first form
) *op-precedence-hash
*)))))
541 (defun klammer (string-list)
542 (prepend-to-first string-list
"(")
543 (append-to-last string-list
")")
546 (defmethod expression-precedence ((expression expression
))
549 (defmethod expression-precedence ((form op-form
))
550 (gethash (operator form
) *op-precedence-hash
*))
552 (defmethod js-to-strings ((form op-form
) start-pos
)
553 (let* ((precedence (expression-precedence form
))
555 (mapcar #'(lambda (x)
556 (let ((string-list (js-to-strings x
(+ start-pos
2))))
557 (if (>= (expression-precedence x
) precedence
)
558 (klammer string-list
)
561 (max-length (- 80 start-pos
2))
562 (op-string (format nil
"~A " (operator form
))))
563 (dwim-join value-string-lists max-length
:join-before op-string
)
566 (defjsmacro 1-
(form)
569 (defjsmacro 1+ (form)
572 (defjsclass one-op
(expression)
573 ((pre-p :initarg
:pre-p
575 :accessor one-op-pre-p
)
579 (defmethod js-to-strings ((one-op one-op
) start-pos
)
580 (let* ((value (value one-op
))
581 (value-strings (js-to-strings value start-pos
)))
582 (when (typep value
'op-form
)
583 (setf value-strings
(klammer value-strings
)))
584 (if (one-op-pre-p one-op
)
585 (prepend-to-first value-strings
587 (append-to-last value-strings
590 (define-js-compiler-macro incf
(x)
591 (make-instance 'one-op
:pre-p t
:op
"++"
592 :value
(js-compile-to-expression x
)))
593 (define-js-compiler-macro ++ (x)
594 (make-instance 'one-op
:pre-p nil
:op
"++"
595 :value
(js-compile-to-expression x
)))
596 (define-js-compiler-macro decf
(x)
597 (make-instance 'one-op
:pre-p t
:op
"--"
598 :value
(js-compile-to-expression x
)))
599 (define-js-compiler-macro --
(x)
600 (make-instance 'one-op
:pre-p nil
:op
"--"
601 :value
(js-compile-to-expression x
)))
604 (define-js-compiler-macro not
(x)
605 (let ((value (js-compile-to-expression x
)))
606 (if (and (typep value
'op-form
)
607 (= (length (op-args value
)) 2))
608 (let ((new-op (case (operator value
)
619 (make-instance 'op-form
:operator new-op
620 :args
(op-args value
))
621 (make-instance 'one-op
:pre-p t
:op
"!"
623 (make-instance 'one-op
:pre-p t
:op
"!"
628 (defjsclass function-call
(expression)
629 ((function :initarg
:function
:accessor f-function
)
630 (args :initarg
:args
:accessor f-args
)))
632 (defun funcall-form-p (form)
634 (not (op-form-p form
))
635 (not (js-compiler-macro-form-p form
))))
637 (defmethod js-to-strings ((form function-call
) start-pos
)
638 (let* ((value-string-lists
639 (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
641 (max-length (- 80 start-pos
2))
642 (args (dwim-join value-string-lists max-length
643 :start
"(" :end
")" :join-after
",")))
644 (etypecase (f-function form
)
646 (dwim-join (list (append (dwim-join (list (js-to-strings (f-function form
) (+ start-pos
2)))
648 :start
"(" :end
")" :separator
"")
652 ((or js-variable js-aref js-slot-value
)
653 (dwim-join (list (js-to-strings (f-function form
) (+ start-pos
2))
658 (defjsclass method-call
(expression)
659 ((method :initarg
:method
:accessor m-method
)
660 (object :initarg
:object
:accessor m-object
)
661 (args :initarg
:args
:accessor m-args
)))
663 (defmethod js-to-strings ((form method-call
) start-pos
)
664 (let ((fname (dwim-join (list (js-to-strings (m-object form
) (+ start-pos
2))
665 (list (symbol-to-js (m-method form
))))
669 (let ((butlast (butlast fname
))
670 (last (car (last fname
))))
672 (dwim-join (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
679 (defun method-call-p (form)
680 (and (funcall-form-p form
)
681 (symbolp (first form
))
682 (eql (char (symbol-name (first form
)) 0) #\.
)))
686 (defjsclass js-body
(expression)
687 ((stmts :initarg
:stmts
:accessor b-stmts
)
688 (indent :initarg
:indent
:initform
"" :accessor b-indent
)))
690 (define-js-compiler-macro progn
(&rest body
)
691 (make-instance 'js-body
692 :stmts
(mapcar #'js-compile-to-statement body
)))
694 (defmethod initialize-instance :after
((body js-body
) &rest initargs
)
695 (declare (ignore initargs
))
696 (let* ((stmts (b-stmts body
))
698 (last-stmt (car last
)))
699 (when (typep last-stmt
'js-body
)
701 (nconc (butlast stmts
)
702 (b-stmts last-stmt
))))))
705 (defmethod js-to-statement-strings ((body js-body
) start-pos
)
706 (dwim-join (mapcar #'(lambda (x) (js-to-statement-strings x
(+ start-pos
2)))
710 :append-to-last
#'special-append-to-last
711 :start
(b-indent body
) :collect nil
714 (defmethod js-to-strings ((body js-body
) start-pos
)
715 (dwim-join (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
718 :append-to-last
#'special-append-to-last
720 :start
(b-indent body
)))
722 (defjsclass js-sub-body
(js-body)
725 (defmethod js-to-statement-strings ((body js-sub-body
) start-pos
)
726 (declare (ignore start-pos
))
727 (nconc (list "{") (call-next-method) (list "}")))
729 (defmethod expression-precedence ((body js-body
))
730 (if (= (length (b-stmts body
)) 1)
731 (expression-precedence (first (b-stmts body
)))
732 (gethash 'comma
*op-precedence-hash
*)))
734 ;;; function definition
736 (defjsclass js-lambda
(expression)
737 ((args :initarg
:args
:accessor lambda-args
)
738 (body :initarg
:body
:accessor lambda-body
)))
740 (define-js-compiler-macro lambda
(args &rest body
)
741 (make-instance 'js-lambda
742 :args
(mapcar #'js-compile-to-symbol args
)
743 :body
(make-instance 'js-body
745 :stmts
(mapcar #'js-compile-to-statement body
))))
747 (defmethod js-to-strings ((lambda js-lambda
) start-pos
)
748 (let ((fun-header (dwim-join (mapcar #'(lambda (x)
749 (list (symbol-to-js x
)))
750 (lambda-args lambda
))
752 :start
(function-start-string lambda
)
753 :end
") {" :join-after
","))
754 (fun-body (js-to-statement-strings (lambda-body lambda
) (+ start-pos
2))))
755 (nconc fun-header fun-body
(list "}"))))
757 (defmethod function-start-string ((lambda js-lambda
))
760 (defmethod js-to-statement-strings ((lambda js-lambda
) start-pos
)
761 (js-to-strings lambda start-pos
))
763 (defjsclass js-defun
(js-lambda)
764 ((name :initarg
:name
:accessor defun-name
)))
766 (define-js-compiler-macro defun
(name args
&rest body
)
767 (make-instance 'js-defun
768 :name
(js-compile-to-symbol name
)
769 :args
(mapcar #'js-compile-to-symbol args
)
770 :body
(make-instance 'js-body
772 :stmts
(mapcar #'js-compile-to-statement body
))))
774 (defmethod function-start-string ((defun js-defun))
775 (format nil
"function ~A(" (symbol-to-js (defun-name defun
))))
779 (defjsclass js-object
(expression)
780 ((slots :initarg
:slots
783 (define-js-compiler-macro create
(&rest args
)
784 (make-instance 'js-object
785 :slots
(loop for
(name val
) on args by
#'cddr
786 collect
(list (js-compile-to-symbol name
)
787 (js-compile-to-expression val
)))))
789 (defmethod js-to-strings ((object js-object
) start-pos
)
790 (let ((value-string-lists
791 (mapcar #'(lambda (slot)
792 (dwim-join (list (js-to-strings (second slot
) (+ start-pos
4)))
794 :start
(concatenate 'string
(symbol-to-js (first slot
)) " : ")
795 :white-space
" ")) (o-slots object
)))
796 (max-length (- 80 start-pos
2)))
797 (dwim-join value-string-lists max-length
804 (defjsclass js-slot-value
(expression)
805 ((object :initarg
:object
810 (define-js-compiler-macro slot-value
(obj slot
)
811 (make-instance 'js-slot-value
:object
(js-compile-to-expression obj
)
812 :slot
(js-compile slot
)))
814 (defmethod js-to-strings ((sv js-slot-value
) start-pos
)
815 (append-to-last (js-to-strings (sv-object sv
) start-pos
)
816 (if (symbolp (sv-slot sv
))
817 (format nil
".~A" (symbol-to-js (sv-slot sv
)))
818 (format nil
"[~A]" (first (js-to-strings (sv-slot sv
) 0))))))
820 (defjsmacro with-slots
(slots object
&rest body
)
821 `(symbol-macrolet ,(mapcar #'(lambda (slot)
822 `(,slot
'(slot-value ,object
',slot
)))
828 (define-js-compiler-macro macrolet
(macros &rest body
)
829 (let* ((macro-env (make-hash-table :test
'equal
))
830 (*js-macro-env
* (cons macro-env
*js-macro-env
*)))
831 (dolist (macro macros
)
832 (destructuring-bind (name arglist
&rest body
) macro
833 (setf (gethash (symbol-name name
) macro-env
)
834 (compile nil
`(lambda ,arglist
,@body
)))))
835 (js-compile `(progn ,@body
))))
837 (defjsmacro symbol-macrolet
(macros &rest body
)
838 `(macrolet ,(mapcar #'(lambda (macro)
839 `(,(first macro
) () ,@(rest macro
))) macros
)
844 (defjsmacro lisp
(&rest forms
)
845 (eval (cons 'progn forms
)))
849 (defjsclass js-if
(expression)
850 ((test :initarg
:test
857 (define-js-compiler-macro if
(test then
&optional else
)
858 (make-instance 'js-if
:test
(js-compile-to-expression test
)
859 :then
(js-compile-to-body then
:indent
" ")
861 (js-compile-to-body else
:indent
" "))))
863 (defmethod initialize-instance :after
((if js-if
) &rest initargs
)
864 (declare (ignore initargs
))
865 (when (and (if-then if
)
866 (typep (if-then if
) 'js-sub-body
))
867 (change-class (if-then if
) 'js-body
))
868 (when (and (if-else if
)
869 (typep (if-else if
) 'js-sub-body
))
870 (change-class (if-else if
) 'js-body
)))
872 (defmethod js-to-statement-strings ((if js-if
) start-pos
)
873 (let ((if-strings (dwim-join (list (js-to-strings (if-test if
) 0))
877 (then-strings (js-to-statement-strings (if-then if
) (+ start-pos
2)))
878 (else-strings (when (if-else if
)
879 (js-to-statement-strings (if-else if
)
881 (nconc if-strings then-strings
(if else-strings
882 (nconc (list "} else {") else-strings
(list "}"))
885 (defmethod expression-precedence ((if js-if
))
886 (gethash 'if
*op-precedence-hash
*))
888 (defmethod js-to-strings ((if js-if
) start-pos
)
889 (assert (typep (if-then if
) 'expression
))
891 (assert (typep (if-else if
) 'expression
)))
892 (dwim-join (list (append-to-last (js-to-strings (if-test if
) start-pos
) " ?")
893 (let* ((new-then (make-instance 'js-body
894 :stmts
(b-stmts (if-then if
))
896 (res (js-to-strings new-then start-pos
)))
897 (if (>= (expression-precedence (if-then if
))
898 (expression-precedence if
))
903 (let* ((new-else (make-instance 'js-body
904 :stmts
(b-stmts (if-else if
))
906 (res (js-to-strings new-else start-pos
)))
907 (if (>= (expression-precedence (if-else if
))
908 (expression-precedence if
))
915 (defjsmacro when
(test &rest body
)
916 `(if ,test
(progn ,@body
)))
918 (defjsmacro unless
(test &rest body
)
919 `(if (not ,test
) (progn ,@body
)))
921 ;;; single keyword expressions and statements
923 (defmacro define-js-single-op
(name &optional
(superclass 'expression
))
924 (let ((js-name (intern (concatenate 'string
"JS-" (symbol-name name
)) #.
*package
*)))
926 (defjsclass ,js-name
(,superclass
)
928 (define-js-compiler-macro ,name
(value)
929 (make-instance ',js-name
:value
(js-compile-to-expression value
)))
930 (defmethod ,(if (eql superclass
'expression
)
932 'js-to-statement-strings
) ((,name
,js-name
) start-pos
)
933 (dwim-join (list (js-to-strings (value ,name
) (+ start-pos
2)))
935 :start
,(concatenate 'string
(string-downcase (symbol-name name
)) " ")
936 :white-space
" ")))))
939 (define-js-single-op return statement
)
940 (define-js-single-op throw statement
)
941 (define-js-single-op delete
)
942 (define-js-single-op void
)
943 (define-js-single-op typeof
)
944 (define-js-single-op new
)
946 ;; TODO this may not be the best integrated implementation of
947 ;; instanceof into the rest of the code
948 (defjsclass js-instanceof
(expression)
950 (type :initarg
:type
)))
952 (define-js-compiler-macro instanceof
(value type
)
953 (make-instance 'js-instanceof
954 :value
(js-compile-to-expression value
)
955 :type
(js-compile-to-expression type
)))
957 (defmethod js-to-strings ((instanceof js-instanceof
) start-pos
)
959 (list (js-to-strings (value instanceof
) (+ start-pos
2))
961 (js-to-strings (slot-value instanceof
'type
) (+ start-pos
2)))
968 (defjsclass js-setf
(expression)
969 ((lhs :initarg
:lhs
:accessor setf-lhs
)
970 (rhsides :initarg
:rhsides
:accessor setf-rhsides
)))
972 (defun assignment-op (op)
988 (defun make-js-test (lhs rhs
)
989 (if (and (typep rhs
'op-form
)
990 (member lhs
(op-args rhs
) :test
#'js-equal
))
991 (let ((args-without (remove lhs
(op-args rhs
)
992 :count
1 :test
#'js-equal
))
993 (args-without-first (remove lhs
(op-args rhs
)
996 (one (list (make-instance 'number-literal
:value
1))))
998 (format t
"OPERATOR: ~S, ARGS-WITHOUT: ~S, ARGS-WITHOUT-FIRST ~S~%"
1002 (cond ((and (js-equal args-without one
)
1003 (eql (operator rhs
) '+))
1004 (make-instance 'one-op
:pre-p nil
:op
"++"
1006 ((and (js-equal args-without-first one
)
1007 (eql (operator rhs
) '-
))
1008 (make-instance 'one-op
:pre-p nil
:op
"--"
1010 ((and (assignment-op (operator rhs
))
1011 (member (operator rhs
)
1013 (make-instance 'op-form
1014 :operator
(assignment-op (operator rhs
))
1015 :args
(list lhs
(make-instance 'op-form
1016 :operator
(operator rhs
)
1017 :args args-without
))))
1018 ((and (assignment-op (operator rhs
))
1019 (js-equal (first (op-args rhs
)) lhs
))
1020 (make-instance 'op-form
1021 :operator
(assignment-op (operator rhs
))
1022 :args
(list lhs
(make-instance 'op-form
1023 :operator
(operator rhs
)
1024 :args
(cdr (op-args rhs
))))))
1025 (t (make-instance 'js-setf
:lhs lhs
:rhsides
(list rhs
)))))
1026 (make-instance 'js-setf
:lhs lhs
:rhsides
(list rhs
))))
1028 (define-js-compiler-macro setf
(&rest args
)
1029 (let ((assignments (loop for
(lhs rhs
) on args by
#'cddr
1030 for rexpr
= (js-compile-to-expression rhs
)
1031 for lexpr
= (js-compile-to-expression lhs
)
1032 collect
(make-js-test lexpr rexpr
))))
1033 (if (= (length assignments
) 1)
1035 (make-instance 'js-body
:indent
"" :stmts assignments
))))
1037 (defmethod js-to-strings ((setf js-setf
) start-pos
)
1038 (dwim-join (cons (js-to-strings (setf-lhs setf
) start-pos
)
1039 (mapcar #'(lambda (x) (js-to-strings x start-pos
)) (setf-rhsides setf
)))
1043 (defmethod expression-precedence ((setf js-setf
))
1044 (gethash '= *op-precedence-hash
*))
1048 (defjsclass js-defvar
(statement)
1049 ((names :initarg
:names
:accessor var-names
)
1050 (value :initarg
:value
:accessor var-value
)))
1052 (define-js-compiler-macro defvar
(name &optional value
)
1053 (make-instance 'js-defvar
:names
(list (js-compile-to-symbol name
))
1054 :value
(when value
(js-compile-to-expression value
))))
1056 (defmethod js-to-statement-strings ((defvar js-defvar
) start-pos
)
1057 (dwim-join (nconc (mapcar #'(lambda (x) (list (symbol-to-js x
))) (var-names defvar
))
1058 (when (var-value defvar
)
1059 (list (js-to-strings (var-value defvar
) start-pos
))))
1062 :start
"var " :end
";"))
1066 (define-js-compiler-macro let
(decls &rest body
)
1067 (let ((single-defvar (make-instance 'js-defvar
1068 :names
(mapcar #'js-compile-to-symbol
1069 (remove-if-not #'atom decls
))
1071 (defvars (mapcar #'(lambda (decl)
1072 (let ((name (first decl
))
1073 (value (second decl
)))
1074 (make-instance 'js-defvar
1075 :names
(list (js-compile-to-symbol name
))
1076 :value
(js-compile-to-expression value
))))
1077 (remove-if #'atom decls
))))
1078 (make-instance 'js-sub-body
1080 :stmts
(nconc (when (var-names single-defvar
) (list single-defvar
))
1082 (mapcar #'js-compile-to-statement body
)))))
1086 (defjsclass js-for
(statement)
1087 ((vars :initarg
:vars
:accessor for-vars
)
1088 (steps :initarg
:steps
:accessor for-steps
)
1089 (check :initarg
:check
:accessor for-check
)
1090 (body :initarg
:body
:accessor for-body
)))
1092 (defun make-for-vars (decls)
1093 (loop for decl in decls
1094 for var
= (if (atom decl
) decl
(first decl
))
1095 for init
= (if (atom decl
) nil
(second decl
))
1096 collect
(make-instance 'js-defvar
:names
(list (js-compile-to-symbol var
))
1097 :value
(js-compile-to-expression init
))))
1099 (defun make-for-steps (decls)
1100 (loop for decl in decls
1101 when
(= (length decl
) 3)
1102 collect
(js-compile-to-expression (third decl
))))
1104 (define-js-compiler-macro do
(decls termination
&rest body
)
1105 (let ((vars (make-for-vars decls
))
1106 (steps (make-for-steps decls
))
1107 (check (js-compile-to-expression (list 'not
(first termination
))))
1108 (body (js-compile-to-body (cons 'progn body
) :indent
" ")))
1109 (make-instance 'js-for
1115 (defjsmacro dotimes
(iter &rest body
)
1116 (let ((var (first iter
))
1117 (times (second iter
)))
1118 `(do ((,var
0 (1+ ,var
)))
1122 (defjsmacro dolist
(i-array &rest body
)
1123 (let ((var (first i-array
))
1124 (array (second i-array
))
1125 (arrvar (js-gensym "arr"))
1126 (idx (js-gensym "i")))
1127 `(let ((,arrvar
,array
))
1128 (do ((,idx
0 (1+ ,idx
)))
1129 ((>= ,idx
(slot-value ,arrvar
'length
)))
1130 (let ((,var
(aref ,arrvar
,idx
)))
1133 (defmethod js-to-statement-strings ((for js-for
) start-pos
)
1134 (let* ((init (dwim-join (mapcar #'(lambda (x)
1135 (dwim-join (list (list (symbol-to-js (first (var-names x
))))
1136 (js-to-strings (var-value x
)
1142 :start
"var " :join-after
","))
1143 (check (js-to-strings (for-check for
) (+ start-pos
2)))
1144 (steps (dwim-join (mapcar #'(lambda (x var
)
1146 (list (list (symbol-to-js (first (var-names var
))))
1147 (js-to-strings x
(- start-pos
2)))
1154 (header (dwim-join (list init check steps
)
1156 :start
"for (" :end
") {"
1158 (body (js-to-statement-strings (for-body for
) (+ start-pos
2))))
1159 (nconc header body
(list "}"))))
1161 (defjsclass for-each
(statement)
1162 ((name :initarg
:name
:accessor fe-name
)
1163 (value :initarg
:value
:accessor fe-value
)
1164 (body :initarg
:body
:accessor fe-body
)))
1166 (define-js-compiler-macro doeach
(decl &rest body
)
1167 (make-instance 'for-each
:name
(js-compile-to-symbol (first decl
))
1168 :value
(js-compile-to-expression (second decl
))
1169 :body
(js-compile-to-body (cons 'progn body
) :indent
" ")))
1171 (defmethod js-to-statement-strings ((fe for-each
) start-pos
)
1172 (let ((header (dwim-join (list (list (symbol-to-js (fe-name fe
)))
1174 (js-to-strings (fe-value fe
) (+ start-pos
2)))
1178 (body (js-to-statement-strings (fe-body fe
) (+ start-pos
2))))
1179 (nconc header body
(list "}"))))
1181 (defjsclass js-while
(statement)
1182 ((check :initarg
:check
:accessor while-check
)
1183 (body :initarg
:body
:accessor while-body
)))
1185 (define-js-compiler-macro while
(check &rest body
)
1186 (make-instance 'js-while
1187 :check
(js-compile-to-expression check
)
1188 :body
(js-compile-to-body (cons 'progn body
) :indent
" ")))
1190 (defmethod js-to-statement-strings ((while js-while
) start-pos
)
1191 (let ((header (dwim-join (list (js-to-strings (while-check while
) (+ start-pos
2)))
1195 (body (js-to-statement-strings (while-body while
) (+ start-pos
2))))
1196 (nconc header body
(list "}"))))
1200 (defjsclass js-with
(statement)
1201 ((obj :initarg
:obj
:accessor with-obj
)
1202 (body :initarg
:body
:accessor with-body
)))
1204 (define-js-compiler-macro with
(statement &rest body
)
1205 (make-instance 'js-with
1206 :obj
(js-compile-to-expression (first statement
))
1207 :body
(js-compile-to-body (cons 'progn body
) :indent
" ")))
1209 (defmethod js-to-statement-strings ((with js-with
) start-pos
)
1210 (nconc (dwim-join (list (js-to-strings (with-obj with
) (+ start-pos
2)))
1212 :start
"with (" :end
") {")
1213 (js-to-statement-strings (with-body with
) (+ start-pos
2))
1218 (defjsclass js-switch
(statement)
1219 ((value :initarg
:value
:accessor case-value
)
1220 (clauses :initarg
:clauses
:accessor case-clauses
)))
1222 (define-js-compiler-macro switch
(value &rest clauses
)
1223 (let ((clauses (mapcar #'(lambda (clause)
1224 (let ((val (first clause
))
1225 (body (cdr clause
)))
1226 (list (if (eql val
'default
)
1228 (js-compile-to-expression val
))
1229 (js-compile-to-body (cons 'progn body
) :indent
" "))))
1231 (check (js-compile-to-expression value
)))
1232 (make-instance 'js-switch
:value check
1235 (defmethod js-to-statement-strings ((case js-switch
) start-pos
)
1236 (let ((body (mapcan #'(lambda (clause)
1237 (let ((val (car clause
))
1238 (body (second clause
)))
1239 (dwim-join (list (if (eql val
'default
)
1241 (js-to-strings val
(+ start-pos
2)))
1242 (js-to-statement-strings body
(+ start-pos
2)))
1244 :start
(if (eql val
'default
) " default" " case ")
1246 :join-after
":"))) (case-clauses case
))))
1249 (format t
"body: ~S~%" body
)
1250 (nconc (dwim-join (list (js-to-strings (case-value case
) (+ start-pos
2)))
1252 :start
"switch (" :end
") {")
1256 (defjsmacro case
(value &rest clauses
)
1257 (labels ((make-clause (val body more
)
1259 (append (mapcar #'list
(butlast val
))
1260 (make-clause (first (last val
)) body more
)))
1261 ((member val
'(t otherwise
))
1262 (make-clause 'default body more
))
1263 (more `((,val
,@body break
)))
1264 (t `((,val
,@body
))))))
1265 `(switch ,value
,@(mapcon #'(lambda (x)
1266 (make-clause (car (first x
))
1273 (defjsclass js-try
(statement)
1274 ((body :initarg
:body
:accessor try-body
)
1275 (catch :initarg
:catch
:accessor try-catch
)
1276 (finally :initarg
:finally
:accessor try-finally
)))
1278 (define-js-compiler-macro try
(body &rest clauses
)
1279 (let ((body (js-compile-to-body body
:indent
" "))
1280 (catch (cdr (assoc :catch clauses
)))
1281 (finally (cdr (assoc :finally clauses
))))
1282 (make-instance 'js-try
1284 :catch
(when catch
(list (js-compile-to-symbol (caar catch
))
1285 (js-compile-to-body (cons 'progn
(cdr catch
))
1287 :finally
(when finally
(js-compile-to-body (cons 'progn finally
)
1290 (defmethod js-to-statement-strings ((try js-try
) start-pos
)
1291 (let* ((catch (try-catch try
))
1292 (finally (try-finally try
))
1293 (catch-list (when catch
1295 (dwim-join (list (list (symbol-to-js (first catch
))))
1299 (js-to-statement-strings (second catch
) (+ start-pos
2)))))
1300 (finally-list (when finally
1301 (nconc (list "} finally {")
1302 (js-to-statement-strings finally
(+ start-pos
2))))))
1303 (nconc (list "try {")
1304 (js-to-statement-strings (try-body try
) (+ start-pos
2))
1311 (defjsclass regex
(expression)
1314 (define-js-compiler-macro regex
(regex)
1315 (make-instance 'regex
:value
(string regex
)))
1317 (defun first-slash-p (string)
1318 (and (> (length string
) 0)
1319 (eq (char string
0) '#\
/)))
1321 (defmethod js-to-strings ((regex regex
) start-pos
)
1322 (declare (ignore start-pos
))
1323 (let ((slash (if (first-slash-p (value regex
)) nil
"/")))
1324 (list (format nil
(concatenate 'string slash
"~A" slash
) (value regex
)))))
1326 ;;; conditional compilation
1328 (defjsclass cc-if
()
1329 ((test :initarg
:test
:accessor cc-if-test
)
1330 (body :initarg
:body
:accessor cc-if-body
)))
1332 (defmethod js-to-statement-strings ((cc cc-if
) start-pos
)
1333 (nconc (list (format nil
"/*@if ~A" (cc-if-test cc
)))
1334 (mapcan #'(lambda (x) (js-to-strings x start-pos
)) (cc-if-body cc
))
1337 (define-js-compiler-macro cc-if
(test &rest body
)
1338 (make-instance 'cc-if
:test test
1339 :body
(mapcar #'js-compile body
)))
1343 (defun js-compile (form)
1344 (setf form
(js-expand-form form
))
1345 (cond ((stringp form
)
1346 (make-instance 'string-literal
:value form
))
1348 (make-instance 'string-literal
:value
(string form
)))
1350 (make-instance 'number-literal
:value form
))
1352 (let ((c-macro (js-get-compiler-macro form
)))
1355 (make-instance 'js-variable
:value form
))))
1357 (eql (first form
) 'quote
))
1360 (js-compile-list form
))
1361 (t (error "Unknown atomar expression ~S" form
))))
1363 (defun js-compile-list (form)
1364 (let* ((name (car form
))
1366 (js-form (js-get-compiler-macro name
)))
1368 (apply js-form args
))
1371 (make-instance 'op-form
1372 :operator
(js-convert-op-name (js-compile-to-symbol (first form
)))
1373 :args
(mapcar #'js-compile-to-expression
(rest form
))))
1375 ((method-call-p form
)
1376 (make-instance 'method-call
1377 :method
(js-compile-to-symbol (first form
))
1378 :object
(js-compile-to-expression (second form
))
1379 :args
(mapcar #'js-compile-to-expression
(cddr form
))))
1381 ((funcall-form-p form
)
1382 (make-instance 'function-call
1383 :function
(js-compile-to-expression (first form
))
1384 :args
(mapcar #'js-compile-to-expression
(rest form
))))
1386 (t (error "Unknown form ~S" form
)))))
1388 (defun js-compile-to-expression (form)
1389 (let ((res (js-compile form
)))
1390 (assert (typep res
'expression
))
1393 (defun js-compile-to-symbol (form)
1394 (let ((res (js-compile form
)))
1395 (when (typep res
'js-variable
)
1396 (setf res
(value res
)))
1397 (assert (symbolp res
))
1400 (defun js-compile-to-statement (form)
1401 (let ((res (js-compile form
)))
1402 (assert (typep res
'statement
))
1405 (defun js-compile-to-body (form &key
(indent ""))
1406 (let ((res (js-compile-to-statement form
)))
1407 (if (typep res
'js-body
)
1408 (progn (setf (b-indent res
) indent
)
1410 (make-instance 'js-body
1412 :stmts
(list res
)))))
1416 (defjsmacro floor
(expr)
1417 `(*Math.floor
,expr
))
1419 (defjsmacro random
()
1424 (define-js-compiler-macro js
(&rest body
)
1425 (make-instance 'string-literal
1426 :value
(string-join (js-to-statement-strings
1427 (js-compile (cons 'progn body
)) 0) " ")))
1429 (define-js-compiler-macro js-inline
(&rest body
)
1430 (make-instance 'string-literal
1434 (string-join (js-to-statement-strings
1435 (js-compile (cons 'progn body
)) 0) " "))))
1438 (defmacro js
(&rest body
)
1439 `(js* '(progn ,@body
)))
1441 (defmacro js
* (&rest body
)
1442 "Return the javascript string representing BODY.
1446 (js-to-statement-strings (js-compile (list 'progn
,@body
)) 0)
1447 (string #\Newline
)))
1449 (defun js-to-string (expr)
1451 (js-to-statement-strings (js-compile expr
) 0)
1452 (string #\Newline
)))
1454 (defun js-to-line (expr)
1456 (js-to-statement-strings (js-compile expr
) 0) " "))
1458 (defmacro js-file
(&rest body
)
1463 (defmacro js-script
(&rest body
)
1464 `((:script
:type
"text/javascript")
1465 (:princ
(format nil
"~%// <![CDATA[~%"))
1466 (:princ
(js ,@body
))
1467 (:princ
(format nil
"~%// ]]>~%"))))
1469 (defmacro js-inline
(&rest body
)
1470 `(js-inline* '(progn ,@body
)))
1472 (defmacro js-inline
* (&rest body
)
1473 "Just like JS-INLINE except that BODY is evaluated before being
1474 converted to javascript."
1475 `(concatenate 'string
"javascript:"
1476 (string-join (js-to-statement-strings (js-compile (list 'progn
,@body
)) 0) " ")))