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