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