Non-nested switch cases in codegen
[jscl.git] / src / compiler.lisp
1 ;;; compiler.lisp ---
2
3 ;; copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
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.
10 ;;
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.
15 ;;
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/>.
18
19 ;;;; Compiler
20
21 (/debug "loading compiler.lisp!")
22
23 ;;; Translate the Lisp code to Javascript. It will compile the special
24 ;;; forms. Some primitive functions are compiled as special forms
25 ;;; too. The respective real functions are defined in the target (see
26 ;;; the beginning of this file) as well as some primitive functions.
27
28 (defun interleave (list element &optional after-last-p)
29   (unless (null list)
30     (with-collect
31       (collect (car list))
32       (dolist (x (cdr list))
33         (collect element)
34         (collect x))
35       (when after-last-p
36         (collect element)))))
37
38 (defun code (&rest args)
39   (mapconcat (lambda (arg)
40                (cond
41                  ((null arg) "")
42                  ((integerp arg) (integer-to-string arg))
43                  ((floatp arg) (float-to-string arg))
44                  ((stringp arg) arg)
45                  (t
46                   (with-output-to-string (*standard-output*)
47                     (js-expr arg)))))
48              args))
49
50 ;;; Wrap X with a Javascript code to convert the result from
51 ;;; Javascript generalized booleans to T or NIL.
52 (defun js!bool (x)
53   `(if ,x ,(ls-compile t) ,(ls-compile nil)))
54
55 ;;; Concatenate the arguments and wrap them with a self-calling
56 ;;; Javascript anonymous function. It is used to make some Javascript
57 ;;; statements valid expressions and provide a private scope as well.
58 ;;; It could be defined as function, but we could do some
59 ;;; preprocessing in the future.
60 (defmacro js!selfcall (&body body)
61   ``(call (function nil (code ,,@body))))
62
63 (defmacro js!selfcall* (&body body)
64   ``(call (function nil ,,@body)))
65
66
67 ;;; Like CODE, but prefix each line with four spaces. Two versions
68 ;;; of this function are available, because the Ecmalisp version is
69 ;;; very slow and bootstraping was annoying.
70
71 ;;; A Form can return a multiple values object calling VALUES, like
72 ;;; values(arg1, arg2, ...). It will work in any context, as well as
73 ;;; returning an individual object. However, if the special variable
74 ;;; `*multiple-value-p*' is NIL, is granted that only the primary
75 ;;; value will be used, so we can optimize to avoid the VALUES
76 ;;; function call.
77 (defvar *multiple-value-p* nil)
78
79 ;;; Environment
80
81 (def!struct binding
82   name
83   type
84   value
85   declarations)
86
87 (def!struct lexenv
88   variable
89   function
90   block
91   gotag)
92
93 (defun lookup-in-lexenv (name lexenv namespace)
94   (find name (ecase namespace
95                 (variable (lexenv-variable lexenv))
96                 (function (lexenv-function lexenv))
97                 (block    (lexenv-block    lexenv))
98                 (gotag    (lexenv-gotag    lexenv)))
99         :key #'binding-name))
100
101 (defun push-to-lexenv (binding lexenv namespace)
102   (ecase namespace
103     (variable (push binding (lexenv-variable lexenv)))
104     (function (push binding (lexenv-function lexenv)))
105     (block    (push binding (lexenv-block    lexenv)))
106     (gotag    (push binding (lexenv-gotag    lexenv)))))
107
108 (defun extend-lexenv (bindings lexenv namespace)
109   (let ((env (copy-lexenv lexenv)))
110     (dolist (binding (reverse bindings) env)
111       (push-to-lexenv binding env namespace))))
112
113
114 (defvar *environment* (make-lexenv))
115
116 (defvar *variable-counter* 0)
117
118 (defun gvarname (symbol)
119   (declare (ignore symbol))
120   (incf *variable-counter*)
121   (concat "v" (integer-to-string *variable-counter*)))
122
123 (defun translate-variable (symbol)
124   (awhen (lookup-in-lexenv symbol *environment* 'variable)
125     (binding-value it)))
126
127 (defun extend-local-env (args)
128   (let ((new (copy-lexenv *environment*)))
129     (dolist (symbol args new)
130       (let ((b (make-binding :name symbol :type 'variable :value (gvarname symbol))))
131         (push-to-lexenv b new 'variable)))))
132
133 ;;; Toplevel compilations
134 (defvar *toplevel-compilations* nil)
135
136 (defun toplevel-compilation (string)
137   (push string *toplevel-compilations*))
138
139 (defun get-toplevel-compilations ()
140   (reverse *toplevel-compilations*))
141
142 (defun %compile-defmacro (name lambda)
143   (toplevel-compilation (ls-compile `',name))
144   (let ((binding (make-binding :name name :type 'macro :value lambda)))
145     (push-to-lexenv binding  *environment* 'function))
146   name)
147
148 (defun global-binding (name type namespace)
149   (or (lookup-in-lexenv name *environment* namespace)
150       (let ((b (make-binding :name name :type type :value nil)))
151         (push-to-lexenv b *environment* namespace)
152         b)))
153
154 (defun claimp (symbol namespace claim)
155   (let ((b (lookup-in-lexenv symbol *environment* namespace)))
156     (and b (member claim (binding-declarations b)))))
157
158 (defun !proclaim (decl)
159   (case (car decl)
160     (special
161      (dolist (name (cdr decl))
162        (let ((b (global-binding name 'variable 'variable)))
163          (push 'special (binding-declarations b)))))
164     (notinline
165      (dolist (name (cdr decl))
166        (let ((b (global-binding name 'function 'function)))
167          (push 'notinline (binding-declarations b)))))
168     (constant
169      (dolist (name (cdr decl))
170        (let ((b (global-binding name 'variable 'variable)))
171          (push 'constant (binding-declarations b)))))))
172
173 #+jscl
174 (fset 'proclaim #'!proclaim)
175
176 (defun %define-symbol-macro (name expansion)
177   (let ((b (make-binding :name name :type 'macro :value expansion)))
178     (push-to-lexenv b *environment* 'variable)
179     name))
180
181 #+jscl
182 (defmacro define-symbol-macro (name expansion)
183   `(%define-symbol-macro ',name ',expansion))
184
185
186 ;;; Special forms
187
188 (defvar *compilations* nil)
189
190 (defmacro define-compilation (name args &body body)
191   ;; Creates a new primitive `name' with parameters args and
192   ;; @body. The body can access to the local environment through the
193   ;; variable *ENVIRONMENT*.
194   `(push (list ',name (lambda ,args (block ,name ,@body)))
195          *compilations*))
196
197 (define-compilation if (condition true &optional false)
198   `(if (!== ,(ls-compile condition) ,(ls-compile nil))
199        ,(ls-compile true *multiple-value-p*)
200        ,(ls-compile false *multiple-value-p*)))
201
202 (defvar *ll-keywords* '(&optional &rest &key))
203
204 (defun list-until-keyword (list)
205   (if (or (null list) (member (car list) *ll-keywords*))
206       nil
207       (cons (car list) (list-until-keyword (cdr list)))))
208
209 (defun ll-section (keyword ll)
210   (list-until-keyword (cdr (member keyword ll))))
211
212 (defun ll-required-arguments (ll)
213   (list-until-keyword ll))
214
215 (defun ll-optional-arguments-canonical (ll)
216   (mapcar #'ensure-list (ll-section '&optional ll)))
217
218 (defun ll-optional-arguments (ll)
219   (mapcar #'car (ll-optional-arguments-canonical ll)))
220
221 (defun ll-rest-argument (ll)
222   (let ((rest (ll-section '&rest ll)))
223     (when (cdr rest)
224       (error "Bad lambda-list `~S'." ll))
225     (car rest)))
226
227 (defun ll-keyword-arguments-canonical (ll)
228   (flet ((canonicalize (keyarg)
229            ;; Build a canonical keyword argument descriptor, filling
230            ;; the optional fields. The result is a list of the form
231            ;; ((keyword-name var) init-form svar).
232            (let ((arg (ensure-list keyarg)))
233              (cons (if (listp (car arg))
234                        (car arg)
235                        (list (intern (symbol-name (car arg)) "KEYWORD") (car arg)))
236                    (cdr arg)))))
237     (mapcar #'canonicalize (ll-section '&key ll))))
238
239 (defun ll-keyword-arguments (ll)
240   (mapcar (lambda (keyarg) (second (first keyarg)))
241           (ll-keyword-arguments-canonical ll)))
242
243 (defun ll-svars (lambda-list)
244   (let ((args
245          (append
246           (ll-keyword-arguments-canonical lambda-list)
247           (ll-optional-arguments-canonical lambda-list))))
248     (remove nil (mapcar #'third args))))
249
250 (defun lambda-name/docstring-wrapper (name docstring code)
251   (if (or name docstring)
252       (js!selfcall*
253         `(var (func ,code))
254         (when name      `(= (get func "fname") ,name))
255         (when docstring `(= (get func "docstring") ,docstring))
256         `(return func))
257       code))
258
259 (defun lambda-check-argument-count
260     (n-required-arguments n-optional-arguments rest-p)
261   ;; Note: Remember that we assume that the number of arguments of a
262   ;; call is at least 1 (the values argument).
263   (let ((min n-required-arguments)
264         (max (if rest-p 'n/a (+ n-required-arguments n-optional-arguments))))
265     (block nil
266       ;; Special case: a positive exact number of arguments.
267       (when (and (< 0 min) (eql min max))
268         (return `(call |checkArgs| |nargs| ,min)))
269       ;; General case:
270       `(progn
271          ,(when (< 0 min)     `(call |checkArgsAtLeast| |nargs| ,min))
272          ,(when (numberp max) `(call |checkArgsAtMost|  |nargs| ,max))))))
273
274 (defun compile-lambda-optional (ll)
275   (let* ((optional-arguments (ll-optional-arguments-canonical ll))
276          (n-required-arguments (length (ll-required-arguments ll)))
277          (n-optional-arguments (length optional-arguments)))
278     (when optional-arguments
279       `(switch |nargs|
280                ,@(with-collect
281                   (dotimes (idx n-optional-arguments)
282                     (let ((arg (nth idx optional-arguments)))
283                       (collect `(case ,(+ idx n-required-arguments)))
284                       (collect `(= ,(make-symbol (translate-variable (car arg)))
285                                    ,(ls-compile (cadr arg))))
286                       (collect (when (third arg)
287                                  `(= ,(make-symbol (translate-variable (third arg)))
288                                      ,(ls-compile nil))))))
289                   (collect 'default)
290                   (collect '(break)))))))
291
292 (defun compile-lambda-rest (ll)
293   (let ((n-required-arguments (length (ll-required-arguments ll)))
294         (n-optional-arguments (length (ll-optional-arguments ll)))
295         (rest-argument (ll-rest-argument ll)))
296     (when rest-argument
297       (let ((js!rest (make-symbol (translate-variable rest-argument))))
298         `(progn
299            (var (,js!rest ,(ls-compile nil)))
300            (var i)
301            (for ((= i (- |nargs| 1))
302                  (>= i ,(+ n-required-arguments n-optional-arguments))
303                  (post-- i))
304                 (= ,js!rest (object "car" (property |arguments| (+ i 2))
305                                     "cdr" ,js!rest))))))))
306
307 (defun compile-lambda-parse-keywords (ll)
308   (let ((n-required-arguments
309          (length (ll-required-arguments ll)))
310         (n-optional-arguments
311          (length (ll-optional-arguments ll)))
312         (keyword-arguments
313          (ll-keyword-arguments-canonical ll)))
314     `(progn
315        ;; Declare variables
316        ,@(with-collect
317           (dolist (keyword-argument keyword-arguments)
318             (destructuring-bind ((keyword-name var) &optional initform svar)
319                 keyword-argument
320               (declare (ignore keyword-name initform))
321               (collect `(var ,(make-symbol (translate-variable var))))
322               (when svar
323                 (collect
324                     `(var (,(make-symbol (translate-variable svar))
325                             ,(ls-compile nil))))))))
326        
327        ;; Parse keywords
328        ,(flet ((parse-keyword (keyarg)
329                 (destructuring-bind ((keyword-name var) &optional initform svar) keyarg
330                   ;; ((keyword-name var) init-form svar)
331                   `(progn
332                      (for ((= i ,(+ n-required-arguments n-optional-arguments))
333                            (< i |nargs|)
334                            (+= i 2))
335                           ;; ....
336                           (if (=== (property |arguments| (+ i 2))
337                                    ,(ls-compile keyword-name))
338                               (progn
339                                 (= ,(make-symbol (translate-variable var))
340                                    (property |arguments| (+ i 3)))
341                                 ,(when svar `(= ,(make-symbol (translate-variable svar))
342                                                 ,(ls-compile t)))
343                                 (break))))
344                      (if (== i |nargs|)
345                          (= ,(make-symbol (translate-variable var))
346                             ,(ls-compile initform)))))))
347          (when keyword-arguments
348            `(progn
349               (var i)
350               ,@(mapcar #'parse-keyword keyword-arguments))))
351        
352        ;; Check for unknown keywords
353        ,(when keyword-arguments
354          `(progn
355             (var (start ,(+ n-required-arguments n-optional-arguments)))
356             (if (== (% (- |nargs| start) 2) 1)
357                 (throw "Odd number of keyword arguments."))
358             (for ((= i start) (< i |nargs|) (+= i 2))
359                  (if (and ,@(mapcar (lambda (keyword-argument)
360                                  (destructuring-bind ((keyword-name var) &optional initform svar)
361                                      keyword-argument
362                                    (declare (ignore var initform svar))
363                                    `(!== (property |arguments| (+ i 2)) ,(ls-compile keyword-name))))
364                                keyword-arguments))
365                      (throw (+ "Unknown keyword argument "
366                                (call |xstring|
367                                      (property
368                                       (property |arguments| (+ i 2))
369                                       "name")))))))))))
370
371 (defun parse-lambda-list (ll)
372   (values (ll-required-arguments ll)
373           (ll-optional-arguments ll)
374           (ll-keyword-arguments  ll)
375           (ll-rest-argument      ll)))
376
377 ;;; Process BODY for declarations and/or docstrings. Return as
378 ;;; multiple values the BODY without docstrings or declarations, the
379 ;;; list of declaration forms and the docstring.
380 (defun parse-body (body &key declarations docstring)
381   (let ((value-declarations)
382         (value-docstring))
383     ;; Parse declarations
384     (when declarations
385       (do* ((rest body (cdr rest))
386             (form (car rest) (car rest)))
387            ((or (atom form) (not (eq (car form) 'declare)))
388             (setf body rest))
389         (push form value-declarations)))
390     ;; Parse docstring
391     (when (and docstring
392                (stringp (car body))
393                (not (null (cdr body))))
394       (setq value-docstring (car body))
395       (setq body (cdr body)))
396     (values body value-declarations value-docstring)))
397
398 ;;; Compile a lambda function with lambda list LL and body BODY. If
399 ;;; NAME is given, it should be a constant string and it will become
400 ;;; the name of the function. If BLOCK is non-NIL, a named block is
401 ;;; created around the body. NOTE: No block (even anonymous) is
402 ;;; created if BLOCk is NIL.
403 (defun compile-lambda (ll body &key name block)
404   (multiple-value-bind (required-arguments
405                         optional-arguments
406                         keyword-arguments
407                         rest-argument)
408       (parse-lambda-list ll)
409     (multiple-value-bind (body decls documentation)
410         (parse-body body :declarations t :docstring t)
411       (declare (ignore decls))
412       (let ((n-required-arguments (length required-arguments))
413             (n-optional-arguments (length optional-arguments))
414             (*environment* (extend-local-env
415                             (append (ensure-list rest-argument)
416                                     required-arguments
417                                     optional-arguments
418                                     keyword-arguments
419                                     (ll-svars ll)))))
420         (lambda-name/docstring-wrapper name documentation
421          `(function (|values| |nargs| ,@(mapcar (lambda (x)
422                                                   (make-symbol (translate-variable x)))
423                                                 (append required-arguments optional-arguments)))
424                      ;; Check number of arguments
425                     ,(lambda-check-argument-count n-required-arguments
426                                                   n-optional-arguments
427                                                   (or rest-argument keyword-arguments))
428                     ,(compile-lambda-optional ll)
429                     ,(compile-lambda-rest ll)
430                     ,(compile-lambda-parse-keywords ll)
431
432                     ,(let ((*multiple-value-p* t))
433                           (if block
434                               (ls-compile-block `((block ,block ,@body)) t)
435                               (ls-compile-block body t)))))))))
436
437
438 (defun setq-pair (var val)
439   (let ((b (lookup-in-lexenv var *environment* 'variable)))
440     (cond
441       ((and b
442             (eq (binding-type b) 'variable)
443             (not (member 'special (binding-declarations b)))
444             (not (member 'constant (binding-declarations b))))
445        ;; TODO: Unnecesary make-symbol when codegen migration is
446        ;; finished.
447        `(= ,(make-symbol (binding-value b)) ,(ls-compile val)))
448       ((and b (eq (binding-type b) 'macro))
449        (ls-compile `(setf ,var ,val)))
450       (t
451        (ls-compile `(set ',var ,val))))))
452
453
454 (define-compilation setq (&rest pairs)
455   (let ((result nil))
456     (when (null pairs)
457       (return-from setq (ls-compile nil)))
458     (while t
459       (cond
460         ((null pairs)
461          (return))
462         ((null (cdr pairs))
463          (error "Odd pairs in SETQ"))
464         (t
465          (push `,(setq-pair (car pairs) (cadr pairs)) result)
466          (setq pairs (cddr pairs)))))
467     `(progn ,@(reverse result))))
468
469
470 ;;; Compilation of literals an object dumping
471
472 ;;; BOOTSTRAP MAGIC: We record the macro definitions as lists during
473 ;;; the bootstrap. Once everything is compiled, we want to dump the
474 ;;; whole global environment to the output file to reproduce it in the
475 ;;; run-time. However, the environment must contain expander functions
476 ;;; rather than lists. We do not know how to dump function objects
477 ;;; itself, so we mark the list definitions with this object and the
478 ;;; compiler will be called when this object has to be dumped.
479 ;;; Backquote/unquote does a similar magic, but this use is exclusive.
480 ;;;
481 ;;; Indeed, perhaps to compile the object other macros need to be
482 ;;; evaluated. For this reason we define a valid macro-function for
483 ;;; this symbol.
484 (defvar *magic-unquote-marker* (gensym "MAGIC-UNQUOTE"))
485 #-jscl
486 (setf (macro-function *magic-unquote-marker*)
487       (lambda (form &optional environment)
488         (declare (ignore environment))
489         (second form)))
490
491 (defvar *literal-table* nil)
492 (defvar *literal-counter* 0)
493
494 (defun genlit ()
495   (incf *literal-counter*)
496   (concat "l" (integer-to-string *literal-counter*)))
497
498 (defun dump-symbol (symbol)
499   #-jscl
500   (let ((package (symbol-package symbol)))
501     (if (eq package (find-package "KEYWORD"))
502         `(new (call |Symbol| ,(dump-string (symbol-name symbol)) ,(dump-string (package-name package))))
503         `(new (call |Symbol| ,(dump-string (symbol-name symbol))))))
504   #+jscl
505   (let ((package (symbol-package symbol)))
506     (if (null package)
507         `(new (call |Symbol| ,(dump-string (symbol-name symbol))))
508         (ls-compile `(intern ,(symbol-name symbol) ,(package-name package))))))
509
510 (defun dump-cons (cons)
511   (let ((head (butlast cons))
512         (tail (last cons)))
513     `(call |QIList|
514            ,@(mapcar (lambda (x) `(code ,(literal x t))) head)
515            (code ,(literal (car tail) t))
516            (code ,(literal (cdr tail) t)))))
517
518 (defun dump-array (array)
519   (let ((elements (vector-to-list array)))
520     (list-to-vector (mapcar (lambda (x) `(code ,(literal x)))
521                             elements))))
522
523 (defun dump-string (string)
524   `(call |make_lisp_string| ,string))
525
526 (defun literal (sexp &optional recursive)
527   (cond
528     ((integerp sexp) (integer-to-string sexp))
529     ((floatp sexp) (float-to-string sexp))
530     ((characterp sexp) (js-escape-string (string sexp)))
531     (t
532      (or (cdr (assoc sexp *literal-table* :test #'eql))
533          (let ((dumped (typecase sexp
534                          (symbol (dump-symbol sexp))
535                          (string (dump-string sexp))
536                          (cons
537                           ;; BOOTSTRAP MAGIC: See the root file
538                           ;; jscl.lisp and the function
539                           ;; `dump-global-environment' for futher
540                           ;; information.
541                           (if (eq (car sexp) *magic-unquote-marker*)
542                               (ls-compile (second sexp))
543                               (dump-cons sexp)))
544                          (array (dump-array sexp)))))
545            (if (and recursive (not (symbolp sexp)))
546                dumped
547                (let ((jsvar (genlit)))
548                  (push (cons sexp jsvar) *literal-table*)
549                  (toplevel-compilation `(code "var " ,jsvar " = " ,dumped))
550                  (when (keywordp sexp)
551                    (toplevel-compilation `(code ,jsvar ".value = " ,jsvar)))
552                  jsvar)))))))
553
554
555 (define-compilation quote (sexp)
556   (literal sexp))
557
558 (define-compilation %while (pred &rest body)
559   (js!selfcall*
560     `(while (!== ,(ls-compile pred) ,(ls-compile nil))
561        0                                ; TODO: Force
562                                         ; braces. Unnecesary when code
563                                         ; is gone
564        ,(ls-compile-block body))
565    `(return ,(ls-compile nil))))
566
567 (define-compilation function (x)
568   (cond
569     ((and (listp x) (eq (car x) 'lambda))
570      (compile-lambda (cadr x) (cddr x)))
571     ((and (listp x) (eq (car x) 'named-lambda))
572      (destructuring-bind (name ll &rest body) (cdr x)
573        (compile-lambda ll body
574                        :name (symbol-name name)
575                        :block name)))
576     ((symbolp x)
577      (let ((b (lookup-in-lexenv x *environment* 'function)))
578        (if b
579            (binding-value b)
580            (ls-compile `(symbol-function ',x)))))))
581
582
583 (defun make-function-binding (fname)
584   (make-binding :name fname :type 'function :value (gvarname fname)))
585
586 (defun compile-function-definition (list)
587   (compile-lambda (car list) (cdr list)))
588
589 (defun translate-function (name)
590   (let ((b (lookup-in-lexenv name *environment* 'function)))
591     (and b (binding-value b))))
592
593 (define-compilation flet (definitions &rest body)
594   (let* ((fnames (mapcar #'car definitions))
595          (cfuncs (mapcar (lambda (def)
596                            (compile-lambda (cadr def)
597                                            `((block ,(car def)
598                                                ,@(cddr def)))))
599                          definitions))
600          (*environment*
601           (extend-lexenv (mapcar #'make-function-binding fnames)
602                          *environment*
603                          'function)))
604     `(call (function ,(mapcar #'make-symbol (mapcar #'translate-function fnames))
605                 ,(ls-compile-block body t))
606            ,@cfuncs)))
607
608 (define-compilation labels (definitions &rest body)
609   (let* ((fnames (mapcar #'car definitions))
610          (*environment*
611           (extend-lexenv (mapcar #'make-function-binding fnames)
612                          *environment*
613                          'function)))
614     (js!selfcall*
615       `(progn
616          ,@(mapcar (lambda (func)
617                      `(var (,(make-symbol (translate-function (car func)))
618                              ,(compile-lambda (cadr func)
619                                               `((block ,(car func) ,@(cddr func)))))))
620                    definitions))
621       (ls-compile-block body t))))
622
623
624 (defvar *compiling-file* nil)
625 (define-compilation eval-when-compile (&rest body)
626   (if *compiling-file*
627       (progn
628         (eval (cons 'progn body))
629         (ls-compile 0))
630       (ls-compile `(progn ,@body))))
631
632 (defmacro define-transformation (name args form)
633   `(define-compilation ,name ,args
634      (ls-compile ,form)))
635
636 (define-compilation progn (&rest body)
637   (if (null (cdr body))
638       (ls-compile (car body) *multiple-value-p*)
639       `(progn
640          ,@(append (mapcar #'ls-compile (butlast body))
641                    (list (ls-compile (car (last body)) t))))))
642
643 (define-compilation macrolet (definitions &rest body)
644   (let ((*environment* (copy-lexenv *environment*)))
645     (dolist (def definitions)
646       (destructuring-bind (name lambda-list &body body) def
647         (let ((binding (make-binding :name name :type 'macro :value
648                                      (let ((g!form (gensym)))
649                                        `(lambda (,g!form)
650                                           (destructuring-bind ,lambda-list ,g!form
651                                             ,@body))))))
652           (push-to-lexenv binding  *environment* 'function))))
653     (ls-compile `(progn ,@body) *multiple-value-p*)))
654
655
656 (defun special-variable-p (x)
657   (and (claimp x 'variable 'special) t))
658
659 ;;; Wrap CODE to restore the symbol values of the dynamic
660 ;;; bindings. BINDINGS is a list of pairs of the form
661 ;;; (SYMBOL . PLACE),  where PLACE is a Javascript variable
662 ;;; name to initialize the symbol value and where to stored
663 ;;; the old value.
664 (defun let-binding-wrapper (bindings body)
665   (when (null bindings)
666     (return-from let-binding-wrapper body))
667   `(progn
668      (try (var tmp)
669           ,@(with-collect
670              (dolist (b bindings)
671                (let ((s (ls-compile `',(car b))))
672                  (collect `(= tmp (get ,s "value")))
673                  (collect `(= (get ,s "value") ,(cdr b)))
674                  (collect `(= ,(cdr b) tmp)))))
675           ,body)
676      (finally
677       ,@(with-collect
678          (dolist (b bindings)
679            (let ((s (ls-compile `(quote ,(car b)))))
680              (collect `(= (get ,s "value") ,(cdr b)))))))))
681
682 (define-compilation let (bindings &rest body)
683   (let* ((bindings (mapcar #'ensure-list bindings))
684          (variables (mapcar #'first bindings))
685          (cvalues (mapcar #'ls-compile (mapcar #'second bindings)))
686          (*environment* (extend-local-env (remove-if #'special-variable-p variables)))
687          (dynamic-bindings))
688     `(call (function ,(mapcar (lambda (x)
689                                 (if (special-variable-p x)
690                                     (let ((v (gvarname x)))
691                                       (push (cons x (make-symbol v)) dynamic-bindings)
692                                       (make-symbol v))
693                                     (make-symbol (translate-variable x))))
694                               variables)
695                      ,(let ((body (ls-compile-block body t t)))
696                            `,(let-binding-wrapper dynamic-bindings body)))
697            ,@cvalues)))
698
699
700 ;;; Return the code to initialize BINDING, and push it extending the
701 ;;; current lexical environment if the variable is not special.
702 (defun let*-initialize-value (binding)
703   (let ((var (first binding))
704         (value (second binding)))
705     (if (special-variable-p var)
706         `(code ,(ls-compile `(setq ,var ,value)) ";" )
707         (let* ((v (gvarname var))
708                (b (make-binding :name var :type 'variable :value v)))
709           (prog1 `(code "var " ,v " = " ,(ls-compile value) ";" )
710             (push-to-lexenv b *environment* 'variable))))))
711
712 ;;; Wrap BODY to restore the symbol values of SYMBOLS after body. It
713 ;;; DOES NOT generate code to initialize the value of the symbols,
714 ;;; unlike let-binding-wrapper.
715 (defun let*-binding-wrapper (symbols body)
716   (when (null symbols)
717     (return-from let*-binding-wrapper body))
718   (let ((store (mapcar (lambda (s) (cons s (gvarname s)))
719                        (remove-if-not #'special-variable-p symbols))))
720     `(code
721       "try {"
722       (code
723        ,@(mapcar (lambda (b)
724                    (let ((s (ls-compile `(quote ,(car b)))))
725                      `(code "var " ,(cdr b) " = " ,s ".value;" )))
726                  store)
727        ,body)
728       "}"
729       "finally {"
730       (code
731        ,@(mapcar (lambda (b)
732                    (let ((s (ls-compile `(quote ,(car b)))))
733                      `(code ,s ".value" " = " ,(cdr b) ";" )))
734                  store))
735       "}" )))
736
737 (define-compilation let* (bindings &rest body)
738   (let ((bindings (mapcar #'ensure-list bindings))
739         (*environment* (copy-lexenv *environment*)))
740     (js!selfcall
741       (let ((specials (remove-if-not #'special-variable-p (mapcar #'first bindings)))
742             (body `(code ,@(mapcar #'let*-initialize-value bindings)
743                          ,(ls-compile-block body t t))))
744         (let*-binding-wrapper specials body)))))
745
746
747 (define-compilation block (name &rest body)
748   ;; We use Javascript exceptions to implement non local control
749   ;; transfer. Exceptions has dynamic scoping, so we use a uniquely
750   ;; generated object to identify the block. The instance of a empty
751   ;; array is used to distinguish between nested dynamic Javascript
752   ;; exceptions. See https://github.com/davazp/jscl/issues/64 for
753   ;; futher details.
754   (let* ((idvar (gvarname name))
755          (b (make-binding :name name :type 'block :value idvar)))
756     (when *multiple-value-p*
757       (push 'multiple-value (binding-declarations b)))
758     (let* ((*environment* (extend-lexenv (list b) *environment* 'block))
759            (cbody (ls-compile-block body t)))
760       (if (member 'used (binding-declarations b))
761           (js!selfcall
762             "try {"
763             "var " idvar " = [];"
764             `(code ,cbody)
765             "}"
766             "catch (cf){"
767             "    if (cf.type == 'block' && cf.id == " idvar ")"
768             (if *multiple-value-p*
769                 "        return values.apply(this, forcemv(cf.values));"
770                 "        return cf.values;")
771
772             "    else"
773             "        throw cf;"
774             "}" )
775           (js!selfcall cbody)))))
776
777 (define-compilation return-from (name &optional value)
778   (let* ((b (lookup-in-lexenv name *environment* 'block))
779          (multiple-value-p (member 'multiple-value (binding-declarations b))))
780     (when (null b)
781       (error "Return from unknown block `~S'." (symbol-name name)))
782     (push 'used (binding-declarations b))
783     ;; The binding value is the name of a variable, whose value is the
784     ;; unique identifier of the block as exception. We can't use the
785     ;; variable name itself, because it could not to be unique, so we
786     ;; capture it in a closure.
787     (js!selfcall*
788       (when multiple-value-p
789         `(var (|values| |mv|)))
790       `(throw
791            (object
792             "type" "block"
793             "id" ,(make-symbol (binding-value b))
794             "values" ,(ls-compile value multiple-value-p)
795             "message" ,(concat "Return from unknown block '" (symbol-name name) "'."))))))
796
797 (define-compilation catch (id &rest body)
798   (js!selfcall*
799     `(var (|id| ,(ls-compile id)))
800     `(try
801       ,(ls-compile-block body t))
802     `(catch (|cf|)
803        (if (and (== (get |cf| "type") "catch")
804                 (== (get |cf| "id") |id|))
805            ,(if *multiple-value-p*
806                 `(return (call (get |values| "apply")
807                                this
808                                (call |forcemv| (get |cf| "values"))))
809                 `(return (call (get |pv| "apply")
810                                this
811                                (call |forcemv| (get |cf| "values")))))
812            (throw |cf|)))))
813
814 (define-compilation throw (id value)
815   (js!selfcall*
816     `(var (|values| |mv|))
817     `(throw (object
818              |type| "catch"
819              |id| ,(ls-compile id)
820              |values| ,(ls-compile value t)
821              |message| "Throw uncatched."))))
822
823 (defun go-tag-p (x)
824   (or (integerp x) (symbolp x)))
825
826 (defun declare-tagbody-tags (tbidx body)
827   (let* ((go-tag-counter 0)
828          (bindings
829           (mapcar (lambda (label)
830                     (let ((tagidx (incf go-tag-counter)))
831                       (make-binding :name label :type 'gotag :value (list tbidx tagidx))))
832                   (remove-if-not #'go-tag-p body))))
833     (extend-lexenv bindings *environment* 'gotag)))
834
835 (define-compilation tagbody (&rest body)
836   ;; Ignore the tagbody if it does not contain any go-tag. We do this
837   ;; because 1) it is easy and 2) many built-in forms expand to a
838   ;; implicit tagbody, so we save some space.
839   (unless (some #'go-tag-p body)
840     (return-from tagbody (ls-compile `(progn ,@body nil))))
841   ;; The translation assumes the first form in BODY is a label
842   (unless (go-tag-p (car body))
843     (push (gensym "START") body))
844   ;; Tagbody compilation
845   (let ((branch (gvarname 'branch))
846         (tbidx (gvarname 'tbidx)))
847     (let ((*environment* (declare-tagbody-tags tbidx body))
848           initag)
849       (let ((b (lookup-in-lexenv (first body) *environment* 'gotag)))
850         (setq initag (second (binding-value b))))
851       (js!selfcall
852         ;; TAGBODY branch to take
853         "var " branch " = " initag ";"
854         "var " tbidx " = [];"
855         "tbloop:"
856         "while (true) {"
857         `(code "try {"
858                ,(let ((content nil))
859                   `(code "switch(" ,branch "){"
860                         "case " ,initag ":"
861                         ,@(dolist (form (cdr body) (reverse content))
862                           (push (if (not (go-tag-p form))
863                                     `(code ,(ls-compile form) ";" )
864                                     (let ((b (lookup-in-lexenv form *environment* 'gotag)))
865                                       `(code "case " ,(second (binding-value b)) ":" )))
866                                 content))
867                            "default:"
868                            "    break tbloop;"
869                            "}" ))
870                "}"
871                "catch (jump) {"
872                "    if (jump.type == 'tagbody' && jump.id == " ,tbidx ")"
873                "        " ,branch " = jump.label;"
874                "    else"
875                "        throw(jump);"
876                "}" )
877         "}"
878         "return " (ls-compile nil) ";" ))))
879
880 (define-compilation go (label)
881   (let ((b (lookup-in-lexenv label *environment* 'gotag))
882         (n (cond
883              ((symbolp label) (symbol-name label))
884              ((integerp label) (integer-to-string label)))))
885     (when (null b)
886       (error "Unknown tag `~S'" label))
887     (js!selfcall*
888       `(throw
889            (object
890             "type" "tagbody"
891             "id" ,(make-symbol (first (binding-value b)))
892             "label" ,(second (binding-value b))
893             "message" ,(concat "Attempt to GO to non-existing tag " n))))))
894
895 (define-compilation unwind-protect (form &rest clean-up)
896   (js!selfcall*
897     `(var (|ret| ,(ls-compile nil)))
898     `(try
899        (= |ret| ,(ls-compile form)))
900     `(finally
901       ,(ls-compile-block clean-up))
902     `(return |ret|)))
903
904 (define-compilation multiple-value-call (func-form &rest forms)
905   (js!selfcall
906     "var func = " (ls-compile func-form) ";"
907     "var args = [" (if *multiple-value-p* "values" "pv") ", 0];"
908     "return "
909     (js!selfcall
910       "var values = mv;"
911       "var vs;"
912       `(code
913         ,@(mapcar (lambda (form)
914                     `(code "vs = " ,(ls-compile form t) ";"
915                            "if (typeof vs === 'object' && 'multiple-value' in vs)"
916                            (code " args = args.concat(vs);" )
917                            " else "
918                            (code "args.push(vs);" )))
919                   forms))
920       "args[1] = args.length-2;"
921       "return func.apply(window, args);" ) ";" ))
922
923 (define-compilation multiple-value-prog1 (first-form &rest forms)
924   (js!selfcall
925     "var args = " (ls-compile first-form *multiple-value-p*) ";"
926     (ls-compile-block forms)
927     "return args;" ))
928
929 (define-transformation backquote (form)
930   (bq-completely-process form))
931
932
933 ;;; Primitives
934
935 (defvar *builtins* nil)
936
937 (defmacro define-raw-builtin (name args &body body)
938   ;; Creates a new primitive function `name' with parameters args and
939   ;; @body. The body can access to the local environment through the
940   ;; variable *ENVIRONMENT*.
941   `(push (list ',name (lambda ,args (block ,name ,@body)))
942          *builtins*))
943
944 (defmacro define-builtin (name args &body body)
945   `(define-raw-builtin ,name ,args
946      (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args)
947        ,@body)))
948
949 ;;; DECLS is a list of (JSVARNAME TYPE LISPFORM) declarations.
950 (defmacro type-check (decls &body body)
951   `(js!selfcall
952      ,@(mapcar (lambda (decl)
953                  `(let ((name ,(first decl))
954                         (value ,(third decl)))
955                     `(code "var " ,name " = " ,value ";" )))
956                decls)
957      ,@(mapcar (lambda (decl)
958                  `(let ((name ,(first decl))
959                         (type ,(second decl)))
960                     `(code "if (typeof " ,name " != '" ,type "')"
961                            (code "throw 'The value ' + "
962                                  ,name
963                                  " + ' is not a type "
964                                  ,type
965                                  ".';"
966                                  ))))
967                decls)
968      `(code "return " ,,@body ";" )))
969
970 ;;; VARIABLE-ARITY compiles variable arity operations. ARGS stands for
971 ;;; a variable which holds a list of forms. It will compile them and
972 ;;; store the result in some Javascript variables. BODY is evaluated
973 ;;; with ARGS bound to the list of these variables to generate the
974 ;;; code which performs the transformation on these variables.
975
976 (defun variable-arity-call (args function)
977   (unless (consp args)
978     (error "ARGS must be a non-empty list"))
979   (let ((counter 0)
980         (fargs '())
981         (prelude '()))
982     (dolist (x args)
983       (cond
984         ((or (floatp x) (numberp x)) (push x fargs))
985         (t (let ((v (make-symbol (code "x" (incf counter)))))
986              (push v fargs)
987              (push `(code "var " ,v " = " ,(ls-compile x) ";"
988                           "if (typeof " ,v " !== 'number') throw 'Not a number!';")
989                    prelude)))))
990     (js!selfcall
991       `(code ,@(reverse prelude))
992       (funcall function (reverse fargs)))))
993
994
995 (defmacro variable-arity (args &body body)
996   (unless (symbolp args)
997     (error "`~S' is not a symbol." args))
998   `(variable-arity-call ,args
999                         (lambda (,args)
1000                           `(code "return " ,,@body ";" ))))
1001
1002 (defun num-op-num (x op y)
1003   (type-check (("x" "number" x) ("y" "number" y))
1004     `(code "x" ,op "y")))
1005
1006 (define-raw-builtin + (&rest numbers)
1007   (if (null numbers)
1008       0
1009       (variable-arity numbers
1010         `(+ ,@numbers))))
1011
1012 (define-raw-builtin - (x &rest others)
1013   (let ((args (cons x others)))
1014     (variable-arity args `(- ,@args))))
1015
1016 (define-raw-builtin * (&rest numbers)
1017   (if (null numbers)
1018       1
1019       (variable-arity numbers `(* ,@numbers))))
1020
1021 (define-raw-builtin / (x &rest others)
1022   (let ((args (cons x others)))
1023     (variable-arity args
1024       (if (null others)
1025           `(/ 1 ,(car args))
1026           (reduce (lambda (x y) `(/ ,x ,y))
1027                   args)))))
1028
1029 (define-builtin mod (x y) (num-op-num x "%" y))
1030
1031
1032 (defun comparison-conjuntion (vars op)
1033   (cond
1034     ((null (cdr vars))
1035      'true)
1036     ((null (cddr vars))
1037      `(,op ,(car vars) ,(cadr vars)))
1038     (t
1039      `(and (,op ,(car vars) ,(cadr vars))
1040            ,(comparison-conjuntion (cdr vars) op)))))
1041
1042 (defmacro define-builtin-comparison (op sym)
1043   `(define-raw-builtin ,op (x &rest args)
1044      (let ((args (cons x args)))
1045        (variable-arity args
1046          (js!bool (comparison-conjuntion args ',sym))))))
1047
1048 (define-builtin-comparison > >)
1049 (define-builtin-comparison < <)
1050 (define-builtin-comparison >= >=)
1051 (define-builtin-comparison <= <=)
1052 (define-builtin-comparison = ==)
1053 (define-builtin-comparison /= !=)
1054
1055 (define-builtin numberp (x)
1056   (js!bool `(== (typeof ,x) "number")))
1057
1058 (define-builtin floor (x)
1059   (type-check (("x" "number" x))
1060     "Math.floor(x)"))
1061
1062 (define-builtin expt (x y)
1063   (type-check (("x" "number" x)
1064                ("y" "number" y))
1065     "Math.pow(x, y)"))
1066
1067 (define-builtin float-to-string (x)
1068   (type-check (("x" "number" x))
1069     "make_lisp_string(x.toString())"))
1070
1071 (define-builtin cons (x y)
1072   `(object "car" ,x "cdr" ,y))
1073
1074 (define-builtin consp (x)
1075   (js!bool
1076    (js!selfcall
1077      "var tmp = " x ";"
1078      "return (typeof tmp == 'object' && 'car' in tmp);" )))
1079
1080 (define-builtin car (x)
1081   (js!selfcall*
1082     `(var (tmp ,x))
1083     `(return (if (=== tmp ,(ls-compile nil))
1084                  ,(ls-compile nil)
1085                  (get tmp "car")))))
1086
1087 (define-builtin cdr (x)
1088   (js!selfcall*
1089     `(var (tmp ,x))
1090     `(return (if (=== tmp ,(ls-compile nil))
1091                  ,(ls-compile nil)
1092                  (get tmp "cdr")))))
1093
1094 (define-builtin rplaca (x new)
1095   (type-check (("x" "object" x))
1096     `(code "(x.car = " ,new ", x)")))
1097
1098 (define-builtin rplacd (x new)
1099   (type-check (("x" "object" x))
1100     `(code "(x.cdr = " ,new ", x)")))
1101
1102 (define-builtin symbolp (x)
1103   (js!bool `(instanceof ,x |Symbol|)))
1104
1105 (define-builtin make-symbol (name)
1106   `(new (call |Symbol| ,name)))
1107
1108 (define-builtin symbol-name (x)
1109   `(get ,x "name"))
1110
1111 (define-builtin set (symbol value)
1112   `(= (get ,symbol "value") ,value))
1113
1114 (define-builtin fset (symbol value)
1115   `(= (get ,symbol "fvalue") ,value))
1116
1117 (define-builtin boundp (x)
1118   (js!bool `(!== (get ,x "value") undefined)))
1119
1120 (define-builtin fboundp (x)
1121   (js!bool `(!== (get ,x "fvalue") undefined)))
1122
1123 (define-builtin symbol-value (x)
1124   (js!selfcall*
1125     `(var (symbol ,x)
1126           (value (get symbol "value")))
1127     `(if (=== value undefined)
1128          (throw (+ "Variable `" (call |xstring| (get symbol "name")) "' is unbound.")))
1129     `(return value)))
1130
1131 (define-builtin symbol-function (x)
1132   (js!selfcall*
1133     `(var (symbol ,x)
1134           (func (get symbol "fvalue")))
1135     `(if (=== func undefined)
1136          (throw (+ "Function `" (call |xstring| (get symbol "name")) "' is undefined.")))
1137     `(return func)))
1138
1139 (define-builtin symbol-plist (x)
1140   `(or (get ,x "plist") ,(ls-compile nil)))
1141
1142 (define-builtin lambda-code (x)
1143   `(call |make_lisp_string| (call (get ,x "toString"))))
1144
1145 (define-builtin eq (x y)
1146   (js!bool `(=== ,x ,y)))
1147
1148 (define-builtin char-code (x)
1149   (type-check (("x" "string" x))
1150     "char_to_codepoint(x)"))
1151
1152 (define-builtin code-char (x)
1153   (type-check (("x" "number" x))
1154     "char_from_codepoint(x)"))
1155
1156 (define-builtin characterp (x)
1157   (js!bool
1158    (js!selfcall*
1159      `(var (x ,x))
1160      `(return (and (== (typeof x) "string")
1161                    (or (== (get x "length") 1)
1162                        (== (get x "length") 2)))))))
1163
1164 (define-builtin char-upcase (x)
1165   `(call |safe_char_upcase| ,x))
1166
1167 (define-builtin char-downcase (x)
1168   `(call |safe_char_downcase| ,x))
1169
1170 (define-builtin stringp (x)
1171   (js!bool
1172    (js!selfcall*
1173      `(var (x ,x))
1174      `(return (and (and (===(typeof x) "object")
1175                         (in "length" x))
1176                    (== (get x "stringp") 1))))))
1177
1178 (define-raw-builtin funcall (func &rest args)
1179   (js!selfcall*
1180     `(var (f ,(ls-compile func)))
1181     `(return (call (if (=== (typeof f) "function")
1182                        f
1183                        (get f "fvalue"))
1184                    ,@(list* (if *multiple-value-p* '|values| '|pv|)
1185                             (length args)
1186                             (mapcar #'ls-compile args))))))
1187
1188 (define-raw-builtin apply (func &rest args)
1189   (if (null args)
1190       `(code "(" ,(ls-compile func) ")()")
1191       (let ((args (butlast args))
1192             (last (car (last args))))
1193         (js!selfcall
1194           "var f = " (ls-compile func) ";"
1195           "var args = [" `(code
1196                            ,@(interleave (list* (if *multiple-value-p* "values" "pv")
1197                                                 (integer-to-string (length args))
1198                                                 (mapcar #'ls-compile args))
1199                                          ", "))
1200           "];"
1201           "var tail = (" (ls-compile last) ");"
1202           "while (tail != " (ls-compile nil) "){"
1203           "    args.push(tail.car);"
1204           "    args[1] += 1;"
1205           "    tail = tail.cdr;"
1206           "}"
1207           "return (typeof f === 'function'? f : f.fvalue).apply(this, args);" ))))
1208
1209 (define-builtin js-eval (string)
1210   (if *multiple-value-p*
1211       (js!selfcall*
1212         `(var (v (call |globalEval| (call |xstring| ,string))))
1213         `(return (call (get |values| "apply") this (call |forcemv| v))))
1214       `(call |globalEval| (call |xstring| ,string))))
1215
1216 (define-builtin %throw (string)
1217   (js!selfcall* `(throw ,string)))
1218
1219 (define-builtin functionp (x)
1220   (js!bool `(=== (typeof ,x) "function")))
1221
1222 (define-builtin %write-string (x)
1223   `(call (get |lisp| "write") ,x))
1224
1225 (define-builtin /debug (x)
1226   `(call (get |console| "log") (call |xstring| ,x)))
1227
1228
1229 ;;; Storage vectors. They are used to implement arrays and (in the
1230 ;;; future) structures.
1231
1232 (define-builtin storage-vector-p (x)
1233   (js!bool
1234    (js!selfcall*
1235      `(var (x ,x))
1236      `(return (and (=== (typeof x) "object") (in "length" x))))))
1237
1238 (define-builtin make-storage-vector (n)
1239   (js!selfcall*
1240     `(var (r #()))
1241     `(= (get r "length") ,n)
1242     `(return r)))
1243
1244 (define-builtin storage-vector-size (x)
1245   `(get ,x "length"))
1246
1247 (define-builtin resize-storage-vector (vector new-size)
1248   `(= (get ,vector "length") ,new-size))
1249
1250 (define-builtin storage-vector-ref (vector n)
1251   (js!selfcall*
1252     `(var (x (property ,vector ,n)))
1253     `(if (=== x undefined) (throw "Out of range."))
1254     `(return x)))
1255
1256 (define-builtin storage-vector-set (vector n value)
1257   (js!selfcall*
1258     `(var (x ,vector))
1259     `(var (i ,n))
1260     `(if (or (< i 0) (>= i (get x "length")))
1261          (throw "Out of range."))
1262     `(return (= (property x i) ,value))))
1263
1264 (define-builtin concatenate-storage-vector (sv1 sv2)
1265   (js!selfcall*
1266     `(var (sv1 ,sv1))
1267     `(var (r (call (get sv1 "concat") ,sv2)))
1268     `(= (get r "type") (get sv1 "type"))
1269     `(= (get r "stringp") (get sv1 "stringp"))
1270     `(return r)))
1271
1272 (define-builtin get-internal-real-time ()
1273   `(call (get (new (call |Date|)) "getTime")))
1274
1275 (define-builtin values-array (array)
1276   (if *multiple-value-p*
1277       `(call (get |values| "apply") this ,array)
1278       `(call (get |pv| "apply") this ,array)))
1279
1280 (define-raw-builtin values (&rest args)
1281   (if *multiple-value-p*
1282       `(call |values| ,@(mapcar #'ls-compile args))
1283       `(call |pv| ,@(mapcar #'ls-compile args))))
1284
1285 ;;; Javascript FFI
1286
1287 (define-builtin new ()
1288   '(object))
1289
1290 (define-raw-builtin oget* (object key &rest keys)
1291   (js!selfcall*
1292     `(progn
1293        (var (tmp (property ,(ls-compile object) (call |xstring| ,(ls-compile key)))))
1294        ,@(mapcar (lambda (key)
1295                    `(progn
1296                       (if (=== tmp undefined) (return ,(ls-compile nil)))
1297                       (= tmp (property tmp (call |xstring| ,(ls-compile key))))))
1298                  keys))
1299     `(return (if (=== tmp undefined) ,(ls-compile nil) tmp))))
1300
1301 (define-raw-builtin oset* (value object key &rest keys)
1302   (let ((keys (cons key keys)))
1303     (js!selfcall*
1304       `(progn
1305          (var (obj ,(ls-compile object)))
1306          ,@(mapcar (lambda (key)
1307                      `(progn
1308                         (= obj (property obj (call |xstring| ,(ls-compile key))))
1309                         (if (=== object undefined)
1310                             (throw "Impossible to set object property."))))
1311                    (butlast keys))
1312          (var (tmp
1313                (= (property obj (call |xstring| ,(ls-compile (car (last keys)))))
1314                   ,(ls-compile value))))
1315          (return (if (=== tmp undefined)
1316                      ,(ls-compile nil)
1317                      tmp))))))
1318
1319 (define-raw-builtin oget (object key &rest keys)
1320   `(call |js_to_lisp| ,(ls-compile `(oget* ,object ,key ,@keys))))
1321
1322 (define-raw-builtin oset (value object key &rest keys)
1323   (ls-compile `(oset* (lisp-to-js ,value) ,object ,key ,@keys)))
1324
1325 (define-builtin objectp (x)
1326   (js!bool `(=== (typeof ,x) "object")))
1327
1328 (define-builtin lisp-to-js (x) `(call |lisp_to_js| ,x))
1329 (define-builtin js-to-lisp (x) `(call |js_to_lisp| ,x))
1330
1331
1332 (define-builtin in (key object)
1333   (js!bool `(in (call |xstring| ,key) ,object)))
1334
1335 (define-builtin map-for-in (function object)
1336   (js!selfcall*
1337     `(var (f ,function)
1338           (g (if (=== (typeof f) "function") f (get f "fvalue")))
1339           (o ,object))
1340     `(for-in (key o)
1341        (call g ,(if *multiple-value-p* '|values| '|pv|) 1 (get o "key")))
1342     `(return ,(ls-compile nil))))
1343
1344 (define-compilation %js-vref (var)
1345   `(call |js_to_lisp| ,(make-symbol var)))
1346
1347 (define-compilation %js-vset (var val)
1348   `(= ,(make-symbol var) (call |lisp_to_js| ,(ls-compile val))))
1349
1350 (define-setf-expander %js-vref (var)
1351   (let ((new-value (gensym)))
1352     (unless (stringp var)
1353       (error "`~S' is not a string." var))
1354     (values nil
1355             (list var)
1356             (list new-value)
1357             `(%js-vset ,var ,new-value)
1358             `(%js-vref ,var))))
1359
1360
1361 #-jscl
1362 (defvar *macroexpander-cache*
1363   (make-hash-table :test #'eq))
1364
1365 (defun !macro-function (symbol)
1366   (unless (symbolp symbol)
1367     (error "`~S' is not a symbol." symbol))
1368   (let ((b (lookup-in-lexenv symbol *environment* 'function)))
1369     (if (and b (eq (binding-type b) 'macro))
1370         (let ((expander (binding-value b)))
1371           (cond
1372             #-jscl
1373             ((gethash b *macroexpander-cache*)
1374              (setq expander (gethash b *macroexpander-cache*)))
1375             ((listp expander)
1376              (let ((compiled (eval expander)))
1377                ;; The list representation are useful while
1378                ;; bootstrapping, as we can dump the definition of the
1379                ;; macros easily, but they are slow because we have to
1380                ;; evaluate them and compile them now and again. So, let
1381                ;; us replace the list representation version of the
1382                ;; function with the compiled one.
1383                ;;
1384                #+jscl (setf (binding-value b) compiled)
1385                #-jscl (setf (gethash b *macroexpander-cache*) compiled)
1386                (setq expander compiled))))
1387           expander)
1388         nil)))
1389
1390 (defun !macroexpand-1 (form)
1391   (cond
1392     ((symbolp form)
1393      (let ((b (lookup-in-lexenv form *environment* 'variable)))
1394        (if (and b (eq (binding-type b) 'macro))
1395            (values (binding-value b) t)
1396            (values form nil))))
1397     ((and (consp form) (symbolp (car form)))
1398      (let ((macrofun (!macro-function (car form))))
1399        (if macrofun
1400            (values (funcall macrofun (cdr form)) t)
1401            (values form nil))))
1402     (t
1403      (values form nil))))
1404
1405 (defun compile-funcall (function args)
1406   (let* ((values-funcs (if *multiple-value-p* "values" "pv"))
1407          (arglist `(code "(" ,@(interleave (list* values-funcs
1408                                                   (integer-to-string (length args))
1409                                                   (mapcar #'ls-compile args))
1410                                            ", ")
1411                          ")")))
1412     (unless (or (symbolp function)
1413                 (and (consp function)
1414                      (member (car function) '(lambda oget))))
1415       (error "Bad function designator `~S'" function))
1416     (cond
1417       ((translate-function function)
1418        `(code ,(translate-function function) ,arglist))
1419       ((and (symbolp function)
1420             #+jscl (eq (symbol-package function) (find-package "COMMON-LISP"))
1421             #-jscl t)
1422        `(code ,(ls-compile `',function) ".fvalue" ,arglist))
1423       #+jscl((symbolp function)
1424        `(code ,(ls-compile `#',function) ,arglist))
1425       ((and (consp function) (eq (car function) 'lambda))
1426        `(code ,(ls-compile `#',function) ,arglist))
1427       ((and (consp function) (eq (car function) 'oget))
1428        `(code ,(ls-compile function) ,arglist))
1429       (t
1430        (error "Bad function descriptor")))))
1431
1432 (defun ls-compile-block (sexps &optional return-last-p decls-allowed-p)
1433   (multiple-value-bind (sexps decls)
1434       (parse-body sexps :declarations decls-allowed-p)
1435     (declare (ignore decls))
1436     (if return-last-p
1437         `(code ,(ls-compile-block (butlast sexps) nil decls-allowed-p)
1438                "return " ,(ls-compile (car (last sexps)) *multiple-value-p*) ";")
1439         `(code
1440           ,@(interleave (mapcar #'ls-compile sexps) ";
1441 " *newline*)
1442           ";" ,*newline*))))
1443
1444 (defun ls-compile* (sexp &optional multiple-value-p)
1445   (multiple-value-bind (sexp expandedp) (!macroexpand-1 sexp)
1446     (when expandedp
1447       (return-from ls-compile* (ls-compile sexp multiple-value-p)))
1448     ;; The expression has been macroexpanded. Now compile it!
1449     (let ((*multiple-value-p* multiple-value-p))
1450       (cond
1451         ((symbolp sexp)
1452          (let ((b (lookup-in-lexenv sexp *environment* 'variable)))
1453            (cond
1454              ((and b (not (member 'special (binding-declarations b))))
1455               (binding-value b))
1456              ((or (keywordp sexp)
1457                   (and b (member 'constant (binding-declarations b))))
1458               `(code ,(ls-compile `',sexp) ".value"))
1459              (t
1460               (ls-compile `(symbol-value ',sexp))))))
1461         ((or (integerp sexp) (floatp sexp) (characterp sexp) (stringp sexp) (arrayp sexp))
1462          (literal sexp))
1463         ((listp sexp)
1464          (let ((name (car sexp))
1465                (args (cdr sexp)))
1466            (cond
1467              ;; Special forms
1468              ((assoc name *compilations*)
1469               (let ((comp (second (assoc name *compilations*))))
1470                 (apply comp args)))
1471              ;; Built-in functions
1472              ((and (assoc name *builtins*)
1473                    (not (claimp name 'function 'notinline)))
1474               (let ((comp (second (assoc name *builtins*))))
1475                 (apply comp args)))
1476              (t
1477               (compile-funcall name args)))))
1478         (t
1479          (error "How should I compile `~S'?" sexp))))))
1480
1481 (defun ls-compile (sexp &optional multiple-value-p)
1482   `(code "(" ,(ls-compile* sexp multiple-value-p) ")"))
1483
1484
1485 (defvar *compile-print-toplevels* nil)
1486
1487 (defun truncate-string (string &optional (width 60))
1488   (let ((n (or (position #\newline string)
1489                (min width (length string)))))
1490     (subseq string 0 n)))
1491
1492 (defun convert-toplevel (sexp &optional multiple-value-p)
1493   (let ((*toplevel-compilations* nil))
1494     (cond
1495       ;; Non-empty toplevel progn
1496       ((and (consp sexp)
1497             (eq (car sexp) 'progn)
1498             (cdr sexp))
1499        `(progn
1500           ,@(mapcar (lambda (s) (convert-toplevel s t))
1501                     (cdr sexp))))
1502       (t
1503        (when *compile-print-toplevels*
1504          (let ((form-string (prin1-to-string sexp)))
1505            (format t "Compiling ~a..." (truncate-string form-string))))
1506        (let ((code (ls-compile sexp multiple-value-p)))
1507          `(code
1508            ,@(interleave (get-toplevel-compilations) ";
1509 " t)
1510            ,(when code
1511                   `(code ,code ";"))))))))
1512
1513 (defun ls-compile-toplevel (sexp &optional multiple-value-p)
1514   (with-output-to-string (*standard-output*)
1515     (js (convert-toplevel sexp multiple-value-p))))