Debian Codegraph @

scheme/ikarus.main.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17
18 ;;; this is here to test that we can import things from other
19 ;;; libraries within the compiler itself.
20
21 (library (ikarus startup)
22 (export print-greeting init-library-path host-info split-path)
23 (import (except (ikarus) host-info) (ikarus include))
24 (include "ikarus.config.ss")
25
26 (define (host-info) target)
27
28 (define (split-path s)
29 (define (nodata i s ls)
30 (cond
31 [(= i (string-length s)) ls]
32 [(char=? (string-ref s i) #\:) (nodata (+ i 1) s ls)]
33 [else (data (+ i 1) s ls (list (string-ref s i)))]))
34 (define (data i s ls ac)
35 (cond
36 [(= i (string-length s))
37 (cons (list->string (reverse ac)) ls)]
38 [(char=? (string-ref s i) #\:)
39 (nodata (+ i 1) s
40 (cons (list->string (reverse ac)) ls))]
41 [else (data (+ i 1) s ls (cons (string-ref s i) ac))]))
42 (reverse (nodata 0 s '())))
43
44
45 (define (print-greeting)
46 (printf "Ikarus Scheme version ~a~a~a~a\n"
47 ikarus-version
48 (if (zero? (string-length ikarus-revision)) "" "+")
49 (if (= (fixnum-width) 30)
50 ""
51 ", 64-bit")
52 (if (zero? (string-length ikarus-revision))
53 ""
54 (format " (revision ~a, build ~a)"
55 (+ 1 (string->number ikarus-revision))
56 (let-syntax ([ds (lambda (x) (date-string))])
57 ds))))
58 (display "Copyright (c) 2006-2009 Abdulaziz Ghuloum\n\n"))
59
60 (define (init-library-path)
61 (library-path
62 (append
63 (cond
64 [(getenv "IKARUS_LIBRARY_PATH") => split-path]
65 [else '(".")])
66 (list ikarus-lib-dir)))
67 (let ([prefix
68 (lambda (ext ls)
69 (append (map (lambda (x) (string-append ext x)) ls) ls))])
70 (library-extensions
71 (prefix "/main"
72 (prefix ".ikarus"
73 (library-extensions)))))))
74
75
76 ;;; Finally, we're ready to evaluate the files and enter the cafe.
77
78 (library (ikarus main)
79 (export)
80 (import (except (ikarus) load-r6rs-script)
81 (except (ikarus startup) host-info)
82 (only (ikarus.compiler) generate-debug-calls)
83 (only (ikarus.debugger) guarded-start)
84 (only (psyntax library-manager) current-library-expander)
85 (only (ikarus.reader.annotated) read-source-file)
86 (only (ikarus.symbol-table) initialize-symbol-table!)
87 (only (ikarus load) load-r6rs-script))
88
89 (define rcfiles #t) ;; #f for no rcfile, list for specific list
90
91 (define (parse-command-line-arguments)
92 (let f ([args (command-line-arguments)] [k void])
93 (define (invalid-rc-error)
94 (die 'ikarus "--no-rcfile is invalid with --rcfile"))
95 (cond
96 [(null? args) (values '() #f #f '() k)]
97 [(member (car args) '("-d" "--debug"))
98 (f (cdr args) (lambda () (k) (generate-debug-calls #t)))]
99 [(member (car args) '("-nd" "--no-debug"))
100 (f (cdr args) (lambda () (k) (generate-debug-calls #f)))]
101 [(string=? (car args) "-O2")
102 (f (cdr args) (lambda () (k) (optimize-level 2)))]
103 [(string=? (car args) "-O1")
104 (f (cdr args) (lambda () (k) (optimize-level 1)))]
105 [(string=? (car args) "-O0")
106 (f (cdr args) (lambda () (k) (optimize-level 0)))]
107 [(string=? (car args) "--no-rcfile")
108 (unless (boolean? rcfiles) (invalid-rc-error))
109 (set! rcfiles #f)
110 (f (cdr args) k)]
111 [(string=? (car args) "--rcfile")
112 (let ([d (cdr args)])
113 (when (null? d) (die 'ikarus "--rcfile requires a script name"))
114 (set! rcfiles
115 (cons (car d)
116 (case rcfiles
117 [(#t) '()]
118 [(#f) (invalid-rc-error)]
119 [else rcfiles])))
120 (f (cdr d) k))]
121 [(string=? (car args) "--")
122 (values '() #f #f (cdr args) k)]
123 [(string=? (car args) "--script")
124 (let ([d (cdr args)])
125 (cond
126 [(null? d) (die 'ikarus "--script requires a script name")]
127 [else (values '() (car d) 'script (cdr d) k)]))]
128 [(string=? (car args) "--r6rs-script")
129 (let ([d (cdr args)])
130 (cond
131 [(null? d) (die 'ikarus "--r6rs-script requires a script name")]
132 [else (values '() (car d) 'r6rs-script (cdr d) k)]))]
133 [(string=? (car args) "--r6rs-repl")
134 (let ([d (cdr args)])
135 (cond
136 [(null? d) (die 'ikarus "--r6rs-repl requires a script name")]
137 [else (values '() (car d) 'r6rs-repl (cdr d) k)]))]
138 [(string=? (car args) "--compile-dependencies")
139 (let ([d (cdr args)])
140 (cond
141 [(null? d)
142 (die 'ikarus "--compile-dependencies requires a script name")]
143 [else
144 (values '() (car d) 'compile (cdr d) k)]))]
145 [else
146 (let-values ([(f* script script-type a* k) (f (cdr args) k)])
147 (values (cons (car args) f*) script script-type a* k))])))
148
149 (initialize-symbol-table!)
150 (init-library-path)
151 (let-values ([(files script script-type args init-command-line-args)
152 (parse-command-line-arguments)])
153
154 (define (assert-null files who)
155 (unless (null? files)
156 (apply die 'ikarus
157 (format "load files not allowed for ~a" who)
158 files)))
159
160 (define (start proc)
161 (if (generate-debug-calls)
162 (guarded-start proc)
163 (proc)))
164
165 (define-syntax doit
166 (syntax-rules ()
167 [(_ e e* ...)
168 (start (lambda () e e* ...))]))
169
170 (define (default-rc-files)
171 (cond
172 [(getenv "IKARUS_RC_FILES") => split-path]
173 [(getenv "HOME") =>
174 (lambda (home)
175 (let ([f (string-append home "/.ikarusrc")])
176 (if (file-exists? f)
177 (list f)
178 '())))]
179 [else '()]))
180
181 (for-each
182 (lambda (filename)
183 (with-exception-handler
184 (lambda (con)
185 (raise-continuable
186 (condition
187 (make-who-condition 'ikarus)
188 (make-message-condition
189 (format "loading rc file ~a failed" filename))
190 con)))
191 (lambda ()
192 (load-r6rs-script filename #f #t))))
193 (case rcfiles
194 [(#t) (default-rc-files)]
195 [(#f) '()]
196 [else (reverse rcfiles)]))
197
198 (init-command-line-args)
199
200 (cond
201 [(memq script-type '(r6rs-script r6rs-repl))
202 (let ([f (lambda ()
203 (doit
204 (command-line-arguments (cons script args))
205 (for-each
206 (lambda (filename)
207 (for-each
208 (lambda (src)
209 ((current-library-expander) src))
210 (read-source-file filename)))
211 files)
212 (load-r6rs-script script #f #t)))])
213 (cond
214 [(eq? script-type 'r6rs-script) (f)]
215 [else
216 (print-greeting)
217 (let ([env (f)])
218 (interaction-environment env)
219 (new-cafe
220 (lambda (x)
221 (doit (eval x env)))))]))]
222 [(eq? script-type 'compile)
223 (assert-null files "--compile-dependencies")
224 (doit
225 (command-line-arguments (cons script args))
226 (load-r6rs-script script #t #f))]
227 [(eq? script-type 'script) ; no greeting, no cafe
228 (command-line-arguments (cons script args))
229 (doit
230 (for-each load files)
231 (load script))]
232 [else
233 (print-greeting)
234 (command-line-arguments (cons "*interactive*" args))
235 (doit (for-each load files))
236 (new-cafe
237 (lambda (x)
238 (doit (eval x (interaction-environment)))))])
239
240 (exit 0)))