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