3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
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.
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.
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/>.
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.
28 (%compile-defmacro 'defmacro
30 (lambda (name args &rest body)
32 (%compile-defmacro ',name
34 (lambda ,(mapcar #'(lambda (x)
41 (defmacro declaim (&rest decls)
43 ,@(mapcar (lambda (decl) `(!proclaim ',decl)) decls)))
45 (defmacro defconstant (name value &optional docstring)
47 (declaim (special ,name))
48 (declaim (constant ,name))
50 ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
54 (defconstant nil 'nil)
57 (defmacro lambda (args &body body)
58 `(function (lambda ,args ,@body)))
60 (defmacro when (condition &body body)
61 `(if ,condition (progn ,@body) nil))
63 (defmacro unless (condition &body body)
64 `(if ,condition nil (progn ,@body)))
66 (defmacro defvar (name value &optional docstring)
68 (declaim (special ,name))
69 (unless (boundp ',name) (setq ,name ,value))
70 ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
73 (defmacro defparameter (name value &optional docstring)
76 ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
79 (defmacro named-lambda (name args &rest body)
80 (let ((x (gensym "FN")))
81 `(let ((,x (lambda ,args ,@body)))
82 (oset ,x "fname" ,name)
85 (defmacro defun (name args &rest body)
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)))))
97 (defmacro return (&optional value)
98 `(return-from nil ,value))
100 (defmacro while (condition &body body)
101 `(block nil (%while ,condition ,@body)))
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*))))
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)))
120 (defun eql (x y) (eq x y))
122 (defun not (x) (if x nil t))
124 (defun cons (x y ) (cons x y))
125 (defun consp (x) (consp x))
128 "Return the CAR part of a cons, or NIL if X is null."
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))
145 (defun list (&rest args) args)
151 (defmacro incf (x &optional (delta 1))
152 `(setq ,x (+ ,x ,delta)))
154 (defmacro decf (x &optional (delta 1))
155 `(setq ,x (- ,x ,delta)))
157 (defmacro push (x place)
158 `(setq ,place (cons ,x ,place)))
160 (defmacro dolist (iter &body body)
161 (let ((var (first iter))
164 (let ((,g!list ,(second iter))
167 (setq ,var (car ,g!list))
169 (setq ,g!list (cdr ,g!list)))
172 (defmacro dotimes (iter &body body)
173 (let ((g!to (gensym))
176 (result (third iter)))
180 (%while (< ,var ,g!to)
185 (defmacro cond (&rest clausules)
188 (if (eq (caar clausules) t)
189 `(progn ,@(cdar clausules))
190 `(if ,(caar clausules)
191 (progn ,@(cdar clausules))
192 (cond ,@(cdr clausules))))))
194 (defmacro case (form &rest clausules)
195 (let ((!form (gensym)))
196 `(let ((,!form ,form))
198 ,@(mapcar (lambda (clausule)
199 (if (eq (car clausule) t)
201 `((eql ,!form ',(car clausule))
205 (defmacro ecase (form &rest clausules)
210 (error "ECASE expression failed."))))))
212 (defmacro and (&rest forms)
223 (defmacro or (&rest forms)
231 `(let ((,g ,(car forms)))
232 (if ,g ,g (or ,@(cdr forms))))))))
234 (defmacro prog1 (form &body body)
235 (let ((value (gensym)))
236 `(let ((,value ,form))
240 (defmacro prog2 (form1 result &body body)
241 `(prog1 (progn ,form1 ,result) ,@body)))
244 ;;; This couple of helper functions will be defined in both Common
245 ;;; Lisp and in Ecmalisp.
246 (defun ensure-list (x)
251 (defun !reduce (func list initial)
256 (funcall func initial (car list)))))
258 ;;; Go on growing the Lisp language in Ecmalisp, with more high
259 ;;; level utilities as well as correct versions of other
263 (defun + (&rest args)
268 (defun - (x &rest others)
275 (defun append-two (list1 list2)
279 (append (cdr list1) list2))))
281 (defun append (&rest lists)
282 (!reduce #'append-two lists '()))
284 (defun revappend (list1 list2)
286 (push (car list1) list2)
287 (setq list1 (cdr list1)))
290 (defun reverse (list)
291 (revappend list '()))
293 (defmacro psetq (&rest pairs)
294 (let ( ;; For each pair, we store here a list of the form
295 ;; (VARIABLE GENSYM VALUE).
299 ((null pairs) (return))
301 (error "Odd paris in PSETQ"))
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))
309 `(let ,(mapcar #'cdr assignments)
310 (setq ,@(!reduce #'append (mapcar #'butlast assignments) '())))))
312 (defmacro do (varlist endlist &body body)
314 (let ,(mapcar (lambda (x) (list (first x) (second x))) varlist)
317 (return (progn ,(cdr endlist))))
322 (and (consp (cddr v))
323 (list (first v) (third v))))
326 (defmacro do* (varlist endlist &body body)
328 (let* ,(mapcar (lambda (x) (list (first x) (second x))) varlist)
331 (return (progn ,(cdr endlist))))
336 (and (consp (cddr v))
337 (list (first v) (third v))))
340 (defun list-length (list)
342 (while (not (null list))
344 (setq list (cdr list)))
356 (defun concat-two (s1 s2)
359 (defmacro with-collect (&body body)
360 (let ((head (gensym))
362 `(let* ((,head (cons 'sentinel nil))
365 (rplacd ,tail (cons x nil))
366 (setq ,tail (cdr ,tail))
371 (defun map1 (func list)
374 (collect (funcall func (car list)))
375 (setq list (cdr list)))))
377 (defmacro loop (&body body)
380 (defun mapcar (func list &rest lists)
381 (let ((lists (cons list lists)))
385 (let ((elems (map1 #'car lists)))
386 (do ((tail lists (cdr tail)))
388 (when (null (car tail)) (return-from loop))
389 (rplaca tail (cdar tail)))
390 (collect (apply func elems))))))))
392 (defun identity (x) x)
394 (defun constantly (x)
399 (mapcar #'identity x))
401 (defun code-char (x) x)
402 (defun char-code (x) x)
403 (defun char= (x y) (= x y))
406 (and (numberp x) (= (floor x) x)))
408 (defun plusp (x) (< 0 x))
409 (defun minusp (x) (< x 0))
412 (or (consp x) (null x)))
414 (defun nthcdr (n list)
415 (while (and (plusp n) list)
417 (setq list (cdr list)))
421 (car (nthcdr n list)))
424 (while (consp (cdr x))
430 (cons (car x) (butlast (cdr x)))))
432 (defun member (x list)
434 (when (eql x (car list))
436 (setq list (cdr list))))
438 (defun remove (x list)
443 (remove x (cdr list)))
445 (cons (car list) (remove x (cdr list))))))
447 (defun remove-if (func list)
451 ((funcall func (car list))
452 (remove-if func (cdr list)))
454 (cons (car list) (remove-if func (cdr list))))))
456 (defun remove-if-not (func list)
460 ((funcall func (car list))
461 (cons (car list) (remove-if-not func (cdr list))))
463 (remove-if-not func (cdr list)))))
465 (defun digit-char-p (x)
466 (if (and (<= #\0 x) (<= x #\9))
470 (defun digit-char (weight)
472 (char "0123456789" weight)))
474 (defun subseq (seq a &optional b)
481 (error "Unsupported argument."))))
483 (defun some (function seq)
488 (while (< index size)
489 (when (funcall function (char seq index))
490 (return-from some t))
495 (when (funcall function x)
498 (error "Unknown sequence."))))
500 (defun every (function seq)
505 (while (< index size)
506 (unless (funcall function (char seq index))
507 (return-from every nil))
512 (unless (funcall function x)
515 (error "Unknown sequence."))))
517 (defun assoc (x alist)
519 (if (eql x (caar alist))
521 (setq alist (cdr alist))))
525 (cond ((stringp x) x)
526 ((symbolp x) (symbol-name x))
527 (t (char-to-string x))))
529 (defun string= (s1 s2)
532 (defun fdefinition (x)
539 (error "Invalid function"))))
541 (defun disassemble (function)
542 (write-line (lambda-code (fdefinition function)))
545 (defun documentation (x type)
546 "Return the documentation of X. TYPE must be the symbol VARIABLE or FUNCTION."
549 (let ((func (fdefinition x)))
550 (oget func "docstring")))
553 (error "Wrong argument type! it should be a symbol"))
556 (defmacro multiple-value-bind (variables value-from &body body)
557 `(multiple-value-call (lambda (&optional ,@variables &rest ,(gensym))
561 (defmacro multiple-value-list (value-from)
562 `(multiple-value-call #'list ,value-from))
565 ;;; Generalized references (SETF)
567 (defvar *setf-expanders* nil)
569 (defun get-setf-expansion (place)
571 (let ((value (gensym)))
575 `(setq ,place ,value)
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)))))
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))
590 (defmacro setf (&rest pairs)
595 (error "Odd number of arguments to setf."))
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
609 ,@(do ((pairs pairs (cddr pairs))
610 (result '() (cons `(setf ,(car pairs) ,(cadr pairs)) result)))
612 (reverse result)))))))
614 (define-setf-expander car (x)
615 (let ((cons (gensym))
616 (new-value (gensym)))
620 `(progn (rplaca ,cons ,new-value) ,new-value)
623 (define-setf-expander cdr (x)
624 (let ((cons (gensym))
625 (new-value (gensym)))
629 `(progn (rplacd ,cons ,new-value) ,new-value)
634 (defvar *package-list* nil)
636 (defun list-all-packages ()
639 (defun make-package (name &key 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*)
650 (and (objectp x) (in "symbols" x)))
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)
660 (defun find-package-or-fail (package-designator)
661 (or (find-package package-designator)
662 (error "Package unknown.")))
664 (defun package-name (package-designator)
665 (let ((package (find-package-or-fail package-designator)))
666 (oget package "packageName")))
668 (defun %package-symbols (package-designator)
669 (let ((package (find-package-or-fail package-designator)))
670 (oget package "symbols")))
672 (defun package-use-list (package-designator)
673 (let ((package (find-package-or-fail package-designator)))
674 (oget package "use")))
676 (defun %package-external-symbols (package-designator)
677 (let ((package (find-package-or-fail package-designator)))
678 (oget package "exports")))
680 (defvar *common-lisp-package*
683 (defvar *user-package*
684 (make-package "CL-USER" :use (list *common-lisp-package*)))
686 (defvar *keyword-package*
687 (make-package "KEYWORD"))
690 (and (symbolp x) (eq (symbol-package x) *keyword-package*)))
692 (defvar *package* *common-lisp-package*)
694 (defmacro in-package (package-designator)
696 (setq *package* (find-package-or-fail ,package-designator))))
698 ;; This function is used internally to initialize the CL package
699 ;; with the symbols built during bootstrap.
700 (defun %intern-symbol (symbol)
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)))
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)))
717 (values (oget externals name) :external))
719 (values (oget symbols name) :internal))
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)))))))))
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)
731 (values symbol foundp)
732 (let ((symbols (%package-symbols package)))
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)))))))
742 (defun symbol-package (symbol)
743 (unless (symbolp symbol)
744 (error "it is not a symbol"))
745 (oget symbol "package"))
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))))
752 (defun get-universal-time ()
753 (+ (get-unix-time) 2208988800)))
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.
761 (defmacro while (condition &body body)
766 (defmacro eval-when-compile (&body body)
767 `(eval-when (:compile-toplevel :load-toplevel :execute)
770 (defun concat-two (s1 s2)
771 (concatenate 'string s1 s2))
773 (defun aset (array idx value)
774 (setf (aref array idx) value)))
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.
781 (defvar *newline* (string (code-char 10)))
783 (defun concat (&rest strs)
784 (!reduce #'concat-two strs ""))
786 (defmacro concatf (variable &body form)
787 `(setq ,variable (concat ,variable (progn ,@form))))
789 ;;; Concatenate a list of strings, with a separator
790 (defun join (list &optional (separator ""))
799 (join (cdr list) separator)))))
801 (defun join-trailing (list &optional (separator ""))
804 (concat (car list) separator (join-trailing (cdr list) separator))))
806 (defun mapconcat (func list)
807 (join (mapcar func list)))
809 (defun vector-to-list (vector)
811 (size (length vector)))
812 (dotimes (i size (reverse list))
813 (push (aref vector i) list))))
815 (defun list-to-vector (list)
816 (let ((v (make-array (length list)))
824 (defun values-list (list)
825 (values-array (list-to-vector list)))
827 (defun values (&rest args)
830 (defun integer-to-string (x)
835 (concat "-" (integer-to-string (- 0 x))))
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)))
849 (defun prin1-to-string (form)
852 (multiple-value-bind (symbol foundp)
853 (find-symbol (symbol-name form) *package*)
854 (if (and foundp (eq symbol form))
856 (let ((package (symbol-package form))
857 (name (symbol-name form)))
860 ((eq package (find-package "KEYWORD")) "")
861 (t (package-name package)))
863 ((integerp form) (integer-to-string form))
864 ((stringp form) (concat "\"" (escape-string form) "\""))
866 (let ((name (oget form "fname")))
868 (concat "#<FUNCTION " name ">")
869 (concat "#<FUNCTION>"))))
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)))))
879 (concat "#" (prin1-to-string (vector-to-list form))))
881 (concat "#<PACKAGE " (package-name form) ">"))))
883 (defun write-line (x)
885 (write-string *newline*)
889 (write-string "WARNING: ")
893 (write-line (prin1-to-string x))
899 ;;; The Lisp reader, parse strings and return Lisp objects. The main
900 ;;; entry points are `ls-read' and `ls-read-from-string'.
902 (defun make-string-stream (string)
905 (defun %peek-char (stream)
906 (and (< (cdr stream) (length (car stream)))
907 (char (car stream) (cdr stream))))
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))))))
914 (defun whitespacep (ch)
915 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
917 (defun skip-whitespaces (stream)
919 (setq ch (%peek-char stream))
920 (while (and ch (whitespacep ch))
922 (setq ch (%peek-char stream)))))
924 (defun terminalp (ch)
925 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
927 (defun read-until (stream func)
930 (setq ch (%peek-char stream))
931 (while (and ch (not (funcall func ch)))
932 (setq string (concat string (string ch)))
934 (setq ch (%peek-char stream)))
937 (defun skip-whitespaces-and-comments (stream)
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)))))
946 (defun %read-list (stream)
947 (skip-whitespaces-and-comments stream)
948 (let ((ch (%peek-char stream)))
951 (error "Unspected EOF"))
957 (prog1 (ls-read stream)
958 (skip-whitespaces-and-comments stream)
959 (unless (char= (%read-char stream) #\))
960 (error "')' was expected."))))
962 (cons (ls-read stream) (%read-list stream))))))
964 (defun read-string (stream)
967 (setq ch (%read-char stream))
968 (while (not (eql ch #\"))
970 (error "Unexpected EOF"))
972 (setq ch (%read-char stream)))
973 (setq string (concat string (string ch)))
974 (setq ch (%read-char stream)))
977 (defun read-sharp (stream)
979 (ecase (%read-char stream)
981 (list 'function (ls-read stream)))
982 (#\( (list-to-vector (%read-list stream)))
983 (#\: (make-symbol (string-upcase (read-until stream #'terminalp))))
986 (concat (string (%read-char stream))
987 (read-until stream #'terminalp))))
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))))))
994 (let ((feature (read-until stream #'terminalp)))
996 ((string= feature "common-lisp")
997 (ls-read stream) ;ignore
999 ((string= feature "ecmalisp")
1002 (error "Unknown reader form.")))))))
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)
1012 (while (and (< index size)
1013 (not (char= (char string index) #\:)))
1016 ;; No package prefix
1019 (setq package *package*)
1024 (setq package "KEYWORD")
1025 (setq package (string-upcase (subseq string 0 index))))
1027 (when (char= (char string 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))))
1041 (defun !parse-integer (string junk-allow)
1045 (size (length string))
1047 (when (zerop size) (return (values nil 0)))
1049 (case (char string 0)
1054 (unless (and (< index size)
1055 (setq value (digit-char-p (char string index))))
1056 (return (values nil index)))
1059 (while (< index size)
1060 (let ((digit (digit-char-p (char string index))))
1061 (unless digit (return))
1062 (setq value (+ (* value 10) digit))
1066 (char= (char string index) #\space))
1067 (values (* sign value) index)
1068 (values nil index)))))
1071 (defun parse-integer (string)
1072 (!parse-integer string nil))
1074 (defvar *eof* (gensym))
1075 (defun ls-read (stream)
1076 (skip-whitespaces-and-comments stream)
1077 (let ((ch (%peek-char stream)))
1079 ((or (null ch) (char= ch #\)))
1083 (%read-list stream))
1086 (list 'quote (ls-read stream)))
1089 (list 'backquote (ls-read stream)))
1092 (read-string stream))
1095 (if (eql (%peek-char stream) #\@)
1096 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
1097 (list 'unquote (ls-read stream))))
1099 (read-sharp stream))
1101 (let ((string (read-until stream #'terminalp)))
1102 (or (values (!parse-integer string nil))
1103 (read-symbol string)))))))
1105 (defun ls-read-from-string (string)
1106 (ls-read (make-string-stream string)))
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.
1116 (defun code (&rest args)
1117 (mapconcat (lambda (arg)
1120 ((integerp arg) (integer-to-string arg))
1122 (t (error "Unknown argument."))))
1125 ;;; Wrap X with a Javascript code to convert the result from
1126 ;;; Javascript generalized booleans to T or NIL.
1128 (code "(" x "?" (ls-compile t) ": " (ls-compile nil) ")"))
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) "})()"))
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.
1143 (defun indent (&rest string)
1144 (let ((input (apply #'code string)))
1147 (size (length input)))
1148 (when (plusp (length input)) (concatf output " "))
1149 (while (< index size)
1151 (if (and (char= (char input index) #\newline)
1153 (not (char= (char input (1+ index)) #\newline)))
1154 (concat (string #\newline) " ")
1155 (string (char input index)))))
1156 (concatf output str))
1161 (defun indent (&rest string)
1162 (with-output-to-string (*standard-output*)
1163 (with-input-from-string (input (apply #'code string))
1165 for line = (read-line input nil)
1167 do (write-string " ")
1168 do (write-line line)))))
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
1177 (defvar *multiple-value-p* nil)
1179 (defun make-binding (name type value &optional declarations)
1180 (list name type value declarations))
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))
1187 (defun set-binding-value (b value)
1188 (rplaca (cddr b) value))
1190 (defun set-binding-declarations (b value)
1191 (rplaca (cdddr b) value))
1193 (defun push-binding-declaration (decl b)
1194 (set-binding-declarations b (cons decl (binding-declarations b))))
1197 (defun make-lexenv ()
1198 (list nil nil nil nil))
1200 (defun copy-lexenv (lexenv)
1203 (defun push-to-lexenv (binding lexenv 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))))))
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))))
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)))))
1222 (defvar *environment* (make-lexenv))
1224 (defvar *variable-counter* 0)
1225 (defun gvarname (symbol)
1226 (code "v" (incf *variable-counter*)))
1228 (defun translate-variable (symbol)
1229 (binding-value (lookup-in-lexenv symbol *environment* 'variable)))
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)))))
1237 ;;; Toplevel compilations
1238 (defvar *toplevel-compilations* nil)
1240 (defun toplevel-compilation (string)
1241 (push string *toplevel-compilations*))
1243 (defun null-or-empty-p (x)
1246 (defun get-toplevel-compilations ()
1247 (reverse (remove-if #'null-or-empty-p *toplevel-compilations*)))
1249 (defun %compile-defmacro (name lambda)
1250 (toplevel-compilation (ls-compile `',name))
1251 (push-to-lexenv (make-binding name 'macro lambda) *environment* 'function)
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)
1260 (defun claimp (symbol namespace claim)
1261 (let ((b (lookup-in-lexenv symbol *environment* namespace)))
1262 (and b (member claim (binding-declarations b)))))
1264 (defun !proclaim (decl)
1267 (dolist (name (cdr decl))
1268 (let ((b (global-binding name 'variable 'variable)))
1269 (push-binding-declaration 'special b))))
1271 (dolist (name (cdr decl))
1272 (let ((b (global-binding name 'function 'function)))
1273 (push-binding-declaration 'notinline b))))
1275 (dolist (name (cdr decl))
1276 (let ((b (global-binding name 'variable 'variable)))
1277 (push-binding-declaration 'constant b))))))
1280 (fset 'proclaim #'!proclaim)
1284 (defvar *compilations* nil)
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)))
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*)
1299 (defvar *lambda-list-keywords* '(&optional &rest &key))
1301 (defun list-until-keyword (list)
1302 (if (or (null list) (member (car list) *lambda-list-keywords*))
1304 (cons (car list) (list-until-keyword (cdr list)))))
1306 (defun lambda-list-section (keyword lambda-list)
1307 (list-until-keyword (cdr (member keyword lambda-list))))
1309 (defun lambda-list-required-arguments (lambda-list)
1310 (list-until-keyword lambda-list))
1312 (defun lambda-list-optional-arguments-with-default (lambda-list)
1313 (mapcar #'ensure-list (lambda-list-section '&optional lambda-list)))
1315 (defun lambda-list-optional-arguments (lambda-list)
1316 (mapcar #'car (lambda-list-optional-arguments-with-default lambda-list)))
1318 (defun lambda-list-rest-argument (lambda-list)
1319 (let ((rest (lambda-list-section '&rest lambda-list)))
1321 (error "Bad lambda-list"))
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))
1333 (if (listp (car arg))
1334 (setq var (cadr (car arg))
1335 keyword-name (car (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))))
1341 (defun lambda-list-keyword-arguments (lambda-list)
1342 (mapcar (lambda (keyarg) (second (first keyarg)))
1343 (lambda-list-keyword-arguments-canonical lambda-list)))
1345 (defun lambda-docstring-wrapper (docstring &rest strs)
1348 "var func = " (join strs) ";" *newline*
1349 "func.docstring = '" docstring "';" *newline*
1350 "return func;" *newline*)
1351 (apply #'code strs)))
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))))
1360 ;; Special case: a positive exact number of arguments.
1361 (when (and (< 1 min) (eql min max))
1362 (return (code "checkArgs(arguments, " min ");" *newline*)))
1366 (code "checkArgsAtLeast(arguments, " min ");" *newline*))
1368 (code "checkArgsAtMost(arguments, " max ");" *newline*))))))
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))
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))
1386 (ls-compile (cadr arg))
1390 (push (code "default: break;" *newline*) cases)
1391 (join (reverse cases))))
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)))
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)
1404 (indent js!rest " = {car: arguments[i], cdr: ") js!rest "};"
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)))
1413 (lambda-list-keyword-arguments-canonical lambda-list)))
1415 ;; Declare variables
1416 (mapconcat (lambda (arg)
1417 (let ((var (second (car arg))))
1418 (code "var " (translate-variable var) "; " *newline*)))
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*
1426 "if (arguments[i] === " (ls-compile (caar keyarg)) "){" *newline*
1427 (indent (translate-variable (cadr (car keyarg)))
1428 " = arguments[i+1];"
1434 "if (i == arguments.length){" *newline*
1436 (translate-variable (cadr (car keyarg)))
1438 (ls-compile (cadr keyarg))
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*
1449 (join (mapcar (lambda (x)
1450 (concat "arguments[i] !== " (ls-compile (caar x))))
1455 "throw 'Unknown keyword argument ' + arguments[i].name;" *newline*))
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))
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)
1475 keyword-arguments))))
1476 (lambda-docstring-wrapper
1479 (join (cons "values"
1480 (mapcar #'translate-variable
1481 (append required-arguments optional-arguments)))
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)))
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)))))
1506 (define-compilation setq (&rest pairs)
1510 ((null pairs) (return))
1512 (error "Odd paris in SETQ"))
1515 (concat (setq-pair (car pairs) (cadr pairs))
1516 (if (null (cddr pairs)) "" ", ")))
1517 (setq pairs (cddr pairs)))))
1518 (code "(" result ")")))
1520 ;;; FFI Variable accessors
1521 (define-compilation js-vref (var)
1524 (define-compilation js-vset (var val)
1525 (code "(" var " = " (ls-compile val) ")"))
1529 (defun escape-string (string)
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 "\\"))
1540 (setq output (concat output (string ch))))
1545 (defvar *literal-symbols* nil)
1546 (defvar *literal-counter* 0)
1549 (code "l" (incf *literal-counter*)))
1551 (defun literal (sexp &optional recursive)
1553 ((integerp sexp) (integer-to-string sexp))
1554 ((stringp sexp) (code "\"" (escape-string sexp) "\""))
1556 (or (cdr (assoc sexp *literal-symbols*))
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)) "\"}")))
1565 (let ((package (symbol-package sexp)))
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))
1573 (let* ((head (butlast sexp))
1576 (join-trailing (mapcar (lambda (x) (literal x t)) head) ",")
1577 (literal (car tail) t)
1579 (literal (cdr tail) t)
1584 (toplevel-compilation (code "var " v " = " c))
1587 (let ((elements (vector-to-list sexp)))
1588 (let ((c (concat "[" (join (mapcar #'literal elements) ", ") "]")))
1592 (toplevel-compilation (code "var " v " = " c))
1595 (define-compilation quote (sexp)
1598 (define-compilation %while (pred &rest body)
1600 "while(" (ls-compile pred) " !== " (ls-compile nil) "){" *newline*
1601 (indent (ls-compile-block body))
1603 "return " (ls-compile nil) ";" *newline*))
1605 (define-compilation function (x)
1607 ((and (listp x) (eq (car x) 'lambda))
1608 (compile-lambda (cadr x) (cddr x)))
1610 (let ((b (lookup-in-lexenv x *environment* 'function)))
1613 (ls-compile `(symbol-function ',x)))))))
1616 (defun make-function-binding (fname)
1617 (make-binding fname 'function (gvarname fname)))
1619 (defun compile-function-definition (list)
1620 (compile-lambda (car list) (cdr list)))
1622 (defun translate-function (name)
1623 (let ((b (lookup-in-lexenv name *environment* 'function)))
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))
1631 (extend-lexenv (mapcar #'make-function-binding fnames)
1635 (join (mapcar #'translate-function fnames) ",")
1637 (let ((body (ls-compile-block body t)))
1639 "})(" (join cfuncs ",") ")")))
1641 (define-compilation labels (definitions &rest body)
1642 (let* ((fnames (mapcar #'car definitions))
1644 (extend-lexenv (mapcar #'make-function-binding fnames)
1648 (mapconcat (lambda (func)
1649 (code "var " (translate-function (car func))
1650 " = " (compile-lambda (cadr func) (cddr func))
1653 (ls-compile-block body t))))
1657 (defvar *compiling-file* nil)
1658 (define-compilation eval-when-compile (&rest body)
1659 (if *compiling-file*
1661 (eval (cons 'progn body))
1663 (ls-compile `(progn ,@body))))
1665 (defmacro define-transformation (name args form)
1666 `(define-compilation ,name ,args
1667 (ls-compile ,form)))
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))))
1674 (defun special-variable-p (x)
1675 (and (claimp x 'variable 'special) t))
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
1682 (defun let-binding-wrapper (bindings body)
1683 (when (null bindings)
1684 (return-from let-binding-wrapper body))
1687 (indent "var tmp;" *newline*
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*)))
1697 "finally {" *newline*
1699 (mapconcat (lambda (b)
1700 (let ((s (ls-compile `(quote ,(car b)))))
1701 (code s ".value" " = " (cdr b) ";" *newline*)))
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)))
1712 (join (mapcar (lambda (x)
1713 (if (special-variable-p x)
1714 (let ((v (gvarname x)))
1715 (push (cons x v) dynamic-bindings)
1717 (translate-variable x)))
1721 (let ((body (ls-compile-block body t)))
1722 (indent (let-binding-wrapper dynamic-bindings body)))
1723 "})(" (join cvalues ",") ")")))
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))))))
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))))
1749 (mapconcat (lambda (b)
1750 (let ((s (ls-compile `(quote ,(car b)))))
1751 (code "var " (cdr b) " = " s ".value;" *newline*)))
1755 "finally {" *newline*
1757 (mapconcat (lambda (b)
1758 (let ((s (ls-compile `(quote ,(car b)))))
1759 (code s ".value" " = " (cdr b) ";" *newline*)))
1763 (define-compilation let* (bindings &rest body)
1764 (let ((bindings (mapcar #'ensure-list bindings))
1765 (*environment* (copy-lexenv *environment*)))
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)))))
1773 (defvar *block-counter* 0)
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))
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;")
1794 " throw cf;" *newline*
1796 (js!selfcall cbody)))))
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))))
1802 (error (concat "Unknown block `" (symbol-name name) "'.")))
1803 (push-binding-declaration 'used b)
1805 (when multiple-value-p (code "var values = mv;" *newline*))
1808 "id: " (binding-value b) ", "
1809 "values: " (ls-compile value multiple-value-p) ", "
1810 "message: 'Return from unknown block " (symbol-name name) ".'"
1813 (define-compilation catch (id &rest body)
1815 "var id = " (ls-compile id) ";" *newline*
1817 (indent (ls-compile-block body t)) *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));")
1826 " throw cf;" *newline*
1829 (define-compilation throw (id value)
1831 "var values = mv;" *newline*
1834 "id: " (ls-compile id) ", "
1835 "values: " (ls-compile value t) ", "
1836 "message: 'Throw uncatched.'"
1840 (defvar *tagbody-counter* 0)
1841 (defvar *go-tag-counter* 0)
1844 (or (integerp x) (symbolp x)))
1846 (defun declare-tagbody-tags (tbidx body)
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)))
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))
1867 (let ((b (lookup-in-lexenv (first body) *environment* 'gotag)))
1868 (setq initag (second (binding-value b))))
1870 "var tagbody_" tbidx " = " initag ";" *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)
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*
1887 "catch (jump) {" *newline*
1888 " if (jump.type == 'tagbody' && jump.id == " tbidx ")" *newline*
1889 " tagbody_" tbidx " = jump.label;" *newline*
1891 " throw(jump);" *newline*
1894 "return " (ls-compile nil) ";" *newline*))))
1896 (define-compilation go (label)
1897 (let ((b (lookup-in-lexenv label *environment* 'gotag))
1899 ((symbolp label) (symbol-name label))
1900 ((integerp label) (integer-to-string label)))))
1902 (error (concat "Unknown tag `" n "'.")))
1906 "id: " (first (binding-value b)) ", "
1907 "label: " (second (binding-value b)) ", "
1908 "message: 'Attempt to GO to non-existing tag " n "'"
1911 (define-compilation unwind-protect (form &rest clean-up)
1913 "var ret = " (ls-compile nil) ";" *newline*
1915 (indent "ret = " (ls-compile form) ";" *newline*)
1916 "} finally {" *newline*
1917 (indent (ls-compile-block clean-up))
1919 "return ret;" *newline*))
1921 (define-compilation multiple-value-call (func-form &rest forms)
1923 "var func = " (ls-compile func-form) ";" *newline*
1924 "var args = [" (if *multiple-value-p* "values" "pv") "];" *newline*
1927 "var values = mv;" *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*)
1934 (indent "args.push(vs);" *newline*)))
1936 "return func.apply(window, args);" *newline*) ";" *newline*))
1938 (define-compilation multiple-value-prog1 (first-form &rest forms)
1940 "var args = " (ls-compile first-form *multiple-value-p*) ";" *newline*
1941 (ls-compile-block forms)
1942 "return args;" *newline*))
1946 ;;; A little backquote implementation without optimizations of any
1947 ;;; kind for ecmalisp.
1948 (defun backquote-expand-1 (form)
1954 ((eq (car form) 'unquote)
1956 ((eq (car form) 'backquote)
1957 (backquote-expand-1 (backquote-expand-1 (cadr form))))
1962 ((and (listp s) (eq (car s) 'unquote))
1963 (list 'list (cadr s)))
1964 ((and (listp s) (eq (car s) 'unquote-splicing))
1967 (list 'list (backquote-expand-1 s)))))
1970 (defun backquote-expand (form)
1971 (if (and (listp form) (eq (car form) 'backquote))
1972 (backquote-expand-1 (cadr form))
1975 (defmacro backquote (form)
1976 (backquote-expand-1 form))
1978 (define-transformation backquote (form)
1979 (backquote-expand-1 form))
1983 (defvar *builtins* nil)
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)))
1992 (defmacro define-builtin (name args &body body)
1994 (define-raw-builtin ,name ,args
1995 (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args)
1998 ;;; DECLS is a list of (JSVARNAME TYPE LISPFORM) declarations.
1999 (defmacro type-check (decls &body body)
2001 ,@(mapcar (lambda (decl)
2002 `(code "var " ,(first decl) " = " ,(third decl) ";" *newline*))
2004 ,@(mapcar (lambda (decl)
2005 `(code "if (typeof " ,(first decl) " != '" ,(second decl) "')" *newline*
2006 (indent "throw 'The value ' + "
2008 " + ' is not a type "
2013 (code "return " (progn ,@body) ";" *newline*)))
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.
2021 (defun variable-arity-call (args function)
2022 (unless (consp args)
2023 (error "ARGS must be a non-empty list"))
2029 (push (integer-to-string x) fargs)
2030 (let ((v (code "x" (incf counter))))
2033 (code "var " v " = " (ls-compile x) ";" *newline*
2034 "if (typeof " v " !== 'number') throw 'Not a number!';"
2036 (js!selfcall prelude (funcall function (reverse fargs)))))
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
2044 (code "return " ,@body ";" *newline*))))
2046 (defun num-op-num (x op y)
2047 (type-check (("x" "number" x) ("y" "number" y))
2050 (define-raw-builtin + (&rest numbers)
2053 (variable-arity numbers
2054 (join numbers "+"))))
2056 (define-raw-builtin - (x &rest others)
2057 (let ((args (cons x others)))
2058 (variable-arity args
2060 (concat "-" (car args))
2063 (define-raw-builtin * (&rest numbers)
2066 (variable-arity numbers
2067 (join numbers "*"))))
2069 (define-raw-builtin / (x &rest others)
2070 (let ((args (cons x others)))
2071 (variable-arity args
2073 (concat "1 /" (car args))
2076 (define-builtin mod (x y) (num-op-num x "%" y))
2079 (defun comparison-conjuntion (vars op)
2084 (concat (car vars) op (cadr vars)))
2086 (concat (car vars) op (cadr vars)
2088 (comparison-conjuntion (cdr vars) op)))))
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))))))
2096 (define-builtin-comparison > ">")
2097 (define-builtin-comparison < "<")
2098 (define-builtin-comparison >= ">=")
2099 (define-builtin-comparison <= "<=")
2100 (define-builtin-comparison = "==")
2102 (define-builtin numberp (x)
2103 (js!bool (code "(typeof (" x ") == \"number\")")))
2105 (define-builtin floor (x)
2106 (type-check (("x" "number" x))
2109 (define-builtin cons (x y)
2110 (code "({car: " x ", cdr: " y "})"))
2112 (define-builtin consp (x)
2115 "var tmp = " x ";" *newline*
2116 "return (typeof tmp == 'object' && 'car' in tmp);" *newline*)))
2118 (define-builtin car (x)
2120 "var tmp = " x ";" *newline*
2121 "return tmp === " (ls-compile nil)
2122 "? " (ls-compile nil)
2123 ": tmp.car;" *newline*))
2125 (define-builtin cdr (x)
2127 "var tmp = " x ";" *newline*
2128 "return tmp === " (ls-compile nil) "? "
2130 ": tmp.cdr;" *newline*))
2132 (define-builtin rplaca (x new)
2133 (type-check (("x" "object" x))
2134 (code "(x.car = " new ", x)")))
2136 (define-builtin rplacd (x new)
2137 (type-check (("x" "object" x))
2138 (code "(x.cdr = " new ", x)")))
2140 (define-builtin symbolp (x)
2143 "var tmp = " x ";" *newline*
2144 "return (typeof tmp == 'object' && 'name' in tmp);" *newline*)))
2146 (define-builtin make-symbol (name)
2147 (type-check (("name" "string" name))
2150 (define-builtin symbol-name (x)
2151 (code "(" x ").name"))
2153 (define-builtin set (symbol value)
2154 (code "(" symbol ").value = " value))
2156 (define-builtin fset (symbol value)
2157 (code "(" symbol ").fvalue = " value))
2159 (define-builtin boundp (x)
2160 (js!bool (code "(" x ".value !== undefined)")))
2162 (define-builtin symbol-value (x)
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*))
2169 (define-builtin symbol-function (x)
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*))
2176 (define-builtin symbol-plist (x)
2177 (code "((" x ").plist || " (ls-compile nil) ")"))
2179 (define-builtin lambda-code (x)
2180 (code "(" x ").toString()"))
2182 (define-builtin eq (x y) (js!bool (code "(" x " === " y ")")))
2183 (define-builtin equal (x y) (js!bool (code "(" x " == " y ")")))
2185 (define-builtin char-to-string (x)
2186 (type-check (("x" "number" x))
2187 "String.fromCharCode(x)"))
2189 (define-builtin stringp (x)
2190 (js!bool (code "(typeof(" x ") == \"string\")")))
2192 (define-builtin string-upcase (x)
2193 (type-check (("x" "string" x))
2196 (define-builtin string-length (x)
2197 (type-check (("x" "string" x))
2200 (define-raw-builtin slice (string a &optional b)
2202 "var str = " (ls-compile string) ";" *newline*
2203 "var a = " (ls-compile a) ";" *newline*
2205 (when b (code "b = " (ls-compile b) ";" *newline*))
2206 "return str.slice(a,b);" *newline*))
2208 (define-builtin char (string index)
2209 (type-check (("string" "string" string)
2210 ("index" "number" index))
2211 "string.charCodeAt(index)"))
2213 (define-builtin concat-two (string1 string2)
2214 (type-check (("string1" "string" string1)
2215 ("string2" "string" string2))
2216 "string1.concat(string2)"))
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))
2225 (define-raw-builtin apply (func &rest args)
2227 (code "(" (ls-compile func) ")()")
2228 (let ((args (butlast args))
2229 (last (car (last args))))
2231 "var f = " (ls-compile func) ";" *newline*
2232 "var args = [" (join (cons (if *multiple-value-p* "values" "pv")
2233 (mapcar #'ls-compile args))
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*
2241 "return f.apply(this, args);" *newline*))))
2243 (define-builtin js-eval (string)
2244 (type-check (("string" "string" string))
2245 (if *multiple-value-p*
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*)
2252 "return values.apply(this, v);" *newline*)
2253 "eval.apply(window, [string])")))
2255 (define-builtin error (string)
2256 (js!selfcall "throw " string ";" *newline*))
2258 (define-builtin new () "{}")
2260 (define-builtin objectp (x)
2261 (js!bool (code "(typeof (" x ") === 'object')")))
2263 (define-builtin oget (object key)
2265 "var tmp = " "(" object ")[" key "];" *newline*
2266 "return tmp == undefined? " (ls-compile nil) ": tmp ;" *newline*))
2268 (define-builtin oset (object key value)
2269 (code "((" object ")[" key "] = " value ")"))
2271 (define-builtin in (key object)
2272 (js!bool (code "((" key ") in (" object "))")))
2274 (define-builtin functionp (x)
2275 (js!bool (code "(typeof " x " == 'function')")))
2277 (define-builtin write-string (x)
2278 (type-check (("x" "string" x))
2281 (define-builtin make-array (n)
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*))
2288 (define-builtin arrayp (x)
2291 "var x = " x ";" *newline*
2292 "return typeof x === 'object' && 'length' in x;")))
2294 (define-builtin aref (array n)
2296 "var x = " "(" array ")[" n "];" *newline*
2297 "if (x === undefined) throw 'Out of range';" *newline*
2298 "return x;" *newline*))
2300 (define-builtin aset (array n value)
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*))
2307 (define-builtin get-unix-time ()
2308 (code "(Math.round(new Date() / 1000))"))
2310 (define-builtin values-array (array)
2311 (if *multiple-value-p*
2312 (code "values.apply(this, " array ")")
2313 (code "pv.apply(this, " array ")")))
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) ", ") ")")))
2322 (let ((b (lookup-in-lexenv x *environment* 'function)))
2323 (and (eq (binding-type b) 'macro)
2326 (defun ls-macroexpand-1 (form)
2327 (let ((macro-binding (macro (car form))))
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.
2339 #+ecmalisp (set-binding-value macro-binding compiled)
2340 (setq expander compiled)))
2341 (apply expander (cdr form)))
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)) ", ") ")")))
2348 ((translate-function function)
2349 (concat (translate-function function) arglist))
2350 ((and (symbolp function)
2351 #+ecmalisp (eq (symbol-package function) (find-package "COMMON-LISP"))
2353 (code (ls-compile `',function) ".fvalue" arglist))
2355 (code (ls-compile `#',function) arglist)))))
2357 (defun ls-compile-block (sexps &optional return-last-p)
2359 (code (ls-compile-block (butlast sexps))
2360 "return " (ls-compile (car (last sexps)) *multiple-value-p*) ";")
2362 (remove-if #'null-or-empty-p (mapcar #'ls-compile sexps))
2363 (concat ";" *newline*))))
2365 (defun ls-compile (sexp &optional multiple-value-p)
2366 (let ((*multiple-value-p* multiple-value-p))
2369 (let ((b (lookup-in-lexenv sexp *environment* 'variable)))
2371 ((and b (not (member 'special (binding-declarations b))))
2373 ((or (keywordp sexp)
2374 (member 'constant (binding-declarations b)))
2375 (code (ls-compile `',sexp) ".value"))
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))
2382 (let ((name (car sexp))
2386 ((assoc name *compilations*)
2387 (let ((comp (second (assoc name *compilations*))))
2389 ;; Built-in functions
2390 ((and (assoc name *builtins*)
2391 (not (claimp name 'function 'notinline)))
2392 (let ((comp (second (assoc name *builtins*))))
2396 (ls-compile (ls-macroexpand-1 sexp) multiple-value-p)
2397 (compile-funcall name args))))))
2399 (error "How should I compile this?")))))
2401 (defun ls-compile-toplevel (sexp &optional multiple-value-p)
2402 (let ((*toplevel-compilations* nil))
2404 ((and (consp sexp) (eq (car sexp) 'progn))
2405 (let ((subs (mapcar (lambda (s)
2406 (ls-compile-toplevel s t))
2408 (join (remove-if #'null-or-empty-p subs))))
2410 (let ((code (ls-compile sexp multiple-value-p)))
2411 (code (join-trailing (get-toplevel-compilations)
2412 (code ";" *newline*))
2414 (code code ";" *newline*))))))))
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.
2424 (js-eval (ls-compile-toplevel x t)))
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))
2452 (setq *package* *user-package*)
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)))
2463 ;; Set the initial global environment to be equal to the host global
2464 ;; environment at this point of the compilation.
2466 (toplevel-compilation
2467 (ls-compile `(setq *environment* ',*environment*))))
2470 (toplevel-compilation
2473 ,@(mapcar (lambda (s) `(%intern-symbol (js-vref ,(cdr s))))
2475 (setq *literal-symbols* ',*literal-symbols*)
2476 (setq *variable-counter* ,*variable-counter*)
2477 (setq *gensym-counter* ,*gensym-counter*)
2478 (setq *block-counter* ,*block-counter*)))))
2481 (toplevel-compilation
2483 `(setq *literal-counter* ,*literal-counter*)))))
2486 ;;; Finally, we provide a couple of functions to easily bootstrap
2487 ;;; this. It just calls the compiler with this file as input.
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)
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)))
2504 for x = (ls-read in)
2506 for compilation = (ls-compile-toplevel x)
2507 when (plusp (length compilation))
2508 do (write-string compilation out))))))
2511 (setq *environment* (make-lexenv))
2512 (setq *literal-symbols* nil)
2513 (setq *variable-counter* 0
2517 (ls-compile-file "ecmalisp.lisp" "ecmalisp.js")))