minimal tests for format
[jscl.git] / jscl.lisp
1 ;;; jscl.lisp ---
2
3 ;; Copyright (C) 2012, 2013 David Vazquez
4 ;; Copyright (C) 2012 Raimon Grau
5
6 ;; This program 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 ;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 (defvar *source*
20   '(("boot"      :target)
21     ("compat"    :host)
22     ("utils"     :both)
23     ("print"     :target)
24     ("read"      :both)
25     ("compiler"  :both)
26     ("toplevel"  :target)
27     ;; Tests
28     ("tests"            :test)
29     ("format"           :test)
30     ("setf"             :test)
31     ("eval"             :test)
32     ("tests-report"     :test)))
33
34 (defun source-pathname
35     (filename &key (directory '(:relative "src")) (type nil) (defaults filename))
36   (if type
37       (make-pathname :type type :directory directory :defaults defaults)
38       (make-pathname            :directory directory :defaults defaults)))
39
40 ;;; Compile jscl into the host
41 (with-compilation-unit ()
42   (dolist (input *source*)
43     (when (member (cadr input) '(:host :both))
44       (compile-file (source-pathname (car input))))))
45
46 ;;; Load jscl into the host
47 (dolist (input *source*)
48   (when (member (cadr input) '(:host :both))
49     (load (source-pathname (car input)))))
50
51 (defun read-whole-file (filename)
52   (with-open-file (in filename)
53     (let ((seq (make-array (file-length in) :element-type 'character)))
54       (read-sequence seq in)
55       seq)))
56
57 (defun ls-compile-file (filename out &key print)
58   (let ((*compiling-file* t)
59         (*compile-print-toplevels* print))
60     (let* ((source (read-whole-file filename))
61            (in (make-string-stream source)))
62       (format t "Compiling ~a...~%" filename)
63       (loop
64          with eof-mark = (gensym)
65          for x = (ls-read in nil eof-mark)
66          until (eq x eof-mark)
67          for compilation = (ls-compile-toplevel x)
68          when (plusp (length compilation))
69          do (write-string compilation out)))))
70
71 (defun bootstrap ()
72   (setq *environment* (make-lexenv))
73   (setq *literal-symbols* nil)
74   (setq *variable-counter* 0
75         *gensym-counter* 0
76         *literal-counter* 0
77         *block-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 *source*)
86       (when (member (cadr input) '(:test))
87         (ls-compile-file (source-pathname (car input)
88                                           :directory '(:relative "tests")
89                                           :type "lisp")
90                          out)))))