3 ;; copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;; General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Translate the Lisp code to Javascript. It will compile the special
22 ;;; forms. Some primitive functions are compiled as special forms
23 ;;; too. The respective real functions are defined in the target (see
24 ;;; the beginning of this file) as well as some primitive functions.
26 (defun code (&rest args)
27 (mapconcat (lambda (arg)
30 ((integerp arg) (integer-to-string arg))
31 ((floatp arg) (float-to-string arg))
33 (t (error "Unknown argument `~S'." arg))))
36 ;;; Wrap X with a Javascript code to convert the result from
37 ;;; Javascript generalized booleans to T or NIL.
39 (code "(" x "?" (ls-compile t) ": " (ls-compile nil) ")"))
41 ;;; Concatenate the arguments and wrap them with a self-calling
42 ;;; Javascript anonymous function. It is used to make some Javascript
43 ;;; statements valid expressions and provide a private scope as well.
44 ;;; It could be defined as function, but we could do some
45 ;;; preprocessing in the future.
46 (defmacro js!selfcall (&body body)
47 `(code "(function(){" *newline* (indent ,@body) "})()"))
49 ;;; Like CODE, but prefix each line with four spaces. Two versions
50 ;;; of this function are available, because the Ecmalisp version is
51 ;;; very slow and bootstraping was annoying.
54 (defun indent (&rest string)
55 (let ((input (apply #'code string)))
58 (size (length input)))
59 (when (plusp (length input)) (concatf output " "))
62 (if (and (char= (char input index) #\newline)
64 (not (char= (char input (1+ index)) #\newline)))
65 (concat (string #\newline) " ")
66 (string (char input index)))))
72 (defun indent (&rest string)
73 (with-output-to-string (*standard-output*)
74 (with-input-from-string (input (apply #'code string))
76 for line = (read-line input nil)
79 do (write-line line)))))
82 ;;; A Form can return a multiple values object calling VALUES, like
83 ;;; values(arg1, arg2, ...). It will work in any context, as well as
84 ;;; returning an individual object. However, if the special variable
85 ;;; `*multiple-value-p*' is NIL, is granted that only the primary
86 ;;; value will be used, so we can optimize to avoid the VALUES
88 (defvar *multiple-value-p* nil)
104 (defun lookup-in-lexenv (name lexenv namespace)
105 (find name (ecase namespace
106 (variable (lexenv-variable lexenv))
107 (function (lexenv-function lexenv))
108 (block (lexenv-block lexenv))
109 (gotag (lexenv-gotag lexenv)))
110 :key #'binding-name))
112 (defun push-to-lexenv (binding lexenv namespace)
114 (variable (push binding (lexenv-variable lexenv)))
115 (function (push binding (lexenv-function lexenv)))
116 (block (push binding (lexenv-block lexenv)))
117 (gotag (push binding (lexenv-gotag lexenv)))))
119 (defun extend-lexenv (bindings lexenv namespace)
120 (let ((env (copy-lexenv lexenv)))
121 (dolist (binding (reverse bindings) env)
122 (push-to-lexenv binding env namespace))))
125 (defvar *environment* (make-lexenv))
127 (defvar *variable-counter* 0)
129 (defun gvarname (symbol)
130 (declare (ignore symbol))
131 (code "v" (incf *variable-counter*)))
133 (defun translate-variable (symbol)
134 (awhen (lookup-in-lexenv symbol *environment* 'variable)
137 (defun extend-local-env (args)
138 (let ((new (copy-lexenv *environment*)))
139 (dolist (symbol args new)
140 (let ((b (make-binding :name symbol :type 'variable :value (gvarname symbol))))
141 (push-to-lexenv b new 'variable)))))
143 ;;; Toplevel compilations
144 (defvar *toplevel-compilations* nil)
146 (defun toplevel-compilation (string)
147 (push string *toplevel-compilations*))
149 (defun null-or-empty-p (x)
152 (defun get-toplevel-compilations ()
153 (reverse (remove-if #'null-or-empty-p *toplevel-compilations*)))
155 (defun %compile-defmacro (name lambda)
156 (toplevel-compilation (ls-compile `',name))
157 (let ((binding (make-binding :name name :type 'macro :value lambda)))
158 (push-to-lexenv binding *environment* 'function))
161 (defun global-binding (name type namespace)
162 (or (lookup-in-lexenv name *environment* namespace)
163 (let ((b (make-binding :name name :type type :value nil)))
164 (push-to-lexenv b *environment* namespace)
167 (defun claimp (symbol namespace claim)
168 (let ((b (lookup-in-lexenv symbol *environment* namespace)))
169 (and b (member claim (binding-declarations b)))))
171 (defun !proclaim (decl)
174 (dolist (name (cdr decl))
175 (let ((b (global-binding name 'variable 'variable)))
176 (push 'special (binding-declarations b)))))
178 (dolist (name (cdr decl))
179 (let ((b (global-binding name 'function 'function)))
180 (push 'notinline (binding-declarations b)))))
182 (dolist (name (cdr decl))
183 (let ((b (global-binding name 'variable 'variable)))
184 (push 'constant (binding-declarations b)))))))
187 (fset 'proclaim #'!proclaim)
189 (defun %define-symbol-macro (name expansion)
190 (let ((b (make-binding :name name :type 'macro :value expansion)))
191 (push-to-lexenv b *environment* 'variable)
195 (defmacro define-symbol-macro (name expansion)
196 `(%define-symbol-macro ',name ',expansion))
201 (defvar *compilations* nil)
203 (defmacro define-compilation (name args &body body)
204 ;; Creates a new primitive `name' with parameters args and
205 ;; @body. The body can access to the local environment through the
206 ;; variable *ENVIRONMENT*.
207 `(push (list ',name (lambda ,args (block ,name ,@body)))
210 (define-compilation if (condition true false)
211 (code "(" (ls-compile condition) " !== " (ls-compile nil)
212 " ? " (ls-compile true *multiple-value-p*)
213 " : " (ls-compile false *multiple-value-p*)
216 (defvar *ll-keywords* '(&optional &rest &key))
218 (defun list-until-keyword (list)
219 (if (or (null list) (member (car list) *ll-keywords*))
221 (cons (car list) (list-until-keyword (cdr list)))))
223 (defun ll-section (keyword ll)
224 (list-until-keyword (cdr (member keyword ll))))
226 (defun ll-required-arguments (ll)
227 (list-until-keyword ll))
229 (defun ll-optional-arguments-canonical (ll)
230 (mapcar #'ensure-list (ll-section '&optional ll)))
232 (defun ll-optional-arguments (ll)
233 (mapcar #'car (ll-optional-arguments-canonical ll)))
235 (defun ll-rest-argument (ll)
236 (let ((rest (ll-section '&rest ll)))
238 (error "Bad lambda-list `~S'." ll))
241 (defun ll-keyword-arguments-canonical (ll)
242 (flet ((canonicalize (keyarg)
243 ;; Build a canonical keyword argument descriptor, filling
244 ;; the optional fields. The result is a list of the form
245 ;; ((keyword-name var) init-form).
246 (let ((arg (ensure-list keyarg)))
247 (cons (if (listp (car arg))
249 (list (intern (symbol-name (car arg)) "KEYWORD") (car arg)))
251 (mapcar #'canonicalize (ll-section '&key ll))))
253 (defun ll-keyword-arguments (ll)
254 (mapcar (lambda (keyarg) (second (first keyarg)))
255 (ll-keyword-arguments-canonical ll)))
257 (defun ll-svars (lambda-list)
260 (ll-keyword-arguments-canonical lambda-list)
261 (ll-optional-arguments-canonical lambda-list))))
262 (remove nil (mapcar #'third args))))
264 (defun lambda-name/docstring-wrapper (name docstring &rest strs)
265 (if (or name docstring)
267 "var func = " (join strs) ";" *newline*
269 (code "func.fname = " (escape-string name) ";" *newline*))
271 (code "func.docstring = " (escape-string docstring) ";" *newline*))
272 "return func;" *newline*)
273 (apply #'code strs)))
275 (defun lambda-check-argument-count
276 (n-required-arguments n-optional-arguments rest-p)
277 ;; Note: Remember that we assume that the number of arguments of a
278 ;; call is at least 1 (the values argument).
279 (let ((min n-required-arguments)
280 (max (if rest-p 'n/a (+ n-required-arguments n-optional-arguments))))
282 ;; Special case: a positive exact number of arguments.
283 (when (and (< 0 min) (eql min max))
284 (return (code "checkArgs(nargs, " min ");" *newline*)))
288 (code "checkArgsAtLeast(nargs, " min ");" *newline*))
290 (code "checkArgsAtMost(nargs, " max ");" *newline*))))))
292 (defun compile-lambda-optional (ll)
293 (let* ((optional-arguments (ll-optional-arguments-canonical ll))
294 (n-required-arguments (length (ll-required-arguments ll)))
295 (n-optional-arguments (length optional-arguments)))
296 (when optional-arguments
297 (code "switch(nargs){" *newline*
301 (while (< idx n-optional-arguments)
302 (let ((arg (nth idx optional-arguments)))
303 (push (code "case " (+ idx n-required-arguments) ":" *newline*
304 (indent (translate-variable (car arg))
306 (ls-compile (cadr arg)) ";" *newline*)
308 (indent (translate-variable (third arg))
314 (push (code "default: break;" *newline*) cases)
315 (join (reverse cases))))
318 (defun compile-lambda-rest (ll)
319 (let ((n-required-arguments (length (ll-required-arguments ll)))
320 (n-optional-arguments (length (ll-optional-arguments ll)))
321 (rest-argument (ll-rest-argument ll)))
323 (let ((js!rest (translate-variable rest-argument)))
324 (code "var " js!rest "= " (ls-compile nil) ";" *newline*
325 "for (var i = nargs-1; i>=" (+ n-required-arguments n-optional-arguments)
327 (indent js!rest " = {car: arguments[i+2], cdr: " js!rest "};" *newline*))))))
329 (defun compile-lambda-parse-keywords (ll)
330 (let ((n-required-arguments
331 (length (ll-required-arguments ll)))
332 (n-optional-arguments
333 (length (ll-optional-arguments ll)))
335 (ll-keyword-arguments-canonical ll)))
338 (mapconcat (lambda (arg)
339 (let ((var (second (car arg))))
340 (code "var " (translate-variable var) "; " *newline*
342 (code "var " (translate-variable (third arg))
343 " = " (ls-compile nil)
347 (flet ((parse-keyword (keyarg)
348 ;; ((keyword-name var) init-form)
349 (code "for (i=" (+ n-required-arguments n-optional-arguments)
350 "; i<nargs; i+=2){" *newline*
352 "if (arguments[i+2] === " (ls-compile (caar keyarg)) "){" *newline*
353 (indent (translate-variable (cadr (car keyarg)))
356 (let ((svar (third keyarg)))
358 (code (translate-variable svar) " = " (ls-compile t) ";" *newline*)))
363 "if (i == nargs){" *newline*
364 (indent (translate-variable (cadr (car keyarg))) " = " (ls-compile (cadr keyarg)) ";" *newline*)
366 (when keyword-arguments
367 (code "var i;" *newline*
368 (mapconcat #'parse-keyword keyword-arguments))))
369 ;; Check for unknown keywords
370 (when keyword-arguments
371 (code "for (i=" (+ n-required-arguments n-optional-arguments)
372 "; i<nargs; i+=2){" *newline*
374 (join (mapcar (lambda (x)
375 (concat "arguments[i+2] !== " (ls-compile (caar x))))
380 "throw 'Unknown keyword argument ' + xstring(arguments[i].name);" *newline*))
383 (defun parse-lambda-list (ll)
384 (values (ll-required-arguments ll)
385 (ll-optional-arguments ll)
386 (ll-keyword-arguments ll)
387 (ll-rest-argument ll)))
389 ;;; Process BODY for declarations and/or docstrings. Return as
390 ;;; multiple values the BODY without docstrings or declarations, the
391 ;;; list of declaration forms and the docstring.
392 (defun parse-body (body &key declarations docstring)
393 (let ((value-declarations)
395 ;; Parse declarations
397 (do* ((rest body (cdr rest))
398 (form (car rest) (car rest)))
399 ((or (atom form) (not (eq (car form) 'declare)))
401 (push form value-declarations)))
405 (not (null (cdr body))))
406 (setq value-docstring (car body))
407 (setq body (cdr body)))
408 (values body value-declarations value-docstring)))
410 ;;; Compile a lambda function with lambda list LL and body BODY. If
411 ;;; NAME is given, it should be a constant string and it will become
412 ;;; the name of the function. If BLOCK is non-NIL, a named block is
413 ;;; created around the body. NOTE: No block (even anonymous) is
414 ;;; created if BLOCk is NIL.
415 (defun compile-lambda (ll body &key name block)
416 (multiple-value-bind (required-arguments
420 (parse-lambda-list ll)
421 (multiple-value-bind (body decls documentation)
422 (parse-body body :declarations t :docstring t)
423 (declare (ignore decls))
424 (let ((n-required-arguments (length required-arguments))
425 (n-optional-arguments (length optional-arguments))
426 (*environment* (extend-local-env
427 (append (ensure-list rest-argument)
432 (lambda-name/docstring-wrapper name documentation
434 (join (list* "values"
436 (mapcar #'translate-variable
437 (append required-arguments optional-arguments)))
441 ;; Check number of arguments
442 (lambda-check-argument-count n-required-arguments
444 (or rest-argument keyword-arguments))
445 (compile-lambda-optional ll)
446 (compile-lambda-rest ll)
447 (compile-lambda-parse-keywords ll)
448 (let ((*multiple-value-p* t))
450 (ls-compile-block `((block ,block ,@body)) t)
451 (ls-compile-block body t))))
455 (defun setq-pair (var val)
456 (let ((b (lookup-in-lexenv var *environment* 'variable)))
459 (eq (binding-type b) 'variable)
460 (not (member 'special (binding-declarations b)))
461 (not (member 'constant (binding-declarations b))))
462 (code (binding-value b) " = " (ls-compile val)))
463 ((and b (eq (binding-type b) 'macro))
464 (ls-compile `(setf ,var ,val)))
466 (ls-compile `(set ',var ,val))))))
469 (define-compilation setq (&rest pairs)
473 ((null pairs) (return))
475 (error "Odd pairs in SETQ"))
478 (concat (setq-pair (car pairs) (cadr pairs))
479 (if (null (cddr pairs)) "" ", ")))
480 (setq pairs (cddr pairs)))))
481 (code "(" result ")")))
484 ;;; Compilation of literals an object dumping
485 (defun escape-string (string)
487 (size (length string))
488 (seen-single-quote nil)
489 (seen-double-quote nil))
490 (flet ((%escape-string (string escape-single-quote-p)
493 (while (< index size)
494 (let ((ch (char string index)))
496 (setq output (concat output "\\")))
497 (when (and escape-single-quote-p (char= ch #\'))
498 (setq output (concat output "\\")))
499 (when (char= ch #\newline)
500 (setq output (concat output "\\"))
502 (setq output (concat output (string ch))))
505 ;; First, scan the string for single/double quotes
506 (while (< index size)
507 (let ((ch (char string index)))
509 (setq seen-single-quote t))
511 (setq seen-double-quote t)))
513 ;; Then pick the appropriate way to escape the quotes
515 ((not seen-single-quote)
516 (concat "'" (%escape-string string nil) "'"))
517 ((not seen-double-quote)
518 (concat "\"" (%escape-string string nil) "\""))
519 (t (concat "'" (%escape-string string t) "'"))))))
521 ;;; BOOTSTRAP MAGIC: We record the macro definitions as lists during
522 ;;; the bootstrap. Once everything is compiled, we want to dump the
523 ;;; whole global environment to the output file to reproduce it in the
524 ;;; run-time. However, the environment must contain expander functions
525 ;;; rather than lists. We do not know how to dump function objects
526 ;;; itself, so we mark the list definitions with this object and the
527 ;;; compiler will be called when this object has to be dumped.
528 ;;; Backquote/unquote does a similar magic, but this use is exclusive.
530 ;;; Indeed, perhaps to compile the object other macros need to be
531 ;;; evaluated. For this reason we define a valid macro-function for
533 (defvar *magic-unquote-marker* (gensym "MAGIC-UNQUOTE"))
535 (setf (macro-function *magic-unquote-marker*)
536 (lambda (form &optional environment)
537 (declare (ignore environment))
540 (defvar *literal-table* nil)
541 (defvar *literal-counter* 0)
544 (code "l" (incf *literal-counter*)))
546 (defun dump-symbol (symbol)
548 (let ((package (symbol-package symbol)))
549 (if (eq package (find-package "KEYWORD"))
550 (code "(new Symbol(" (dump-string (symbol-name symbol)) ", " (dump-string (package-name package)) "))")
551 (code "(new Symbol(" (dump-string (symbol-name symbol)) "))")))
553 (let ((package (symbol-package symbol)))
555 (code "(new Symbol(" (dump-string (symbol-name symbol)) "))")
556 (ls-compile `(intern ,(symbol-name symbol) ,(package-name package))))))
558 (defun dump-cons (cons)
559 (let ((head (butlast cons))
562 (join-trailing (mapcar (lambda (x) (literal x t)) head) ",")
563 (literal (car tail) t)
565 (literal (cdr tail) t)
568 (defun dump-array (array)
569 (let ((elements (vector-to-list array)))
570 (concat "[" (join (mapcar #'literal elements) ", ") "]")))
572 (defun dump-string (string)
573 (code "make_lisp_string(" (escape-string string) ")"))
575 (defun literal (sexp &optional recursive)
577 ((integerp sexp) (integer-to-string sexp))
578 ((floatp sexp) (float-to-string sexp))
579 ((characterp sexp) (escape-string (string sexp)))
581 (or (cdr (assoc sexp *literal-table* :test #'eql))
582 (let ((dumped (typecase sexp
583 (symbol (dump-symbol sexp))
584 (string (dump-string sexp))
586 ;; BOOTSTRAP MAGIC: See the root file
587 ;; jscl.lisp and the function
588 ;; `dump-global-environment' for futher
590 (if (eq (car sexp) *magic-unquote-marker*)
591 (ls-compile (second sexp))
593 (array (dump-array sexp)))))
594 (if (and recursive (not (symbolp sexp)))
596 (let ((jsvar (genlit)))
597 (push (cons sexp jsvar) *literal-table*)
598 (toplevel-compilation (code "var " jsvar " = " dumped))
599 (when (keywordp sexp)
600 (toplevel-compilation (code jsvar ".value = " jsvar)))
604 (define-compilation quote (sexp)
607 (define-compilation %while (pred &rest body)
609 "while(" (ls-compile pred) " !== " (ls-compile nil) "){" *newline*
610 (indent (ls-compile-block body))
612 "return " (ls-compile nil) ";" *newline*))
614 (define-compilation function (x)
616 ((and (listp x) (eq (car x) 'lambda))
617 (compile-lambda (cadr x) (cddr x)))
618 ((and (listp x) (eq (car x) 'named-lambda))
619 ;; TODO: destructuring-bind now! Do error checking manually is
621 (let ((name (cadr x))
624 (compile-lambda ll body
625 :name (symbol-name name)
628 (let ((b (lookup-in-lexenv x *environment* 'function)))
631 (ls-compile `(symbol-function ',x)))))))
634 (defun make-function-binding (fname)
635 (make-binding :name fname :type 'function :value (gvarname fname)))
637 (defun compile-function-definition (list)
638 (compile-lambda (car list) (cdr list)))
640 (defun translate-function (name)
641 (let ((b (lookup-in-lexenv name *environment* 'function)))
642 (and b (binding-value b))))
644 (define-compilation flet (definitions &rest body)
645 (let* ((fnames (mapcar #'car definitions))
646 (cfuncs (mapcar (lambda (def)
647 (compile-lambda (cadr def)
652 (extend-lexenv (mapcar #'make-function-binding fnames)
656 (join (mapcar #'translate-function fnames) ",")
658 (let ((body (ls-compile-block body t)))
660 "})(" (join cfuncs ",") ")")))
662 (define-compilation labels (definitions &rest body)
663 (let* ((fnames (mapcar #'car definitions))
665 (extend-lexenv (mapcar #'make-function-binding fnames)
669 (mapconcat (lambda (func)
670 (code "var " (translate-function (car func))
671 " = " (compile-lambda (cadr func)
672 `((block ,(car func) ,@(cddr func))))
675 (ls-compile-block body t))))
678 (defvar *compiling-file* nil)
679 (define-compilation eval-when-compile (&rest body)
682 (eval (cons 'progn body))
684 (ls-compile `(progn ,@body))))
686 (defmacro define-transformation (name args form)
687 `(define-compilation ,name ,args
690 (define-compilation progn (&rest body)
691 (if (null (cdr body))
692 (ls-compile (car body) *multiple-value-p*)
695 (remove-if #'null-or-empty-p
697 (mapcar #'ls-compile (butlast body))
698 (list (ls-compile (car (last body)) t))))
702 (defun special-variable-p (x)
703 (and (claimp x 'variable 'special) t))
705 ;;; Wrap CODE to restore the symbol values of the dynamic
706 ;;; bindings. BINDINGS is a list of pairs of the form
707 ;;; (SYMBOL . PLACE), where PLACE is a Javascript variable
708 ;;; name to initialize the symbol value and where to stored
710 (defun let-binding-wrapper (bindings body)
711 (when (null bindings)
712 (return-from let-binding-wrapper body))
715 (indent "var tmp;" *newline*
718 (let ((s (ls-compile `(quote ,(car b)))))
719 (code "tmp = " s ".value;" *newline*
720 s ".value = " (cdr b) ";" *newline*
721 (cdr b) " = tmp;" *newline*)))
725 "finally {" *newline*
727 (mapconcat (lambda (b)
728 (let ((s (ls-compile `(quote ,(car b)))))
729 (code s ".value" " = " (cdr b) ";" *newline*)))
733 (define-compilation let (bindings &rest body)
734 (let* ((bindings (mapcar #'ensure-list bindings))
735 (variables (mapcar #'first bindings))
736 (cvalues (mapcar #'ls-compile (mapcar #'second bindings)))
737 (*environment* (extend-local-env (remove-if #'special-variable-p variables)))
740 (join (mapcar (lambda (x)
741 (if (special-variable-p x)
742 (let ((v (gvarname x)))
743 (push (cons x v) dynamic-bindings)
745 (translate-variable x)))
749 (let ((body (ls-compile-block body t t)))
750 (indent (let-binding-wrapper dynamic-bindings body)))
751 "})(" (join cvalues ",") ")")))
754 ;;; Return the code to initialize BINDING, and push it extending the
755 ;;; current lexical environment if the variable is not special.
756 (defun let*-initialize-value (binding)
757 (let ((var (first binding))
758 (value (second binding)))
759 (if (special-variable-p var)
760 (code (ls-compile `(setq ,var ,value)) ";" *newline*)
761 (let* ((v (gvarname var))
762 (b (make-binding :name var :type 'variable :value v)))
763 (prog1 (code "var " v " = " (ls-compile value) ";" *newline*)
764 (push-to-lexenv b *environment* 'variable))))))
766 ;;; Wrap BODY to restore the symbol values of SYMBOLS after body. It
767 ;;; DOES NOT generate code to initialize the value of the symbols,
768 ;;; unlike let-binding-wrapper.
769 (defun let*-binding-wrapper (symbols body)
771 (return-from let*-binding-wrapper body))
772 (let ((store (mapcar (lambda (s) (cons s (gvarname s)))
773 (remove-if-not #'special-variable-p symbols))))
777 (mapconcat (lambda (b)
778 (let ((s (ls-compile `(quote ,(car b)))))
779 (code "var " (cdr b) " = " s ".value;" *newline*)))
783 "finally {" *newline*
785 (mapconcat (lambda (b)
786 (let ((s (ls-compile `(quote ,(car b)))))
787 (code s ".value" " = " (cdr b) ";" *newline*)))
791 (define-compilation let* (bindings &rest body)
792 (let ((bindings (mapcar #'ensure-list bindings))
793 (*environment* (copy-lexenv *environment*)))
795 (let ((specials (remove-if-not #'special-variable-p (mapcar #'first bindings)))
796 (body (concat (mapconcat #'let*-initialize-value bindings)
797 (ls-compile-block body t t))))
798 (let*-binding-wrapper specials body)))))
801 (define-compilation block (name &rest body)
802 ;; We use Javascript exceptions to implement non local control
803 ;; transfer. Exceptions has dynamic scoping, so we use a uniquely
804 ;; generated object to identify the block. The instance of a empty
805 ;; array is used to distinguish between nested dynamic Javascript
806 ;; exceptions. See https://github.com/davazp/jscl/issues/64 for
808 (let* ((idvar (gvarname name))
809 (b (make-binding :name name :type 'block :value idvar)))
810 (when *multiple-value-p*
811 (push 'multiple-value (binding-declarations b)))
812 (let* ((*environment* (extend-lexenv (list b) *environment* 'block))
813 (cbody (ls-compile-block body t)))
814 (if (member 'used (binding-declarations b))
817 "var " idvar " = [];" *newline*
820 "catch (cf){" *newline*
821 " if (cf.type == 'block' && cf.id == " idvar ")" *newline*
822 (if *multiple-value-p*
823 " return values.apply(this, forcemv(cf.values));"
824 " return cf.values;")
827 " throw cf;" *newline*
829 (js!selfcall cbody)))))
831 (define-compilation return-from (name &optional value)
832 (let* ((b (lookup-in-lexenv name *environment* 'block))
833 (multiple-value-p (member 'multiple-value (binding-declarations b))))
835 (error "Return from unknown block `~S'." (symbol-name name)))
836 (push 'used (binding-declarations b))
837 ;; The binding value is the name of a variable, whose value is the
838 ;; unique identifier of the block as exception. We can't use the
839 ;; variable name itself, because it could not to be unique, so we
840 ;; capture it in a closure.
842 (when multiple-value-p (code "var values = mv;" *newline*))
845 "id: " (binding-value b) ", "
846 "values: " (ls-compile value multiple-value-p) ", "
847 "message: 'Return from unknown block " (symbol-name name) ".'"
850 (define-compilation catch (id &rest body)
852 "var id = " (ls-compile id) ";" *newline*
854 (indent (ls-compile-block body t)) *newline*
856 "catch (cf){" *newline*
857 " if (cf.type == 'catch' && cf.id == id)" *newline*
858 (if *multiple-value-p*
859 " return values.apply(this, forcemv(cf.values));"
860 " return pv.apply(this, forcemv(cf.values));")
863 " throw cf;" *newline*
866 (define-compilation throw (id value)
868 "var values = mv;" *newline*
871 "id: " (ls-compile id) ", "
872 "values: " (ls-compile value t) ", "
873 "message: 'Throw uncatched.'"
877 (or (integerp x) (symbolp x)))
879 (defun declare-tagbody-tags (tbidx body)
880 (let* ((go-tag-counter 0)
882 (mapcar (lambda (label)
883 (let ((tagidx (integer-to-string (incf go-tag-counter))))
884 (make-binding :name label :type 'gotag :value (list tbidx tagidx))))
885 (remove-if-not #'go-tag-p body))))
886 (extend-lexenv bindings *environment* 'gotag)))
888 (define-compilation tagbody (&rest body)
889 ;; Ignore the tagbody if it does not contain any go-tag. We do this
890 ;; because 1) it is easy and 2) many built-in forms expand to a
891 ;; implicit tagbody, so we save some space.
892 (unless (some #'go-tag-p body)
893 (return-from tagbody (ls-compile `(progn ,@body nil))))
894 ;; The translation assumes the first form in BODY is a label
895 (unless (go-tag-p (car body))
896 (push (gensym "START") body))
897 ;; Tagbody compilation
898 (let ((branch (gvarname 'branch))
899 (tbidx (gvarname 'tbidx)))
900 (let ((*environment* (declare-tagbody-tags tbidx body))
902 (let ((b (lookup-in-lexenv (first body) *environment* 'gotag)))
903 (setq initag (second (binding-value b))))
905 ;; TAGBODY branch to take
906 "var " branch " = " initag ";" *newline*
907 "var " tbidx " = [];" *newline*
909 "while (true) {" *newline*
910 (indent "try {" *newline*
911 (indent (let ((content ""))
912 (code "switch(" branch "){" *newline*
913 "case " initag ":" *newline*
914 (dolist (form (cdr body) content)
916 (if (not (go-tag-p form))
917 (indent (ls-compile form) ";" *newline*)
918 (let ((b (lookup-in-lexenv form *environment* 'gotag)))
919 (code "case " (second (binding-value b)) ":" *newline*)))))
921 " break tbloop;" *newline*
924 "catch (jump) {" *newline*
925 " if (jump.type == 'tagbody' && jump.id == " tbidx ")" *newline*
926 " " branch " = jump.label;" *newline*
928 " throw(jump);" *newline*
931 "return " (ls-compile nil) ";" *newline*))))
933 (define-compilation go (label)
934 (let ((b (lookup-in-lexenv label *environment* 'gotag))
936 ((symbolp label) (symbol-name label))
937 ((integerp label) (integer-to-string label)))))
939 (error "Unknown tag `~S'" label))
943 "id: " (first (binding-value b)) ", "
944 "label: " (second (binding-value b)) ", "
945 "message: 'Attempt to GO to non-existing tag " n "'"
948 (define-compilation unwind-protect (form &rest clean-up)
950 "var ret = " (ls-compile nil) ";" *newline*
952 (indent "ret = " (ls-compile form) ";" *newline*)
953 "} finally {" *newline*
954 (indent (ls-compile-block clean-up))
956 "return ret;" *newline*))
958 (define-compilation multiple-value-call (func-form &rest forms)
960 "var func = " (ls-compile func-form) ";" *newline*
961 "var args = [" (if *multiple-value-p* "values" "pv") ", 0];" *newline*
964 "var values = mv;" *newline*
966 (mapconcat (lambda (form)
967 (code "vs = " (ls-compile form t) ";" *newline*
968 "if (typeof vs === 'object' && 'multiple-value' in vs)" *newline*
969 (indent "args = args.concat(vs);" *newline*)
971 (indent "args.push(vs);" *newline*)))
973 "args[1] = args.length-2;" *newline*
974 "return func.apply(window, args);" *newline*) ";" *newline*))
976 (define-compilation multiple-value-prog1 (first-form &rest forms)
978 "var args = " (ls-compile first-form *multiple-value-p*) ";" *newline*
979 (ls-compile-block forms)
980 "return args;" *newline*))
982 (define-transformation backquote (form)
983 (bq-completely-process form))
988 (defvar *builtins* nil)
990 (defmacro define-raw-builtin (name args &body body)
991 ;; Creates a new primitive function `name' with parameters args and
992 ;; @body. The body can access to the local environment through the
993 ;; variable *ENVIRONMENT*.
994 `(push (list ',name (lambda ,args (block ,name ,@body)))
997 (defmacro define-builtin (name args &body body)
998 `(define-raw-builtin ,name ,args
999 (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args)
1002 ;;; DECLS is a list of (JSVARNAME TYPE LISPFORM) declarations.
1003 (defmacro type-check (decls &body body)
1005 ,@(mapcar (lambda (decl)
1006 `(code "var " ,(first decl) " = " ,(third decl) ";" *newline*))
1008 ,@(mapcar (lambda (decl)
1009 `(code "if (typeof " ,(first decl) " != '" ,(second decl) "')" *newline*
1010 (indent "throw 'The value ' + "
1012 " + ' is not a type "
1017 (code "return " (progn ,@body) ";" *newline*)))
1019 ;;; VARIABLE-ARITY compiles variable arity operations. ARGS stands for
1020 ;;; a variable which holds a list of forms. It will compile them and
1021 ;;; store the result in some Javascript variables. BODY is evaluated
1022 ;;; with ARGS bound to the list of these variables to generate the
1023 ;;; code which performs the transformation on these variables.
1025 (defun variable-arity-call (args function)
1026 (unless (consp args)
1027 (error "ARGS must be a non-empty list"))
1033 ((floatp x) (push (float-to-string x) fargs))
1034 ((numberp x) (push (integer-to-string x) fargs))
1035 (t (let ((v (code "x" (incf counter))))
1038 (code "var " v " = " (ls-compile x) ";" *newline*
1039 "if (typeof " v " !== 'number') throw 'Not a number!';"
1041 (js!selfcall prelude (funcall function (reverse fargs)))))
1044 (defmacro variable-arity (args &body body)
1045 (unless (symbolp args)
1046 (error "`~S' is not a symbol." args))
1047 `(variable-arity-call ,args
1049 (code "return " ,@body ";" *newline*))))
1051 (defun num-op-num (x op y)
1052 (type-check (("x" "number" x) ("y" "number" y))
1055 (define-raw-builtin + (&rest numbers)
1058 (variable-arity numbers
1059 (join numbers "+"))))
1061 (define-raw-builtin - (x &rest others)
1062 (let ((args (cons x others)))
1063 (variable-arity args
1065 (concat "-" (car args))
1068 (define-raw-builtin * (&rest numbers)
1071 (variable-arity numbers
1072 (join numbers "*"))))
1074 (define-raw-builtin / (x &rest others)
1075 (let ((args (cons x others)))
1076 (variable-arity args
1078 (concat "1 /" (car args))
1081 (define-builtin mod (x y) (num-op-num x "%" y))
1084 (defun comparison-conjuntion (vars op)
1089 (concat (car vars) op (cadr vars)))
1091 (concat (car vars) op (cadr vars)
1093 (comparison-conjuntion (cdr vars) op)))))
1095 (defmacro define-builtin-comparison (op sym)
1096 `(define-raw-builtin ,op (x &rest args)
1097 (let ((args (cons x args)))
1098 (variable-arity args
1099 (js!bool (comparison-conjuntion args ,sym))))))
1101 (define-builtin-comparison > ">")
1102 (define-builtin-comparison < "<")
1103 (define-builtin-comparison >= ">=")
1104 (define-builtin-comparison <= "<=")
1105 (define-builtin-comparison = "==")
1106 (define-builtin-comparison /= "!=")
1108 (define-builtin numberp (x)
1109 (js!bool (code "(typeof (" x ") == \"number\")")))
1111 (define-builtin floor (x)
1112 (type-check (("x" "number" x))
1115 (define-builtin expt (x y)
1116 (type-check (("x" "number" x)
1120 (define-builtin float-to-string (x)
1121 (type-check (("x" "number" x))
1122 "make_lisp_string(x.toString())"))
1124 (define-builtin cons (x y)
1125 (code "({car: " x ", cdr: " y "})"))
1127 (define-builtin consp (x)
1130 "var tmp = " x ";" *newline*
1131 "return (typeof tmp == 'object' && 'car' in tmp);" *newline*)))
1133 (define-builtin car (x)
1135 "var tmp = " x ";" *newline*
1136 "return tmp === " (ls-compile nil)
1137 "? " (ls-compile nil)
1138 ": tmp.car;" *newline*))
1140 (define-builtin cdr (x)
1142 "var tmp = " x ";" *newline*
1143 "return tmp === " (ls-compile nil) "? "
1145 ": tmp.cdr;" *newline*))
1147 (define-builtin rplaca (x new)
1148 (type-check (("x" "object" x))
1149 (code "(x.car = " new ", x)")))
1151 (define-builtin rplacd (x new)
1152 (type-check (("x" "object" x))
1153 (code "(x.cdr = " new ", x)")))
1155 (define-builtin symbolp (x)
1156 (js!bool (code "(" x " instanceof Symbol)")))
1158 (define-builtin make-symbol (name)
1159 (code "(new Symbol(" name "))"))
1161 (define-builtin symbol-name (x)
1162 (code "(" x ").name"))
1164 (define-builtin set (symbol value)
1165 (code "(" symbol ").value = " value))
1167 (define-builtin fset (symbol value)
1168 (code "(" symbol ").fvalue = " value))
1170 (define-builtin boundp (x)
1171 (js!bool (code "(" x ".value !== undefined)")))
1173 (define-builtin fboundp (x)
1174 (js!bool (code "(" x ".fvalue !== undefined)")))
1176 (define-builtin symbol-value (x)
1178 "var symbol = " x ";" *newline*
1179 "var value = symbol.value;" *newline*
1180 "if (value === undefined) throw \"Variable `\" + xstring(symbol.name) + \"' is unbound.\";" *newline*
1181 "return value;" *newline*))
1183 (define-builtin symbol-function (x)
1185 "var symbol = " x ";" *newline*
1186 "var func = symbol.fvalue;" *newline*
1187 "if (func === undefined) throw \"Function `\" + xstring(symbol.name) + \"' is undefined.\";" *newline*
1188 "return func;" *newline*))
1190 (define-builtin symbol-plist (x)
1191 (code "((" x ").plist || " (ls-compile nil) ")"))
1193 (define-builtin lambda-code (x)
1194 (code "make_lisp_string((" x ").toString())"))
1196 (define-builtin eq (x y)
1197 (js!bool (code "(" x " === " y ")")))
1199 (define-builtin char-code (x)
1200 (type-check (("x" "string" x))
1201 "char_to_codepoint(x)"))
1203 (define-builtin code-char (x)
1204 (type-check (("x" "number" x))
1205 "char_from_codepoint(x)"))
1207 (define-builtin characterp (x)
1210 "var x = " x ";" *newline*
1211 "return (typeof(" x ") == \"string\") && (x.length == 1 || x.length == 2);")))
1213 (define-builtin char-to-string (x)
1215 "var r = [" x "];" *newline*
1216 "r.type = 'character';"
1219 (define-builtin char-upcase (x)
1220 (code "safe_char_upcase(" x ")"))
1222 (define-builtin char-downcase (x)
1223 (code "safe_char_downcase(" x ")"))
1225 (define-builtin stringp (x)
1228 "var x = " x ";" *newline*
1229 "return typeof(x) == 'object' && 'length' in x && x.type == 'character';")))
1231 (define-builtin string-upcase (x)
1232 (code "make_lisp_string(xstring(" x ").toUpperCase())"))
1234 (define-builtin string-length (x)
1237 (define-raw-builtin slice (vector a &optional b)
1239 "var vector = " (ls-compile vector) ";" *newline*
1240 "var a = " (ls-compile a) ";" *newline*
1242 (when b (code "b = " (ls-compile b) ";" *newline*))
1243 "return vector.slice(a,b);" *newline*))
1245 (define-builtin char (string index)
1246 (code string "[" index "]"))
1248 (define-builtin concat-two (string1 string2)
1250 "var r = " string1 ".concat(" string2 ");" *newline*
1251 "r.type = 'character';"
1252 "return r;" *newline*))
1254 (define-raw-builtin funcall (func &rest args)
1256 "var f = " (ls-compile func) ";" *newline*
1257 "return (typeof f === 'function'? f: f.fvalue)("
1258 (join (list* (if *multiple-value-p* "values" "pv")
1259 (integer-to-string (length args))
1260 (mapcar #'ls-compile args))
1264 (define-raw-builtin apply (func &rest args)
1266 (code "(" (ls-compile func) ")()")
1267 (let ((args (butlast args))
1268 (last (car (last args))))
1270 "var f = " (ls-compile func) ";" *newline*
1271 "var args = [" (join (list* (if *multiple-value-p* "values" "pv")
1272 (integer-to-string (length args))
1273 (mapcar #'ls-compile args))
1276 "var tail = (" (ls-compile last) ");" *newline*
1277 "while (tail != " (ls-compile nil) "){" *newline*
1278 " args.push(tail.car);" *newline*
1279 " args[1] += 1;" *newline*
1280 " tail = tail.cdr;" *newline*
1282 "return (typeof f === 'function'? f : f.fvalue).apply(this, args);" *newline*))))
1284 (define-builtin js-eval (string)
1285 (if *multiple-value-p*
1287 "var v = globalEval(xstring(" string "));" *newline*
1288 "return values.apply(this, forcemv(v));" *newline*)
1289 (code "globalEval(xstring(" string "))")))
1291 (define-builtin %throw (string)
1292 (js!selfcall "throw " string ";" *newline*))
1294 (define-builtin new () "{}")
1296 (define-builtin objectp (x)
1297 (js!bool (code "(typeof (" x ") === 'object')")))
1299 (define-builtin oget (object key)
1301 "var tmp = " "(" object ")[xstring(" key ")];" *newline*
1302 "return tmp == undefined? " (ls-compile nil) ": tmp ;" *newline*))
1304 (define-builtin oset (object key value)
1305 (code "((" object ")[xstring(" key ")] = " value ")"))
1307 (define-builtin in (key object)
1308 (js!bool (code "(xstring(" key ") in (" object "))")))
1310 (define-builtin map-for-in (function object)
1312 "var f = " function ";" *newline*
1313 "var g = (typeof f === 'function' ? f : f.fvalue);" *newline*
1314 "var o = " object ";" *newline*
1315 "for (var key in o){" *newline*
1316 (indent "g(" (if *multiple-value-p* "values" "pv") ", 1, o[key]);" *newline*)
1318 " return " (ls-compile nil) ";" *newline*))
1320 (define-builtin functionp (x)
1321 (js!bool (code "(typeof " x " == 'function')")))
1323 (define-builtin write-string (x)
1324 (code "lisp.write(" x ")"))
1326 (define-builtin make-array (n)
1328 "var r = [];" *newline*
1329 "for (var i = 0; i < " n "; i++)" *newline*
1330 (indent "r.push(" (ls-compile nil) ");" *newline*)
1331 "return r;" *newline*))
1333 ;;; FIXME: should take optional min-extension.
1334 ;;; FIXME: should use fill-pointer instead of the absolute end of array
1335 (define-builtin vector-push-extend (new vector)
1337 "var v = " vector ";" *newline*
1338 "v.push(" new ");" *newline*
1341 (define-builtin arrayp (x)
1344 "var x = " x ";" *newline*
1345 "return typeof x === 'object' && 'length' in x;")))
1347 (define-builtin aref (array n)
1349 "var x = " "(" array ")[" n "];" *newline*
1350 "if (x === undefined) throw 'Out of range';" *newline*
1351 "return x;" *newline*))
1353 (define-builtin aset (array n value)
1355 "var x = " array ";" *newline*
1356 "var i = " n ";" *newline*
1357 "if (i < 0 || i >= x.length) throw 'Out of range';" *newline*
1358 "return x[i] = " value ";" *newline*))
1360 (define-builtin afind (value array)
1362 "var v = " value ";" *newline*
1363 "var x = " array ";" *newline*
1364 "return x.indexOf(v);" *newline*))
1366 (define-builtin aresize (array new-size)
1368 "var x = " array ";" *newline*
1369 "var n = " new-size ";" *newline*
1370 "return x.length = n;" *newline*))
1372 (define-builtin get-internal-real-time ()
1373 "(new Date()).getTime()")
1375 (define-builtin values-array (array)
1376 (if *multiple-value-p*
1377 (code "values.apply(this, " array ")")
1378 (code "pv.apply(this, " array ")")))
1380 (define-raw-builtin values (&rest args)
1381 (if *multiple-value-p*
1382 (code "values(" (join (mapcar #'ls-compile args) ", ") ")")
1383 (code "pv(" (join (mapcar #'ls-compile args) ", ") ")")))
1388 (define-compilation %js-vref (var)
1389 (code "js_to_lisp(" var ")"))
1391 (define-compilation %js-vset (var val)
1392 (code "(" var " = lisp_to_js(" (ls-compile val) "))"))
1394 (define-setf-expander %js-vref (var)
1395 (let ((new-value (gensym)))
1396 (unless (stringp var)
1397 (error "`~S' is not a string." var))
1401 `(%js-vset ,var ,new-value)
1406 (defvar *macroexpander-cache*
1407 (make-hash-table :test #'eq))
1409 (defun !macro-function (symbol)
1410 (unless (symbolp symbol)
1411 (error "`~S' is not a symbol." symbol))
1412 (let ((b (lookup-in-lexenv symbol *environment* 'function)))
1413 (if (and b (eq (binding-type b) 'macro))
1414 (let ((expander (binding-value b)))
1417 ((gethash b *macroexpander-cache*)
1418 (setq expander (gethash b *macroexpander-cache*)))
1420 (let ((compiled (eval expander)))
1421 ;; The list representation are useful while
1422 ;; bootstrapping, as we can dump the definition of the
1423 ;; macros easily, but they are slow because we have to
1424 ;; evaluate them and compile them now and again. So, let
1425 ;; us replace the list representation version of the
1426 ;; function with the compiled one.
1428 #+jscl (setf (binding-value b) compiled)
1429 #-jscl (setf (gethash b *macroexpander-cache*) compiled)
1430 (setq expander compiled))))
1434 (defun !macroexpand-1 (form)
1437 (let ((b (lookup-in-lexenv form *environment* 'variable)))
1438 (if (and b (eq (binding-type b) 'macro))
1439 (values (binding-value b) t)
1440 (values form nil))))
1441 ((and (consp form) (symbolp (car form)))
1442 (let ((macrofun (!macro-function (car form))))
1444 (values (funcall macrofun (cdr form)) t)
1445 (values form nil))))
1447 (values form nil))))
1449 (defun compile-funcall (function args)
1450 (let* ((values-funcs (if *multiple-value-p* "values" "pv"))
1451 (arglist (concat "(" (join (list* values-funcs
1452 (integer-to-string (length args))
1453 (mapcar #'ls-compile args)) ", ") ")")))
1454 (unless (or (symbolp function)
1455 (and (consp function)
1456 (eq (car function) 'lambda)))
1457 (error "Bad function designator `~S'" function))
1459 ((translate-function function)
1460 (concat (translate-function function) arglist))
1461 ((and (symbolp function)
1462 #+jscl (eq (symbol-package function) (find-package "COMMON-LISP"))
1464 (code (ls-compile `',function) ".fvalue" arglist))
1466 (code (ls-compile `#',function) arglist)))))
1468 (defun ls-compile-block (sexps &optional return-last-p decls-allowed-p)
1469 (multiple-value-bind (sexps decls)
1470 (parse-body sexps :declarations decls-allowed-p)
1471 (declare (ignore decls))
1473 (code (ls-compile-block (butlast sexps) nil decls-allowed-p)
1474 "return " (ls-compile (car (last sexps)) *multiple-value-p*) ";")
1476 (remove-if #'null-or-empty-p (mapcar #'ls-compile sexps))
1477 (concat ";" *newline*)))))
1479 (defun ls-compile (sexp &optional multiple-value-p)
1480 (multiple-value-bind (sexp expandedp) (!macroexpand-1 sexp)
1482 (return-from ls-compile (ls-compile sexp multiple-value-p)))
1483 ;; The expression has been macroexpanded. Now compile it!
1484 (let ((*multiple-value-p* multiple-value-p))
1487 (let ((b (lookup-in-lexenv sexp *environment* 'variable)))
1489 ((and b (not (member 'special (binding-declarations b))))
1491 ((or (keywordp sexp)
1492 (and b (member 'constant (binding-declarations b))))
1493 (code (ls-compile `',sexp) ".value"))
1495 (ls-compile `(symbol-value ',sexp))))))
1496 ((or (integerp sexp) (floatp sexp) (characterp sexp) (stringp sexp) (arrayp sexp))
1499 (let ((name (car sexp))
1503 ((assoc name *compilations*)
1504 (let ((comp (second (assoc name *compilations*))))
1506 ;; Built-in functions
1507 ((and (assoc name *builtins*)
1508 (not (claimp name 'function 'notinline)))
1509 (let ((comp (second (assoc name *builtins*))))
1512 (compile-funcall name args)))))
1514 (error "How should I compile `~S'?" sexp))))))
1517 (defvar *compile-print-toplevels* nil)
1519 (defun truncate-string (string &optional (width 60))
1520 (let ((n (or (position #\newline string)
1521 (min width (length string)))))
1522 (subseq string 0 n)))
1524 (defun ls-compile-toplevel (sexp &optional multiple-value-p)
1525 (let ((*toplevel-compilations* nil))
1527 ((and (consp sexp) (eq (car sexp) 'progn))
1528 (let ((subs (mapcar (lambda (s)
1529 (ls-compile-toplevel s t))
1531 (join (remove-if #'null-or-empty-p subs))))
1533 (when *compile-print-toplevels*
1534 (let ((form-string (prin1-to-string sexp)))
1535 (format t "Compiling ~a..." (truncate-string form-string))))
1536 (let ((code (ls-compile sexp multiple-value-p)))
1537 (code (join-trailing (get-toplevel-compilations)
1538 (code ";" *newline*))
1540 (code code ";" *newline*))))))))