Reorder file loading and fix define-setf-expander
[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 ;;; List of all the source files that need to be compiled, and whether they
26 ;;; are to be compiled just by the host, by the target JSCL, or by both.
27 ;;; All files have a `.lisp' extension, and
28 ;;; are relative to src/
29 ;;; Subdirectories are indicated by the presence of a list rather than a
30 ;;; keyword in the second element of the list. For example, this list:
31 ;;;  (("foo"    :target)
32 ;;;   ("bar"
33 ;;;     ("baz"  :host)
34 ;;;     ("quux" :both)))
35 ;;; Means that src/foo.lisp and src/bar/quux.lisp need to be compiled in the
36 ;;; target, and that src/bar/baz.lisp and src/bar/quux.lisp need to be
37 ;;; compiled in the host
38 (defvar *source*
39   '(("boot"          :target)
40     ("compat"        :host)
41     ("utils"         :both)
42     ("numbers"       :target)
43     ("char"          :target)
44     ("list"          :target)
45     ("array"         :target)
46     ("string"        :target)
47     ("sequence"      :target)
48     ("stream"        :target)
49     ("print"         :target)
50     ("documentation" :target)
51     ("misc"          :target)
52     ("ffi"           :target)
53     ("package"       :target)
54
55     ("read"          :both)
56     ("defstruct"     :both)
57     ("lambda-list"   :both)
58     ("backquote"     :both)
59     ("compiler"
60      ("codegen"      :both)
61      ("compiler"     :both))
62     ("toplevel"      :target)))
63
64 (defun get-files (file-list type dir)
65   "Traverse FILE-LIST and retrieve a list of the files within which match
66    either TYPE or :BOTH, processing subdirectories."
67   (let ((file (car file-list)))
68     (cond
69       ((null file-list)
70        ())
71       ((listp (cadr file))
72        (append
73          (get-files (cdr file)      type (append dir (list (car file))))
74          (get-files (cdr file-list) type dir)))
75       ((member (cadr file) (list type :both))
76        (cons (source-pathname (car file) :directory dir :type "lisp")
77              (get-files (cdr file-list) type dir)))
78       (t
79        (get-files (cdr file-list) type dir)))))
80
81 (defmacro do-source (name type &body body)
82   "Iterate over all the source files that need to be compiled in the host or
83    the target, depending on the TYPE argument."
84   (unless (member type '(:host :target))
85     (error "TYPE must be one of :HOST or :TARGET, not ~S" type))
86   `(dolist (,name (get-files *source* ,type '(:relative "src")))
87      ,@body))
88
89 (defun source-pathname
90     (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
91   (if type
92       (make-pathname :type type :directory directory :defaults defaults)
93       (make-pathname            :directory directory :defaults defaults)))
94
95 ;;; Compile jscl into the host
96 (with-compilation-unit ()
97   (do-source input :host
98     (multiple-value-bind (fasl warn fail) (compile-file input)
99       (declare (ignore fasl warn))
100       (when fail
101         (error "Compilation of ~A failed." input)))))
102
103 ;;; Load jscl into the host
104 (do-source input :host
105   (load input))
106
107 (defun read-whole-file (filename)
108   (with-open-file (in filename)
109     (let ((seq (make-array (file-length in) :element-type 'character)))
110       (read-sequence seq in)
111       seq)))
112
113 (defun !compile-file (filename out &key print)
114   (let ((*compiling-file* t)
115         (*compile-print-toplevels* print))
116     (let* ((source (read-whole-file filename))
117            (in (make-string-stream source)))
118       (format t "Compiling ~a...~%" (enough-namestring filename))
119       (loop
120          with eof-mark = (gensym)
121          for x = (ls-read in nil eof-mark)
122          until (eq x eof-mark)
123          do (let ((compilation (compile-toplevel x)))
124               (when (plusp (length compilation))
125                 (write-string compilation out)))))))
126
127 (defun dump-global-environment (stream)
128   (flet ((late-compile (form)
129            (let ((*standard-output* stream))
130              (write-string (compile-toplevel form)))))
131     ;; We assume that environments have a friendly list representation
132     ;; for the compiler and it can be dumped.
133     (dolist (b (lexenv-function *environment*))
134       (when (eq (binding-type b) 'macro)
135         (setf (binding-value b) `(,*magic-unquote-marker* ,(binding-value b)))))
136     (late-compile `(setq *environment* ',*environment*))
137     ;; Set some counter variable properly, so user compiled code will
138     ;; not collide with the compiler itself.
139     (late-compile
140      `(progn
141         (progn ,@(mapcar (lambda (s) `(%intern-symbol (%js-vref ,(string (cdr s)))))
142                          (remove-if-not #'symbolp *literal-table* :key #'car)))
143         (setq *literal-table* ',*literal-table*)
144         (setq *variable-counter* ,*variable-counter*)
145         (setq *gensym-counter* ,*gensym-counter*)))
146     (late-compile `(setq *literal-counter* ,*literal-counter*))))
147
148
149 (defun bootstrap ()
150   (let ((*features* (cons :jscl *features*))
151         (*package* (find-package "JSCL")))
152     (setq *environment* (make-lexenv))
153     (setq *literal-table* nil)
154     (setq *variable-counter* 0
155           *gensym-counter* 0
156           *literal-counter* 0)
157     (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
158       (write-string (read-whole-file (source-pathname "prelude.js")) out)
159       (do-source input :target
160         (!compile-file input out))
161       (dump-global-environment out))
162     ;; Tests
163     (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
164       (dolist (input (append (directory "tests.lisp")
165                              (directory "tests/*.lisp")
166                              (directory "tests-report.lisp")))
167         (!compile-file input out)))))
168
169
170 ;;; Run the tests in the host Lisp implementation. It is a quick way
171 ;;; to improve the level of trust of the tests.
172 (defun run-tests-in-host ()
173   (let ((*package* (find-package "JSCL")))
174     (load "tests.lisp")
175     (let ((*use-html-output-p* nil))
176       (declare (special *use-html-output-p*))
177       (dolist (input (directory "tests/*.lisp"))
178         (load input)))
179     (load "tests-report.lisp")))