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