syncase macros compiling!
[bpt/guile.git] / module / system / base / compile.scm
1 ;;; High-level compiler interface
2
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Code:
21
22 (define-module (system base compile)
23 :use-syntax (system base syntax)
24 :use-module (system base language)
25 :use-module (system il compile)
26 :use-module (system il glil)
27 :use-module ((system vm core)
28 :select (the-vm vm-load objcode->u8vector load-objcode))
29 :use-module (system vm assemble)
30 :use-module (ice-9 regex)
31 :export (syntax-error compile-file load-source-file load-file
32 compiled-file-name
33 scheme-eval read-file-in compile-in))
34
35 ;;;
36 ;;; Compiler environment
37 ;;;
38
39 (define (syntax-error loc msg exp)
40 (throw 'syntax-error loc msg exp))
41
42 (define-macro (call-with-compile-error-catch thunk)
43 `(catch 'syntax-error
44 ,thunk
45 (lambda (key loc msg exp)
46 (if (pair? loc)
47 (format #t "~A:~A: ~A: ~A~%" (car loc) (cdr loc) msg exp)
48 (format #t "unknown location: ~A: ~A~%" msg exp)))))
49
50 (export-syntax call-with-compile-error-catch)
51
52
53 \f
54 ;;;
55 ;;; Compiler
56 ;;;
57
58 (define (scheme) (lookup-language 'scheme))
59
60 (define (compile-file file . opts)
61 (let ((comp (compiled-file-name file)))
62 (catch 'nothing-at-all
63 (lambda ()
64 (call-with-compile-error-catch
65 (lambda ()
66 (call-with-output-file comp
67 (lambda (port)
68 (let* ((source (read-file-in file (scheme)))
69 (objcode (apply compile-in source (current-module)
70 (scheme) opts)))
71 (if (memq :c opts)
72 (pprint-glil objcode port)
73 (uniform-vector-write (objcode->u8vector objcode) port)))))
74 (format #t "wrote `~A'\n" comp))))
75 (lambda (key . args)
76 (format #t "ERROR: during compilation of ~A:\n" file)
77 (display "ERROR: ")
78 (apply format #t (cadr args) (caddr args))
79 (newline)
80 (format #t "ERROR: ~A ~A ~A\n" key (car args) (cadddr args))
81 (delete-file comp)))))
82
83 ; (let ((c-f compile-file))
84 ; ;; XXX: Debugging output
85 ; (set! compile-file
86 ; (lambda (file . opts)
87 ; (format #t "compile-file: ~a ~a~%" file opts)
88 ; (let ((result (apply c-f (cons file opts))))
89 ; (format #t "compile-file: returned ~a~%" result)
90 ; result))))
91
92 (define (load-source-file file . opts)
93 (let ((source (read-file-in file (scheme))))
94 (apply compile-in source (current-module) (scheme) opts)))
95
96 (define (load-file file . opts)
97 (let ((comp (compiled-file-name file)))
98 (if (file-exists? comp)
99 (load-objcode comp)
100 (apply load-source-file file opts))))
101
102 (define (compiled-file-name file)
103 (let ((m (string-match "\\.[^.]*$" file)))
104 (string-append (if m (match:prefix m) file) ".go")))
105
106 (define (scheme-eval x e)
107 (vm-load (the-vm) (compile-in x e (scheme))))
108
109 \f
110 ;;;
111 ;;; Scheme compiler interface
112 ;;;
113
114 (define (read-file-in file lang)
115 (call-with-input-file file (language-read-file lang)))
116
117 (define (compile-in x e lang . opts)
118 (catch 'result
119 (lambda ()
120 ;; expand
121 (set! x ((language-expander lang) x e))
122 (if (memq :e opts) (throw 'result x))
123 ;; translate
124 (set! x ((language-translator lang) x e))
125 (if (memq :t opts) (throw 'result x))
126 ;; compile
127 (set! x (apply compile x e opts))
128 (if (memq :c opts) (throw 'result x))
129 ;; assemble
130 (apply assemble x e opts))
131 (lambda (key val) val)))
132
133 ;;;
134 ;;;
135 ;;;
136
137 (define (compile-and-load file . opts)
138 (let ((comp (object-file-name file)))
139 (if (or (not (file-exists? comp))
140 (> (stat:mtime (stat file)) (stat:mtime (stat comp))))
141 (compile-file file))
142 (load-compiled-file comp)))
143
144 (define (load/compile file . opts)
145 (let* ((file (file-full-name file))
146 (compiled (object-file-name file)))
147 (if (or (not (file-exists? compiled))
148 (> (stat:mtime (stat file)) (stat:mtime (stat compiled))))
149 (apply compile-file file #f opts))
150 (if (memq #:b opts)
151 (apply vm-trace (the-vm) (load-objcode compiled) opts)
152 ((the-vm) (load-objcode compiled)))))
153
154 (define (file-full-name filename)
155 (let* ((port (current-load-port))
156 (oldname (and port (port-filename port))))
157 (if (and oldname
158 (> (string-length filename) 0)
159 (not (char=? (string-ref filename 0) #\/))
160 (not (string=? (dirname oldname) ".")))
161 (string-append (dirname oldname) "/" filename)
162 filename)))