149a7779fbb38d971d882b9e9e08aceacc87abaa
[jscl.git] / ecmalisp.lisp
1 ;;; ecmalisp.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; This program 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 ;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 ;;; This code is executed when ecmalisp compiles this file
20 ;;; itself. The compiler provides compilation of some special forms,
21 ;;; as well as funcalls and macroexpansion, but no functions. So, we
22 ;;; define the Lisp world from scratch. This code has to define enough
23 ;;; language to the compiler to be able to run.
24
25 #+ecmalisp
26 (progn
27   (eval-when-compile
28     (%compile-defmacro 'defmacro
29                        '(function
30                          (lambda (name args &rest body)
31                           `(eval-when-compile
32                              (%compile-defmacro ',name
33                                                 '(function
34                                                   (lambda ,(mapcar #'(lambda (x)
35                                                                        (if (eq x '&body)
36                                                                            '&rest
37                                                                            x))
38                                                                    args)
39                                                    ,@body))))))))
40
41   (defmacro declaim (&rest decls)
42     `(eval-when-compile
43        ,@(mapcar (lambda (decl) `(!proclaim ',decl)) decls)))
44
45   (defmacro defconstant (name value &optional docstring)
46     `(progn
47        (declaim (special ,name))
48        (declaim (constant ,name))
49        (setq ,name ,value)
50        ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
51        ',name))
52
53   (defconstant t 't)
54   (defconstant nil 'nil)
55   (js-vset "nil" nil)
56
57   (defmacro lambda (args &body body)
58     `(function (lambda ,args ,@body)))
59
60   (defmacro when (condition &body body)
61     `(if ,condition (progn ,@body) nil))
62
63   (defmacro unless (condition &body body)
64     `(if ,condition nil (progn ,@body)))
65
66   (defmacro defvar (name value &optional docstring)
67     `(progn
68        (declaim (special ,name))
69        (unless (boundp ',name) (setq ,name ,value))
70        ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
71        ',name))
72
73   (defmacro defparameter (name value &optional docstring)
74     `(progn
75        (setq ,name ,value)
76        ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
77        ',name))
78
79   (defmacro named-lambda (name args &rest body)
80     (let ((x (gensym "FN")))
81       `(let ((,x (lambda ,args ,@body)))
82          (oset ,x "fname" ,name)
83          ,x)))
84
85   (defmacro defun (name args &rest body)
86     `(progn
87        (fset ',name
88              (named-lambda ,(symbol-name name) ,args
89                ,@(if (and (stringp (car body)) (not (null (cdr body))))
90                      `(,(car body) (block ,name ,@(cdr body)))
91                      `((block ,name ,@body)))))
92        ',name))
93
94   (defun null (x)
95     (eq x nil))
96
97   (defmacro return (&optional value)
98     `(return-from nil ,value))
99
100   (defmacro while (condition &body body)
101     `(block nil (%while ,condition ,@body)))
102
103   (defvar *gensym-counter* 0)
104   (defun gensym (&optional (prefix "G"))
105     (setq *gensym-counter* (+ *gensym-counter* 1))
106     (make-symbol (concat-two prefix (integer-to-string *gensym-counter*))))
107
108   (defun boundp (x)
109     (boundp x))
110
111   ;; Basic functions
112   (defun = (x y) (= x y))
113   (defun * (x y) (* x y))
114   (defun / (x y) (/ x y))
115   (defun 1+ (x) (+ x 1))
116   (defun 1- (x) (- x 1))
117   (defun zerop (x) (= x 0))
118   (defun truncate (x y) (floor (/ x y)))
119
120   (defun eql (x y) (eq x y))
121
122   (defun not (x) (if x nil t))
123
124   (defun cons (x y ) (cons x y))
125   (defun consp (x) (consp x))
126
127   (defun car (x)
128     "Return the CAR part of a cons, or NIL if X is null."
129     (car x))
130
131   (defun cdr (x) (cdr x))
132   (defun caar (x) (car (car x)))
133   (defun cadr (x) (car (cdr x)))
134   (defun cdar (x) (cdr (car x)))
135   (defun cddr (x) (cdr (cdr x)))
136   (defun caddr (x) (car (cdr (cdr x))))
137   (defun cdddr (x) (cdr (cdr (cdr x))))
138   (defun cadddr (x) (car (cdr (cdr (cdr x)))))
139   (defun first (x) (car x))
140   (defun second (x) (cadr x))
141   (defun third (x) (caddr x))
142   (defun fourth (x) (cadddr x))
143   (defun rest (x) (cdr x))
144
145   (defun list (&rest args) args)
146   (defun atom (x)
147     (not (consp x)))
148
149   ;; Basic macros
150
151   (defmacro incf (x &optional (delta 1))
152     `(setq ,x (+ ,x ,delta)))
153
154   (defmacro decf (x &optional (delta 1))
155     `(setq ,x (- ,x ,delta)))
156
157   (defmacro push (x place)
158     `(setq ,place (cons ,x ,place)))
159
160   (defmacro dolist (iter &body body)
161     (let ((var (first iter))
162           (g!list (gensym)))
163       `(block nil
164          (let ((,g!list ,(second iter))
165                (,var nil))
166            (%while ,g!list
167                    (setq ,var (car ,g!list))
168                    (tagbody ,@body)
169                    (setq ,g!list (cdr ,g!list)))
170            ,(third iter)))))
171
172   (defmacro dotimes (iter &body body)
173     (let ((g!to (gensym))
174           (var (first iter))
175           (to (second iter))
176           (result (third iter)))
177       `(block nil
178          (let ((,var 0)
179                (,g!to ,to))
180            (%while (< ,var ,g!to)
181                    (tagbody ,@body)
182                    (incf ,var))
183            ,result))))
184
185   (defmacro cond (&rest clausules)
186     (if (null clausules)
187         nil
188         (if (eq (caar clausules) t)
189             `(progn ,@(cdar clausules))
190             `(if ,(caar clausules)
191                  (progn ,@(cdar clausules))
192                  (cond ,@(cdr clausules))))))
193
194   (defmacro case (form &rest clausules)
195     (let ((!form (gensym)))
196       `(let ((,!form ,form))
197          (cond
198            ,@(mapcar (lambda (clausule)
199                        (if (eq (car clausule) t)
200                            clausule
201                            `((eql ,!form ',(car clausule))
202                              ,@(cdr clausule))))
203                      clausules)))))
204
205   (defmacro ecase (form &rest clausules)
206     `(case ,form
207        ,@(append
208           clausules
209           `((t
210              (error "ECASE expression failed."))))))
211
212   (defmacro and (&rest forms)
213     (cond
214       ((null forms)
215        t)
216       ((null (cdr forms))
217        (car forms))
218       (t
219        `(if ,(car forms)
220             (and ,@(cdr forms))
221             nil))))
222
223   (defmacro or (&rest forms)
224     (cond
225       ((null forms)
226        nil)
227       ((null (cdr forms))
228        (car forms))
229       (t
230        (let ((g (gensym)))
231          `(let ((,g ,(car forms)))
232             (if ,g ,g (or ,@(cdr forms))))))))
233
234   (defmacro prog1 (form &body body)
235     (let ((value (gensym)))
236       `(let ((,value ,form))
237          ,@body
238          ,value)))
239
240   (defmacro prog2 (form1 result &body body)
241     `(prog1 (progn ,form1 ,result) ,@body)))
242
243
244 ;;; This couple of helper functions will be defined in both Common
245 ;;; Lisp and in Ecmalisp.
246 (defun ensure-list (x)
247   (if (listp x)
248       x
249       (list x)))
250
251 (defun !reduce (func list initial)
252   (if (null list)
253       initial
254       (!reduce func
255                (cdr list)
256                (funcall func initial (car list)))))
257
258 ;;; Go on growing the Lisp language in Ecmalisp, with more high
259 ;;; level utilities as well as correct versions of other
260 ;;; constructions.
261 #+ecmalisp
262 (progn
263   (defun + (&rest args)
264     (let ((r 0))
265       (dolist (x args r)
266         (incf r x))))
267
268   (defun - (x &rest others)
269     (if (null others)
270         (- x)
271         (let ((r x))
272           (dolist (y others r)
273             (decf r y)))))
274
275   (defun append-two (list1 list2)
276     (if (null list1)
277         list2
278         (cons (car list1)
279               (append (cdr list1) list2))))
280
281   (defun append (&rest lists)
282     (!reduce #'append-two lists '()))
283
284   (defun revappend (list1 list2)
285     (while list1
286       (push (car list1) list2)
287       (setq list1 (cdr list1)))
288     list2)
289
290   (defun reverse (list)
291     (revappend list '()))
292
293   (defmacro psetq (&rest pairs)
294     (let ( ;; For each pair, we store here a list of the form
295           ;; (VARIABLE GENSYM VALUE).
296           (assignments '()))
297       (while t
298         (cond
299           ((null pairs) (return))
300           ((null (cdr pairs))
301            (error "Odd paris in PSETQ"))
302           (t
303            (let ((variable (car pairs))
304                  (value (cadr pairs)))
305              (push `(,variable ,(gensym) ,value)  assignments)
306              (setq pairs (cddr pairs))))))
307       (setq assignments (reverse assignments))
308       ;;
309       `(let ,(mapcar #'cdr assignments)
310          (setq ,@(!reduce #'append (mapcar #'butlast assignments) '())))))
311
312   (defmacro do (varlist endlist &body body)
313     `(block nil
314        (let ,(mapcar (lambda (x) (list (first x) (second x))) varlist)
315          (while t
316            (when ,(car endlist)
317              (return (progn ,(cdr endlist))))
318            (tagbody ,@body)
319            (psetq
320             ,@(apply #'append
321                      (mapcar (lambda (v)
322                                (and (consp (cddr v))
323                                     (list (first v) (third v))))
324                              varlist)))))))
325
326   (defmacro do* (varlist endlist &body body)
327     `(block nil
328        (let* ,(mapcar (lambda (x) (list (first x) (second x))) varlist)
329          (while t
330            (when ,(car endlist)
331              (return (progn ,(cdr endlist))))
332            (tagbody ,@body)
333            (setq
334             ,@(apply #'append
335                      (mapcar (lambda (v)
336                                (and (consp (cddr v))
337                                     (list (first v) (third v))))
338                              varlist)))))))
339
340   (defun list-length (list)
341     (let ((l 0))
342       (while (not (null list))
343         (incf l)
344         (setq list (cdr list)))
345       l))
346
347   (defun length (seq)
348     (cond
349       ((stringp seq)
350        (string-length seq))
351       ((arrayp seq)
352        (oget seq "length"))
353       ((listp seq)
354        (list-length seq))))
355
356   (defun concat-two (s1 s2)
357     (concat-two s1 s2))
358
359   (defmacro with-collect (&body body)
360     (let ((head (gensym))
361           (tail (gensym)))
362       `(let* ((,head (cons 'sentinel nil))
363               (,tail ,head))
364          (flet ((collect (x)
365                   (rplacd ,tail (cons x nil))
366                   (setq ,tail (cdr ,tail))
367                   x))
368            ,@body)
369          (cdr ,head))))
370
371   (defun map1 (func list)
372     (with-collect
373       (while list
374         (collect (funcall func (car list)))
375         (setq list (cdr list)))))
376
377   (defmacro loop (&body body)
378     `(while t ,@body))
379
380   (defun mapcar (func list &rest lists)
381     (let ((lists (cons list lists)))
382       (with-collect
383         (block loop
384           (loop
385              (let ((elems (map1 #'car lists)))
386                (do ((tail lists (cdr tail)))
387                    ((null tail))
388                  (when (null (car tail)) (return-from loop))
389                  (rplaca tail (cdar tail)))
390                (collect (apply func elems))))))))
391
392   (defun identity (x) x)
393
394   (defun constantly (x)
395     (lambda (&rest args)
396       x))
397
398   (defun copy-list (x)
399     (mapcar #'identity x))
400
401   (defun code-char (x) x)
402   (defun char-code (x) x)
403   (defun char= (x y) (= x y))
404
405   (defun integerp (x)
406     (and (numberp x) (= (floor x) x)))
407
408   (defun plusp (x) (< 0 x))
409   (defun minusp (x) (< x 0))
410
411   (defun listp (x)
412     (or (consp x) (null x)))
413
414   (defun nthcdr (n list)
415     (while (and (plusp n) list)
416       (setq n (1- n))
417       (setq list (cdr list)))
418     list)
419
420   (defun nth (n list)
421     (car (nthcdr n list)))
422
423   (defun last (x)
424     (while (consp (cdr x))
425       (setq x (cdr x)))
426     x)
427
428   (defun butlast (x)
429     (and (consp (cdr x))
430          (cons (car x) (butlast (cdr x)))))
431
432   (defun member (x list)
433     (while list
434       (when (eql x (car list))
435         (return list))
436       (setq list (cdr list))))
437
438   (defun remove (x list)
439     (cond
440       ((null list)
441        nil)
442       ((eql x (car list))
443        (remove x (cdr list)))
444       (t
445        (cons (car list) (remove x (cdr list))))))
446
447   (defun remove-if (func list)
448     (cond
449       ((null list)
450        nil)
451       ((funcall func (car list))
452        (remove-if func (cdr list)))
453       (t
454        (cons (car list) (remove-if func (cdr list))))))
455
456   (defun remove-if-not (func list)
457     (cond
458       ((null list)
459        nil)
460       ((funcall func (car list))
461        (cons (car list) (remove-if-not func (cdr list))))
462       (t
463        (remove-if-not func (cdr list)))))
464
465   (defun digit-char-p (x)
466     (if (and (<= #\0 x) (<= x #\9))
467         (- x #\0)
468         nil))
469
470   (defun digit-char (weight)
471     (and (<= 0 weight 9)
472          (char "0123456789" weight)))
473
474   (defun subseq (seq a &optional b)
475     (cond
476       ((stringp seq)
477        (if b
478            (slice seq a b)
479            (slice seq a)))
480       (t
481        (error "Unsupported argument."))))
482
483   (defun some (function seq)
484     (cond
485       ((stringp seq)
486        (let ((index 0)
487              (size (length seq)))
488          (while (< index size)
489            (when (funcall function (char seq index))
490              (return-from some t))
491            (incf index))
492          nil))
493       ((listp seq)
494        (dolist (x seq nil)
495          (when (funcall function x)
496            (return t))))
497       (t
498        (error "Unknown sequence."))))
499
500   (defun every (function seq)
501     (cond
502       ((stringp seq)
503        (let ((index 0)
504              (size (length seq)))
505          (while (< index size)
506            (unless (funcall function (char seq index))
507              (return-from every nil))
508            (incf index))
509          t))
510       ((listp seq)
511        (dolist (x seq t)
512          (unless (funcall function x)
513            (return))))
514       (t
515        (error "Unknown sequence."))))
516
517   (defun assoc (x alist)
518     (while alist
519       (if (eql x (caar alist))
520           (return)
521           (setq alist (cdr alist))))
522     (car alist))
523
524   (defun string (x)
525     (cond ((stringp x) x)
526           ((symbolp x) (symbol-name x))
527           (t (char-to-string x))))
528
529   (defun string= (s1 s2)
530     (equal s1 s2))
531
532   (defun fdefinition (x)
533     (cond
534       ((functionp x)
535        x)
536       ((symbolp x)
537        (symbol-function x))
538       (t
539        (error "Invalid function"))))
540
541   (defun disassemble (function)
542     (write-line (lambda-code (fdefinition function)))
543     nil)
544
545   (defun documentation (x type)
546     "Return the documentation of X. TYPE must be the symbol VARIABLE or FUNCTION."
547     (ecase type
548       (function
549        (let ((func (fdefinition x)))
550          (oget func "docstring")))
551       (variable
552        (unless (symbolp x)
553          (error "Wrong argument type! it should be a symbol"))
554        (oget x "vardoc"))))
555
556   (defmacro multiple-value-bind (variables value-from &body body)
557     `(multiple-value-call (lambda (&optional ,@variables &rest ,(gensym))
558                             ,@body)
559        ,value-from))
560
561   (defmacro multiple-value-list (value-from)
562     `(multiple-value-call #'list ,value-from))
563
564
565   ;;; Generalized references (SETF)
566
567   (defvar *setf-expanders* nil)
568
569   (defun get-setf-expansion (place)
570     (if (symbolp place)
571         (let ((value (gensym)))
572           (values nil
573                   nil
574                   `(,value)
575                   `(setq ,place ,value)
576                   place))
577         (let* ((access-fn (car place))
578              (expander (cdr (assoc access-fn *setf-expanders*))))
579           (when (null expander)
580             (error "Unknown generalized reference."))
581           (apply expander (cdr place)))))
582
583   (defmacro define-setf-expander (access-fn lambda-list &body body)
584     (unless (symbolp access-fn)
585       (error "ACCESS-FN must be a symbol."))
586     `(progn (push (cons ',access-fn (lambda ,lambda-list ,@body))
587                   *setf-expanders*)
588             ',access-fn))
589
590   (defmacro setf (&rest pairs)
591     (cond
592       ((null pairs)
593        nil)
594       ((null (cdr pairs))
595        (error "Odd number of arguments to setf."))
596       ((null (cddr pairs))
597        (let ((place (first pairs))
598              (value (second pairs)))
599          (multiple-value-bind (vars vals store-vars writer-form reader-form)
600              (get-setf-expansion place)
601            ;; TODO: Optimize the expansion a little bit to avoid let*
602            ;; or multiple-value-bind when unnecesary.
603            `(let* ,(mapcar #'list vars vals)
604               (multiple-value-bind ,store-vars
605                   ,value
606                 ,writer-form)))))
607       (t
608        `(progn
609           ,@(do ((pairs pairs (cddr pairs))
610                  (result '() (cons `(setf ,(car pairs) ,(cadr pairs)) result)))
611                 ((null pairs)
612                  (reverse result)))))))
613
614   (define-setf-expander car (x)
615     (let ((cons (gensym))
616           (new-value (gensym)))
617       (values (list cons)
618               (list x)
619               (list new-value)
620               `(progn (rplaca ,cons ,new-value) ,new-value)
621               `(car ,cons))))
622
623   (define-setf-expander cdr (x)
624     (let ((cons (gensym))
625           (new-value (gensym)))
626       (values (list cons)
627               (list x)
628               (list new-value)
629               `(progn (rplacd ,cons ,new-value) ,new-value)
630               `(car ,cons))))
631
632   ;;; Packages
633
634   (defvar *package-list* nil)
635
636   (defun list-all-packages ()
637     *package-list*)
638
639   (defun make-package (name &optional use)
640     (let ((package (new))
641           (use (mapcar #'find-package-or-fail use)))
642       (oset package "packageName" name)
643       (oset package "symbols" (new))
644       (oset package "exports" (new))
645       (oset package "use" use)
646       (push package *package-list*)
647       package))
648
649   (defun packagep (x)
650     (and (objectp x) (in "symbols" x)))
651
652   (defun find-package (package-designator)
653     (when (packagep package-designator)
654       (return-from find-package package-designator))
655     (let ((name (string package-designator)))
656       (dolist (package *package-list*)
657         (when (string= (package-name package) name)
658           (return package)))))
659
660   (defun find-package-or-fail (package-designator)
661     (or (find-package package-designator)
662         (error "Package unknown.")))
663
664   (defun package-name (package-designator)
665     (let ((package (find-package-or-fail package-designator)))
666       (oget package "packageName")))
667
668   (defun %package-symbols (package-designator)
669     (let ((package (find-package-or-fail package-designator)))
670       (oget package "symbols")))
671
672   (defun package-use-list (package-designator)
673     (let ((package (find-package-or-fail package-designator)))
674       (oget package "use")))
675
676   (defun %package-external-symbols (package-designator)
677     (let ((package (find-package-or-fail package-designator)))
678       (oget package "exports")))
679
680   (defvar *common-lisp-package*
681     (make-package "CL"))
682
683   (defvar *user-package*
684     (make-package "CL-USER" (list *common-lisp-package*)))
685
686   (defvar *keyword-package*
687     (make-package "KEYWORD"))
688
689   (defun keywordp (x)
690     (and (symbolp x) (eq (symbol-package x) *keyword-package*)))
691
692   (defvar *package* *common-lisp-package*)
693
694   (defmacro in-package (package-designator)
695     `(eval-when-compile
696        (setq *package* (find-package-or-fail ,package-designator))))
697
698   ;; This function is used internally to initialize the CL package
699   ;; with the symbols built during bootstrap.
700   (defun %intern-symbol (symbol)
701     (let* ((package
702             (if (in "package" symbol)
703                 (find-package-or-fail (oget symbol "package"))
704                 *common-lisp-package*))
705            (symbols (%package-symbols package)))
706       (oset symbol "package" package)
707       (when (eq package *keyword-package*)
708         (oset symbol "value" symbol))
709       (oset symbols (symbol-name symbol) symbol)))
710
711   (defun find-symbol (name &optional (package *package*))
712     (let* ((package (find-package-or-fail package))
713            (externals (%package-external-symbols package))
714            (symbols (%package-symbols package)))
715       (cond
716         ((in name externals)
717          (values (oget externals name) :external))
718         ((in name symbols)
719          (values (oget symbols name) :internal))
720         (t
721          (dolist (used (package-use-list package) (values nil nil))
722            (let ((exports (%package-external-symbols used)))
723              (when (in name exports)
724                (return (values (oget exports name) :inherit)))))))))
725
726   (defun intern (name &optional (package *package*))
727     (let ((package (find-package-or-fail package)))
728       (multiple-value-bind (symbol foundp)
729           (find-symbol name package)
730         (if foundp
731             (values symbol foundp)
732             (let ((symbols (%package-symbols package)))
733               (oget symbols name)
734               (let ((symbol (make-symbol name)))
735                 (oset symbol "package" package)
736                 (when (eq package *keyword-package*)
737                   (oset symbol "value" symbol)
738                   (export (list symbol) package))
739                 (oset symbols name symbol)
740                 (values symbol nil)))))))
741
742   (defun symbol-package (symbol)
743     (unless (symbolp symbol)
744       (error "it is not a symbol"))
745     (oget symbol "package"))
746
747   (defun export (symbols &optional (package *package*))
748     (let ((exports (%package-external-symbols package)))
749       (dolist (symb symbols t)
750         (oset exports (symbol-name symb) symb))))
751
752   (defun get-universal-time ()
753     (+ (get-unix-time) 2208988800)))
754
755
756 ;;; The compiler offers some primitives and special forms which are
757 ;;; not found in Common Lisp, for instance, while. So, we grow Common
758 ;;; Lisp a bit to it can execute the rest of the file.
759 #+common-lisp
760 (progn
761   (defmacro while (condition &body body)
762     `(do ()
763          ((not ,condition))
764        ,@body))
765
766   (defmacro eval-when-compile (&body body)
767     `(eval-when (:compile-toplevel :load-toplevel :execute)
768        ,@body))
769
770   (defun concat-two (s1 s2)
771     (concatenate 'string s1 s2))
772
773   (defun aset (array idx value)
774     (setf (aref array idx) value)))
775
776 ;;; At this point, no matter if Common Lisp or ecmalisp is compiling
777 ;;; from here, this code will compile on both. We define some helper
778 ;;; functions now for string manipulation and so on. They will be
779 ;;; useful in the compiler, mostly.
780
781 (defvar *newline* (string (code-char 10)))
782
783 (defun concat (&rest strs)
784   (!reduce #'concat-two strs ""))
785
786 (defmacro concatf (variable &body form)
787   `(setq ,variable (concat ,variable (progn ,@form))))
788
789 ;;; Concatenate a list of strings, with a separator
790 (defun join (list &optional (separator ""))
791   (cond
792     ((null list)
793      "")
794     ((null (cdr list))
795      (car list))
796     (t
797      (concat (car list)
798              separator
799              (join (cdr list) separator)))))
800
801 (defun join-trailing (list &optional (separator ""))
802   (if (null list)
803       ""
804       (concat (car list) separator (join-trailing (cdr list) separator))))
805
806 (defun mapconcat (func list)
807   (join (mapcar func list)))
808
809 (defun vector-to-list (vector)
810   (let ((list nil)
811         (size (length vector)))
812     (dotimes (i size (reverse list))
813       (push (aref vector i) list))))
814
815 (defun list-to-vector (list)
816   (let ((v (make-array (length list)))
817         (i 0))
818     (dolist (x list v)
819       (aset v i x)
820       (incf i))))
821
822 #+ecmalisp
823 (progn
824   (defun values-list (list)
825     (values-array (list-to-vector list)))
826
827   (defun values (&rest args)
828     (values-list args)))
829
830 (defun integer-to-string (x)
831   (cond
832     ((zerop x)
833      "0")
834     ((minusp x)
835      (concat "-" (integer-to-string (- 0 x))))
836     (t
837      (let ((digits nil))
838        (while (not (zerop x))
839          (push (mod x 10) digits)
840          (setq x (truncate x 10)))
841        (mapconcat (lambda (x) (string (digit-char x)))
842                   digits)))))
843
844
845 ;;; Printer
846
847 #+ecmalisp
848 (progn
849   (defun prin1-to-string (form)
850     (cond
851       ((symbolp form)
852        (multiple-value-bind (symbol foundp)
853            (find-symbol (symbol-name form) *package*)
854          (if (and foundp (eq symbol form))
855              (symbol-name form)
856              (let ((package (symbol-package form))
857                    (name (symbol-name form)))
858                (concat (cond
859                          ((null package) "#")
860                          ((eq package (find-package "KEYWORD")) "")
861                          (t (package-name package)))
862                        ":" name)))))
863       ((integerp form) (integer-to-string form))
864       ((stringp form) (concat "\"" (escape-string form) "\""))
865       ((functionp form)
866        (let ((name (oget form "fname")))
867          (if name
868              (concat "#<FUNCTION " name ">")
869              (concat "#<FUNCTION>"))))
870       ((listp form)
871        (concat "("
872                (join-trailing (mapcar #'prin1-to-string (butlast form)) " ")
873                (let ((last (last form)))
874                  (if (null (cdr last))
875                      (prin1-to-string (car last))
876                      (concat (prin1-to-string (car last)) " . " (prin1-to-string (cdr last)))))
877                ")"))
878       ((arrayp form)
879        (concat "#" (prin1-to-string (vector-to-list form))))
880       ((packagep form)
881        (concat "#<PACKAGE " (package-name form) ">"))))
882
883   (defun write-line (x)
884     (write-string x)
885     (write-string *newline*)
886     x)
887
888   (defun warn (string)
889     (write-string "WARNING: ")
890     (write-line string))
891
892   (defun print (x)
893     (write-line (prin1-to-string x))
894     x))
895
896
897 ;;;; Reader
898
899 ;;; The Lisp reader, parse strings and return Lisp objects. The main
900 ;;; entry points are `ls-read' and `ls-read-from-string'.
901
902 (defun make-string-stream (string)
903   (cons string 0))
904
905 (defun %peek-char (stream)
906   (and (< (cdr stream) (length (car stream)))
907        (char (car stream) (cdr stream))))
908
909 (defun %read-char (stream)
910   (and (< (cdr stream) (length (car stream)))
911        (prog1 (char (car stream) (cdr stream))
912          (rplacd stream (1+ (cdr stream))))))
913
914 (defun whitespacep (ch)
915   (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
916
917 (defun skip-whitespaces (stream)
918   (let (ch)
919     (setq ch (%peek-char stream))
920     (while (and ch (whitespacep ch))
921       (%read-char stream)
922       (setq ch (%peek-char stream)))))
923
924 (defun terminalp (ch)
925   (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
926
927 (defun read-until (stream func)
928   (let ((string "")
929         (ch))
930     (setq ch (%peek-char stream))
931     (while (and ch (not (funcall func ch)))
932       (setq string (concat string (string ch)))
933       (%read-char stream)
934       (setq ch (%peek-char stream)))
935     string))
936
937 (defun skip-whitespaces-and-comments (stream)
938   (let (ch)
939     (skip-whitespaces stream)
940     (setq ch (%peek-char stream))
941     (while (and ch (char= ch #\;))
942       (read-until stream (lambda (x) (char= x #\newline)))
943       (skip-whitespaces stream)
944       (setq ch (%peek-char stream)))))
945
946 (defun %read-list (stream)
947   (skip-whitespaces-and-comments stream)
948   (let ((ch (%peek-char stream)))
949     (cond
950       ((null ch)
951        (error "Unspected EOF"))
952       ((char= ch #\))
953        (%read-char stream)
954        nil)
955       ((char= ch #\.)
956        (%read-char stream)
957        (prog1 (ls-read stream)
958          (skip-whitespaces-and-comments stream)
959          (unless (char= (%read-char stream) #\))
960            (error "')' was expected."))))
961       (t
962        (cons (ls-read stream) (%read-list stream))))))
963
964 (defun read-string (stream)
965   (let ((string "")
966         (ch nil))
967     (setq ch (%read-char stream))
968     (while (not (eql ch #\"))
969       (when (null ch)
970         (error "Unexpected EOF"))
971       (when (eql ch #\\)
972         (setq ch (%read-char stream)))
973       (setq string (concat string (string ch)))
974       (setq ch (%read-char stream)))
975     string))
976
977 (defun read-sharp (stream)
978   (%read-char stream)
979   (ecase (%read-char stream)
980     (#\'
981      (list 'function (ls-read stream)))
982     (#\( (list-to-vector (%read-list stream)))
983     (#\: (make-symbol (string-upcase (read-until stream #'terminalp))))
984     (#\\
985      (let ((cname
986             (concat (string (%read-char stream))
987                     (read-until stream #'terminalp))))
988        (cond
989          ((string= cname "space") (char-code #\space))
990          ((string= cname "tab") (char-code #\tab))
991          ((string= cname "newline") (char-code #\newline))
992          (t (char-code (char cname 0))))))
993     (#\+
994      (let ((feature (read-until stream #'terminalp)))
995        (cond
996          ((string= feature "common-lisp")
997           (ls-read stream)              ;ignore
998           (ls-read stream))
999          ((string= feature "ecmalisp")
1000           (ls-read stream))
1001          (t
1002           (error "Unknown reader form.")))))))
1003
1004 ;;; Parse a string of the form NAME, PACKAGE:NAME or
1005 ;;; PACKAGE::NAME and return the name. If the string is of the
1006 ;;; form 1) or 3), but the symbol does not exist, it will be created
1007 ;;; and interned in that package.
1008 (defun read-symbol (string)
1009   (let ((size (length string))
1010         package name internalp index)
1011     (setq index 0)
1012     (while (and (< index size)
1013                 (not (char= (char string index) #\:)))
1014       (incf index))
1015     (cond
1016       ;; No package prefix
1017       ((= index size)
1018        (setq name string)
1019        (setq package *package*)
1020        (setq internalp t))
1021       (t
1022        ;; Package prefix
1023        (if (zerop index)
1024            (setq package "KEYWORD")
1025            (setq package (string-upcase (subseq string 0 index))))
1026        (incf index)
1027        (when (char= (char string index) #\:)
1028          (setq internalp t)
1029          (incf index))
1030        (setq name (subseq string index))))
1031     ;; Canonalize symbol name and package
1032     (setq name (string-upcase name))
1033     (setq package (find-package package))
1034     ;; TODO: PACKAGE:SYMBOL should signal error if SYMBOL is not an
1035     ;; external symbol from PACKAGE.
1036     (if (or internalp (eq package (find-package "KEYWORD")))
1037         (intern name package)
1038         (find-symbol name package))))
1039
1040
1041 (defun !parse-integer (string junk-allow)
1042   (block nil
1043     (let ((value 0)
1044           (index 0)
1045           (size (length string))
1046           (sign 1))
1047       (when (zerop size) (return (values nil 0)))
1048       ;; Optional sign
1049       (case (char string 0)
1050         (#\+ (incf index))
1051         (#\- (setq sign -1)
1052              (incf index)))
1053       ;; First digit
1054       (unless (and (< index size)
1055                    (setq value (digit-char-p (char string index))))
1056         (return (values nil index)))
1057       (incf index)
1058       ;; Other digits
1059       (while (< index size)
1060         (let ((digit (digit-char-p (char string index))))
1061           (unless digit (return))
1062           (setq value (+ (* value 10) digit))
1063           (incf index)))
1064       (if (or junk-allow
1065               (= index size)
1066               (char= (char string index) #\space))
1067           (values (* sign value) index)
1068           (values nil index)))))
1069
1070 #+ecmalisp
1071 (defun parse-integer (string)
1072   (!parse-integer string nil))
1073
1074 (defvar *eof* (gensym))
1075 (defun ls-read (stream)
1076   (skip-whitespaces-and-comments stream)
1077   (let ((ch (%peek-char stream)))
1078     (cond
1079       ((or (null ch) (char= ch #\)))
1080        *eof*)
1081       ((char= ch #\()
1082        (%read-char stream)
1083        (%read-list stream))
1084       ((char= ch #\')
1085        (%read-char stream)
1086        (list 'quote (ls-read stream)))
1087       ((char= ch #\`)
1088        (%read-char stream)
1089        (list 'backquote (ls-read stream)))
1090       ((char= ch #\")
1091        (%read-char stream)
1092        (read-string stream))
1093       ((char= ch #\,)
1094        (%read-char stream)
1095        (if (eql (%peek-char stream) #\@)
1096            (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
1097            (list 'unquote (ls-read stream))))
1098       ((char= ch #\#)
1099        (read-sharp stream))
1100       (t
1101        (let ((string (read-until stream #'terminalp)))
1102          (or (values (!parse-integer string nil))
1103              (read-symbol string)))))))
1104
1105 (defun ls-read-from-string (string)
1106   (ls-read (make-string-stream string)))
1107
1108
1109 ;;;; Compiler
1110
1111 ;;; Translate the Lisp code to Javascript. It will compile the special
1112 ;;; forms. Some primitive functions are compiled as special forms
1113 ;;; too. The respective real functions are defined in the target (see
1114 ;;; the beginning of this file) as well as some primitive functions.
1115
1116 (defun code (&rest args)
1117   (mapconcat (lambda (arg)
1118                (cond
1119                  ((null arg) "")
1120                  ((integerp arg) (integer-to-string arg))
1121                  ((stringp arg) arg)
1122                  (t (error "Unknown argument."))))
1123              args))
1124
1125 ;;; Wrap X with a Javascript code to convert the result from
1126 ;;; Javascript generalized booleans to T or NIL.
1127 (defun js!bool (x)
1128   (code "(" x "?" (ls-compile t) ": " (ls-compile nil) ")"))
1129
1130 ;;; Concatenate the arguments and wrap them with a self-calling
1131 ;;; Javascript anonymous function. It is used to make some Javascript
1132 ;;; statements valid expressions and provide a private scope as well.
1133 ;;; It could be defined as function, but we could do some
1134 ;;; preprocessing in the future.
1135 (defmacro js!selfcall (&body body)
1136   `(code "(function(){" *newline* (indent ,@body) "})()"))
1137
1138 ;;; Like CODE, but prefix each line with four spaces. Two versions
1139 ;;; of this function are available, because the Ecmalisp version is
1140 ;;; very slow and bootstraping was annoying.
1141
1142 #+ecmalisp
1143 (defun indent (&rest string)
1144   (let ((input (apply #'code string)))
1145     (let ((output "")
1146           (index 0)
1147           (size (length input)))
1148       (when (plusp (length input)) (concatf output "    "))
1149       (while (< index size)
1150         (let ((str
1151                (if (and (char= (char input index) #\newline)
1152                         (< index (1- size))
1153                         (not (char= (char input (1+ index)) #\newline)))
1154                    (concat (string #\newline) "    ")
1155                    (string (char input index)))))
1156           (concatf output str))
1157         (incf index))
1158       output)))
1159
1160 #+common-lisp
1161 (defun indent (&rest string)
1162   (with-output-to-string (*standard-output*)
1163     (with-input-from-string (input (apply #'code string))
1164       (loop
1165          for line = (read-line input nil)
1166          while line
1167          do (write-string "    ")
1168          do (write-line line)))))
1169
1170
1171 ;;; A Form can return a multiple values object calling VALUES, like
1172 ;;; values(arg1, arg2, ...). It will work in any context, as well as
1173 ;;; returning an individual object. However, if the special variable
1174 ;;; `*multiple-value-p*' is NIL, is granted that only the primary
1175 ;;; value will be used, so we can optimize to avoid the VALUES
1176 ;;; function call.
1177 (defvar *multiple-value-p* nil)
1178
1179 (defun make-binding (name type value &optional declarations)
1180   (list name type value declarations))
1181
1182 (defun binding-name (b) (first b))
1183 (defun binding-type (b) (second b))
1184 (defun binding-value (b) (third b))
1185 (defun binding-declarations (b) (fourth b))
1186
1187 (defun set-binding-value (b value)
1188   (rplaca (cddr b) value))
1189
1190 (defun set-binding-declarations (b value)
1191   (rplaca (cdddr b) value))
1192
1193 (defun push-binding-declaration (decl b)
1194   (set-binding-declarations b (cons decl (binding-declarations b))))
1195
1196
1197 (defun make-lexenv ()
1198   (list nil nil nil nil))
1199
1200 (defun copy-lexenv (lexenv)
1201   (copy-list lexenv))
1202
1203 (defun push-to-lexenv (binding lexenv namespace)
1204   (ecase namespace
1205     (variable   (rplaca        lexenv  (cons binding (car lexenv))))
1206     (function   (rplaca   (cdr lexenv) (cons binding (cadr lexenv))))
1207     (block      (rplaca  (cddr lexenv) (cons binding (caddr lexenv))))
1208     (gotag      (rplaca (cdddr lexenv) (cons binding (cadddr lexenv))))))
1209
1210 (defun extend-lexenv (bindings lexenv namespace)
1211   (let ((env (copy-lexenv lexenv)))
1212     (dolist (binding (reverse bindings) env)
1213       (push-to-lexenv binding env namespace))))
1214
1215 (defun lookup-in-lexenv (name lexenv namespace)
1216   (assoc name (ecase namespace
1217                 (variable (first lexenv))
1218                 (function (second lexenv))
1219                 (block (third lexenv))
1220                 (gotag (fourth lexenv)))))
1221
1222 (defvar *environment* (make-lexenv))
1223
1224 (defvar *variable-counter* 0)
1225 (defun gvarname (symbol)
1226   (code "v" (incf *variable-counter*)))
1227
1228 (defun translate-variable (symbol)
1229   (binding-value (lookup-in-lexenv symbol *environment* 'variable)))
1230
1231 (defun extend-local-env (args)
1232   (let ((new (copy-lexenv *environment*)))
1233     (dolist (symbol args new)
1234       (let ((b (make-binding symbol 'variable (gvarname symbol))))
1235         (push-to-lexenv b new 'variable)))))
1236
1237 ;;; Toplevel compilations
1238 (defvar *toplevel-compilations* nil)
1239
1240 (defun toplevel-compilation (string)
1241   (push string *toplevel-compilations*))
1242
1243 (defun null-or-empty-p (x)
1244   (zerop (length x)))
1245
1246 (defun get-toplevel-compilations ()
1247   (reverse (remove-if #'null-or-empty-p *toplevel-compilations*)))
1248
1249 (defun %compile-defmacro (name lambda)
1250   (toplevel-compilation (ls-compile `',name))
1251   (push-to-lexenv (make-binding name 'macro lambda) *environment* 'function)
1252   name)
1253
1254 (defun global-binding (name type namespace)
1255   (or (lookup-in-lexenv name *environment* namespace)
1256       (let ((b (make-binding name type nil)))
1257         (push-to-lexenv b *environment* namespace)
1258         b)))
1259
1260 (defun claimp (symbol namespace claim)
1261   (let ((b (lookup-in-lexenv symbol *environment* namespace)))
1262     (and b (member claim (binding-declarations b)))))
1263
1264 (defun !proclaim (decl)
1265   (case (car decl)
1266     (special
1267      (dolist (name (cdr decl))
1268        (let ((b (global-binding name 'variable 'variable)))
1269          (push-binding-declaration 'special b))))
1270     (notinline
1271      (dolist (name (cdr decl))
1272        (let ((b (global-binding name 'function 'function)))
1273          (push-binding-declaration 'notinline b))))
1274     (constant
1275      (dolist (name (cdr decl))
1276        (let ((b (global-binding name 'variable 'variable)))
1277          (push-binding-declaration 'constant b))))))
1278
1279 #+ecmalisp
1280 (fset 'proclaim #'!proclaim)
1281
1282 ;;; Special forms
1283
1284 (defvar *compilations* nil)
1285
1286 (defmacro define-compilation (name args &body body)
1287   ;; Creates a new primitive `name' with parameters args and
1288   ;; @body. The body can access to the local environment through the
1289   ;; variable *ENVIRONMENT*.
1290   `(push (list ',name (lambda ,args (block ,name ,@body)))
1291          *compilations*))
1292
1293 (define-compilation if (condition true false)
1294   (code "(" (ls-compile condition) " !== " (ls-compile nil)
1295         " ? " (ls-compile true *multiple-value-p*)
1296         " : " (ls-compile false *multiple-value-p*)
1297         ")"))
1298
1299 (defvar *lambda-list-keywords* '(&optional &rest &key))
1300
1301 (defun list-until-keyword (list)
1302   (if (or (null list) (member (car list) *lambda-list-keywords*))
1303       nil
1304       (cons (car list) (list-until-keyword (cdr list)))))
1305
1306 (defun lambda-list-section (keyword lambda-list)
1307   (list-until-keyword (cdr (member keyword lambda-list))))
1308
1309 (defun lambda-list-required-arguments (lambda-list)
1310   (list-until-keyword lambda-list))
1311
1312 (defun lambda-list-optional-arguments-with-default (lambda-list)
1313   (mapcar #'ensure-list (lambda-list-section '&optional lambda-list)))
1314
1315 (defun lambda-list-optional-arguments (lambda-list)
1316   (mapcar #'car (lambda-list-optional-arguments-with-default lambda-list)))
1317
1318 (defun lambda-list-rest-argument (lambda-list)
1319   (let ((rest (lambda-list-section '&rest lambda-list)))
1320     (when (cdr rest)
1321       (error "Bad lambda-list"))
1322     (car rest)))
1323
1324 (defun lambda-list-keyword-arguments-canonical (lambda-list)
1325   (flet ((canonalize (keyarg)
1326            ;; Build a canonical keyword argument descriptor, filling
1327            ;; the optional fields. The result is a list of the form
1328            ;; ((keyword-name var) init-form).
1329            (let* ((arg (ensure-list keyarg))
1330                   (init-form (cadr arg))
1331                   var
1332                   keyword-name)
1333              (if (listp (car arg))
1334                  (setq var (cadr (car arg))
1335                        keyword-name (car (car arg)))
1336                  (setq var (car arg)
1337                        keyword-name (intern (symbol-name (car arg)) "KEYWORD")))
1338              `((,keyword-name ,var) ,init-form))))
1339     (mapcar #'canonalize (lambda-list-section '&key lambda-list))))
1340
1341 (defun lambda-list-keyword-arguments (lambda-list)
1342   (mapcar (lambda (keyarg) (second (first keyarg)))
1343           (lambda-list-keyword-arguments-canonical lambda-list)))
1344
1345 (defun lambda-docstring-wrapper (docstring &rest strs)
1346   (if docstring
1347       (js!selfcall
1348         "var func = " (join strs) ";" *newline*
1349         "func.docstring = '" docstring "';" *newline*
1350         "return func;" *newline*)
1351       (apply #'code strs)))
1352
1353 (defun lambda-check-argument-count
1354     (n-required-arguments n-optional-arguments rest-p)
1355   ;; Note: Remember that we assume that the number of arguments of a
1356   ;; call is at least 1 (the values argument).
1357   (let ((min (1+ n-required-arguments))
1358         (max (if rest-p 'n/a (+ 1 n-required-arguments n-optional-arguments))))
1359     (block nil
1360       ;; Special case: a positive exact number of arguments.
1361       (when (and (< 1 min) (eql min max))
1362         (return (code "checkArgs(arguments, " min ");" *newline*)))
1363       ;; General case:
1364       (code
1365        (when (< 1 min)
1366          (code "checkArgsAtLeast(arguments, " min ");" *newline*))
1367        (when (numberp max)
1368          (code "checkArgsAtMost(arguments, " max ");" *newline*))))))
1369
1370 (defun compile-lambda-optional (lambda-list)
1371   (let* ((optional-arguments (lambda-list-optional-arguments lambda-list))
1372          (n-required-arguments (length (lambda-list-required-arguments lambda-list)))
1373          (n-optional-arguments (length optional-arguments)))
1374     (when optional-arguments
1375       (code "switch(arguments.length-1){" *newline*
1376             (let ((optional-and-defaults
1377                    (lambda-list-optional-arguments-with-default lambda-list))
1378                   (cases nil)
1379                   (idx 0))
1380               (progn
1381                 (while (< idx n-optional-arguments)
1382                   (let ((arg (nth idx optional-and-defaults)))
1383                     (push (code "case " (+ idx n-required-arguments) ":" *newline*
1384                                 (translate-variable (car arg))
1385                                 "="
1386                                 (ls-compile (cadr arg))
1387                                 ";" *newline*)
1388                           cases)
1389                     (incf idx)))
1390                 (push (code "default: break;" *newline*) cases)
1391                 (join (reverse cases))))
1392             "}" *newline*))))
1393
1394 (defun compile-lambda-rest (lambda-list)
1395   (let ((n-required-arguments (length (lambda-list-required-arguments lambda-list)))
1396         (n-optional-arguments (length (lambda-list-optional-arguments lambda-list)))
1397         (rest-argument (lambda-list-rest-argument lambda-list)))
1398     (when rest-argument
1399       (let ((js!rest (translate-variable rest-argument)))
1400         (code "var " js!rest "= " (ls-compile nil) ";" *newline*
1401               "for (var i = arguments.length-1; i>="
1402               (+ 1 n-required-arguments n-optional-arguments)
1403               "; i--)" *newline*
1404               (indent js!rest " = {car: arguments[i], cdr: ") js!rest "};"
1405               *newline*)))))
1406
1407 (defun compile-lambda-parse-keywords (lambda-list)
1408   (let ((n-required-arguments
1409          (length (lambda-list-required-arguments lambda-list)))
1410         (n-optional-arguments
1411          (length (lambda-list-optional-arguments lambda-list)))
1412         (keyword-arguments
1413          (lambda-list-keyword-arguments-canonical lambda-list)))
1414     (code
1415      ;; Declare variables
1416      (mapconcat (lambda (arg)
1417                   (let ((var (second (car arg))))
1418                     (code "var " (translate-variable var) "; " *newline*)))
1419                 keyword-arguments)
1420      ;; Parse keywords
1421      (flet ((parse-keyword (keyarg)
1422               ;; ((keyword-name var) init-form)
1423               (code "for (i=" (+ 1 n-required-arguments n-optional-arguments)
1424                     "; i<arguments.length; i+=2){" *newline*
1425                     (indent
1426                      "if (arguments[i] === " (ls-compile (caar keyarg)) "){" *newline*
1427                      (indent (translate-variable (cadr (car keyarg)))
1428                              " = arguments[i+1];"
1429                              *newline*
1430                              "break;" *newline*)
1431                      "}" *newline*)
1432                     "}" *newline*
1433                     ;; Default value
1434                     "if (i == arguments.length){" *newline*
1435                     (indent
1436                      (translate-variable (cadr (car keyarg)))
1437                      " = "
1438                      (ls-compile (cadr keyarg))
1439                      ";" *newline*)
1440                     "}" *newline*)))
1441        (when keyword-arguments
1442          (code "var i;" *newline*
1443                (mapconcat #'parse-keyword keyword-arguments))))
1444      ;; Check for unknown keywords
1445      (when keyword-arguments
1446        (code "for (i=" (+ 1 n-required-arguments n-optional-arguments)
1447              "; i<arguments.length; i+=2){" *newline*
1448              (indent "if ("
1449                      (join (mapcar (lambda (x)
1450                                      (concat "arguments[i] !== " (ls-compile (caar x))))
1451                                    keyword-arguments)
1452                            " && ")
1453                      ")" *newline*
1454                      (indent
1455                       "throw 'Unknown keyword argument ' + arguments[i].name;" *newline*))
1456              "}" *newline*)))))
1457
1458 (defun compile-lambda (lambda-list body)
1459   (let ((required-arguments (lambda-list-required-arguments lambda-list))
1460         (optional-arguments (lambda-list-optional-arguments lambda-list))
1461         (keyword-arguments  (lambda-list-keyword-arguments  lambda-list))
1462         (rest-argument      (lambda-list-rest-argument      lambda-list))
1463         documentation)
1464     ;; Get the documentation string for the lambda function
1465     (when (and (stringp (car body))
1466                (not (null (cdr body))))
1467       (setq documentation (car body))
1468       (setq body (cdr body)))
1469     (let ((n-required-arguments (length required-arguments))
1470           (n-optional-arguments (length optional-arguments))
1471           (*environment* (extend-local-env
1472                           (append (ensure-list rest-argument)
1473                                   required-arguments
1474                                   optional-arguments
1475                                   keyword-arguments))))
1476       (lambda-docstring-wrapper
1477        documentation
1478        "(function ("
1479        (join (cons "values"
1480                    (mapcar #'translate-variable
1481                            (append required-arguments optional-arguments)))
1482              ",")
1483        "){" *newline*
1484        (indent
1485         ;; Check number of arguments
1486         (lambda-check-argument-count n-required-arguments
1487                                      n-optional-arguments
1488                                      (or rest-argument keyword-arguments))
1489         (compile-lambda-optional lambda-list)
1490         (compile-lambda-rest lambda-list)
1491         (compile-lambda-parse-keywords lambda-list)
1492         (let ((*multiple-value-p* t))
1493           (ls-compile-block body t)))
1494        "})"))))
1495
1496
1497
1498 (defun setq-pair (var val)
1499   (let ((b (lookup-in-lexenv var *environment* 'variable)))
1500     (if (and (eq (binding-type b) 'variable)
1501              (not (member 'special (binding-declarations b)))
1502              (not (member 'constant (binding-declarations b))))
1503         (code (binding-value b) " = " (ls-compile val))
1504         (ls-compile `(set ',var ,val)))))
1505
1506 (define-compilation setq (&rest pairs)
1507   (let ((result ""))
1508     (while t
1509       (cond
1510         ((null pairs) (return))
1511         ((null (cdr pairs))
1512          (error "Odd paris in SETQ"))
1513         (t
1514          (concatf result
1515            (concat (setq-pair (car pairs) (cadr pairs))
1516                    (if (null (cddr pairs)) "" ", ")))
1517          (setq pairs (cddr pairs)))))
1518     (code "(" result ")")))
1519
1520 ;;; FFI Variable accessors
1521 (define-compilation js-vref (var)
1522   var)
1523
1524 (define-compilation js-vset (var val)
1525   (code "(" var " = " (ls-compile val) ")"))
1526
1527
1528 ;;; Literals
1529 (defun escape-string (string)
1530   (let ((output "")
1531         (index 0)
1532         (size (length string)))
1533     (while (< index size)
1534       (let ((ch (char string index)))
1535         (when (or (char= ch #\") (char= ch #\\))
1536           (setq output (concat output "\\")))
1537         (when (or (char= ch #\newline))
1538           (setq output (concat output "\\"))
1539           (setq ch #\n))
1540         (setq output (concat output (string ch))))
1541       (incf index))
1542     output))
1543
1544
1545 (defvar *literal-symbols* nil)
1546 (defvar *literal-counter* 0)
1547
1548 (defun genlit ()
1549   (code "l" (incf *literal-counter*)))
1550
1551 (defun literal (sexp &optional recursive)
1552   (cond
1553     ((integerp sexp) (integer-to-string sexp))
1554     ((stringp sexp) (code "\"" (escape-string sexp) "\""))
1555     ((symbolp sexp)
1556      (or (cdr (assoc sexp *literal-symbols*))
1557          (let ((v (genlit))
1558                (s #+common-lisp
1559                  (let ((package (symbol-package sexp)))
1560                    (if (eq package (find-package "KEYWORD"))
1561                        (code "{name: \"" (escape-string (symbol-name sexp))
1562                              "\", 'package': '" (package-name package) "'}")
1563                        (code "{name: \"" (escape-string (symbol-name sexp)) "\"}")))
1564                  #+ecmalisp
1565                  (let ((package (symbol-package sexp)))
1566                    (if (null package)
1567                        (code "{name: \"" (escape-string (symbol-name sexp)) "\"}")
1568                        (ls-compile `(intern ,(symbol-name sexp) ,(package-name package)))))))
1569            (push (cons sexp v) *literal-symbols*)
1570            (toplevel-compilation (code "var " v " = " s))
1571            v)))
1572     ((consp sexp)
1573      (let* ((head (butlast sexp))
1574             (tail (last sexp))
1575             (c (code "QIList("
1576                      (join-trailing (mapcar (lambda (x) (literal x t)) head) ",")
1577                      (literal (car tail) t)
1578                      ","
1579                      (literal (cdr tail) t)
1580                      ")")))
1581        (if recursive
1582            c
1583            (let ((v (genlit)))
1584              (toplevel-compilation (code "var " v " = " c))
1585              v))))
1586     ((arrayp sexp)
1587      (let ((elements (vector-to-list sexp)))
1588        (let ((c (concat "[" (join (mapcar #'literal elements) ", ") "]")))
1589          (if recursive
1590              c
1591              (let ((v (genlit)))
1592                (toplevel-compilation (code "var " v " = " c))
1593                v)))))))
1594
1595 (define-compilation quote (sexp)
1596   (literal sexp))
1597
1598 (define-compilation %while (pred &rest body)
1599   (js!selfcall
1600     "while(" (ls-compile pred) " !== " (ls-compile nil) "){" *newline*
1601     (indent (ls-compile-block body))
1602     "}"
1603     "return " (ls-compile nil) ";" *newline*))
1604
1605 (define-compilation function (x)
1606   (cond
1607     ((and (listp x) (eq (car x) 'lambda))
1608      (compile-lambda (cadr x) (cddr x)))
1609     ((symbolp x)
1610      (let ((b (lookup-in-lexenv x *environment* 'function)))
1611        (if b
1612            (binding-value b)
1613            (ls-compile `(symbol-function ',x)))))))
1614
1615
1616 (defun make-function-binding (fname)
1617   (make-binding fname 'function (gvarname fname)))
1618
1619 (defun compile-function-definition (list)
1620   (compile-lambda (car list) (cdr list)))
1621
1622 (defun translate-function (name)
1623   (let ((b (lookup-in-lexenv name *environment* 'function)))
1624     (binding-value b)))
1625
1626 (define-compilation flet (definitions &rest body)
1627   (let* ((fnames (mapcar #'car definitions))
1628          (fbody  (mapcar #'cdr definitions))
1629          (cfuncs (mapcar #'compile-function-definition fbody))
1630          (*environment*
1631           (extend-lexenv (mapcar #'make-function-binding fnames)
1632                          *environment*
1633                          'function)))
1634     (code "(function("
1635           (join (mapcar #'translate-function fnames) ",")
1636           "){" *newline*
1637           (let ((body (ls-compile-block body t)))
1638             (indent body))
1639           "})(" (join cfuncs ",") ")")))
1640
1641 (define-compilation labels (definitions &rest body)
1642   (let* ((fnames (mapcar #'car definitions))
1643          (*environment*
1644           (extend-lexenv (mapcar #'make-function-binding fnames)
1645                          *environment*
1646                          'function)))
1647     (js!selfcall
1648       (mapconcat (lambda (func)
1649                    (code "var " (translate-function (car func))
1650                          " = " (compile-lambda (cadr func) (cddr func))
1651                          ";" *newline*))
1652                  definitions)
1653       (ls-compile-block body t))))
1654
1655
1656
1657 (defvar *compiling-file* nil)
1658 (define-compilation eval-when-compile (&rest body)
1659   (if *compiling-file*
1660       (progn
1661         (eval (cons 'progn body))
1662         nil)
1663       (ls-compile `(progn ,@body))))
1664
1665 (defmacro define-transformation (name args form)
1666   `(define-compilation ,name ,args
1667      (ls-compile ,form)))
1668
1669 (define-compilation progn (&rest body)
1670   (if (null (cdr body))
1671       (ls-compile (car body) *multiple-value-p*)
1672       (js!selfcall (ls-compile-block body t))))
1673
1674 (defun special-variable-p (x)
1675   (and (claimp x 'variable 'special) t))
1676
1677 ;;; Wrap CODE to restore the symbol values of the dynamic
1678 ;;; bindings. BINDINGS is a list of pairs of the form
1679 ;;; (SYMBOL . PLACE),  where PLACE is a Javascript variable
1680 ;;; name to initialize the symbol value and where to stored
1681 ;;; the old value.
1682 (defun let-binding-wrapper (bindings body)
1683   (when (null bindings)
1684     (return-from let-binding-wrapper body))
1685   (code
1686    "try {" *newline*
1687    (indent "var tmp;" *newline*
1688            (mapconcat
1689             (lambda (b)
1690               (let ((s (ls-compile `(quote ,(car b)))))
1691                 (code "tmp = " s ".value;" *newline*
1692                       s ".value = " (cdr b) ";" *newline*
1693                       (cdr b) " = tmp;" *newline*)))
1694             bindings)
1695            body *newline*)
1696    "}" *newline*
1697    "finally {"  *newline*
1698    (indent
1699     (mapconcat (lambda (b)
1700                  (let ((s (ls-compile `(quote ,(car b)))))
1701                    (code s ".value" " = " (cdr b) ";" *newline*)))
1702                bindings))
1703    "}" *newline*))
1704
1705 (define-compilation let (bindings &rest body)
1706   (let* ((bindings (mapcar #'ensure-list bindings))
1707          (variables (mapcar #'first bindings))
1708          (cvalues (mapcar #'ls-compile (mapcar #'second bindings)))
1709          (*environment* (extend-local-env (remove-if #'special-variable-p variables)))
1710          (dynamic-bindings))
1711     (code "(function("
1712           (join (mapcar (lambda (x)
1713                           (if (special-variable-p x)
1714                               (let ((v (gvarname x)))
1715                                 (push (cons x v) dynamic-bindings)
1716                                 v)
1717                               (translate-variable x)))
1718                         variables)
1719                 ",")
1720           "){" *newline*
1721           (let ((body (ls-compile-block body t)))
1722             (indent (let-binding-wrapper dynamic-bindings body)))
1723           "})(" (join cvalues ",") ")")))
1724
1725
1726 ;;; Return the code to initialize BINDING, and push it extending the
1727 ;;; current lexical environment if the variable is not special.
1728 (defun let*-initialize-value (binding)
1729   (let ((var (first binding))
1730         (value (second binding)))
1731     (if (special-variable-p var)
1732         (code (ls-compile `(setq ,var ,value)) ";" *newline*)
1733         (let* ((v (gvarname var))
1734                (b (make-binding var 'variable v)))
1735           (prog1 (code "var " v " = " (ls-compile value) ";" *newline*)
1736             (push-to-lexenv b *environment* 'variable))))))
1737
1738 ;;; Wrap BODY to restore the symbol values of SYMBOLS after body. It
1739 ;;; DOES NOT generate code to initialize the value of the symbols,
1740 ;;; unlike let-binding-wrapper.
1741 (defun let*-binding-wrapper (symbols body)
1742   (when (null symbols)
1743     (return-from let*-binding-wrapper body))
1744   (let ((store (mapcar (lambda (s) (cons s (gvarname s)))
1745                        (remove-if-not #'special-variable-p symbols))))
1746     (code
1747      "try {" *newline*
1748      (indent
1749       (mapconcat (lambda (b)
1750                    (let ((s (ls-compile `(quote ,(car b)))))
1751                      (code "var " (cdr b) " = " s ".value;" *newline*)))
1752                  store)
1753       body)
1754      "}" *newline*
1755      "finally {" *newline*
1756      (indent
1757       (mapconcat (lambda (b)
1758                    (let ((s (ls-compile `(quote ,(car b)))))
1759                      (code s ".value" " = " (cdr b) ";" *newline*)))
1760                  store))
1761      "}" *newline*)))
1762
1763 (define-compilation let* (bindings &rest body)
1764   (let ((bindings (mapcar #'ensure-list bindings))
1765         (*environment* (copy-lexenv *environment*)))
1766     (js!selfcall
1767       (let ((specials (remove-if-not #'special-variable-p (mapcar #'first bindings)))
1768             (body (concat (mapconcat #'let*-initialize-value bindings)
1769                           (ls-compile-block body t))))
1770         (let*-binding-wrapper specials body)))))
1771
1772
1773 (defvar *block-counter* 0)
1774
1775 (define-compilation block (name &rest body)
1776   (let* ((tr (incf *block-counter*))
1777          (b (make-binding name 'block tr)))
1778     (when *multiple-value-p*
1779       (push-binding-declaration 'multiple-value b))
1780     (let* ((*environment* (extend-lexenv (list b) *environment* 'block))
1781            (cbody (ls-compile-block body t)))
1782       (if (member 'used (binding-declarations b))
1783           (js!selfcall
1784             "try {" *newline*
1785             (indent cbody)
1786             "}" *newline*
1787             "catch (cf){" *newline*
1788             "    if (cf.type == 'block' && cf.id == " tr ")" *newline*
1789             (if *multiple-value-p*
1790                 "        return values.apply(this, forcemv(cf.values));"
1791                 "        return cf.values;")
1792             *newline*
1793             "    else" *newline*
1794             "        throw cf;" *newline*
1795             "}" *newline*)
1796           (js!selfcall cbody)))))
1797
1798 (define-compilation return-from (name &optional value)
1799   (let* ((b (lookup-in-lexenv name *environment* 'block))
1800          (multiple-value-p (member 'multiple-value (binding-declarations b))))
1801     (when (null b)
1802       (error (concat "Unknown block `" (symbol-name name) "'.")))
1803     (push-binding-declaration 'used b)
1804     (js!selfcall
1805       (when multiple-value-p (code "var values = mv;" *newline*))
1806       "throw ({"
1807       "type: 'block', "
1808       "id: " (binding-value b) ", "
1809       "values: " (ls-compile value multiple-value-p) ", "
1810       "message: 'Return from unknown block " (symbol-name name) ".'"
1811       "})")))
1812
1813 (define-compilation catch (id &rest body)
1814   (js!selfcall
1815     "var id = " (ls-compile id) ";" *newline*
1816     "try {" *newline*
1817     (indent (ls-compile-block body t)) *newline*
1818     "}" *newline*
1819     "catch (cf){" *newline*
1820     "    if (cf.type == 'catch' && cf.id == id)" *newline*
1821     (if *multiple-value-p*
1822         "        return values.apply(this, forcemv(cf.values));"
1823         "        return pv.apply(this, forcemv(cf.values));")
1824     *newline*
1825     "    else" *newline*
1826     "        throw cf;" *newline*
1827     "}" *newline*))
1828
1829 (define-compilation throw (id value)
1830   (js!selfcall
1831     "var values = mv;" *newline*
1832     "throw ({"
1833     "type: 'catch', "
1834     "id: " (ls-compile id) ", "
1835     "values: " (ls-compile value t) ", "
1836     "message: 'Throw uncatched.'"
1837     "})"))
1838
1839
1840 (defvar *tagbody-counter* 0)
1841 (defvar *go-tag-counter* 0)
1842
1843 (defun go-tag-p (x)
1844   (or (integerp x) (symbolp x)))
1845
1846 (defun declare-tagbody-tags (tbidx body)
1847   (let ((bindings
1848          (mapcar (lambda (label)
1849                    (let ((tagidx (integer-to-string (incf *go-tag-counter*))))
1850                      (make-binding label 'gotag (list tbidx tagidx))))
1851                  (remove-if-not #'go-tag-p body))))
1852     (extend-lexenv bindings *environment* 'gotag)))
1853
1854 (define-compilation tagbody (&rest body)
1855   ;; Ignore the tagbody if it does not contain any go-tag. We do this
1856   ;; because 1) it is easy and 2) many built-in forms expand to a
1857   ;; implicit tagbody, so we save some space.
1858   (unless (some #'go-tag-p body)
1859     (return-from tagbody (ls-compile `(progn ,@body nil))))
1860   ;; The translation assumes the first form in BODY is a label
1861   (unless (go-tag-p (car body))
1862     (push (gensym "START") body))
1863   ;; Tagbody compilation
1864   (let ((tbidx *tagbody-counter*))
1865     (let ((*environment* (declare-tagbody-tags tbidx body))
1866           initag)
1867       (let ((b (lookup-in-lexenv (first body) *environment* 'gotag)))
1868         (setq initag (second (binding-value b))))
1869       (js!selfcall
1870         "var tagbody_" tbidx " = " initag ";" *newline*
1871         "tbloop:" *newline*
1872         "while (true) {" *newline*
1873         (indent "try {" *newline*
1874                 (indent (let ((content ""))
1875                           (code "switch(tagbody_" tbidx "){" *newline*
1876                                 "case " initag ":" *newline*
1877                                 (dolist (form (cdr body) content)
1878                                   (concatf content
1879                                     (if (not (go-tag-p form))
1880                                         (indent (ls-compile form) ";" *newline*)
1881                                         (let ((b (lookup-in-lexenv form *environment* 'gotag)))
1882                                           (code "case " (second (binding-value b)) ":" *newline*)))))
1883                                 "default:" *newline*
1884                                 "    break tbloop;" *newline*
1885                                 "}" *newline*)))
1886                 "}" *newline*
1887                 "catch (jump) {" *newline*
1888                 "    if (jump.type == 'tagbody' && jump.id == " tbidx ")" *newline*
1889                 "        tagbody_" tbidx " = jump.label;" *newline*
1890                 "    else" *newline*
1891                 "        throw(jump);" *newline*
1892                 "}" *newline*)
1893         "}" *newline*
1894         "return " (ls-compile nil) ";" *newline*))))
1895
1896 (define-compilation go (label)
1897   (let ((b (lookup-in-lexenv label *environment* 'gotag))
1898         (n (cond
1899              ((symbolp label) (symbol-name label))
1900              ((integerp label) (integer-to-string label)))))
1901     (when (null b)
1902       (error (concat "Unknown tag `" n "'.")))
1903     (js!selfcall
1904       "throw ({"
1905       "type: 'tagbody', "
1906       "id: " (first (binding-value b)) ", "
1907       "label: " (second (binding-value b)) ", "
1908       "message: 'Attempt to GO to non-existing tag " n "'"
1909       "})" *newline*)))
1910
1911 (define-compilation unwind-protect (form &rest clean-up)
1912   (js!selfcall
1913     "var ret = " (ls-compile nil) ";" *newline*
1914     "try {" *newline*
1915     (indent "ret = " (ls-compile form) ";" *newline*)
1916     "} finally {" *newline*
1917     (indent (ls-compile-block clean-up))
1918     "}" *newline*
1919     "return ret;" *newline*))
1920
1921 (define-compilation multiple-value-call (func-form &rest forms)
1922   (js!selfcall
1923     "var func = " (ls-compile func-form) ";" *newline*
1924     "var args = [" (if *multiple-value-p* "values" "pv") "];" *newline*
1925     "return "
1926     (js!selfcall
1927       "var values = mv;" *newline*
1928       "var vs;" *newline*
1929       (mapconcat (lambda (form)
1930                    (code "vs = " (ls-compile form t) ";" *newline*
1931                          "if (typeof vs === 'object' && 'multiple-value' in vs)" *newline*
1932                          (indent "args = args.concat(vs);" *newline*)
1933                          "else" *newline*
1934                          (indent "args.push(vs);" *newline*)))
1935                  forms)
1936       "return func.apply(window, args);" *newline*) ";" *newline*))
1937
1938 (define-compilation multiple-value-prog1 (first-form &rest forms)
1939   (js!selfcall
1940     "var args = " (ls-compile first-form *multiple-value-p*) ";" *newline*
1941     (ls-compile-block forms)
1942     "return args;" *newline*))
1943
1944
1945
1946 ;;; A little backquote implementation without optimizations of any
1947 ;;; kind for ecmalisp.
1948 (defun backquote-expand-1 (form)
1949   (cond
1950     ((symbolp form)
1951      (list 'quote form))
1952     ((atom form)
1953      form)
1954     ((eq (car form) 'unquote)
1955      (car form))
1956     ((eq (car form) 'backquote)
1957      (backquote-expand-1 (backquote-expand-1 (cadr form))))
1958     (t
1959      (cons 'append
1960            (mapcar (lambda (s)
1961                      (cond
1962                        ((and (listp s) (eq (car s) 'unquote))
1963                         (list 'list (cadr s)))
1964                        ((and (listp s) (eq (car s) 'unquote-splicing))
1965                         (cadr s))
1966                        (t
1967                         (list 'list (backquote-expand-1 s)))))
1968                    form)))))
1969
1970 (defun backquote-expand (form)
1971   (if (and (listp form) (eq (car form) 'backquote))
1972       (backquote-expand-1 (cadr form))
1973       form))
1974
1975 (defmacro backquote (form)
1976   (backquote-expand-1 form))
1977
1978 (define-transformation backquote (form)
1979   (backquote-expand-1 form))
1980
1981 ;;; Primitives
1982
1983 (defvar *builtins* nil)
1984
1985 (defmacro define-raw-builtin (name args &body body)
1986   ;; Creates a new primitive function `name' with parameters args and
1987   ;; @body. The body can access to the local environment through the
1988   ;; variable *ENVIRONMENT*.
1989   `(push (list ',name (lambda ,args (block ,name ,@body)))
1990          *builtins*))
1991
1992 (defmacro define-builtin (name args &body body)
1993   `(progn
1994      (define-raw-builtin ,name ,args
1995        (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args)
1996          ,@body))))
1997
1998 ;;; DECLS is a list of (JSVARNAME TYPE LISPFORM) declarations.
1999 (defmacro type-check (decls &body body)
2000   `(js!selfcall
2001      ,@(mapcar (lambda (decl)
2002                  `(code "var " ,(first decl) " = " ,(third decl) ";" *newline*))
2003                decls)
2004      ,@(mapcar (lambda (decl)
2005                  `(code "if (typeof " ,(first decl) " != '" ,(second decl) "')" *newline*
2006                         (indent "throw 'The value ' + "
2007                                 ,(first decl)
2008                                 " + ' is not a type "
2009                                 ,(second decl)
2010                                 ".';"
2011                                 *newline*)))
2012                decls)
2013      (code "return " (progn ,@body) ";" *newline*)))
2014
2015 ;;; VARIABLE-ARITY compiles variable arity operations. ARGS stands for
2016 ;;; a variable which holds a list of forms. It will compile them and
2017 ;;; store the result in some Javascript variables. BODY is evaluated
2018 ;;; with ARGS bound to the list of these variables to generate the
2019 ;;; code which performs the transformation on these variables.
2020
2021 (defun variable-arity-call (args function)
2022   (unless (consp args)
2023     (error "ARGS must be a non-empty list"))
2024   (let ((counter 0)
2025         (fargs '())
2026         (prelude ""))
2027     (dolist (x args)
2028       (if (numberp x)
2029           (push (integer-to-string x) fargs)
2030           (let ((v (code "x" (incf counter))))
2031             (push v fargs)
2032             (concatf prelude
2033               (code "var " v " = " (ls-compile x) ";" *newline*
2034                     "if (typeof " v " !== 'number') throw 'Not a number!';"
2035                     *newline*)))))
2036     (js!selfcall prelude (funcall function (reverse fargs)))))
2037
2038
2039 (defmacro variable-arity (args &body body)
2040   (unless (symbolp args)
2041     (error "Bad usage of VARIABLE-ARITY, you must pass a symbol"))
2042   `(variable-arity-call ,args
2043                         (lambda (,args)
2044                           (code "return " ,@body ";" *newline*))))
2045
2046 (defun num-op-num (x op y)
2047   (type-check (("x" "number" x) ("y" "number" y))
2048     (code "x" op "y")))
2049
2050 (define-raw-builtin + (&rest numbers)
2051   (if (null numbers)
2052       "0"
2053       (variable-arity numbers
2054         (join numbers "+"))))
2055
2056 (define-raw-builtin - (x &rest others)
2057   (let ((args (cons x others)))
2058     (variable-arity args
2059       (if (null others)
2060           (concat "-" (car args))
2061           (join args "-")))))
2062
2063 (define-raw-builtin * (&rest numbers)
2064   (if (null numbers)
2065       "1"
2066       (variable-arity numbers
2067         (join numbers "*"))))
2068
2069 (define-raw-builtin / (x &rest others)
2070   (let ((args (cons x others)))
2071     (variable-arity args
2072       (if (null others)
2073           (concat "1 /" (car args))
2074           (join args "/")))))
2075
2076 (define-builtin mod (x y) (num-op-num x "%" y))
2077
2078
2079 (defun comparison-conjuntion (vars op)
2080   (cond
2081     ((null (cdr vars))
2082      "true")
2083     ((null (cddr vars))
2084      (concat (car vars) op (cadr vars)))
2085     (t
2086      (concat (car vars) op (cadr vars)
2087              " && "
2088              (comparison-conjuntion (cdr vars) op)))))
2089
2090 (defmacro define-builtin-comparison (op sym)
2091   `(define-raw-builtin ,op (x &rest args)
2092      (let ((args (cons x args)))
2093        (variable-arity args
2094          (js!bool (comparison-conjuntion args ,sym))))))
2095
2096 (define-builtin-comparison > ">")
2097 (define-builtin-comparison < "<")
2098 (define-builtin-comparison >= ">=")
2099 (define-builtin-comparison <= "<=")
2100 (define-builtin-comparison = "==")
2101
2102 (define-builtin numberp (x)
2103   (js!bool (code "(typeof (" x ") == \"number\")")))
2104
2105 (define-builtin floor (x)
2106   (type-check (("x" "number" x))
2107     "Math.floor(x)"))
2108
2109 (define-builtin cons (x y)
2110   (code "({car: " x ", cdr: " y "})"))
2111
2112 (define-builtin consp (x)
2113   (js!bool
2114    (js!selfcall
2115      "var tmp = " x ";" *newline*
2116      "return (typeof tmp == 'object' && 'car' in tmp);" *newline*)))
2117
2118 (define-builtin car (x)
2119   (js!selfcall
2120     "var tmp = " x ";" *newline*
2121     "return tmp === " (ls-compile nil)
2122     "? " (ls-compile nil)
2123     ": tmp.car;" *newline*))
2124
2125 (define-builtin cdr (x)
2126   (js!selfcall
2127     "var tmp = " x ";" *newline*
2128     "return tmp === " (ls-compile nil) "? "
2129     (ls-compile nil)
2130     ": tmp.cdr;" *newline*))
2131
2132 (define-builtin rplaca (x new)
2133   (type-check (("x" "object" x))
2134     (code "(x.car = " new ", x)")))
2135
2136 (define-builtin rplacd (x new)
2137   (type-check (("x" "object" x))
2138     (code "(x.cdr = " new ", x)")))
2139
2140 (define-builtin symbolp (x)
2141   (js!bool
2142    (js!selfcall
2143      "var tmp = " x ";" *newline*
2144      "return (typeof tmp == 'object' && 'name' in tmp);" *newline*)))
2145
2146 (define-builtin make-symbol (name)
2147   (type-check (("name" "string" name))
2148     "({name: name})"))
2149
2150 (define-builtin symbol-name (x)
2151   (code "(" x ").name"))
2152
2153 (define-builtin set (symbol value)
2154   (code "(" symbol ").value = " value))
2155
2156 (define-builtin fset (symbol value)
2157   (code "(" symbol ").fvalue = " value))
2158
2159 (define-builtin boundp (x)
2160   (js!bool (code "(" x ".value !== undefined)")))
2161
2162 (define-builtin symbol-value (x)
2163   (js!selfcall
2164     "var symbol = " x ";" *newline*
2165     "var value = symbol.value;" *newline*
2166     "if (value === undefined) throw \"Variable `\" + symbol.name + \"' is unbound.\";" *newline*
2167     "return value;" *newline*))
2168
2169 (define-builtin symbol-function (x)
2170   (js!selfcall
2171     "var symbol = " x ";" *newline*
2172     "var func = symbol.fvalue;" *newline*
2173     "if (func === undefined) throw \"Function `\" + symbol.name + \"' is undefined.\";" *newline*
2174     "return func;" *newline*))
2175
2176 (define-builtin symbol-plist (x)
2177   (code "((" x ").plist || " (ls-compile nil) ")"))
2178
2179 (define-builtin lambda-code (x)
2180   (code "(" x ").toString()"))
2181
2182 (define-builtin eq    (x y) (js!bool (code "(" x " === " y ")")))
2183 (define-builtin equal (x y) (js!bool (code "(" x  " == " y ")")))
2184
2185 (define-builtin char-to-string (x)
2186   (type-check (("x" "number" x))
2187     "String.fromCharCode(x)"))
2188
2189 (define-builtin stringp (x)
2190   (js!bool (code "(typeof(" x ") == \"string\")")))
2191
2192 (define-builtin string-upcase (x)
2193   (type-check (("x" "string" x))
2194     "x.toUpperCase()"))
2195
2196 (define-builtin string-length (x)
2197   (type-check (("x" "string" x))
2198     "x.length"))
2199
2200 (define-raw-builtin slice (string a &optional b)
2201   (js!selfcall
2202     "var str = " (ls-compile string) ";" *newline*
2203     "var a = " (ls-compile a) ";" *newline*
2204     "var b;" *newline*
2205     (when b (code "b = " (ls-compile b) ";" *newline*))
2206     "return str.slice(a,b);" *newline*))
2207
2208 (define-builtin char (string index)
2209   (type-check (("string" "string" string)
2210                ("index" "number" index))
2211     "string.charCodeAt(index)"))
2212
2213 (define-builtin concat-two (string1 string2)
2214   (type-check (("string1" "string" string1)
2215                ("string2" "string" string2))
2216     "string1.concat(string2)"))
2217
2218 (define-raw-builtin funcall (func &rest args)
2219   (code "(" (ls-compile func) ")("
2220         (join (cons (if *multiple-value-p* "values" "pv")
2221                     (mapcar #'ls-compile args))
2222               ", ")
2223         ")"))
2224
2225 (define-raw-builtin apply (func &rest args)
2226   (if (null args)
2227       (code "(" (ls-compile func) ")()")
2228       (let ((args (butlast args))
2229             (last (car (last args))))
2230         (js!selfcall
2231           "var f = " (ls-compile func) ";" *newline*
2232           "var args = [" (join (cons (if *multiple-value-p* "values" "pv")
2233                                      (mapcar #'ls-compile args))
2234                                ", ")
2235           "];" *newline*
2236           "var tail = (" (ls-compile last) ");" *newline*
2237           "while (tail != " (ls-compile nil) "){" *newline*
2238           "    args.push(tail.car);" *newline*
2239           "    tail = tail.cdr;" *newline*
2240           "}" *newline*
2241           "return f.apply(this, args);" *newline*))))
2242
2243 (define-builtin js-eval (string)
2244   (type-check (("string" "string" string))
2245     (if *multiple-value-p*
2246         (js!selfcall
2247           "var v = eval.apply(window, [string]);" *newline*
2248           "if (typeof v !== 'object' || !('multiple-value' in v)){" *newline*
2249           (indent "v = [v];" *newline*
2250                   "v['multiple-value'] = true;" *newline*)
2251           "}" *newline*
2252           "return values.apply(this, v);" *newline*)
2253         "eval.apply(window, [string])")))
2254
2255 (define-builtin error (string)
2256   (js!selfcall "throw " string ";" *newline*))
2257
2258 (define-builtin new () "{}")
2259
2260 (define-builtin objectp (x)
2261   (js!bool (code "(typeof (" x ") === 'object')")))
2262
2263 (define-builtin oget (object key)
2264   (js!selfcall
2265     "var tmp = " "(" object ")[" key "];" *newline*
2266     "return tmp == undefined? " (ls-compile nil) ": tmp ;" *newline*))
2267
2268 (define-builtin oset (object key value)
2269   (code "((" object ")[" key "] = " value ")"))
2270
2271 (define-builtin in (key object)
2272   (js!bool (code "((" key ") in (" object "))")))
2273
2274 (define-builtin functionp (x)
2275   (js!bool (code "(typeof " x " == 'function')")))
2276
2277 (define-builtin write-string (x)
2278   (type-check (("x" "string" x))
2279     "lisp.write(x)"))
2280
2281 (define-builtin make-array (n)
2282   (js!selfcall
2283     "var r = [];" *newline*
2284     "for (var i = 0; i < " n "; i++)" *newline*
2285     (indent "r.push(" (ls-compile nil) ");" *newline*)
2286     "return r;" *newline*))
2287
2288 (define-builtin arrayp (x)
2289   (js!bool
2290    (js!selfcall
2291      "var x = " x ";" *newline*
2292      "return typeof x === 'object' && 'length' in x;")))
2293
2294 (define-builtin aref (array n)
2295   (js!selfcall
2296     "var x = " "(" array ")[" n "];" *newline*
2297     "if (x === undefined) throw 'Out of range';" *newline*
2298     "return x;" *newline*))
2299
2300 (define-builtin aset (array n value)
2301   (js!selfcall
2302     "var x = " array ";" *newline*
2303     "var i = " n ";" *newline*
2304     "if (i < 0 || i >= x.length) throw 'Out of range';" *newline*
2305     "return x[i] = " value ";" *newline*))
2306
2307 (define-builtin get-unix-time ()
2308   (code "(Math.round(new Date() / 1000))"))
2309
2310 (define-builtin values-array (array)
2311   (if *multiple-value-p*
2312       (code "values.apply(this, " array ")")
2313       (code "pv.apply(this, " array ")")))
2314
2315 (define-raw-builtin values (&rest args)
2316   (if *multiple-value-p*
2317       (code "values(" (join (mapcar #'ls-compile args) ", ") ")")
2318       (code "pv(" (join (mapcar #'ls-compile args) ", ") ")")))
2319
2320 (defun macro (x)
2321   (and (symbolp x)
2322        (let ((b (lookup-in-lexenv x *environment* 'function)))
2323          (and (eq (binding-type b) 'macro)
2324               b))))
2325
2326 (defun ls-macroexpand-1 (form)
2327   (let ((macro-binding (macro (car form))))
2328     (if macro-binding
2329         (let ((expander (binding-value macro-binding)))
2330           (when (listp expander)
2331             (let ((compiled (eval expander)))
2332               ;; The list representation are useful while
2333               ;; bootstrapping, as we can dump the definition of the
2334               ;; macros easily, but they are slow because we have to
2335               ;; evaluate them and compile them now and again. So, let
2336               ;; us replace the list representation version of the
2337               ;; function with the compiled one.
2338               ;;
2339               #+ecmalisp (set-binding-value macro-binding compiled)
2340               (setq expander compiled)))
2341           (apply expander (cdr form)))
2342         form)))
2343
2344 (defun compile-funcall (function args)
2345   (let* ((values-funcs (if *multiple-value-p* "values" "pv"))
2346          (arglist (concat "(" (join (cons values-funcs (mapcar #'ls-compile args)) ", ") ")")))
2347     (cond
2348       ((translate-function function)
2349        (concat (translate-function function) arglist))
2350       ((and (symbolp function)
2351             #+ecmalisp (eq (symbol-package function) (find-package "COMMON-LISP"))
2352             #+common-lisp t)
2353        (code (ls-compile `',function) ".fvalue" arglist))
2354       (t
2355        (code (ls-compile `#',function) arglist)))))
2356
2357 (defun ls-compile-block (sexps &optional return-last-p)
2358   (if return-last-p
2359       (code (ls-compile-block (butlast sexps))
2360             "return " (ls-compile (car (last sexps)) *multiple-value-p*) ";")
2361       (join-trailing
2362        (remove-if #'null-or-empty-p (mapcar #'ls-compile sexps))
2363        (concat ";" *newline*))))
2364
2365 (defun ls-compile (sexp &optional multiple-value-p)
2366   (let ((*multiple-value-p* multiple-value-p))
2367     (cond
2368       ((symbolp sexp)
2369        (let ((b (lookup-in-lexenv sexp *environment* 'variable)))
2370          (cond
2371            ((and b (not (member 'special (binding-declarations b))))
2372             (binding-value b))
2373            ((or (keywordp sexp)
2374                 (member 'constant (binding-declarations b)))
2375             (code (ls-compile `',sexp) ".value"))
2376            (t
2377             (ls-compile `(symbol-value ',sexp))))))
2378       ((integerp sexp) (integer-to-string sexp))
2379       ((stringp sexp) (code "\"" (escape-string sexp) "\""))
2380       ((arrayp sexp) (literal sexp))
2381       ((listp sexp)
2382        (let ((name (car sexp))
2383              (args (cdr sexp)))
2384          (cond
2385            ;; Special forms
2386            ((assoc name *compilations*)
2387             (let ((comp (second (assoc name *compilations*))))
2388               (apply comp args)))
2389            ;; Built-in functions
2390            ((and (assoc name *builtins*)
2391                  (not (claimp name 'function 'notinline)))
2392             (let ((comp (second (assoc name *builtins*))))
2393               (apply comp args)))
2394            (t
2395             (if (macro name)
2396                 (ls-compile (ls-macroexpand-1 sexp) multiple-value-p)
2397                 (compile-funcall name args))))))
2398       (t
2399        (error "How should I compile this?")))))
2400
2401 (defun ls-compile-toplevel (sexp &optional multiple-value-p)
2402   (let ((*toplevel-compilations* nil))
2403     (cond
2404       ((and (consp sexp) (eq (car sexp) 'progn))
2405        (let ((subs (mapcar (lambda (s)
2406                              (ls-compile-toplevel s t))
2407                            (cdr sexp))))
2408          (join (remove-if #'null-or-empty-p subs))))
2409       (t
2410        (let ((code (ls-compile sexp multiple-value-p)))
2411          (code (join-trailing (get-toplevel-compilations)
2412                               (code ";" *newline*))
2413                (when code
2414                  (code code ";" *newline*))))))))
2415
2416
2417 ;;; Once we have the compiler, we define the runtime environment and
2418 ;;; interactive development (eval), which works calling the compiler
2419 ;;; and evaluating the Javascript result globally.
2420
2421 #+ecmalisp
2422 (progn
2423   (defun eval (x)
2424     (js-eval (ls-compile-toplevel x t)))
2425
2426   (export '(&rest &key &optional &body * *gensym-counter* *package* +
2427             - / 1+ 1- < <= = = > >= and append apply aref arrayp assoc
2428             atom block boundp boundp butlast caar cadddr caddr cadr
2429             car car case catch cdar cdddr cddr cdr cdr char char-code
2430             char= code-char cond cons consp constantly copy-list decf
2431             declaim defconstant defparameter defun defmacro defvar
2432             digit-char digit-char-p disassemble do do* documentation
2433             dolist dotimes ecase eq eql equal error eval every export
2434             fdefinition find-package find-symbol first flet fourth
2435             fset funcall function functionp gensym get-universal-time
2436             go identity if in-package incf integerp integerp intern
2437             keywordp labels lambda last length let let*
2438             list-all-packages list listp make-array make-package
2439             make-symbol mapcar member minusp mod multiple-value-bind
2440             multiple-value-call multiple-value-list
2441             multiple-value-prog1 nil not nth nthcdr null numberp or
2442             package-name package-use-list packagep parse-integer plusp
2443             prin1-to-string print proclaim prog1 prog2 progn psetq
2444             push quote remove remove-if remove-if-not return
2445             return-from revappend reverse rplaca rplacd second set setf
2446             setq some string-upcase string string= stringp subseq
2447             symbol-function symbol-name symbol-package symbol-plist
2448             symbol-value symbolp t tagbody third throw truncate unless
2449             unwind-protect values values-list variable warn when
2450             write-line write-string zerop))
2451
2452   (setq *package* *user-package*)
2453
2454   (js-eval "var lisp")
2455   (js-vset "lisp" (new))
2456   (js-vset "lisp.read" #'ls-read-from-string)
2457   (js-vset "lisp.print" #'prin1-to-string)
2458   (js-vset "lisp.eval" #'eval)
2459   (js-vset "lisp.compile" (lambda (s) (ls-compile-toplevel s t)))
2460   (js-vset "lisp.evalString" (lambda (str) (eval (ls-read-from-string str))))
2461   (js-vset "lisp.compileString" (lambda (str) (ls-compile-toplevel (ls-read-from-string str) t)))
2462
2463   ;; Set the initial global environment to be equal to the host global
2464   ;; environment at this point of the compilation.
2465   (eval-when-compile
2466     (toplevel-compilation
2467      (ls-compile `(setq *environment* ',*environment*))))
2468
2469   (eval-when-compile
2470     (toplevel-compilation
2471      (ls-compile
2472       `(progn
2473          ,@(mapcar (lambda (s) `(%intern-symbol (js-vref ,(cdr s))))
2474                    *literal-symbols*)
2475          (setq *literal-symbols* ',*literal-symbols*)
2476          (setq *variable-counter* ,*variable-counter*)
2477          (setq *gensym-counter* ,*gensym-counter*)
2478          (setq *block-counter* ,*block-counter*)))))
2479
2480   (eval-when-compile
2481     (toplevel-compilation
2482      (ls-compile
2483       `(setq *literal-counter* ,*literal-counter*)))))
2484
2485
2486 ;;; Finally, we provide a couple of functions to easily bootstrap
2487 ;;; this. It just calls the compiler with this file as input.
2488
2489 #+common-lisp
2490 (progn
2491   (defun read-whole-file (filename)
2492     (with-open-file (in filename)
2493       (let ((seq (make-array (file-length in) :element-type 'character)))
2494         (read-sequence seq in)
2495         seq)))
2496
2497   (defun ls-compile-file (filename output)
2498     (let ((*compiling-file* t))
2499       (with-open-file (out output :direction :output :if-exists :supersede)
2500         (write-string (read-whole-file "prelude.js") out)
2501         (let* ((source (read-whole-file filename))
2502                (in (make-string-stream source)))
2503           (loop
2504              for x = (ls-read in)
2505              until (eq x *eof*)
2506              for compilation = (ls-compile-toplevel x)
2507              when (plusp (length compilation))
2508              do (write-string compilation out))))))
2509
2510   (defun bootstrap ()
2511     (setq *environment* (make-lexenv))
2512     (setq *literal-symbols* nil)
2513     (setq *variable-counter* 0
2514           *gensym-counter* 0
2515           *literal-counter* 0
2516           *block-counter* 0)
2517     (ls-compile-file "ecmalisp.lisp" "ecmalisp.js")))