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