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 (string= (char form
(1- len
)) elt
))
108 (concatenate 'string form elt
)))))
109 (cond ((stringp form
)
110 (special-append form elt
))
112 (let ((last (last form
)))
113 (if (stringp (car last
))
114 (rplaca last
(special-append (car last
) elt
))
115 (append-to-last (car last
) elt
))
117 (t (error "unsupported form ~S" form
)))))
119 (defun dwim-join (value-string-lists max-length
124 (white-space (make-string (length start
) :initial-element
#\Space
))
126 (append-to-last #'append-to-last
)
129 (format t
"value-string-lists: ~S~%" value-string-lists
)
131 ;;; collect single value-string-lists until line full
133 (do* ((string-lists value-string-lists
(cdr string-lists
))
134 (string-list (car string-lists
) (car string-lists
))
143 (list (concatenate 'string start end
))
147 (funcall append-to-last
(first res
) end
)))
150 (format t
"string-list: ~S~%" string-list
)
153 (unless (null (cdr string-lists
))
154 (funcall append-to-last string-list join-after
)))
156 (if (and collect
(= (length string-list
) 1))
159 (format t
"cur-elt: ~S line-length ~D, max-length ~D, string: ~S~%"
161 (+ (length (first string-list
))
166 (< (+ (length (first string-list
))
167 (length cur-elt
)) max-length
))
169 (concatenate 'string cur-elt
170 (if (or is-first
(and cur-empty
(string= join-before
"")))
171 "" (concatenate 'string separator join-before
))
176 (setf cur-elt
(concatenate 'string white-space
177 join-before
(first string-list
))
183 (setf cur-elt white-space
185 (setf res
(nconc (nreverse
186 (cons (concatenate 'string
191 (mapcar #'(lambda (x) (concatenate 'string white-space x
))
194 (setf cur-elt white-space cur-empty t
)))))
196 (defmethod js-to-strings ((expression expression
) start-pos
)
197 (declare (ignore start-pos
))
198 (list (princ-to-string (value expression
))))
200 (defmethod js-to-statement-strings ((expression expression
) start-pos
)
201 (js-to-strings expression start-pos
))
203 (defmethod js-to-statement-strings ((statement statement
) start-pos
)
204 (declare (ignore start-pos
))
205 (list (princ-to-string (value statement
))))
209 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
210 (defvar *js-compiler-macros
* (make-hash-table :test
'equal
)
211 "*JS-COMPILER-MACROS* is a hash-table containing the functions corresponding
212 to javascript special forms, indexed by their name. Javascript special
213 forms are compiler macros for JS expressions.")
215 (defun undefine-js-compiler-macro (name)
216 (declare (type symbol name
))
217 (when (gethash (symbol-name name
) *js-compiler-macros
*)
218 (warn "Redefining compiler macro ~S" name
)
219 (remhash (symbol-name name
) *js-compiler-macros
*))))
221 (defmacro define-js-compiler-macro
(name lambda-list
&rest body
)
222 "Define a javascript compiler macro NAME. Arguments are destructured
223 according to LAMBDA-LIST. The resulting JS language types are appended
224 to the ongoing javascript compilation."
225 (let ((js-name (intern (concatenate 'string
"JS-" (symbol-name name
)) #.
*package
*)))
226 `(eval-when (:compile-toplevel
:load-toplevel
:execute
)
227 (defun ,js-name
,lambda-list
,@body
)
228 (setf (gethash ,(symbol-name name
) *js-compiler-macros
*) #',js-name
))))
230 (defun js-compiler-macro-form-p (form)
231 (when (and (symbolp (car form
))
232 (gethash (symbol-name (car form
)) *js-compiler-macros
*))
235 (defun js-get-compiler-macro (name)
237 (gethash (symbol-name name
) *js-compiler-macros
*)))
241 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
242 (defvar *js-macro-toplevel
* (make-hash-table :test
'equal
)
243 "Toplevel of macro expansion, holds all the toplevel javascript macros.")
244 (defvar *js-macro-env
* (list *js-macro-toplevel
*)
245 "Current macro environment."))
247 (defun lookup-macro (name)
248 "Lookup the macro NAME in the current macro expansion
249 environment. Returns the macro and the parent macro environment of
251 (unless (symbolp name
)
252 (return-from lookup-macro nil
))
253 (do ((env *js-macro-env
* (cdr env
)))
255 (let ((val (gethash (symbol-name name
) (car env
))))
257 (return-from lookup-macro
258 (values val
(or (cdr env
)
259 (list *js-macro-toplevel
*))))))))
261 (defmacro defjsmacro
(name args
&rest body
)
262 "Define a javascript macro, and store it in the toplevel macro environment."
263 (let ((lambda-list (gensym)))
264 (undefine-js-compiler-macro name
)
265 `(setf (gethash ,(symbol-name name
) *js-macro-toplevel
*)
266 #'(lambda (&rest
,lambda-list
)
267 (destructuring-bind ,args
,lambda-list
,@body
)))))
269 (defun import-macros-from-lisp (&rest names
)
270 "Import the named lisp macros into the js macro expansion"
273 (undefine-js-compiler-macro name
)
274 (setf (gethash (symbol-name name
) *js-macro-toplevel
*)
276 (macroexpand `(,name
,@args
)))))))
278 (defun js-expand-form (expr)
279 "Expand a javascript form."
281 (multiple-value-bind (js-macro macro-env
)
284 (js-expand-form (let ((*js-macro-env
* macro-env
))
288 ((js-compiler-macro-form-p expr
) expr
)
290 ((equal (first expr
) 'quote
) expr
)
292 (t (let ((js-macro (lookup-macro (car expr
))))
294 (js-expand-form (apply js-macro
(cdr expr
)))
297 (defvar *gen-js-name-counter
* 0)
299 (defun gen-js-name-string (&key
(prefix "_ps_"))
300 "Generates a unique valid javascript identifier ()"
302 prefix
(princ-to-string (incf *gen-js-name-counter
*))))
304 (defun gen-js-name (&key
(prefix "_ps_"))
305 "Generate a new javascript identifier."
306 (intern (gen-js-name-string :prefix prefix
)
309 (defmacro with-unique-js-names
(symbols &body body
)
310 "Evaluate BODY with the variables on SYMBOLS bound to new javascript identifiers.
312 Each element of SYMBOLS is either a symbol or a list of (symbol
314 `(let* ,(mapcar (lambda (symbol)
315 (destructuring-bind (symbol &optional prefix
)
320 `(,symbol
(gen-js-name :prefix
,prefix
))
321 `(,symbol
(gen-js-name)))))
325 (defjsmacro rebind
(variables expression
)
326 ;; Creates a new js lexical environment and copies the given
327 ;; variable(s) there. Executes the body in the new environment. This
328 ;; has the same effect as a new (let () ...) form in lisp but works on
329 ;; the js side for js closures."
331 (unless (listp variables
)
332 (setf variables
(list variables
)))
334 (let ((new-context (new *object
)))
335 ,@(loop for variable in variables
336 do
(setf variable
(symbol-to-js variable
))
337 collect
`(setf (slot-value new-context
,variable
) (slot-value this
,variable
)))
339 (return ,expression
))))))
341 (defvar *var-counter
* 0)
343 (defun js-gensym (&optional
(name "js"))
344 (intern (format nil
"tmp-~A-~A" name
(incf *var-counter
*)) #.
*package
*))
348 (defmacro defjsliteral
(name string
)
349 "Define a Javascript literal that will expand to STRING."
350 `(define-js-compiler-macro ,name
() (make-instance 'expression
:value
,string
)))
352 (defjsliteral this
"this")
353 (defjsliteral t
"true")
354 (defjsliteral nil
"null")
355 (defjsliteral false
"false")
356 (defjsliteral undefined
"undefined")
358 (defmacro defjskeyword
(name string
)
359 "Define a Javascript keyword that will expand to STRING."
360 `(define-js-compiler-macro ,name
() (make-instance 'statement
:value
,string
)))
362 (defjskeyword break
"break")
363 (defjskeyword continue
"continue")
367 (defjsclass array-literal
(expression)
368 ((values :initarg
:values
:accessor array-values
)))
370 (define-js-compiler-macro array
(&rest values
)
371 (make-instance 'array-literal
372 :values
(mapcar #'js-compile-to-expression values
)))
374 (defjsmacro list
(&rest values
)
377 (defmethod js-to-strings ((array array-literal
) start-pos
)
378 (let ((value-string-lists
379 (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
380 (array-values array
)))
381 (max-length (- 80 start-pos
2)))
382 (dwim-join value-string-lists max-length
383 :start
"[ " :end
" ]"
386 (defjsclass js-aref
(expression)
387 ((array :initarg
:array
388 :accessor aref-array
)
389 (index :initarg
:index
390 :accessor aref-index
)))
392 (define-js-compiler-macro aref
(array &rest coords
)
393 (make-instance 'js-aref
394 :array
(js-compile-to-expression array
)
395 :index
(mapcar #'js-compile-to-expression coords
)))
397 (defmethod js-to-strings ((aref js-aref
) start-pos
)
398 (dwim-join (cons (js-to-strings (aref-array aref
) start-pos
)
399 (mapcar #'(lambda (x) (dwim-join (list (js-to-strings x
(+ start-pos
2)))
401 :start
"[" :end
"]"))
403 (- 80 start-pos
2) :separator
""
406 (defjsmacro make-array
(&rest inits
)
407 `(new (*array
,@inits
)))
409 ;;; object literals (maps and hash-tables)
411 (defjsclass object-literal
(expression)
412 ((values :initarg
:values
:accessor object-values
)))
414 (define-js-compiler-macro {} (&rest values
)
415 (make-instance 'object-literal
417 for
(key value
) on values by
#'cddr
418 collect
(cons key
(js-compile-to-expression value
)))))
420 (defmethod js-to-strings ((obj object-literal
) start-pos
)
422 for
(key . value
) in
(object-values obj
)
424 (dwim-join (list (list (format nil
"~A:" (symbol-to-js key
)))
425 (js-to-strings value
(+ start-pos
2)))
427 :start
"" :end
"" :join-after
"")))
429 :start
"{ " :end
" }"
434 (defjsclass string-literal
(expression)
437 (defvar *js-quote-char
* #\'
438 "Specifies which character JS sholud use for delimiting strings.
440 This variable is usefull when have to embed some javascript code
441 in an html attribute delimited by #\\\" as opposed to #\\', or
444 (defmethod js-to-strings ((string string-literal
) start-pos
)
445 (declare (ignore start-pos
)
446 (inline lisp-special-char-to-js
))
447 (list (with-output-to-string (escaped)
448 (write-char *js-quote-char
* escaped
)
450 for char across
(value string
)
451 for code
= (char-code char
)
452 for special
= (lisp-special-char-to-js char
)
456 (write-char #\\ escaped
)
457 (write-char special escaped
))
458 ((or (<= code
#x1f
) (>= code
#x80
))
459 (format escaped
"\\u~4,'0x" code
))
460 (t (write-char char escaped
)))
461 finally
(write-char *js-quote-char
* escaped
)))))
463 (defparameter *js-lisp-escaped-chars
*
467 (#\f .
#.
(code-char 12))
472 (defun lisp-special-char-to-js(lisp-char)
473 (car (rassoc lisp-char
*js-lisp-escaped-chars
*)))
477 (defjsclass number-literal
(expression)
482 (defjsclass js-variable
(expression)
485 (defmethod js-to-strings ((v js-variable
) start-form
)
486 (declare (ignore start-form
))
487 (list (symbol-to-js (value v
))))
489 ;;; arithmetic operators
491 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
493 (defparameter *op-precedence-hash
* (make-hash-table))
495 (defparameter *op-precedences
*
512 (setf *= /= %
= += -
= <<= >>= >>>= \
&= ^
= \|
=)
515 ;;; generate the operator precedences from *OP-PRECEDENCES*
516 (let ((precedence 1))
517 (dolist (ops *op-precedences
*)
519 (setf (gethash op
*op-precedence-hash
*) precedence
))
522 (defun js-convert-op-name (op)
531 (defjsclass op-form
(expression)
532 ((operator :initarg
:operator
:accessor operator
)
533 (args :initarg
:args
:accessor op-args
)))
535 (defun op-form-p (form)
537 (not (js-compiler-macro-form-p form
))
538 (not (null (gethash (first form
) *op-precedence-hash
*)))))
540 (defun klammer (string-list)
541 (prepend-to-first string-list
"(")
542 (append-to-last string-list
")")
545 (defmethod expression-precedence ((expression expression
))
548 (defmethod expression-precedence ((form op-form
))
549 (gethash (operator form
) *op-precedence-hash
*))
551 (defmethod js-to-strings ((form op-form
) start-pos
)
552 (let* ((precedence (expression-precedence form
))
554 (mapcar #'(lambda (x)
555 (let ((string-list (js-to-strings x
(+ start-pos
2))))
556 (if (>= (expression-precedence x
) precedence
)
557 (klammer string-list
)
560 (max-length (- 80 start-pos
2))
561 (op-string (format nil
"~A " (operator form
))))
562 (dwim-join value-string-lists max-length
:join-before op-string
)
565 (defjsmacro 1-
(form)
568 (defjsmacro 1+ (form)
571 (defjsclass one-op
(expression)
572 ((pre-p :initarg
:pre-p
574 :accessor one-op-pre-p
)
578 (defmethod js-to-strings ((one-op one-op
) start-pos
)
579 (let* ((value (value one-op
))
580 (value-strings (js-to-strings value start-pos
)))
581 (when (typep value
'op-form
)
582 (setf value-strings
(klammer value-strings
)))
583 (if (one-op-pre-p one-op
)
584 (prepend-to-first value-strings
586 (append-to-last value-strings
589 (define-js-compiler-macro incf
(x)
590 (make-instance 'one-op
:pre-p t
:op
"++"
591 :value
(js-compile-to-expression x
)))
592 (define-js-compiler-macro ++ (x)
593 (make-instance 'one-op
:pre-p nil
:op
"++"
594 :value
(js-compile-to-expression x
)))
595 (define-js-compiler-macro decf
(x)
596 (make-instance 'one-op
:pre-p t
:op
"--"
597 :value
(js-compile-to-expression x
)))
598 (define-js-compiler-macro --
(x)
599 (make-instance 'one-op
:pre-p nil
:op
"--"
600 :value
(js-compile-to-expression x
)))
603 (define-js-compiler-macro not
(x)
604 (let ((value (js-compile-to-expression x
)))
605 (if (and (typep value
'op-form
)
606 (= (length (op-args value
)) 2))
607 (let ((new-op (case (operator value
)
618 (make-instance 'op-form
:operator new-op
619 :args
(op-args value
))
620 (make-instance 'one-op
:pre-p t
:op
"!"
622 (make-instance 'one-op
:pre-p t
:op
"!"
627 (defjsclass function-call
(expression)
628 ((function :initarg
:function
:accessor f-function
)
629 (args :initarg
:args
:accessor f-args
)))
631 (defun funcall-form-p (form)
633 (not (op-form-p form
))
634 (not (js-compiler-macro-form-p form
))))
636 (defmethod js-to-strings ((form function-call
) start-pos
)
637 (let* ((value-string-lists
638 (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
640 (max-length (- 80 start-pos
2))
641 (args (dwim-join value-string-lists max-length
642 :start
"(" :end
")" :join-after
",")))
643 (etypecase (f-function form
)
645 (dwim-join (list (append (dwim-join (list (js-to-strings (f-function form
) (+ start-pos
2)))
647 :start
"(" :end
")" :separator
"")
651 ((or js-variable js-aref js-slot-value
)
652 (dwim-join (list (js-to-strings (f-function form
) (+ start-pos
2))
657 (defjsclass method-call
(expression)
658 ((method :initarg
:method
:accessor m-method
)
659 (object :initarg
:object
:accessor m-object
)
660 (args :initarg
:args
:accessor m-args
)))
662 (defmethod js-to-strings ((form method-call
) start-pos
)
663 (let ((fname (dwim-join (list (js-to-strings (m-object form
) (+ start-pos
2))
664 (list (symbol-to-js (m-method form
))))
668 (let ((butlast (butlast fname
))
669 (last (car (last fname
))))
671 (dwim-join (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
678 (defun method-call-p (form)
679 (and (funcall-form-p form
)
680 (symbolp (first form
))
681 (eql (char (symbol-name (first form
)) 0) #\.
)))
685 (defjsclass js-body
(expression)
686 ((stmts :initarg
:stmts
:accessor b-stmts
)
687 (indent :initarg
:indent
:initform
"" :accessor b-indent
)))
689 (define-js-compiler-macro progn
(&rest body
)
690 (make-instance 'js-body
691 :stmts
(mapcar #'js-compile-to-statement body
)))
693 (defmethod initialize-instance :after
((body js-body
) &rest initargs
)
694 (declare (ignore initargs
))
695 (let* ((stmts (b-stmts body
))
697 (last-stmt (car last
)))
698 (when (typep last-stmt
'js-body
)
700 (nconc (butlast stmts
)
701 (b-stmts last-stmt
))))))
704 (defmethod js-to-statement-strings ((body js-body
) start-pos
)
705 (dwim-join (mapcar #'(lambda (x) (js-to-statement-strings x
(+ start-pos
2)))
709 :append-to-last
#'special-append-to-last
710 :start
(b-indent body
) :collect nil
713 (defmethod js-to-strings ((body js-body
) start-pos
)
714 (dwim-join (mapcar #'(lambda (x) (js-to-strings x
(+ start-pos
2)))
717 :append-to-last
#'special-append-to-last
719 :start
(b-indent body
)))
721 (defjsclass js-sub-body
(js-body)
724 (defmethod js-to-statement-strings ((body js-sub-body
) start-pos
)
725 (declare (ignore start-pos
))
726 (nconc (list "{") (call-next-method) (list "}")))
728 (defmethod expression-precedence ((body js-body
))
729 (if (= (length (b-stmts body
)) 1)
730 (expression-precedence (first (b-stmts body
)))
731 (gethash 'comma
*op-precedence-hash
*)))
733 ;;; function definition
735 (defjsclass js-lambda
(expression)
736 ((args :initarg
:args
:accessor lambda-args
)
737 (body :initarg
:body
:accessor lambda-body
)))
739 (define-js-compiler-macro lambda
(args &rest body
)
740 (make-instance 'js-lambda
741 :args
(mapcar #'js-compile-to-symbol args
)
742 :body
(make-instance 'js-body
744 :stmts
(mapcar #'js-compile-to-statement body
))))
746 (defmethod js-to-strings ((lambda js-lambda
) start-pos
)
747 (let ((fun-header (dwim-join (mapcar #'(lambda (x)
748 (list (symbol-to-js x
)))
749 (lambda-args lambda
))
751 :start
(function-start-string lambda
)
752 :end
") {" :join-after
","))
753 (fun-body (js-to-statement-strings (lambda-body lambda
) (+ start-pos
2))))
754 (nconc fun-header fun-body
(list "}"))))
756 (defmethod function-start-string ((lambda js-lambda
))
759 (defmethod js-to-statement-strings ((lambda js-lambda
) start-pos
)
760 (js-to-strings lambda start-pos
))
762 (defjsclass js-defun
(js-lambda)
763 ((name :initarg
:name
:accessor defun-name
)))
765 (define-js-compiler-macro defun
(name args
&rest body
)
766 (make-instance 'js-defun
767 :name
(js-compile-to-symbol name
)
768 :args
(mapcar #'js-compile-to-symbol args
)
769 :body
(make-instance 'js-body
771 :stmts
(mapcar #'js-compile-to-statement body
))))
773 (defmethod function-start-string ((defun js-defun))
774 (format nil
"function ~A(" (symbol-to-js (defun-name defun
))))
778 (defjsclass js-object
(expression)
779 ((slots :initarg
:slots
782 (define-js-compiler-macro create
(&rest args
)
783 (make-instance 'js-object
784 :slots
(loop for
(name val
) on args by
#'cddr
785 collect
(list (js-compile-to-symbol name
)
786 (js-compile-to-expression val
)))))
788 (defmethod js-to-strings ((object js-object
) start-pos
)
789 (let ((value-string-lists
790 (mapcar #'(lambda (slot)
791 (dwim-join (list (js-to-strings (second slot
) (+ start-pos
4)))
793 :start
(concatenate 'string
(symbol-to-js (first slot
)) " : ")
794 :white-space
" ")) (o-slots object
)))
795 (max-length (- 80 start-pos
2)))
796 (dwim-join value-string-lists max-length
803 (defjsclass js-slot-value
(expression)
804 ((object :initarg
:object
809 (define-js-compiler-macro slot-value
(obj slot
)
810 (make-instance 'js-slot-value
:object
(js-compile-to-expression obj
)
811 :slot
(js-compile slot
)))
813 (defmethod js-to-strings ((sv js-slot-value
) start-pos
)
814 (append-to-last (js-to-strings (sv-object sv
) start-pos
)
815 (if (symbolp (sv-slot sv
))
816 (format nil
".~A" (symbol-to-js (sv-slot sv
)))
817 (format nil
"[~A]" (first (js-to-strings (sv-slot sv
) 0))))))
819 (defjsmacro with-slots
(slots object
&rest body
)
820 `(symbol-macrolet ,(mapcar #'(lambda (slot)
821 `(,slot
'(slot-value ,object
',slot
)))
827 (define-js-compiler-macro macrolet
(macros &rest body
)
828 (let* ((macro-env (make-hash-table :test
'equal
))
829 (*js-macro-env
* (cons macro-env
*js-macro-env
*)))
830 (dolist (macro macros
)
831 (destructuring-bind (name arglist
&rest body
) macro
832 (setf (gethash (symbol-name name
) macro-env
)
833 (compile nil
`(lambda ,arglist
,@body
)))))
834 (js-compile `(progn ,@body
))))
836 (defjsmacro symbol-macrolet
(macros &rest body
)
837 `(macrolet ,(mapcar #'(lambda (macro)
838 `(,(first macro
) () ,@(rest macro
))) macros
)
843 (defjsmacro lisp
(&rest forms
)
844 (eval (cons 'progn forms
)))
848 (defjsclass js-if
(expression)
849 ((test :initarg
:test
856 (define-js-compiler-macro if
(test then
&optional else
)
857 (make-instance 'js-if
:test
(js-compile-to-expression test
)
858 :then
(js-compile-to-body then
:indent
" ")
860 (js-compile-to-body else
:indent
" "))))
862 (defmethod initialize-instance :after
((if js-if
) &rest initargs
)
863 (declare (ignore initargs
))
864 (when (and (if-then if
)
865 (typep (if-then if
) 'js-sub-body
))
866 (change-class (if-then if
) 'js-body
))
867 (when (and (if-else if
)
868 (typep (if-else if
) 'js-sub-body
))
869 (change-class (if-else if
) 'js-body
)))
871 (defmethod js-to-statement-strings ((if js-if
) start-pos
)
872 (let ((if-strings (dwim-join (list (js-to-strings (if-test if
) 0))
876 (then-strings (js-to-statement-strings (if-then if
) (+ start-pos
2)))
877 (else-strings (when (if-else if
)
878 (js-to-statement-strings (if-else if
)
880 (nconc if-strings then-strings
(if else-strings
881 (nconc (list "} else {") else-strings
(list "}"))
884 (defmethod expression-precedence ((if js-if
))
885 (gethash 'if
*op-precedence-hash
*))
887 (defmethod js-to-strings ((if js-if
) start-pos
)
888 (assert (typep (if-then if
) 'expression
))
890 (assert (typep (if-else if
) 'expression
)))
891 (dwim-join (list (append-to-last (js-to-strings (if-test if
) start-pos
) " ?")
892 (let* ((new-then (make-instance 'js-body
893 :stmts
(b-stmts (if-then if
))
895 (res (js-to-strings new-then start-pos
)))
896 (if (>= (expression-precedence (if-then if
))
897 (expression-precedence if
))
902 (let* ((new-else (make-instance 'js-body
903 :stmts
(b-stmts (if-else if
))
905 (res (js-to-strings new-else start-pos
)))
906 (if (>= (expression-precedence (if-else if
))
907 (expression-precedence if
))
914 (defjsmacro when
(test &rest body
)
915 `(if ,test
(progn ,@body
)))
917 (defjsmacro unless
(test &rest body
)
918 `(if (not ,test
) (progn ,@body
)))
920 ;;; single keyword expressions and statements
922 (defmacro define-js-single-op
(name &optional
(superclass 'expression
))
923 (let ((js-name (intern (concatenate 'string
"JS-" (symbol-name name
)) #.
*package
*)))
925 (defjsclass ,js-name
(,superclass
)
927 (define-js-compiler-macro ,name
(value)
928 (make-instance ',js-name
:value
(js-compile-to-expression value
)))
929 (defmethod ,(if (eql superclass
'expression
)
931 'js-to-statement-strings
) ((,name
,js-name
) start-pos
)
932 (dwim-join (list (js-to-strings (value ,name
) (+ start-pos
2)))
934 :start
,(concatenate 'string
(string-downcase (symbol-name name
)) " ")
935 :white-space
" ")))))
938 (define-js-single-op return statement
)
939 (define-js-single-op throw statement
)
940 (define-js-single-op delete
)
941 (define-js-single-op void
)
942 (define-js-single-op typeof
)
943 (define-js-single-op new
)
945 ;; TODO this may not be the best integrated implementation of
946 ;; instanceof into the rest of the code
947 (defjsclass js-instanceof
(expression)
949 (type :initarg
:type
)))
951 (define-js-compiler-macro instanceof
(value type
)
952 (make-instance 'js-instanceof
953 :value
(js-compile-to-expression value
)
954 :type
(js-compile-to-expression type
)))
956 (defmethod js-to-strings ((instanceof js-instanceof
) start-pos
)
958 (list (js-to-strings (value instanceof
) (+ start-pos
2))
960 (js-to-strings (slot-value instanceof
'type
) (+ start-pos
2)))
967 (defjsclass js-setf
(expression)
968 ((lhs :initarg
:lhs
:accessor setf-lhs
)
969 (rhsides :initarg
:rhsides
:accessor setf-rhsides
)))
971 (defun assignment-op (op)
987 (defun make-js-test (lhs rhs
)
988 (if (and (typep rhs
'op-form
)
989 (member lhs
(op-args rhs
) :test
#'js-equal
))
990 (let ((args-without (remove lhs
(op-args rhs
)
991 :count
1 :test
#'js-equal
))
992 (args-without-first (remove lhs
(op-args rhs
)
995 (one (list (make-instance 'number-literal
:value
1))))
997 (format t
"OPERATOR: ~S, ARGS-WITHOUT: ~S, ARGS-WITHOUT-FIRST ~S~%"
1001 (cond ((and (js-equal args-without one
)
1002 (eql (operator rhs
) '+))
1003 (make-instance 'one-op
:pre-p nil
:op
"++"
1005 ((and (js-equal args-without-first one
)
1006 (eql (operator rhs
) '-
))
1007 (make-instance 'one-op
:pre-p nil
:op
"--"
1009 ((and (assignment-op (operator rhs
))
1010 (member (operator rhs
)
1012 (make-instance 'op-form
1013 :operator
(assignment-op (operator rhs
))
1014 :args
(list lhs
(make-instance 'op-form
1015 :operator
(operator rhs
)
1016 :args args-without
))))
1017 ((and (assignment-op (operator rhs
))
1018 (js-equal (first (op-args rhs
)) lhs
))
1019 (make-instance 'op-form
1020 :operator
(assignment-op (operator rhs
))
1021 :args
(list lhs
(make-instance 'op-form
1022 :operator
(operator rhs
)
1023 :args
(cdr (op-args rhs
))))))
1024 (t (make-instance 'js-setf
:lhs lhs
:rhsides
(list rhs
)))))
1025 (make-instance 'js-setf
:lhs lhs
:rhsides
(list rhs
))))
1027 (define-js-compiler-macro setf
(&rest args
)
1028 (let ((assignments (loop for
(lhs rhs
) on args by
#'cddr
1029 for rexpr
= (js-compile-to-expression rhs
)
1030 for lexpr
= (js-compile-to-expression lhs
)
1031 collect
(make-js-test lexpr rexpr
))))
1032 (if (= (length assignments
) 1)
1034 (make-instance 'js-body
:indent
"" :stmts assignments
))))
1036 (defmethod js-to-strings ((setf js-setf
) start-pos
)
1037 (dwim-join (cons (js-to-strings (setf-lhs setf
) start-pos
)
1038 (mapcar #'(lambda (x) (js-to-strings x start-pos
)) (setf-rhsides setf
)))
1042 (defmethod expression-precedence ((setf js-setf
))
1043 (gethash '= *op-precedence-hash
*))
1047 (defjsclass js-defvar
(statement)
1048 ((names :initarg
:names
:accessor var-names
)
1049 (value :initarg
:value
:accessor var-value
)))
1051 (define-js-compiler-macro defvar
(name &optional value
)
1052 (make-instance 'js-defvar
:names
(list (js-compile-to-symbol name
))
1053 :value
(when value
(js-compile-to-expression value
))))
1055 (defmethod js-to-statement-strings ((defvar js-defvar
) start-pos
)
1056 (dwim-join (nconc (mapcar #'(lambda (x) (list (symbol-to-js x
))) (var-names defvar
))
1057 (when (var-value defvar
)
1058 (list (js-to-strings (var-value defvar
) start-pos
))))
1061 :start
"var " :end
";"))
1065 (define-js-compiler-macro let
(decls &rest body
)
1066 (let ((single-defvar (make-instance 'js-defvar
1067 :names
(mapcar #'js-compile-to-symbol
1068 (remove-if-not #'atom decls
))
1070 (defvars (mapcar #'(lambda (decl)
1071 (let ((name (first decl
))
1072 (value (second decl
)))
1073 (make-instance 'js-defvar
1074 :names
(list (js-compile-to-symbol name
))
1075 :value
(js-compile-to-expression value
))))
1076 (remove-if #'atom decls
))))
1077 (make-instance 'js-sub-body
1079 :stmts
(nconc (when (var-names single-defvar
) (list single-defvar
))
1081 (mapcar #'js-compile-to-statement body
)))))
1085 (defjsclass js-for
(statement)
1086 ((vars :initarg
:vars
:accessor for-vars
)
1087 (steps :initarg
:steps
:accessor for-steps
)
1088 (check :initarg
:check
:accessor for-check
)
1089 (body :initarg
:body
:accessor for-body
)))
1091 (defun make-for-vars (decls)
1092 (loop for decl in decls
1093 for var
= (if (atom decl
) decl
(first decl
))
1094 for init
= (if (atom decl
) nil
(second decl
))
1095 collect
(make-instance 'js-defvar
:names
(list (js-compile-to-symbol var
))
1096 :value
(js-compile-to-expression init
))))
1098 (defun make-for-steps (decls)
1099 (loop for decl in decls
1100 when
(= (length decl
) 3)
1101 collect
(js-compile-to-expression (third decl
))))
1103 (define-js-compiler-macro do
(decls termination
&rest body
)
1104 (let ((vars (make-for-vars decls
))
1105 (steps (make-for-steps decls
))
1106 (check (js-compile-to-expression (list 'not
(first termination
))))
1107 (body (js-compile-to-body (cons 'progn body
) :indent
" ")))
1108 (make-instance 'js-for
1114 (defjsmacro dotimes
(iter &rest body
)
1115 (let ((var (first iter
))
1116 (times (second iter
)))
1117 `(do ((,var
0 (1+ ,var
)))
1121 (defjsmacro dolist
(i-array &rest body
)
1122 (let ((var (first i-array
))
1123 (array (second i-array
))
1124 (arrvar (js-gensym "arr"))
1125 (idx (js-gensym "i")))
1126 `(let ((,arrvar
,array
))
1127 (do ((,idx
0 (1+ ,idx
)))
1128 ((>= ,idx
(slot-value ,arrvar
'length
)))
1129 (let ((,var
(aref ,arrvar
,idx
)))
1132 (defmethod js-to-statement-strings ((for js-for
) start-pos
)
1133 (let* ((init (dwim-join (mapcar #'(lambda (x)
1134 (dwim-join (list (list (symbol-to-js (first (var-names x
))))
1135 (js-to-strings (var-value x
)
1141 :start
"var " :join-after
","))
1142 (check (js-to-strings (for-check for
) (+ start-pos
2)))
1143 (steps (dwim-join (mapcar #'(lambda (x var
)
1145 (list (list (symbol-to-js (first (var-names var
))))
1146 (js-to-strings x
(- start-pos
2)))
1153 (header (dwim-join (list init check steps
)
1155 :start
"for (" :end
") {"
1157 (body (js-to-statement-strings (for-body for
) (+ start-pos
2))))
1158 (nconc header body
(list "}"))))
1160 (defjsclass for-each
(statement)
1161 ((name :initarg
:name
:accessor fe-name
)
1162 (value :initarg
:value
:accessor fe-value
)
1163 (body :initarg
:body
:accessor fe-body
)))
1165 (define-js-compiler-macro doeach
(decl &rest body
)
1166 (make-instance 'for-each
:name
(js-compile-to-symbol (first decl
))
1167 :value
(js-compile-to-expression (second decl
))
1168 :body
(js-compile-to-body (cons 'progn body
) :indent
" ")))
1170 (defmethod js-to-statement-strings ((fe for-each
) start-pos
)
1171 (let ((header (dwim-join (list (list (symbol-to-js (fe-name fe
)))
1173 (js-to-strings (fe-value fe
) (+ start-pos
2)))
1177 (body (js-to-statement-strings (fe-body fe
) (+ start-pos
2))))
1178 (nconc header body
(list "}"))))
1180 (defjsclass js-while
(statement)
1181 ((check :initarg
:check
:accessor while-check
)
1182 (body :initarg
:body
:accessor while-body
)))
1184 (define-js-compiler-macro while
(check &rest body
)
1185 (make-instance 'js-while
1186 :check
(js-compile-to-expression check
)
1187 :body
(js-compile-to-body (cons 'progn body
) :indent
" ")))
1189 (defmethod js-to-statement-strings ((while js-while
) start-pos
)
1190 (let ((header (dwim-join (list (js-to-strings (while-check while
) (+ start-pos
2)))
1194 (body (js-to-statement-strings (while-body while
) (+ start-pos
2))))
1195 (nconc header body
(list "}"))))
1199 (defjsclass js-with
(statement)
1200 ((obj :initarg
:obj
:accessor with-obj
)
1201 (body :initarg
:body
:accessor with-body
)))
1203 (define-js-compiler-macro with
(statement &rest body
)
1204 (make-instance 'js-with
1205 :obj
(js-compile-to-expression statement
)
1206 :body
(js-compile-to-body (cons 'progn body
) :indent
" ")))
1208 (defmethod js-to-statement-strings ((with js-with
) start-pos
)
1209 (nconc (dwim-join (list (js-to-strings (with-obj with
) (+ start-pos
2)))
1211 :start
"with (" :end
") {")
1212 (js-to-statement-strings (with-body with
) (+ start-pos
2))
1217 (defjsclass js-switch
(statement)
1218 ((value :initarg
:value
:accessor case-value
)
1219 (clauses :initarg
:clauses
:accessor case-clauses
)))
1221 (define-js-compiler-macro switch
(value &rest clauses
)
1222 (let ((clauses (mapcar #'(lambda (clause)
1223 (let ((val (first clause
))
1224 (body (cdr clause
)))
1225 (list (if (eql val
'default
)
1227 (js-compile-to-expression val
))
1228 (js-compile-to-body (cons 'progn body
) :indent
" "))))
1230 (check (js-compile-to-expression value
)))
1231 (make-instance 'js-switch
:value check
1234 (defmethod js-to-statement-strings ((case js-switch
) start-pos
)
1235 (let ((body (mapcan #'(lambda (clause)
1236 (let ((val (car clause
))
1237 (body (second clause
)))
1238 (dwim-join (list (if (eql val
'default
)
1240 (js-to-strings val
(+ start-pos
2)))
1241 (js-to-statement-strings body
(+ start-pos
2)))
1243 :start
(if (eql val
'default
) " default" " case ")
1245 :join-after
":"))) (case-clauses case
))))
1248 (format t
"body: ~S~%" body
)
1249 (nconc (dwim-join (list (js-to-strings (case-value case
) (+ start-pos
2)))
1251 :start
"switch (" :end
") {")
1255 (defjsmacro case
(value &rest clauses
)
1256 (labels ((make-clause (val body more
)
1258 (append (mapcar #'list
(butlast val
))
1259 (make-clause (first (last val
)) body more
)))
1260 ((member val
'(t otherwise
))
1261 (make-clause 'default body more
))
1262 (more `((,val
,@body break
)))
1263 (t `((,val
,@body
))))))
1264 `(switch ,value
,@(mapcon #'(lambda (x)
1265 (make-clause (car (first x
))
1272 (defjsclass js-try
(statement)
1273 ((body :initarg
:body
:accessor try-body
)
1274 (catch :initarg
:catch
:accessor try-catch
)
1275 (finally :initarg
:finally
:accessor try-finally
)))
1277 (define-js-compiler-macro try
(body &rest clauses
)
1278 (let ((body (js-compile-to-body body
:indent
" "))
1279 (catch (cdr (assoc :catch clauses
)))
1280 (finally (cdr (assoc :finally clauses
))))
1281 (make-instance 'js-try
1283 :catch
(when catch
(list (js-compile-to-symbol (caar catch
))
1284 (js-compile-to-body (cons 'progn
(cdr catch
))
1286 :finally
(when finally
(js-compile-to-body (cons 'progn finally
)
1289 (defmethod js-to-statement-strings ((try js-try
) start-pos
)
1290 (let* ((catch (try-catch try
))
1291 (finally (try-finally try
))
1292 (catch-list (when catch
1294 (dwim-join (list (list (symbol-to-js (first catch
))))
1298 (js-to-statement-strings (second catch
) (+ start-pos
2)))))
1299 (finally-list (when finally
1300 (nconc (list "} finally {")
1301 (js-to-statement-strings finally
(+ start-pos
2))))))
1302 (nconc (list "try {")
1303 (js-to-statement-strings (try-body try
) (+ start-pos
2))
1310 (defjsclass regex
(expression)
1313 (define-js-compiler-macro regex
(regex)
1314 (make-instance 'regex
:value
(string regex
)))
1316 (defun first-slash-p (string)
1317 (and (> (length string
) 0)
1318 (eq (char string
0) '#\
/)))
1320 (defmethod js-to-strings ((regex regex
) start-pos
)
1321 (declare (ignore start-pos
))
1322 (let ((slash (if (first-slash-p (value regex
)) nil
"/")))
1323 (list (format nil
(concatenate 'string slash
"~A" slash
) (value regex
)))))
1325 ;;; conditional compilation
1327 (defjsclass cc-if
()
1328 ((test :initarg
:test
:accessor cc-if-test
)
1329 (body :initarg
:body
:accessor cc-if-body
)))
1331 (defmethod js-to-statement-strings ((cc cc-if
) start-pos
)
1332 (nconc (list (format nil
"/*@if ~A" (cc-if-test cc
)))
1333 (mapcan #'(lambda (x) (js-to-strings x start-pos
)) (cc-if-body cc
))
1336 (define-js-compiler-macro cc-if
(test &rest body
)
1337 (make-instance 'cc-if
:test test
1338 :body
(mapcar #'js-compile body
)))
1342 (defun js-compile (form)
1343 (setf form
(js-expand-form form
))
1344 (cond ((stringp form
)
1345 (make-instance 'string-literal
:value form
))
1347 (make-instance 'string-literal
:value
(string form
)))
1349 (make-instance 'number-literal
:value form
))
1351 (let ((c-macro (js-get-compiler-macro form
)))
1354 (make-instance 'js-variable
:value form
))))
1356 (eql (first form
) 'quote
))
1359 (js-compile-list form
))
1360 (t (error "Unknown atomar expression ~S" form
))))
1362 (defun js-compile-list (form)
1363 (let* ((name (car form
))
1365 (js-form (js-get-compiler-macro name
)))
1367 (apply js-form args
))
1370 (make-instance 'op-form
1371 :operator
(js-convert-op-name (js-compile-to-symbol (first form
)))
1372 :args
(mapcar #'js-compile-to-expression
(rest form
))))
1374 ((method-call-p form
)
1375 (make-instance 'method-call
1376 :method
(js-compile-to-symbol (first form
))
1377 :object
(js-compile-to-expression (second form
))
1378 :args
(mapcar #'js-compile-to-expression
(cddr form
))))
1380 ((funcall-form-p form
)
1381 (make-instance 'function-call
1382 :function
(js-compile-to-expression (first form
))
1383 :args
(mapcar #'js-compile-to-expression
(rest form
))))
1385 (t (error "Unknown form ~S" form
)))))
1387 (defun js-compile-to-expression (form)
1388 (let ((res (js-compile form
)))
1389 (assert (typep res
'expression
))
1392 (defun js-compile-to-symbol (form)
1393 (let ((res (js-compile form
)))
1394 (when (typep res
'js-variable
)
1395 (setf res
(value res
)))
1396 (assert (symbolp res
))
1399 (defun js-compile-to-statement (form)
1400 (let ((res (js-compile form
)))
1401 (assert (typep res
'statement
))
1404 (defun js-compile-to-body (form &key
(indent ""))
1405 (let ((res (js-compile-to-statement form
)))
1406 (if (typep res
'js-body
)
1407 (progn (setf (b-indent res
) indent
)
1409 (make-instance 'js-body
1411 :stmts
(list res
)))))
1415 (defjsmacro floor
(expr)
1416 `(*Math.floor
,expr
))
1418 (defjsmacro random
()
1423 (define-js-compiler-macro js
(&rest body
)
1424 (make-instance 'string-literal
1425 :value
(string-join (js-to-statement-strings
1426 (js-compile (cons 'progn body
)) 0) " ")))
1428 (define-js-compiler-macro js-inline
(&rest body
)
1429 (make-instance 'string-literal
1433 (string-join (js-to-statement-strings
1434 (js-compile (cons 'progn body
)) 0) " "))))
1437 (defmacro js
(&rest body
)
1438 `(js* '(progn ,@body
)))
1440 (defmacro js
* (&rest body
)
1441 "Return the javascript string representing BODY.
1445 (js-to-statement-strings (js-compile (list 'progn
,@body
)) 0)
1446 (string #\Newline
)))
1448 (defun js-to-string (expr)
1450 (js-to-statement-strings (js-compile expr
) 0)
1451 (string #\Newline
)))
1453 (defun js-to-line (expr)
1455 (js-to-statement-strings (js-compile expr
) 0) " "))
1457 (defmacro js-file
(&rest body
)
1462 (defmacro js-script
(&rest body
)
1463 `((:script
:type
"text/javascript")
1464 (:princ
(format nil
"~%// <![CDATA[~%"))
1465 (:princ
(js ,@body
))
1466 (:princ
(format nil
"~%// ]]>~%"))))
1468 (defmacro js-inline
(&rest body
)
1469 `(js-inline* '(progn ,@body
)))
1471 (defmacro js-inline
* (&rest body
)
1472 "Just like JS-INLINE except that BODY is evaluated before being
1473 converted to javascript."
1474 `(concatenate 'string
"javascript:"
1475 (string-join (js-to-statement-strings (js-compile (list 'progn
,@body
)) 0) " ")))