Merge branch 'master' into mutable-strings
[jscl.git] / jscl.lisp
1 ;;; jscl.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; JSCL is free software: you can redistribute it and/or
7 ;; modify it under the terms of the GNU General Public License as
8 ;; published by the Free Software Foundation, either version 3 of the
9 ;; License, or (at your option) any later version.
10 ;;
11 ;; JSCL is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;; General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
18
19 (defvar *source*
20   '(("boot"      :target)
21     ("compat"    :host)
22     ("utils"     :both)
23     ("list"      :target)
24     ("string"    :target)
25     ("print"     :target)
26     ("package"   :target)
27     ("read"      :both)
28     ("compiler"  :both)
29     ("toplevel"  :target)))
30
31 (defun source-pathname
32     (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
33   (if type
34       (make-pathname :type type :directory directory :defaults defaults)
35       (make-pathname            :directory directory :defaults defaults)))
36
37 ;;; Compile jscl into the host
38 (with-compilation-unit ()
39   (dolist (input *source*)
40     (when (member (cadr input) '(:host :both))
41       (let ((fname (source-pathname (car input))))
42         (multiple-value-bind (fasl warn fail) (compile-file fname)
43           (declare (ignore fasl warn))
44           (when fail
45             (error "Compilation of ~A failed." fname)))))))
46
47 ;;; Load jscl into the host
48 (dolist (input *source*)
49   (when (member (cadr input) '(:host :both))
50     (load (source-pathname (car input)))))
51
52 (defun read-whole-file (filename)
53   (with-open-file (in filename :external-format :latin-1)
54     (let ((seq (make-array (file-length in) :element-type 'character)))
55       (read-sequence seq in)
56       seq)))
57
58 (defun ls-compile-file (filename out &key print)
59   (let ((*compiling-file* t)
60         (*compile-print-toplevels* print))
61     (let* ((source (read-whole-file filename))
62            (in (make-string-stream source)))
63       (format t "Compiling ~a...~%" filename)
64       (loop
65          with eof-mark = (gensym)
66          for x = (ls-read in nil eof-mark)
67          until (eq x eof-mark)
68          for compilation = (ls-compile-toplevel x)
69          when (plusp (length compilation))
70          do (write-string compilation out)))))
71
72
73 (defun dump-global-environment (stream)
74   (flet ((late-compile (form)
75            (write-string (ls-compile-toplevel form) stream)))
76     ;; We assume that environments have a friendly list representation
77     ;; for the compiler and it can be dumped.
78     (dolist (b (lexenv-function *environment*))
79       (when (eq (binding-type b) 'macro)
80         (push *magic-unquote-marker* (binding-value b))))
81     (late-compile `(setq *environment* ',*environment*))
82     ;; Set some counter variable properly, so user compiled code will
83     ;; not collide with the compiler itself.
84     (late-compile
85      `(progn
86         ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(cdr s))))
87                   (remove-if-not #'symbolp *literal-table* :key #'car))
88         (setq *literal-table* ',*literal-table*)
89         (setq *variable-counter* ,*variable-counter*)
90         (setq *gensym-counter* ,*gensym-counter*)))
91     (late-compile `(setq *literal-counter* ,*literal-counter*))))
92
93
94 (defun bootstrap ()
95   (setq *environment* (make-lexenv))
96   (setq *literal-table* nil)
97   (setq *variable-counter* 0
98         *gensym-counter* 0
99         *literal-counter* 0)
100   (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
101     (write-string (read-whole-file (source-pathname "prelude.js")) out)
102     (dolist (input *source*)
103       (when (member (cadr input) '(:target :both))
104         (ls-compile-file (source-pathname (car input) :type "lisp") out)))
105     (dump-global-environment out))
106   ;; Tests
107   (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
108     (dolist (input (append (directory "tests.lisp")
109                            (directory "tests/*.lisp")
110                            (directory "tests-report.lisp")))
111       (ls-compile-file input out))))
112
113
114 ;;; Run the tests in the host Lisp implementation. It is a quick way
115 ;;; to improve the level of trust of the tests.
116 (defun run-tests-in-host ()
117   (load "tests.lisp")
118   (let ((*use-html-output-p* nil))
119     (declare (special *use-html-output-p*))
120     (dolist (input (directory "tests/*.lisp"))
121       (load input)))
122   (load "tests-report.lisp"))