Rename LS-COMPILE => CONVERT and LS-COMPILE-TOPLEVEL to COMPILE-TOPLEVEL
[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 (defpackage :jscl
20   (:use :cl)
21   (:export #:bootstrap #:run-tests-in-host))
22
23 (in-package :jscl)
24
25 (defvar *source*
26   '(("boot"             :target)
27     ("compat"           :host)
28     ("utils"            :both)
29     ("numbers"          :target)
30     ("char"             :target)
31     ("list"             :target)
32     ("array"            :target)
33     ("string"           :target)
34     ("sequence"         :target)
35     ("stream"           :target)
36     ("print"            :target)
37     ("package"          :target)
38     ("misc"             :target)
39     ("ffi"              :both)
40     ("read"             :both)
41     ("defstruct"        :both)
42     ("lambda-list"      :both)
43     ("backquote"        :both)
44     ("compiler-codegen" :both)
45     ("compiler"         :both)
46     ("toplevel"         :target)))
47
48 (defun source-pathname
49     (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
50   (if type
51       (make-pathname :type type :directory directory :defaults defaults)
52       (make-pathname            :directory directory :defaults defaults)))
53
54 ;;; Compile jscl into the host
55 (with-compilation-unit ()
56   (dolist (input *source*)
57     (when (member (cadr input) '(:host :both))
58       (let ((fname (source-pathname (car input))))
59         (multiple-value-bind (fasl warn fail) (compile-file fname)
60           (declare (ignore fasl warn))
61           (when fail
62             (error "Compilation of ~A failed." fname)))))))
63
64 ;;; Load jscl into the host
65 (dolist (input *source*)
66   (when (member (cadr input) '(:host :both))
67     (load (source-pathname (car input)))))
68
69 (defun read-whole-file (filename)
70   (with-open-file (in filename)
71     (let ((seq (make-array (file-length in) :element-type 'character)))
72       (read-sequence seq in)
73       seq)))
74
75 (defun !compile-file (filename out &key print)
76   (let ((*compiling-file* t)
77         (*compile-print-toplevels* print))
78     (let* ((source (read-whole-file filename))
79            (in (make-string-stream source)))
80       (format t "Compiling ~a...~%" filename)
81       (loop
82          with eof-mark = (gensym)
83          for x = (ls-read in nil eof-mark)
84          until (eq x eof-mark)
85          do (let ((compilation (compile-toplevel x)))
86               (when (plusp (length compilation))
87                 (write-string compilation out)))))))
88
89 (defun dump-global-environment (stream)
90   (flet ((late-compile (form)
91            (let ((*standard-output* stream))
92              (write-string (compile-toplevel form)))))
93     ;; We assume that environments have a friendly list representation
94     ;; for the compiler and it can be dumped.
95     (dolist (b (lexenv-function *environment*))
96       (when (eq (binding-type b) 'macro)
97         (setf (binding-value b) `(,*magic-unquote-marker* ,(binding-value b)))))
98     (late-compile `(setq *environment* ',*environment*))
99     ;; Set some counter variable properly, so user compiled code will
100     ;; not collide with the compiler itself.
101     (late-compile
102      `(progn
103         (progn ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(string (cdr s)))))
104                          (remove-if-not #'symbolp *literal-table* :key #'car)))
105         (setq *literal-table* ',*literal-table*)
106         (setq *variable-counter* ,*variable-counter*)
107         (setq *gensym-counter* ,*gensym-counter*)))
108     (late-compile `(setq *literal-counter* ,*literal-counter*))))
109
110
111 (defun bootstrap ()
112   (let ((*features* (cons :jscl *features*))
113         (*package* (find-package "JSCL")))
114     (setq *environment* (make-lexenv))
115     (setq *literal-table* nil)
116     (setq *variable-counter* 0
117           *gensym-counter* 0
118           *literal-counter* 0)
119     (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
120       (write-string (read-whole-file (source-pathname "prelude.js")) out)
121       (dolist (input *source*)
122         (when (member (cadr input) '(:target :both))
123           (!compile-file (source-pathname (car input) :type "lisp") out)))
124       (dump-global-environment out))
125     ;; Tests
126     (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
127       (dolist (input (append (directory "tests.lisp")
128                              (directory "tests/*.lisp")
129                              (directory "tests-report.lisp")))
130         (!compile-file input out)))))
131
132
133 ;;; Run the tests in the host Lisp implementation. It is a quick way
134 ;;; to improve the level of trust of the tests.
135 (defun run-tests-in-host ()
136   (let ((*package* (find-package "JSCL")))
137     (load "tests.lisp")
138     (let ((*use-html-output-p* nil))
139       (declare (special *use-html-output-p*))
140       (dolist (input (directory "tests/*.lisp"))
141         (load input)))
142     (load "tests-report.lisp")))