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
29 '(lambda (name args &rest body)
31 (%compile-defmacro ',name
32 '(lambda ,(mapcar (lambda (x)
42 (defmacro when (condition &body body)
43 `(if ,condition (progn ,@body) nil))
45 (defmacro unless (condition &body body)
46 `(if ,condition nil (progn ,@body)))
48 (defmacro defvar (name value &optional docstring)
50 (unless (boundp ',name)
52 ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
55 (defmacro defparameter (name value &optional docstring)
58 ,@(when (stringp docstring) `((oset ',name "vardoc" ,docstring)))
61 (defmacro named-lambda (name args &rest body)
62 (let ((x (gensym "FN")))
63 `(let ((,x (lambda ,args ,@body)))
64 (oset ,x "fname" ,name)
67 (defmacro defun (name args &rest body)
70 (named-lambda ,(symbol-name name) ,args
71 ,@(when (stringp (car body)) `(,(car body)))
73 ,@(if (stringp (car body))
78 (defvar *package* (new))
83 (defmacro return (&optional value)
84 `(return-from nil ,value))
86 (defmacro while (condition &body body)
87 `(block nil (%while ,condition ,@body)))
95 (oset *package* name (make-symbol name))))
97 (defun find-symbol (name)
98 (oget *package* name))
100 (defvar *gensym-counter* 0)
101 (defun gensym (&optional (prefix "G"))
102 (setq *gensym-counter* (+ *gensym-counter* 1))
103 (make-symbol (concat-two prefix (integer-to-string *gensym-counter*))))
109 (defun = (x y) (= x y))
110 (defun + (x y) (+ x y))
111 (defun - (x y) (- x y))
112 (defun * (x y) (* x y))
113 (defun / (x y) (/ x y))
114 (defun 1+ (x) (+ x 1))
115 (defun 1- (x) (- x 1))
116 (defun zerop (x) (= x 0))
117 (defun truncate (x y) (floor (/ x y)))
119 (defun eql (x y) (eq x y))
121 (defun not (x) (if x nil t))
123 (defun cons (x y ) (cons x y))
124 (defun consp (x) (consp x))
127 "Return the CAR part of a cons, or NIL if X is null."
130 (defun cdr (x) (cdr x))
131 (defun caar (x) (car (car x)))
132 (defun cadr (x) (car (cdr x)))
133 (defun cdar (x) (cdr (car x)))
134 (defun cddr (x) (cdr (cdr x)))
135 (defun caddr (x) (car (cdr (cdr x))))
136 (defun cdddr (x) (cdr (cdr (cdr x))))
137 (defun cadddr (x) (car (cdr (cdr (cdr x)))))
138 (defun first (x) (car x))
139 (defun second (x) (cadr x))
140 (defun third (x) (caddr x))
141 (defun fourth (x) (cadddr x))
143 (defun list (&rest args) args)
149 (defmacro incf (x &optional (delta 1))
150 `(setq ,x (+ ,x ,delta)))
152 (defmacro decf (x &optional (delta 1))
153 `(setq ,x (- ,x ,delta)))
155 (defmacro push (x place)
156 `(setq ,place (cons ,x ,place)))
158 (defmacro dolist (iter &body body)
159 (let ((var (first iter))
162 (let ((,g!list ,(second iter))
165 (setq ,var (car ,g!list))
167 (setq ,g!list (cdr ,g!list)))
170 (defmacro dotimes (iter &body body)
171 (let ((g!to (gensym))
174 (result (third iter)))
178 (%while (< ,var ,g!to)
183 (defmacro cond (&rest clausules)
186 (if (eq (caar clausules) t)
187 `(progn ,@(cdar clausules))
188 `(if ,(caar clausules)
189 (progn ,@(cdar clausules))
190 (cond ,@(cdr clausules))))))
192 (defmacro case (form &rest clausules)
193 (let ((!form (gensym)))
194 `(let ((,!form ,form))
196 ,@(mapcar (lambda (clausule)
197 (if (eq (car clausule) t)
199 `((eql ,!form ',(car clausule))
203 (defmacro ecase (form &rest clausules)
208 (error "ECASE expression failed."))))))
210 (defmacro and (&rest forms)
221 (defmacro or (&rest forms)
229 `(let ((,g ,(car forms)))
230 (if ,g ,g (or ,@(cdr forms))))))))
232 (defmacro prog1 (form &body body)
233 (let ((value (gensym)))
234 `(let ((,value ,form))
238 (defmacro prog2 (form1 result &body body)
239 `(prog1 (progn ,form1 ,result) ,@body)))
243 ;;; This couple of helper functions will be defined in both Common
244 ;;; Lisp and in Ecmalisp.
245 (defun ensure-list (x)
250 (defun !reduce (func list initial)
255 (funcall func initial (car list)))))
257 ;;; Go on growing the Lisp language in Ecmalisp, with more high
258 ;;; level utilities as well as correct versions of other
262 (defun append-two (list1 list2)
266 (append (cdr list1) list2))))
268 (defun append (&rest lists)
269 (!reduce #'append-two lists '()))
271 (defun revappend (list1 list2)
273 (push (car list1) list2)
274 (setq list1 (cdr list1)))
277 (defun reverse (list)
278 (revappend list '()))
280 (defun list-length (list)
282 (while (not (null list))
284 (setq list (cdr list)))
292 (defun concat-two (s1 s2)
295 (defun mapcar (func list)
298 (cons (funcall func (car list))
299 (mapcar func (cdr list)))))
301 (defun identity (x) x)
304 (mapcar #'identity x))
306 (defun code-char (x) x)
307 (defun char-code (x) x)
308 (defun char= (x y) (= x y))
311 (and (numberp x) (= (floor x) x)))
313 (defun plusp (x) (< 0 x))
314 (defun minusp (x) (< x 0))
317 (or (consp x) (null x)))
319 (defun nthcdr (n list)
320 (while (and (plusp n) list)
322 (setq list (cdr list)))
326 (car (nthcdr n list)))
329 (while (consp (cdr x))
335 (cons (car x) (butlast (cdr x)))))
337 (defun member (x list)
339 (when (eql x (car list))
341 (setq list (cdr list))))
343 (defun remove (x list)
348 (remove x (cdr list)))
350 (cons (car list) (remove x (cdr list))))))
352 (defun remove-if (func list)
356 ((funcall func (car list))
357 (remove-if func (cdr list)))
359 (cons (car list) (remove-if func (cdr list))))))
361 (defun remove-if-not (func list)
365 ((funcall func (car list))
366 (cons (car list) (remove-if-not func (cdr list))))
368 (remove-if-not func (cdr list)))))
370 (defun digit-char-p (x)
371 (if (and (<= #\0 x) (<= x #\9))
375 (defun subseq (seq a &optional b)
382 (error "Unsupported argument."))))
384 (defun parse-integer (string)
387 (size (length string)))
388 (while (< index size)
389 (setq value (+ (* value 10) (digit-char-p (char string index))))
393 (defun some (function seq)
398 (while (< index size)
399 (when (funcall function (char seq index))
400 (return-from some t))
405 (when (funcall function x)
408 (error "Unknown sequence."))))
410 (defun every (function seq)
415 (while (< index size)
416 (unless (funcall function (char seq index))
417 (return-from every nil))
422 (unless (funcall function x)
425 (error "Unknown sequence."))))
427 (defun assoc (x alist)
429 (if (eql x (caar alist))
431 (setq alist (cdr alist))))
434 (defun string= (s1 s2)
437 (defun fdefinition (x)
444 (error "Invalid function"))))
446 (defun disassemble (function)
447 (write-line (lambda-code (fdefinition function)))
450 (defun documentation (x type)
451 "Return the documentation of X. TYPE must be the symbol VARIABLE or FUNCTION."
454 (let ((func (fdefinition x)))
455 (oget func "docstring")))
458 (error "Wrong argument type! it should be a symbol"))
462 ;;; The compiler offers some primitives and special forms which are
463 ;;; not found in Common Lisp, for instance, while. So, we grow Common
464 ;;; Lisp a bit to it can execute the rest of the file.
467 (defmacro while (condition &body body)
472 (defmacro eval-when-compile (&body body)
473 `(eval-when (:compile-toplevel :load-toplevel :execute)
476 (defun concat-two (s1 s2)
477 (concatenate 'string s1 s2))
479 (defun setcar (cons new)
480 (setf (car cons) new))
481 (defun setcdr (cons new)
482 (setf (cdr cons) new)))
484 ;;; At this point, no matter if Common Lisp or ecmalisp is compiling
485 ;;; from here, this code will compile on both. We define some helper
486 ;;; functions now for string manipulation and so on. They will be
487 ;;; useful in the compiler, mostly.
489 (defvar *newline* (string (code-char 10)))
491 (defun concat (&rest strs)
492 (!reduce #'concat-two strs ""))
494 (defmacro concatf (variable &body form)
495 `(setq ,variable (concat ,variable (progn ,@form))))
497 ;;; Concatenate a list of strings, with a separator
498 (defun join (list &optional (separator ""))
507 (join (cdr list) separator)))))
509 (defun join-trailing (list &optional (separator ""))
512 (concat (car list) separator (join-trailing (cdr list) separator))))
514 (defun mapconcat (func list)
515 (join (mapcar func list)))
517 ;;; Like CONCAT, but prefix each line with four spaces. Two versions
518 ;;; of this function are available, because the Ecmalisp version is
519 ;;; very slow and bootstraping was annoying.
522 (defun indent (&rest string)
523 (let ((input (join string)))
526 (size (length input)))
527 (when (plusp (length input)) (concatf output " "))
528 (while (< index size)
530 (if (and (char= (char input index) #\newline)
532 (not (char= (char input (1+ index)) #\newline)))
533 (concat (string #\newline) " ")
534 (string (char input index)))))
535 (concatf output str))
540 (defun indent (&rest string)
541 (with-output-to-string (*standard-output*)
542 (with-input-from-string (input (join string))
544 for line = (read-line input nil)
546 do (write-string " ")
547 do (write-line line)))))
550 (defun integer-to-string (x)
555 (concat "-" (integer-to-string (- 0 x))))
558 (while (not (zerop x))
559 (push (mod x 10) digits)
560 (setq x (truncate x 10)))
561 (join (mapcar (lambda (d) (string (char "0123456789" d)))
565 ;;; Wrap X with a Javascript code to convert the result from
566 ;;; Javascript generalized booleans to T or NIL.
568 (concat "(" x "?" (ls-compile t) ": " (ls-compile nil) ")"))
570 ;;; Concatenate the arguments and wrap them with a self-calling
571 ;;; Javascript anonymous function. It is used to make some Javascript
572 ;;; statements valid expressions and provide a private scope as well.
573 ;;; It could be defined as function, but we could do some
574 ;;; preprocessing in the future.
575 (defmacro js!selfcall (&body body)
576 `(concat "(function(){" *newline* (indent ,@body) "})()"))
583 (defun prin1-to-string (form)
585 ((symbolp form) (symbol-name form))
586 ((integerp form) (integer-to-string form))
587 ((stringp form) (concat "\"" (escape-string form) "\""))
589 (let ((name (oget form "fname")))
591 (concat "#<FUNCTION " name ">")
592 (concat "#<FUNCTION>"))))
595 (join-trailing (mapcar #'prin1-to-string (butlast form)) " ")
596 (let ((last (last form)))
597 (if (null (cdr last))
598 (prin1-to-string (car last))
599 (concat (prin1-to-string (car last)) " . " (prin1-to-string (cdr last)))))
602 (defun write-line (x)
604 (write-string *newline*)
608 (write-string "WARNING: ")
612 (write-line (prin1-to-string x))
618 ;;; The Lisp reader, parse strings and return Lisp objects. The main
619 ;;; entry points are `ls-read' and `ls-read-from-string'.
621 (defun make-string-stream (string)
624 (defun %peek-char (stream)
625 (and (< (cdr stream) (length (car stream)))
626 (char (car stream) (cdr stream))))
628 (defun %read-char (stream)
629 (and (< (cdr stream) (length (car stream)))
630 (prog1 (char (car stream) (cdr stream))
631 (setcdr stream (1+ (cdr stream))))))
633 (defun whitespacep (ch)
634 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
636 (defun skip-whitespaces (stream)
638 (setq ch (%peek-char stream))
639 (while (and ch (whitespacep ch))
641 (setq ch (%peek-char stream)))))
643 (defun terminalp (ch)
644 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
646 (defun read-until (stream func)
649 (setq ch (%peek-char stream))
650 (while (and ch (not (funcall func ch)))
651 (setq string (concat string (string ch)))
653 (setq ch (%peek-char stream)))
656 (defun skip-whitespaces-and-comments (stream)
658 (skip-whitespaces stream)
659 (setq ch (%peek-char stream))
660 (while (and ch (char= ch #\;))
661 (read-until stream (lambda (x) (char= x #\newline)))
662 (skip-whitespaces stream)
663 (setq ch (%peek-char stream)))))
665 (defun %read-list (stream)
666 (skip-whitespaces-and-comments stream)
667 (let ((ch (%peek-char stream)))
670 (error "Unspected EOF"))
676 (prog1 (ls-read stream)
677 (skip-whitespaces-and-comments stream)
678 (unless (char= (%read-char stream) #\))
679 (error "')' was expected."))))
681 (cons (ls-read stream) (%read-list stream))))))
683 (defun read-string (stream)
686 (setq ch (%read-char stream))
687 (while (not (eql ch #\"))
689 (error "Unexpected EOF"))
691 (setq ch (%read-char stream)))
692 (setq string (concat string (string ch)))
693 (setq ch (%read-char stream)))
696 (defun read-sharp (stream)
698 (ecase (%read-char stream)
700 (list 'function (ls-read stream)))
703 (concat (string (%read-char stream))
704 (read-until stream #'terminalp))))
706 ((string= cname "space") (char-code #\space))
707 ((string= cname "tab") (char-code #\tab))
708 ((string= cname "newline") (char-code #\newline))
709 (t (char-code (char cname 0))))))
711 (let ((feature (read-until stream #'terminalp)))
713 ((string= feature "common-lisp")
714 (ls-read stream) ;ignore
716 ((string= feature "ecmalisp")
719 (error "Unknown reader form.")))))))
721 (defvar *eof* (make-symbol "EOF"))
722 (defun ls-read (stream)
723 (skip-whitespaces-and-comments stream)
724 (let ((ch (%peek-char stream)))
733 (list 'quote (ls-read stream)))
736 (list 'backquote (ls-read stream)))
739 (read-string stream))
742 (if (eql (%peek-char stream) #\@)
743 (progn (%read-char stream) (list 'unquote-splicing (ls-read stream)))
744 (list 'unquote (ls-read stream))))
748 (let ((string (read-until stream #'terminalp)))
749 (if (every #'digit-char-p string)
750 (parse-integer string)
751 (intern (string-upcase string))))))))
753 (defun ls-read-from-string (string)
754 (ls-read (make-string-stream string)))
759 ;;; Translate the Lisp code to Javascript. It will compile the special
760 ;;; forms. Some primitive functions are compiled as special forms
761 ;;; too. The respective real functions are defined in the target (see
762 ;;; the beginning of this file) as well as some primitive functions.
764 (defvar *compilation-unit-checks* '())
766 (defun make-binding (name type translation declared)
767 (list name type translation declared))
769 (defun binding-name (b) (first b))
770 (defun binding-type (b) (second b))
771 (defun binding-value (b) (third b))
772 (defun set-binding-value (b value)
773 (setcar (cdr (cdr b)) value))
775 (defun binding-declared (b)
777 (defun mark-binding-as-declared (b)
778 (setcar (cdddr b) t))
780 (defun make-lexenv ()
781 (list nil nil nil nil))
783 (defun copy-lexenv (lexenv)
786 (defun push-to-lexenv (binding lexenv namespace)
788 (variable (setcar lexenv (cons binding (car lexenv))))
789 (function (setcar (cdr lexenv) (cons binding (cadr lexenv))))
790 (block (setcar (cddr lexenv) (cons binding (caddr lexenv))))
791 (gotag (setcar (cdddr lexenv) (cons binding (cadddr lexenv))))))
793 (defun extend-lexenv (bindings lexenv namespace)
794 (let ((env (copy-lexenv lexenv)))
795 (dolist (binding (reverse bindings) env)
796 (push-to-lexenv binding env namespace))))
798 (defun lookup-in-lexenv (name lexenv namespace)
799 (assoc name (ecase namespace
800 (variable (first lexenv))
801 (function (second lexenv))
802 (block (third lexenv))
803 (gotag (fourth lexenv)))))
805 (defvar *global-environment* (make-lexenv))
806 (defvar *environment* (make-lexenv))
808 (defun clear-undeclared-global-bindings ()
810 (mapcar (lambda (namespace)
811 (remove-if-not #'binding-declared namespace))
815 (defvar *variable-counter* 0)
816 (defun gvarname (symbol)
817 (concat "v" (integer-to-string (incf *variable-counter*))))
819 (defun translate-variable (symbol)
820 (binding-value (lookup-in-lexenv symbol *environment* 'variable)))
822 (defun extend-local-env (args)
823 (let ((new (copy-lexenv *environment*)))
824 (dolist (symbol args new)
825 (let ((b (make-binding symbol 'lexical-variable (gvarname symbol) t)))
826 (push-to-lexenv b new 'variable)))))
828 ;;; Toplevel compilations
829 (defvar *toplevel-compilations* nil)
831 (defun toplevel-compilation (string)
832 (push string *toplevel-compilations*))
834 (defun null-or-empty-p (x)
837 (defun get-toplevel-compilations ()
838 (reverse (remove-if #'null-or-empty-p *toplevel-compilations*)))
840 (defun %compile-defmacro (name lambda)
841 (toplevel-compilation (ls-compile `',name))
842 (push-to-lexenv (make-binding name 'macro lambda t) *environment* 'function))
844 (defvar *compilations* nil)
846 (defun ls-compile-block (sexps &optional return-last-p)
848 (concat (ls-compile-block (butlast sexps))
849 "return " (ls-compile (car (last sexps))) ";")
851 (remove-if #'null-or-empty-p (mapcar #'ls-compile sexps))
852 (concat ";" *newline*))))
854 (defmacro define-compilation (name args &body body)
855 ;; Creates a new primitive `name' with parameters args and
856 ;; @body. The body can access to the local environment through the
857 ;; variable *ENVIRONMENT*.
858 `(push (list ',name (lambda ,args (block ,name ,@body)))
861 (define-compilation if (condition true false)
862 (concat "(" (ls-compile condition) " !== " (ls-compile nil)
863 " ? " (ls-compile true)
864 " : " (ls-compile false)
867 (defvar *lambda-list-keywords* '(&optional &rest))
869 (defun list-until-keyword (list)
870 (if (or (null list) (member (car list) *lambda-list-keywords*))
872 (cons (car list) (list-until-keyword (cdr list)))))
874 (defun lambda-list-required-arguments (lambda-list)
875 (list-until-keyword lambda-list))
877 (defun lambda-list-optional-arguments-with-default (lambda-list)
878 (mapcar #'ensure-list (list-until-keyword (cdr (member '&optional lambda-list)))))
880 (defun lambda-list-optional-arguments (lambda-list)
881 (mapcar #'car (lambda-list-optional-arguments-with-default lambda-list)))
883 (defun lambda-list-rest-argument (lambda-list)
884 (let ((rest (list-until-keyword (cdr (member '&rest lambda-list)))))
886 (error "Bad lambda-list"))
890 (defun lambda-docstring-wrapper (docstring &rest strs)
893 "var func = " (join strs) ";" *newline*
894 "func.docstring = '" docstring "';" *newline*
895 "return func;" *newline*)
898 (define-compilation lambda (lambda-list &rest body)
899 (let ((required-arguments (lambda-list-required-arguments lambda-list))
900 (optional-arguments (lambda-list-optional-arguments lambda-list))
901 (rest-argument (lambda-list-rest-argument lambda-list))
903 ;; Get the documentation string for the lambda function
904 (when (and (stringp (car body))
905 (not (null (cdr body))))
906 (setq documentation (car body))
907 (setq body (cdr body)))
908 (let ((n-required-arguments (length required-arguments))
909 (n-optional-arguments (length optional-arguments))
910 (*environment* (extend-local-env
911 (append (ensure-list rest-argument)
913 optional-arguments))))
914 (lambda-docstring-wrapper
917 (join (mapcar #'translate-variable
918 (append required-arguments optional-arguments))
921 ;; Check number of arguments
923 (if required-arguments
924 (concat "if (arguments.length < " (integer-to-string n-required-arguments)
925 ") throw 'too few arguments';" *newline*)
927 (if (not rest-argument)
928 (concat "if (arguments.length > "
929 (integer-to-string (+ n-required-arguments n-optional-arguments))
930 ") throw 'too many arguments';" *newline*)
932 ;; Optional arguments
933 (if optional-arguments
934 (concat "switch(arguments.length){" *newline*
935 (let ((optional-and-defaults
936 (lambda-list-optional-arguments-with-default lambda-list))
940 (while (< idx n-optional-arguments)
941 (let ((arg (nth idx optional-and-defaults)))
942 (push (concat "case "
943 (integer-to-string (+ idx n-required-arguments)) ":" *newline*
944 (translate-variable (car arg))
946 (ls-compile (cadr arg))
950 (push (concat "default: break;" *newline*) cases)
951 (join (reverse cases))))
954 ;; &rest/&body argument
956 (let ((js!rest (translate-variable rest-argument)))
957 (concat "var " js!rest "= " (ls-compile nil) ";" *newline*
958 "for (var i = arguments.length-1; i>="
959 (integer-to-string (+ n-required-arguments n-optional-arguments))
961 (indent js!rest " = "
962 "{car: arguments[i], cdr: ") js!rest "};"
966 (ls-compile-block body t)) *newline*
969 (define-compilation setq (var val)
970 (let ((b (lookup-in-lexenv var *environment* 'variable)))
971 (if (eq (binding-type b) 'lexical-variable)
972 (concat (binding-value b) " = " (ls-compile val))
973 (ls-compile `(set ',var ,val)))))
975 ;;; FFI Variable accessors
976 (define-compilation js-vref (var)
979 (define-compilation js-vset (var val)
980 (concat "(" var " = " (ls-compile val) ")"))
984 (defun escape-string (string)
987 (size (length string)))
988 (while (< index size)
989 (let ((ch (char string index)))
990 (when (or (char= ch #\") (char= ch #\\))
991 (setq output (concat output "\\")))
992 (when (or (char= ch #\newline))
993 (setq output (concat output "\\"))
995 (setq output (concat output (string ch))))
1000 (defvar *literal-symbols* nil)
1001 (defvar *literal-counter* 0)
1004 (concat "l" (integer-to-string (incf *literal-counter*))))
1006 (defun literal (sexp &optional recursive)
1008 ((integerp sexp) (integer-to-string sexp))
1009 ((stringp sexp) (concat "\"" (escape-string sexp) "\""))
1011 (or (cdr (assoc sexp *literal-symbols*))
1013 (s #+common-lisp (concat "{name: \"" (escape-string (symbol-name sexp)) "\"}")
1014 #+ecmalisp (ls-compile `(intern ,(symbol-name sexp)))))
1015 (push (cons sexp v) *literal-symbols*)
1016 (toplevel-compilation (concat "var " v " = " s))
1019 (let ((c (concat "{car: " (literal (car sexp) t) ", "
1020 "cdr: " (literal (cdr sexp) t) "}")))
1024 (toplevel-compilation (concat "var " v " = " c))
1027 (define-compilation quote (sexp)
1030 (define-compilation %while (pred &rest body)
1032 "while(" (ls-compile pred) " !== " (ls-compile nil) "){" *newline*
1033 (indent (ls-compile-block body))
1035 "return " (ls-compile nil) ";" *newline*))
1037 (define-compilation function (x)
1039 ((and (listp x) (eq (car x) 'lambda))
1042 (ls-compile `(symbol-function ',x)))))
1044 (define-compilation eval-when-compile (&rest body)
1045 (eval (cons 'progn body))
1048 (defmacro define-transformation (name args form)
1049 `(define-compilation ,name ,args
1050 (ls-compile ,form)))
1052 (define-compilation progn (&rest body)
1053 (js!selfcall (ls-compile-block body t)))
1055 (defun dynamic-binding-wrapper (bindings body)
1061 "var tmp;" *newline*
1064 (let ((s (ls-compile `(quote ,(car b)))))
1065 (concat "tmp = " s ".value;" *newline*
1066 s ".value = " (cdr b) ";" *newline*
1067 (cdr b) " = tmp;" *newline*)))
1071 "finally {" *newline*
1075 (let ((s (ls-compile `(quote ,(car b)))))
1076 (concat s ".value" " = " (cdr b))))
1078 (concat ";" *newline*)))
1082 (define-compilation let (bindings &rest body)
1083 (let ((bindings (mapcar #'ensure-list bindings)))
1084 (let ((variables (mapcar #'first bindings))
1085 (values (mapcar #'second bindings)))
1086 (let ((cvalues (mapcar #'ls-compile values))
1087 (*environment* (extend-local-env (remove-if #'boundp variables)))
1089 (concat "(function("
1090 (join (mapcar (lambda (x)
1092 (let ((v (gvarname x)))
1093 (push (cons x v) dynamic-bindings)
1095 (translate-variable x)))
1099 (let ((body (ls-compile-block body t)))
1100 (indent (dynamic-binding-wrapper dynamic-bindings body)))
1101 "})(" (join cvalues ",") ")")))))
1104 (defvar *block-counter* 0)
1106 (define-compilation block (name &rest body)
1107 (let ((tr (integer-to-string (incf *block-counter*))))
1108 (let ((b (make-binding name 'block tr t)))
1111 (let ((*environment* (extend-lexenv (list b) *environment* 'block)))
1112 (indent "return " (ls-compile `(progn ,@body)) ";" *newline*))
1114 "catch (cf){" *newline*
1115 " if (cf.type == 'block' && cf.id == " tr ")" *newline*
1116 " return cf.value;" *newline*
1118 " throw cf;" *newline*
1121 (define-compilation return-from (name &optional value)
1122 (let ((b (lookup-in-lexenv name *environment* 'block)))
1127 "id: " (binding-value b) ", "
1128 "value: " (ls-compile value) ", "
1129 "message: 'Return from unknown block " (symbol-name name) ".'"
1131 (error (concat "Unknown block `" (symbol-name name) "'.")))))
1134 (define-compilation catch (id &rest body)
1136 "var id = " (ls-compile id) ";" *newline*
1138 (indent "return " (ls-compile `(progn ,@body))
1141 "catch (cf){" *newline*
1142 " if (cf.type == 'catch' && cf.id == id)" *newline*
1143 " return cf.value;" *newline*
1145 " throw cf;" *newline*
1148 (define-compilation throw (id &optional value)
1152 "id: " (ls-compile id) ", "
1153 "value: " (ls-compile value) ", "
1154 "message: 'Throw uncatched.'"
1158 (defvar *tagbody-counter* 0)
1159 (defvar *go-tag-counter* 0)
1162 (or (integerp x) (symbolp x)))
1164 (defun declare-tagbody-tags (tbidx body)
1166 (mapcar (lambda (label)
1167 (let ((tagidx (integer-to-string (incf *go-tag-counter*))))
1168 (make-binding label 'gotag (list tbidx tagidx) t)))
1169 (remove-if-not #'go-tag-p body))))
1170 (extend-lexenv bindings *environment* 'gotag)))
1172 (define-compilation tagbody (&rest body)
1173 ;; Ignore the tagbody if it does not contain any go-tag. We do this
1174 ;; because 1) it is easy and 2) many built-in forms expand to a
1175 ;; implicit tagbody, so we save some space.
1176 (unless (some #'go-tag-p body)
1177 (return-from tagbody (ls-compile `(progn ,@body nil))))
1178 ;; The translation assumes the first form in BODY is a label
1179 (unless (go-tag-p (car body))
1180 (push (gensym "START") body))
1181 ;; Tagbody compilation
1182 (let ((tbidx (integer-to-string *tagbody-counter*)))
1183 (let ((*environment* (declare-tagbody-tags tbidx body))
1185 (let ((b (lookup-in-lexenv (first body) *environment* 'gotag)))
1186 (setq initag (second (binding-value b))))
1188 "var tagbody_" tbidx " = " initag ";" *newline*
1190 "while (true) {" *newline*
1191 (indent "try {" *newline*
1192 (indent (let ((content ""))
1193 (concat "switch(tagbody_" tbidx "){" *newline*
1194 "case " initag ":" *newline*
1195 (dolist (form (cdr body) content)
1197 (if (not (go-tag-p form))
1198 (indent (ls-compile form) ";" *newline*)
1199 (let ((b (lookup-in-lexenv form *environment* 'gotag)))
1200 (concat "case " (second (binding-value b)) ":" *newline*)))))
1201 "default:" *newline*
1202 " break tbloop;" *newline*
1205 "catch (jump) {" *newline*
1206 " if (jump.type == 'tagbody' && jump.id == " tbidx ")" *newline*
1207 " tagbody_" tbidx " = jump.label;" *newline*
1209 " throw(jump);" *newline*
1212 "return " (ls-compile nil) ";" *newline*))))
1214 (define-compilation go (label)
1215 (let ((b (lookup-in-lexenv label *environment* 'gotag))
1217 ((symbolp label) (symbol-name label))
1218 ((integerp label) (integer-to-string label)))))
1223 "id: " (first (binding-value b)) ", "
1224 "label: " (second (binding-value b)) ", "
1225 "message: 'Attempt to GO to non-existing tag " n "'"
1227 (error (concat "Unknown tag `" n "'.")))))
1230 (define-compilation unwind-protect (form &rest clean-up)
1232 "var ret = " (ls-compile nil) ";" *newline*
1234 (indent "ret = " (ls-compile form) ";" *newline*)
1235 "} finally {" *newline*
1236 (indent (ls-compile-block clean-up))
1238 "return ret;" *newline*))
1241 ;;; A little backquote implementation without optimizations of any
1242 ;;; kind for ecmalisp.
1243 (defun backquote-expand-1 (form)
1249 ((eq (car form) 'unquote)
1251 ((eq (car form) 'backquote)
1252 (backquote-expand-1 (backquote-expand-1 (cadr form))))
1257 ((and (listp s) (eq (car s) 'unquote))
1258 (list 'list (cadr s)))
1259 ((and (listp s) (eq (car s) 'unquote-splicing))
1262 (list 'list (backquote-expand-1 s)))))
1265 (defun backquote-expand (form)
1266 (if (and (listp form) (eq (car form) 'backquote))
1267 (backquote-expand-1 (cadr form))
1270 (defmacro backquote (form)
1271 (backquote-expand-1 form))
1273 (define-transformation backquote (form)
1274 (backquote-expand-1 form))
1278 (defmacro define-builtin (name args &body body)
1280 (define-compilation ,name ,args
1281 (let ,(mapcar (lambda (arg) `(,arg (ls-compile ,arg))) args)
1284 ;;; DECLS is a list of (JSVARNAME TYPE LISPFORM) declarations.
1285 (defmacro type-check (decls &body body)
1287 ,@(mapcar (lambda (decl)
1288 `(concat "var " ,(first decl) " = " ,(third decl) ";" *newline*))
1290 ,@(mapcar (lambda (decl)
1291 `(concat "if (typeof " ,(first decl) " != '" ,(second decl) "')" *newline*
1292 (indent "throw 'The value ' + "
1294 " + ' is not a type "
1299 (concat "return " (progn ,@body) ";" *newline*)))
1301 (defun num-op-num (x op y)
1302 (type-check (("x" "number" x) ("y" "number" y))
1303 (concat "x" op "y")))
1305 (define-builtin + (x y) (num-op-num x "+" y))
1306 (define-builtin - (x y) (num-op-num x "-" y))
1307 (define-builtin * (x y) (num-op-num x "*" y))
1308 (define-builtin / (x y) (num-op-num x "/" y))
1310 (define-builtin mod (x y) (num-op-num x "%" y))
1312 (define-builtin < (x y) (js!bool (num-op-num x "<" y)))
1313 (define-builtin > (x y) (js!bool (num-op-num x ">" y)))
1314 (define-builtin = (x y) (js!bool (num-op-num x "==" y)))
1315 (define-builtin <= (x y) (js!bool (num-op-num x "<=" y)))
1316 (define-builtin >= (x y) (js!bool (num-op-num x ">=" y)))
1318 (define-builtin numberp (x)
1319 (js!bool (concat "(typeof (" x ") == \"number\")")))
1321 (define-builtin floor (x)
1322 (type-check (("x" "number" x))
1325 (define-builtin cons (x y)
1326 (concat "({car: " x ", cdr: " y "})"))
1328 (define-builtin consp (x)
1331 "var tmp = " x ";" *newline*
1332 "return (typeof tmp == 'object' && 'car' in tmp);" *newline*)))
1334 (define-builtin car (x)
1336 "var tmp = " x ";" *newline*
1337 "return tmp === " (ls-compile nil)
1338 "? " (ls-compile nil)
1339 ": tmp.car;" *newline*))
1341 (define-builtin cdr (x)
1343 "var tmp = " x ";" *newline*
1344 "return tmp === " (ls-compile nil) "? "
1346 ": tmp.cdr;" *newline*))
1348 (define-builtin setcar (x new)
1349 (type-check (("x" "object" x))
1350 (concat "(x.car = " new ")")))
1352 (define-builtin setcdr (x new)
1353 (type-check (("x" "object" x))
1354 (concat "(x.cdr = " new ")")))
1356 (define-builtin symbolp (x)
1359 "var tmp = " x ";" *newline*
1360 "return (typeof tmp == 'object' && 'name' in tmp);" *newline*)))
1362 (define-builtin make-symbol (name)
1363 (type-check (("name" "string" name))
1366 (define-builtin symbol-name (x)
1367 (concat "(" x ").name"))
1369 (define-builtin set (symbol value)
1370 (concat "(" symbol ").value = " value))
1372 (define-builtin fset (symbol value)
1373 (concat "(" symbol ").function = " value))
1375 (define-builtin boundp (x)
1376 (js!bool (concat "(" x ".value !== undefined)")))
1378 (define-builtin symbol-value (x)
1380 "var symbol = " x ";" *newline*
1381 "var value = symbol.value;" *newline*
1382 "if (value === undefined) throw \"Variable `\" + symbol.name + \"' is unbound.\";" *newline*
1383 "return value;" *newline*))
1385 (define-builtin symbol-function (x)
1387 "var symbol = " x ";" *newline*
1388 "var func = symbol.function;" *newline*
1389 "if (func === undefined) throw \"Function `\" + symbol.name + \"' is undefined.\";" *newline*
1390 "return func;" *newline*))
1392 (define-builtin symbol-plist (x)
1393 (concat "((" x ").plist || " (ls-compile nil) ")"))
1395 (define-builtin lambda-code (x)
1396 (concat "(" x ").toString()"))
1399 (define-builtin eq (x y) (js!bool (concat "(" x " === " y ")")))
1400 (define-builtin equal (x y) (js!bool (concat "(" x " == " y ")")))
1402 (define-builtin string (x)
1403 (type-check (("x" "number" x))
1404 "String.fromCharCode(x)"))
1406 (define-builtin stringp (x)
1407 (js!bool (concat "(typeof(" x ") == \"string\")")))
1409 (define-builtin string-upcase (x)
1410 (type-check (("x" "string" x))
1413 (define-builtin string-length (x)
1414 (type-check (("x" "string" x))
1417 (define-compilation slice (string a &optional b)
1419 "var str = " (ls-compile string) ";" *newline*
1420 "var a = " (ls-compile a) ";" *newline*
1423 (concat "b = " (ls-compile b) ";" *newline*)
1425 "return str.slice(a,b);" *newline*))
1427 (define-builtin char (string index)
1428 (type-check (("string" "string" string)
1429 ("index" "number" index))
1430 "string.charCodeAt(index)"))
1432 (define-builtin concat-two (string1 string2)
1433 (type-check (("string1" "string" string1)
1434 ("string2" "string" string2))
1435 "string1.concat(string2)"))
1437 (define-compilation funcall (func &rest args)
1438 (concat "(" (ls-compile func) ")("
1439 (join (mapcar #'ls-compile args)
1443 (define-compilation apply (func &rest args)
1445 (concat "(" (ls-compile func) ")()")
1446 (let ((args (butlast args))
1447 (last (car (last args))))
1449 "var f = " (ls-compile func) ";" *newline*
1450 "var args = [" (join (mapcar #'ls-compile args)
1453 "var tail = (" (ls-compile last) ");" *newline*
1454 "while (tail != " (ls-compile nil) "){" *newline*
1455 " args.push(tail.car);" *newline*
1456 " tail = tail.cdr;" *newline*
1458 "return f.apply(this, args);" *newline*))))
1460 (define-builtin js-eval (string)
1461 (type-check (("string" "string" string))
1462 "eval.apply(window, [string])"))
1464 (define-builtin error (string)
1465 (js!selfcall "throw " string ";" *newline*))
1467 (define-builtin new () "{}")
1469 (define-builtin oget (object key)
1471 "var tmp = " "(" object ")[" key "];" *newline*
1472 "return tmp == undefined? " (ls-compile nil) ": tmp ;" *newline*))
1474 (define-builtin oset (object key value)
1475 (concat "((" object ")[" key "] = " value ")"))
1477 (define-builtin in (key object)
1478 (js!bool (concat "((" key ") in (" object "))")))
1480 (define-builtin functionp (x)
1481 (js!bool (concat "(typeof " x " == 'function')")))
1483 (define-builtin write-string (x)
1484 (type-check (("x" "string" x))
1489 (let ((b (lookup-in-lexenv x *environment* 'function)))
1490 (and (eq (binding-type b) 'macro)
1493 (defun ls-macroexpand-1 (form)
1494 (let ((macro-binding (macro (car form))))
1496 (let ((expander (binding-value macro-binding)))
1497 (when (listp expander)
1498 (let ((compiled (eval expander)))
1499 ;; The list representation are useful while
1500 ;; bootstrapping, as we can dump the definition of the
1501 ;; macros easily, but they are slow because we have to
1502 ;; evaluate them and compile them now and again. So, let
1503 ;; us replace the list representation version of the
1504 ;; function with the compiled one.
1506 #+ecmalisp (set-binding-value macro-binding compiled)
1507 (setq expander compiled)))
1508 (apply expander (cdr form)))
1511 (defun compile-funcall (function args)
1512 (concat (ls-compile `#',function) "("
1513 (join (mapcar #'ls-compile args)
1517 (defun ls-compile (sexp)
1520 (let ((b (lookup-in-lexenv sexp *environment* 'variable)))
1521 (if (eq (binding-type b) 'lexical-variable)
1523 (ls-compile `(symbol-value ',sexp)))))
1524 ((integerp sexp) (integer-to-string sexp))
1525 ((stringp sexp) (concat "\"" (escape-string sexp) "\""))
1527 (if (assoc (car sexp) *compilations*)
1528 (let ((comp (second (assoc (car sexp) *compilations*))))
1529 (apply comp (cdr sexp)))
1530 (if (macro (car sexp))
1531 (ls-compile (ls-macroexpand-1 sexp))
1532 (compile-funcall (car sexp) (cdr sexp)))))))
1534 (defun ls-compile-toplevel (sexp)
1535 (let ((*toplevel-compilations* nil))
1537 ((and (consp sexp) (eq (car sexp) 'progn))
1538 (let ((subs (mapcar #'ls-compile-toplevel (cdr sexp))))
1539 (join (remove-if #'null-or-empty-p subs))))
1541 (let ((code (ls-compile sexp)))
1542 (concat (join-trailing (get-toplevel-compilations)
1543 (concat ";" *newline*))
1545 (concat code ";" *newline*)
1549 ;;; Once we have the compiler, we define the runtime environment and
1550 ;;; interactive development (eval), which works calling the compiler
1551 ;;; and evaluating the Javascript result globally.
1555 (defmacro with-compilation-unit (&body body)
1558 (setq *compilation-unit-checks* nil)
1559 (clear-undeclared-global-bindings)
1561 (dolist (check *compilation-unit-checks*)
1566 (with-compilation-unit
1567 (ls-compile-toplevel x))))
1570 (js-eval "var lisp")
1571 (js-vset "lisp" (new))
1572 (js-vset "lisp.read" #'ls-read-from-string)
1573 (js-vset "lisp.print" #'prin1-to-string)
1574 (js-vset "lisp.eval" #'eval)
1575 (js-vset "lisp.compile" #'ls-compile-toplevel)
1576 (js-vset "lisp.evalString" (lambda (str) (eval (ls-read-from-string str))))
1577 (js-vset "lisp.compileString" (lambda (str) (ls-compile-toplevel (ls-read-from-string str))))
1579 ;; Set the initial global environment to be equal to the host global
1580 ;; environment at this point of the compilation.
1582 (toplevel-compilation
1585 ,@(mapcar (lambda (s)
1586 `(oset *package* ,(symbol-name (car s))
1587 (js-vref ,(cdr s))))
1589 (setq *literal-symbols* ',*literal-symbols*)
1590 (setq *environment* ',*environment*)
1591 (setq *variable-counter* ,*variable-counter*)
1592 (setq *gensym-counter* ,*gensym-counter*)
1593 (setq *block-counter* ,*block-counter*)))))
1596 (toplevel-compilation
1598 `(setq *literal-counter* ,*literal-counter*)))))
1601 ;;; Finally, we provide a couple of functions to easily bootstrap
1602 ;;; this. It just calls the compiler with this file as input.
1606 (defun read-whole-file (filename)
1607 (with-open-file (in filename)
1608 (let ((seq (make-array (file-length in) :element-type 'character)))
1609 (read-sequence seq in)
1612 (defun ls-compile-file (filename output)
1613 (setq *compilation-unit-checks* nil)
1614 (with-open-file (out output :direction :output :if-exists :supersede)
1615 (let* ((source (read-whole-file filename))
1616 (in (make-string-stream source)))
1618 for x = (ls-read in)
1620 for compilation = (ls-compile-toplevel x)
1621 when (plusp (length compilation))
1622 do (write-string compilation out))
1623 (dolist (check *compilation-unit-checks*)
1625 (setq *compilation-unit-checks* nil))))
1628 (setq *environment* (make-lexenv))
1629 (setq *literal-symbols* nil)
1630 (setq *variable-counter* 0
1634 (ls-compile-file "ecmalisp.lisp" "ecmalisp.js")))