(SETF CHAR) and move string related code to string.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 (defvar *source*
20   '(("boot"      :target)
21     ("compat"    :host)
22     ("utils"     :both)
23     ("list"      :target)
24     ("string"    :target)
25     ("print"     :target)
26     ("package"   :target)
27     ("read"      :both)
28     ("compiler"  :both)
29     ("toplevel"  :target)))
30
31 (defun source-pathname
32     (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
33   (if type
34       (make-pathname :type type :directory directory :defaults defaults)
35       (make-pathname            :directory directory :defaults defaults)))
36
37 ;;; Compile jscl into the host
38 (with-compilation-unit ()
39   (dolist (input *source*)
40     (when (member (cadr input) '(:host :both))
41       (let ((fname (source-pathname (car input))))
42         (multiple-value-bind (fasl warn fail) (compile-file fname)
43           (declare (ignore fasl warn))
44           (when fail
45             (error "Compilation of ~A failed." fname)))))))
46
47 ;;; Load jscl into the host
48 (dolist (input *source*)
49   (when (member (cadr input) '(:host :both))
50     (load (source-pathname (car input)))))
51
52 (defun read-whole-file (filename)
53   (with-open-file (in filename :external-format :latin-1)
54     (let ((seq (make-array (file-length in) :element-type 'character)))
55       (read-sequence seq in)
56       seq)))
57
58 (defun ls-compile-file (filename out &key print)
59   (let ((*compiling-file* t)
60         (*compile-print-toplevels* print))
61     (let* ((source (read-whole-file filename))
62            (in (make-string-stream source)))
63       (format t "Compiling ~a...~%" filename)
64       (loop
65          with eof-mark = (gensym)
66          for x = (ls-read in nil eof-mark)
67          until (eq x eof-mark)
68          for compilation = (ls-compile-toplevel x)
69          when (plusp (length compilation))
70          do (write-string compilation out)))))
71
72 (defun bootstrap ()
73   (setq *environment* (make-lexenv))
74   (setq *literal-table* nil)
75   (setq *variable-counter* 0
76         *gensym-counter* 0
77         *literal-counter* 0)
78   (with-open-file (out "jscl.js" :direction :output :if-exists :supersede)
79     (write-string (read-whole-file (source-pathname "prelude.js")) out)
80     (dolist (input *source*)
81       (when (member (cadr input) '(:target :both))
82         (ls-compile-file (source-pathname (car input) :type "lisp") out))))
83   ;; Tests
84   (with-open-file (out "tests.js" :direction :output :if-exists :supersede)
85     (dolist (input (append (directory "tests.lisp")
86                            (directory "tests/*.lisp")
87                            (directory "tests-report.lisp")))
88       (ls-compile-file input out))))
89
90
91 ;;; Run the tests in the host Lisp implementation. It is a quick way
92 ;;; to improve the level of trust of the tests.
93 (defun run-tests-in-host ()
94   (load "tests.lisp")
95   (let ((*use-html-output-p* nil))
96     (declare (special *use-html-output-p*))
97     (dolist (input (directory "tests/*.lisp"))
98       (load input)))
99   (load "tests-report.lisp"))