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