Commit | Line | Data |
---|---|---|
87e00370 | 1 | ;;; -*- mode: scheme; coding: utf-8; -*- |
0f2d19dd | 2 | |
41131340 | 3 | ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010 |
3d2ada2f | 4 | ;;;; Free Software Foundation, Inc. |
20edfbbd | 5 | ;;;; |
73be1d9e MV |
6 | ;;;; This library is free software; you can redistribute it and/or |
7 | ;;;; modify it under the terms of the GNU Lesser General Public | |
8 | ;;;; License as published by the Free Software Foundation; either | |
53befeb7 | 9 | ;;;; version 3 of the License, or (at your option) any later version. |
73be1d9e MV |
10 | ;;;; |
11 | ;;;; This library is distributed in the hope that it will be useful, | |
0f2d19dd | 12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
73be1d9e MV |
13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | ;;;; Lesser General Public License for more details. | |
15 | ;;;; | |
16 | ;;;; You should have received a copy of the GNU Lesser General Public | |
17 | ;;;; License along with this library; if not, write to the Free Software | |
92205699 | 18 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
a482f2cc | 19 | ;;;; |
3d2ada2f | 20 | |
0f2d19dd JB |
21 | \f |
22 | ||
20edfbbd TTN |
23 | ;;; Commentary: |
24 | ||
0f2d19dd JB |
25 | ;;; This file is the first thing loaded into Guile. It adds many mundane |
26 | ;;; definitions and a few that are interesting. | |
27 | ;;; | |
20edfbbd | 28 | ;;; The module system (hence the hierarchical namespace) are defined in this |
0f2d19dd JB |
29 | ;;; file. |
30 | ;;; | |
31 | ||
20edfbbd TTN |
32 | ;;; Code: |
33 | ||
0f2d19dd | 34 | \f |
9fb41cea | 35 | |
9c35c579 AW |
36 | ;; Before compiling, make sure any symbols are resolved in the (guile) |
37 | ;; module, the primary location of those symbols, rather than in | |
38 | ;; (guile-user), the default module that we compile in. | |
39 | ||
40 | (eval-when (compile) | |
41 | (set-current-module (resolve-module '(guile)))) | |
42 | ||
416f26c7 AW |
43 | \f |
44 | ||
45 | ;;; {Error handling} | |
46 | ;;; | |
47 | ||
48 | ;; Define delimited continuation operators, and implement catch and throw in | |
49 | ;; terms of them. | |
50 | ||
d648f569 AW |
51 | (define make-prompt-tag |
52 | (lambda* (#:optional (stem "prompt")) | |
53 | (gensym stem))) | |
54 | ||
c6a32a2c AW |
55 | (define default-prompt-tag |
56 | ;; not sure if we should expose this to the user as a fluid | |
57 | (let ((%default-prompt-tag (make-prompt-tag))) | |
58 | (lambda () | |
59 | %default-prompt-tag))) | |
8fc43b12 AW |
60 | |
61 | (define (call-with-prompt tag thunk handler) | |
416f26c7 | 62 | (@prompt tag (thunk) handler)) |
8fc43b12 | 63 | (define (abort-to-prompt tag . args) |
416f26c7 AW |
64 | (@abort tag args)) |
65 | ||
66 | ||
416f26c7 AW |
67 | ;; Define catch and with-throw-handler, using some common helper routines and a |
68 | ;; shared fluid. Hide the helpers in a lexical contour. | |
69 | ||
70 | (let () | |
71 | ;; Ideally we'd like to be able to give these default values for all threads, | |
72 | ;; even threads not created by Guile; but alack, that does not currently seem | |
73 | ;; possible. So wrap the getters in thunks. | |
74 | (define %running-exception-handlers (make-fluid)) | |
75 | (define %exception-handler (make-fluid)) | |
76 | ||
77 | (define (running-exception-handlers) | |
78 | (or (fluid-ref %running-exception-handlers) | |
79 | (begin | |
80 | (fluid-set! %running-exception-handlers '()) | |
81 | '()))) | |
82 | (define (exception-handler) | |
83 | (or (fluid-ref %exception-handler) | |
84 | (begin | |
85 | (fluid-set! %exception-handler default-exception-handler) | |
86 | default-exception-handler))) | |
87 | ||
88 | (define (default-exception-handler k . args) | |
89 | (cond | |
90 | ((eq? k 'quit) | |
91 | (primitive-exit (cond | |
92 | ((not (pair? args)) 0) | |
93 | ((integer? (car args)) (car args)) | |
94 | ((not (car args)) 1) | |
95 | (else 0)))) | |
96 | (else | |
97 | (format (current-error-port) "guile: uncaught throw to ~a: ~a\n" k args) | |
98 | (primitive-exit 1)))) | |
99 | ||
100 | (define (default-throw-handler prompt-tag catch-k) | |
101 | (let ((prev (exception-handler))) | |
102 | (lambda (thrown-k . args) | |
103 | (if (or (eq? thrown-k catch-k) (eqv? catch-k #t)) | |
8fc43b12 | 104 | (apply abort-to-prompt prompt-tag thrown-k args) |
416f26c7 AW |
105 | (apply prev thrown-k args))))) |
106 | ||
107 | (define (custom-throw-handler prompt-tag catch-k pre) | |
108 | (let ((prev (exception-handler))) | |
109 | (lambda (thrown-k . args) | |
110 | (if (or (eq? thrown-k catch-k) (eqv? catch-k #t)) | |
111 | (let ((running (running-exception-handlers))) | |
112 | (with-fluids ((%running-exception-handlers (cons pre running))) | |
113 | (if (not (memq pre running)) | |
114 | (apply pre thrown-k args)) | |
115 | ;; fall through | |
116 | (if prompt-tag | |
8fc43b12 | 117 | (apply abort-to-prompt prompt-tag thrown-k args) |
416f26c7 AW |
118 | (apply prev thrown-k args)))) |
119 | (apply prev thrown-k args))))) | |
120 | ||
121 | (define! 'catch | |
f31b7b6a | 122 | (lambda* (k thunk handler #:optional pre-unwind-handler) |
416f26c7 AW |
123 | "Invoke @var{thunk} in the dynamic context of @var{handler} for |
124 | exceptions matching @var{key}. If thunk throws to the symbol | |
125 | @var{key}, then @var{handler} is invoked this way: | |
126 | @lisp | |
127 | (handler key args ...) | |
128 | @end lisp | |
129 | ||
130 | @var{key} is a symbol or @code{#t}. | |
131 | ||
132 | @var{thunk} takes no arguments. If @var{thunk} returns | |
133 | normally, that is the return value of @code{catch}. | |
134 | ||
135 | Handler is invoked outside the scope of its own @code{catch}. | |
136 | If @var{handler} again throws to the same key, a new handler | |
137 | from further up the call chain is invoked. | |
138 | ||
139 | If the key is @code{#t}, then a throw to @emph{any} symbol will | |
140 | match this call to @code{catch}. | |
141 | ||
142 | If a @var{pre-unwind-handler} is given and @var{thunk} throws | |
143 | an exception that matches @var{key}, Guile calls the | |
144 | @var{pre-unwind-handler} before unwinding the dynamic state and | |
145 | invoking the main @var{handler}. @var{pre-unwind-handler} should | |
146 | be a procedure with the same signature as @var{handler}, that | |
147 | is @code{(lambda (key . args))}. It is typically used to save | |
148 | the stack at the point where the exception occurred, but can also | |
149 | query other parts of the dynamic state at that point, such as | |
150 | fluid values. | |
151 | ||
152 | A @var{pre-unwind-handler} can exit either normally or non-locally. | |
153 | If it exits normally, Guile unwinds the stack and dynamic context | |
154 | and then calls the normal (third argument) handler. If it exits | |
155 | non-locally, that exit determines the continuation." | |
156 | (if (not (or (symbol? k) (eqv? k #t))) | |
157 | (scm-error "catch" 'wrong-type-arg | |
158 | "Wrong type argument in position ~a: ~a" | |
159 | (list 1 k) (list k))) | |
8fc43b12 AW |
160 | (let ((tag (make-prompt-tag "catch"))) |
161 | (call-with-prompt | |
162 | tag | |
163 | (lambda () | |
164 | (with-fluids | |
165 | ((%exception-handler | |
f31b7b6a AW |
166 | (if pre-unwind-handler |
167 | (custom-throw-handler tag k pre-unwind-handler) | |
168 | (default-throw-handler tag k)))) | |
8fc43b12 AW |
169 | (thunk))) |
170 | (lambda (cont k . args) | |
171 | (apply handler k args)))))) | |
416f26c7 AW |
172 | |
173 | (define! 'with-throw-handler | |
174 | (lambda (k thunk pre-unwind-handler) | |
175 | "Add @var{handler} to the dynamic context as a throw handler | |
176 | for key @var{key}, then invoke @var{thunk}." | |
177 | (if (not (or (symbol? k) (eqv? k #t))) | |
178 | (scm-error "with-throw-handler" 'wrong-type-arg | |
179 | "Wrong type argument in position ~a: ~a" | |
180 | (list 1 k) (list k))) | |
181 | (with-fluids ((%exception-handler | |
182 | (custom-throw-handler #f k pre-unwind-handler))) | |
183 | (thunk)))) | |
184 | ||
185 | (define! 'throw | |
186 | (lambda (key . args) | |
187 | "Invoke the catch form matching @var{key}, passing @var{args} to the | |
188 | @var{handler}. | |
189 | ||
190 | @var{key} is a symbol. It will match catches of the same symbol or of @code{#t}. | |
191 | ||
192 | If there is no handler at all, Guile prints an error and then exits." | |
193 | (if (not (symbol? key)) | |
194 | ((exception-handler) 'wrong-type-arg "throw" | |
195 | "Wrong type argument in position ~a: ~a" (list 1 key) (list key)) | |
196 | (apply (exception-handler) key args))))) | |
197 | ||
198 | ||
199 | \f | |
200 | ||
928258fb AW |
201 | ;;; {R4RS compliance} |
202 | ;;; | |
203 | ||
204 | (primitive-load-path "ice-9/r4rs") | |
205 | ||
206 | \f | |
207 | ||
eb5d1f88 AW |
208 | ;;; {Simple Debugging Tools} |
209 | ;;; | |
210 | ||
211 | ;; peek takes any number of arguments, writes them to the | |
212 | ;; current ouput port, and returns the last argument. | |
213 | ;; It is handy to wrap around an expression to look at | |
214 | ;; a value each time is evaluated, e.g.: | |
215 | ;; | |
9b5a0d84 AW |
216 | ;; (+ 10 (troublesome-fn)) |
217 | ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn))) | |
eb5d1f88 AW |
218 | ;; |
219 | ||
220 | (define (peek . stuff) | |
221 | (newline) | |
222 | (display ";;; ") | |
223 | (write stuff) | |
224 | (newline) | |
225 | (car (last-pair stuff))) | |
226 | ||
227 | (define pk peek) | |
228 | ||
b7742c6b | 229 | |
eb5d1f88 AW |
230 | (define (warn . stuff) |
231 | (with-output-to-port (current-error-port) | |
232 | (lambda () | |
233 | (newline) | |
234 | (display ";;; WARNING ") | |
235 | (display stuff) | |
236 | (newline) | |
237 | (car (last-pair stuff))))) | |
238 | ||
239 | \f | |
240 | ||
21ed9efe | 241 | ;;; {Features} |
3d2ada2f | 242 | ;;; |
21ed9efe MD |
243 | |
244 | (define (provide sym) | |
245 | (if (not (memq sym *features*)) | |
246 | (set! *features* (cons sym *features*)))) | |
247 | ||
3d2ada2f DH |
248 | ;; Return #t iff FEATURE is available to this Guile interpreter. In SLIB, |
249 | ;; provided? also checks to see if the module is available. We should do that | |
250 | ;; too, but don't. | |
251 | ||
50706e94 JB |
252 | (define (provided? feature) |
253 | (and (memq feature *features*) #t)) | |
254 | ||
4d248541 AW |
255 | \f |
256 | ||
257 | ;;; {and-map and or-map} | |
258 | ;;; | |
259 | ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...) | |
260 | ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...) | |
261 | ;;; | |
262 | ||
263 | ;; and-map f l | |
264 | ;; | |
265 | ;; Apply f to successive elements of l until exhaustion or f returns #f. | |
266 | ;; If returning early, return #f. Otherwise, return the last value returned | |
267 | ;; by f. If f has never been called because l is empty, return #t. | |
268 | ;; | |
269 | (define (and-map f lst) | |
270 | (let loop ((result #t) | |
9b5a0d84 | 271 | (l lst)) |
4d248541 | 272 | (and result |
9b5a0d84 AW |
273 | (or (and (null? l) |
274 | result) | |
275 | (loop (f (car l)) (cdr l)))))) | |
4d248541 AW |
276 | |
277 | ;; or-map f l | |
278 | ;; | |
279 | ;; Apply f to successive elements of l until exhaustion or while f returns #f. | |
280 | ;; If returning early, return the return value of f. | |
281 | ;; | |
282 | (define (or-map f lst) | |
283 | (let loop ((result #f) | |
9b5a0d84 | 284 | (l lst)) |
4d248541 | 285 | (or result |
9b5a0d84 AW |
286 | (and (not (null? l)) |
287 | (loop (f (car l)) (cdr l)))))) | |
4d248541 AW |
288 | |
289 | \f | |
290 | ||
3d2ada2f | 291 | ;; let format alias simple-format until the more complete version is loaded |
52cfc69b | 292 | |
8641dd9e GB |
293 | (define format simple-format) |
294 | ||
fdc6aebf KR |
295 | ;; this is scheme wrapping the C code so the final pred call is a tail call, |
296 | ;; per SRFI-13 spec | |
a4c8a02e AW |
297 | (define string-any |
298 | (lambda* (char_pred s #:optional (start 0) (end (string-length s))) | |
fdc6aebf | 299 | (if (and (procedure? char_pred) |
9b5a0d84 AW |
300 | (> end start) |
301 | (<= end (string-length s))) ;; let c-code handle range error | |
302 | (or (string-any-c-code char_pred s start (1- end)) | |
303 | (char_pred (string-ref s (1- end)))) | |
304 | (string-any-c-code char_pred s start end)))) | |
fdc6aebf KR |
305 | |
306 | ;; this is scheme wrapping the C code so the final pred call is a tail call, | |
307 | ;; per SRFI-13 spec | |
a4c8a02e AW |
308 | (define string-every |
309 | (lambda* (char_pred s #:optional (start 0) (end (string-length s))) | |
fdc6aebf | 310 | (if (and (procedure? char_pred) |
9b5a0d84 AW |
311 | (> end start) |
312 | (<= end (string-length s))) ;; let c-code handle range error | |
313 | (and (string-every-c-code char_pred s start (1- end)) | |
314 | (char_pred (string-ref s (1- end)))) | |
315 | (string-every-c-code char_pred s start end)))) | |
fdc6aebf | 316 | |
1b05b324 MV |
317 | ;; A variant of string-fill! that we keep for compatability |
318 | ;; | |
319 | (define (substring-fill! str start end fill) | |
320 | (string-fill! str fill start end)) | |
321 | ||
21ed9efe | 322 | \f |
79451588 | 323 | |
12eae603 AW |
324 | ;; Define a minimal stub of the module API for psyntax, before modules |
325 | ;; have booted. | |
efa6f9d9 | 326 | (define (module-name x) |
a26934a8 | 327 | '(guile)) |
3d5f3091 AW |
328 | (define (module-define! module sym val) |
329 | (let ((v (hashq-ref (%get-pre-modules-obarray) sym))) | |
330 | (if v | |
331 | (variable-set! v val) | |
332 | (hashq-set! (%get-pre-modules-obarray) sym | |
333 | (make-variable val))))) | |
334 | (define (module-ref module sym) | |
335 | (let ((v (module-variable module sym))) | |
336 | (if v (variable-ref v) (error "badness!" (pk module) (pk sym))))) | |
12eae603 AW |
337 | (define (resolve-module . args) |
338 | #f) | |
3d5f3091 | 339 | |
6a952e0e AW |
340 | ;; Input hook to syncase -- so that we might be able to pass annotated |
341 | ;; expressions in. Currently disabled. Maybe we should just use | |
342 | ;; source-properties directly. | |
343 | (define (annotation? x) #f) | |
344 | ||
345 | ;; API provided by psyntax | |
e4721dde | 346 | (define syntax-violation #f) |
22225fc1 AW |
347 | (define datum->syntax #f) |
348 | (define syntax->datum #f) | |
22225fc1 AW |
349 | (define identifier? #f) |
350 | (define generate-temporaries #f) | |
13182603 | 351 | (define bound-identifier=? #f) |
13182603 | 352 | (define free-identifier=? #f) |
5a0132b3 | 353 | |
8a73a6d2 | 354 | ;; $sc-dispatch is an implementation detail of psyntax. It is used by |
6a952e0e | 355 | ;; expanded macros, to dispatch an input against a set of patterns. |
5a0132b3 AW |
356 | (define $sc-dispatch #f) |
357 | ||
6a952e0e | 358 | ;; Load it up! |
13182603 | 359 | (primitive-load-path "ice-9/psyntax-pp") |
4f692ace AW |
360 | ;; The binding for `macroexpand' has now been overridden, making psyntax the |
361 | ;; expander now. | |
79451588 | 362 | |
a1a482e0 AW |
363 | (define-syntax and |
364 | (syntax-rules () | |
365 | ((_) #t) | |
366 | ((_ x) x) | |
367 | ((_ x y ...) (if x (and y ...) #f)))) | |
368 | ||
369 | (define-syntax or | |
370 | (syntax-rules () | |
371 | ((_) #f) | |
372 | ((_ x) x) | |
373 | ((_ x y ...) (let ((t x)) (if t t (or y ...)))))) | |
374 | ||
dc1eed52 AW |
375 | ;; The "maybe-more" bits are something of a hack, so that we can support |
376 | ;; SRFI-61. Rewrites into a standalone syntax-case macro would be | |
377 | ;; appreciated. | |
a1a482e0 | 378 | (define-syntax cond |
dc1eed52 AW |
379 | (syntax-rules (=> else) |
380 | ((_ "maybe-more" test consequent) | |
381 | (if test consequent)) | |
382 | ||
383 | ((_ "maybe-more" test consequent clause ...) | |
384 | (if test consequent (cond clause ...))) | |
385 | ||
386 | ((_ (else else1 else2 ...)) | |
387 | (begin else1 else2 ...)) | |
388 | ||
389 | ((_ (test => receiver) more-clause ...) | |
390 | (let ((t test)) | |
391 | (cond "maybe-more" t (receiver t) more-clause ...))) | |
392 | ||
393 | ((_ (generator guard => receiver) more-clause ...) | |
394 | (call-with-values (lambda () generator) | |
395 | (lambda t | |
396 | (cond "maybe-more" | |
397 | (apply guard t) (apply receiver t) more-clause ...)))) | |
398 | ||
399 | ((_ (test => receiver ...) more-clause ...) | |
400 | (syntax-violation 'cond "wrong number of receiver expressions" | |
401 | '(test => receiver ...))) | |
402 | ((_ (generator guard => receiver ...) more-clause ...) | |
403 | (syntax-violation 'cond "wrong number of receiver expressions" | |
404 | '(generator guard => receiver ...))) | |
405 | ||
406 | ((_ (test) more-clause ...) | |
407 | (let ((t test)) | |
408 | (cond "maybe-more" t t more-clause ...))) | |
409 | ||
410 | ((_ (test body1 body2 ...) more-clause ...) | |
411 | (cond "maybe-more" | |
412 | test (begin body1 body2 ...) more-clause ...)))) | |
a1a482e0 AW |
413 | |
414 | (define-syntax case | |
415 | (syntax-rules (else) | |
416 | ((case (key ...) | |
417 | clauses ...) | |
418 | (let ((atom-key (key ...))) | |
419 | (case atom-key clauses ...))) | |
420 | ((case key | |
421 | (else result1 result2 ...)) | |
422 | (begin result1 result2 ...)) | |
423 | ((case key | |
424 | ((atoms ...) result1 result2 ...)) | |
425 | (if (memv key '(atoms ...)) | |
426 | (begin result1 result2 ...))) | |
427 | ((case key | |
428 | ((atoms ...) result1 result2 ...) | |
429 | clause clauses ...) | |
430 | (if (memv key '(atoms ...)) | |
431 | (begin result1 result2 ...) | |
432 | (case key clause clauses ...))))) | |
433 | ||
434 | (define-syntax do | |
435 | (syntax-rules () | |
436 | ((do ((var init step ...) ...) | |
437 | (test expr ...) | |
438 | command ...) | |
439 | (letrec | |
440 | ((loop | |
441 | (lambda (var ...) | |
442 | (if test | |
443 | (begin | |
444 | (if #f #f) | |
445 | expr ...) | |
446 | (begin | |
447 | command | |
448 | ... | |
449 | (loop (do "step" var step ...) | |
450 | ...)))))) | |
451 | (loop init ...))) | |
452 | ((do "step" x) | |
453 | x) | |
454 | ((do "step" x y) | |
455 | y))) | |
456 | ||
1eec95f8 AW |
457 | (define-syntax delay |
458 | (syntax-rules () | |
459 | ((_ exp) (make-promise (lambda () exp))))) | |
79451588 | 460 | |
cb65f76c AR |
461 | (include-from-path "ice-9/quasisyntax") |
462 | ||
79451588 | 463 | \f |
48fdec21 | 464 | |
3d2ada2f DH |
465 | ;;; {Defmacros} |
466 | ;;; | |
3d2ada2f | 467 | |
13182603 AW |
468 | (define-syntax define-macro |
469 | (lambda (x) | |
97ce9dbf | 470 | "Define a defmacro." |
13182603 | 471 | (syntax-case x () |
97ce9dbf | 472 | ((_ (macro . args) doc body1 body ...) |
a927454d AW |
473 | (string? (syntax->datum #'doc)) |
474 | #'(define-macro macro doc (lambda args body1 body ...))) | |
97ce9dbf | 475 | ((_ (macro . args) body ...) |
a927454d | 476 | #'(define-macro macro #f (lambda args body ...))) |
97ce9dbf | 477 | ((_ macro doc transformer) |
a927454d AW |
478 | (or (string? (syntax->datum #'doc)) |
479 | (not (syntax->datum #'doc))) | |
480 | #'(define-syntax macro | |
481 | (lambda (y) | |
482 | doc | |
a5e95abe AW |
483 | #((macro-type . defmacro) |
484 | (defmacro-args args)) | |
a927454d AW |
485 | (syntax-case y () |
486 | ((_ . args) | |
487 | (let ((v (syntax->datum #'args))) | |
488 | (datum->syntax y (apply transformer v))))))))))) | |
13182603 AW |
489 | |
490 | (define-syntax defmacro | |
491 | (lambda (x) | |
97ce9dbf | 492 | "Define a defmacro, with the old lispy defun syntax." |
13182603 | 493 | (syntax-case x () |
97ce9dbf | 494 | ((_ macro args doc body1 body ...) |
a927454d AW |
495 | (string? (syntax->datum #'doc)) |
496 | #'(define-macro macro doc (lambda args body1 body ...))) | |
97ce9dbf | 497 | ((_ macro args body ...) |
a927454d | 498 | #'(define-macro macro #f (lambda args body ...)))))) |
3d2ada2f DH |
499 | |
500 | (provide 'defmacro) | |
48fdec21 MV |
501 | |
502 | \f | |
503 | ||
3d2ada2f DH |
504 | ;;; {Deprecation} |
505 | ;;; | |
506 | ;;; Depends on: defmacro | |
507 | ;;; | |
508 | ||
509 | (defmacro begin-deprecated forms | |
510 | (if (include-deprecated-features) | |
1b68d041 | 511 | `(begin ,@forms) |
b1e93821 | 512 | `(begin))) |
0f2d19dd JB |
513 | |
514 | \f | |
3d2ada2f | 515 | |
79451588 | 516 | ;;; {Trivial Functions} |
0f2d19dd | 517 | ;;; |
79451588 | 518 | |
6b08d75b | 519 | (define (identity x) x) |
132e5fac | 520 | (define (and=> value procedure) (and value (procedure value))) |
e8ed460e | 521 | (define call/cc call-with-current-continuation) |
79451588 | 522 | |
5cd06d5e | 523 | ;;; apply-to-args is functionally redundant with apply and, worse, |
0f2d19dd JB |
524 | ;;; is less general than apply since it only takes two arguments. |
525 | ;;; | |
20edfbbd | 526 | ;;; On the other hand, apply-to-args is a syntacticly convenient way to |
0f2d19dd JB |
527 | ;;; perform binding in many circumstances when the "let" family of |
528 | ;;; of forms don't cut it. E.g.: | |
529 | ;;; | |
9b5a0d84 AW |
530 | ;;; (apply-to-args (return-3d-mouse-coords) |
531 | ;;; (lambda (x y z) | |
532 | ;;; ...)) | |
0f2d19dd JB |
533 | ;;; |
534 | ||
535 | (define (apply-to-args args fn) (apply fn args)) | |
536 | ||
3d2ada2f | 537 | (defmacro false-if-exception (expr) |
8f9b9683 AW |
538 | `(catch #t |
539 | (lambda () | |
540 | ;; avoid saving backtraces inside false-if-exception | |
eddd16d7 AW |
541 | (with-fluids ((the-last-stack (fluid-ref the-last-stack))) |
542 | ,expr)) | |
8f9b9683 | 543 | (lambda args #f))) |
3d2ada2f DH |
544 | |
545 | \f | |
546 | ||
547 | ;;; {General Properties} | |
548 | ;;; | |
549 | ||
550 | ;; This is a more modern interface to properties. It will replace all | |
551 | ;; other property-like things eventually. | |
552 | ||
553 | (define (make-object-property) | |
554 | (let ((prop (primitive-make-property #f))) | |
555 | (make-procedure-with-setter | |
556 | (lambda (obj) (primitive-property-ref prop obj)) | |
557 | (lambda (obj val) (primitive-property-set! prop obj val))))) | |
558 | ||
0f2d19dd | 559 | \f |
6b08d75b | 560 | |
0f2d19dd JB |
561 | ;;; {Symbol Properties} |
562 | ;;; | |
563 | ||
564 | (define (symbol-property sym prop) | |
565 | (let ((pair (assoc prop (symbol-pref sym)))) | |
566 | (and pair (cdr pair)))) | |
567 | ||
568 | (define (set-symbol-property! sym prop val) | |
569 | (let ((pair (assoc prop (symbol-pref sym)))) | |
570 | (if pair | |
9b5a0d84 AW |
571 | (set-cdr! pair val) |
572 | (symbol-pset! sym (acons prop val (symbol-pref sym)))))) | |
0f2d19dd JB |
573 | |
574 | (define (symbol-property-remove! sym prop) | |
575 | (let ((pair (assoc prop (symbol-pref sym)))) | |
576 | (if pair | |
9b5a0d84 | 577 | (symbol-pset! sym (delq! pair (symbol-pref sym)))))) |
0f2d19dd JB |
578 | |
579 | \f | |
1e531c3a | 580 | |
0f2d19dd JB |
581 | ;;; {Arrays} |
582 | ;;; | |
583 | ||
2042e178 MV |
584 | (define (array-shape a) |
585 | (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind)) | |
586 | (array-dimensions a))) | |
0f2d19dd JB |
587 | |
588 | \f | |
3d2ada2f | 589 | |
0f2d19dd JB |
590 | ;;; {Keywords} |
591 | ;;; | |
592 | ||
0f2d19dd JB |
593 | (define (kw-arg-ref args kw) |
594 | (let ((rem (member kw args))) | |
595 | (and rem (pair? (cdr rem)) (cadr rem)))) | |
596 | ||
597 | \f | |
fa7e9274 | 598 | |
9f9aa47b | 599 | ;;; {Structs} |
3d2ada2f | 600 | ;;; |
fa7e9274 MV |
601 | |
602 | (define (struct-layout s) | |
9f9aa47b | 603 | (struct-ref (struct-vtable s) vtable-index-layout)) |
fa7e9274 MV |
604 | |
605 | \f | |
d7faeb2e | 606 | |
0f2d19dd JB |
607 | ;;; {Records} |
608 | ;;; | |
609 | ||
fa7e9274 MV |
610 | ;; Printing records: by default, records are printed as |
611 | ;; | |
612 | ;; #<type-name field1: val1 field2: val2 ...> | |
613 | ;; | |
614 | ;; You can change that by giving a custom printing function to | |
615 | ;; MAKE-RECORD-TYPE (after the list of field symbols). This function | |
616 | ;; will be called like | |
617 | ;; | |
618 | ;; (<printer> object port) | |
619 | ;; | |
620 | ;; It should print OBJECT to PORT. | |
621 | ||
cf8f1a90 | 622 | (define (inherit-print-state old-port new-port) |
8a30733e MD |
623 | (if (get-print-state old-port) |
624 | (port-with-print-state new-port (get-print-state old-port)) | |
cf8f1a90 MV |
625 | new-port)) |
626 | ||
e31f22eb | 627 | ;; 0: type-name, 1: fields, 2: constructor |
20edfbbd | 628 | (define record-type-vtable |
e31f22eb AW |
629 | ;; FIXME: This should just call make-vtable, not make-vtable-vtable; but for |
630 | ;; that we need to expose the bare vtable-vtable to Scheme. | |
631 | (make-vtable-vtable "prprpw" 0 | |
9b5a0d84 AW |
632 | (lambda (s p) |
633 | (cond ((eq? s record-type-vtable) | |
634 | (display "#<record-type-vtable>" p)) | |
635 | (else | |
636 | (display "#<record-type " p) | |
637 | (display (record-type-name s) p) | |
638 | (display ">" p)))))) | |
0f2d19dd JB |
639 | |
640 | (define (record-type? obj) | |
641 | (and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) | |
642 | ||
b2669c41 | 643 | (define* (make-record-type type-name fields #:optional printer) |
31ac29b6 | 644 | ;; Pre-generate constructors for nfields < 20. |
e31f22eb AW |
645 | (define-syntax make-constructor |
646 | (lambda (x) | |
647 | (define *max-static-argument-count* 20) | |
648 | (define (make-formals n) | |
649 | (let lp ((i 0)) | |
650 | (if (< i n) | |
651 | (cons (datum->syntax | |
652 | x | |
653 | (string->symbol | |
654 | (string (integer->char (+ (char->integer #\a) i))))) | |
655 | (lp (1+ i))) | |
656 | '()))) | |
657 | (syntax-case x () | |
658 | ((_ rtd exp) (not (identifier? #'exp)) | |
659 | #'(let ((n exp)) | |
660 | (make-constructor rtd n))) | |
661 | ((_ rtd nfields) | |
662 | #`(case nfields | |
663 | #,@(let lp ((n 0)) | |
664 | (if (< n *max-static-argument-count*) | |
665 | (cons (with-syntax (((formal ...) (make-formals n)) | |
666 | (n n)) | |
667 | #'((n) | |
668 | (lambda (formal ...) | |
669 | (make-struct rtd 0 formal ...)))) | |
670 | (lp (1+ n))) | |
671 | '())) | |
672 | (else | |
673 | (lambda args | |
674 | (if (= (length args) nfields) | |
675 | (apply make-struct rtd 0 args) | |
676 | (scm-error 'wrong-number-of-args | |
677 | (format #f "make-~a" type-name) | |
678 | "Wrong number of arguments" '() #f))))))))) | |
679 | ||
51797cec AW |
680 | (define (default-record-printer s p) |
681 | (display "#<" p) | |
682 | (display (record-type-name (record-type-descriptor s)) p) | |
683 | (let loop ((fields (record-type-fields (record-type-descriptor s))) | |
684 | (off 0)) | |
685 | (cond | |
686 | ((not (null? fields)) | |
687 | (display " " p) | |
688 | (display (car fields) p) | |
689 | (display ": " p) | |
690 | (display (struct-ref s off) p) | |
691 | (loop (cdr fields) (+ 1 off))))) | |
692 | (display ">" p)) | |
693 | ||
e31f22eb AW |
694 | (let ((rtd (make-struct record-type-vtable 0 |
695 | (make-struct-layout | |
696 | (apply string-append | |
697 | (map (lambda (f) "pw") fields))) | |
b2669c41 | 698 | (or printer default-record-printer) |
e31f22eb AW |
699 | type-name |
700 | (copy-tree fields)))) | |
701 | (struct-set! rtd (+ vtable-offset-user 2) | |
702 | (make-constructor rtd (length fields))) | |
51797cec AW |
703 | ;; Temporary solution: Associate a name to the record type descriptor |
704 | ;; so that the object system can create a wrapper class for it. | |
e31f22eb AW |
705 | (set-struct-vtable-name! rtd (if (symbol? type-name) |
706 | type-name | |
707 | (string->symbol type-name))) | |
708 | rtd)) | |
0f2d19dd JB |
709 | |
710 | (define (record-type-name obj) | |
711 | (if (record-type? obj) | |
9f9aa47b | 712 | (struct-ref obj vtable-offset-user) |
0f2d19dd JB |
713 | (error 'not-a-record-type obj))) |
714 | ||
715 | (define (record-type-fields obj) | |
716 | (if (record-type? obj) | |
9f9aa47b | 717 | (struct-ref obj (+ 1 vtable-offset-user)) |
0f2d19dd JB |
718 | (error 'not-a-record-type obj))) |
719 | ||
d44a0d12 AW |
720 | (define* (record-constructor rtd #:optional field-names) |
721 | (if (not field-names) | |
e31f22eb | 722 | (struct-ref rtd (+ 2 vtable-offset-user)) |
d44a0d12 AW |
723 | (primitive-eval |
724 | `(lambda ,field-names | |
725 | (make-struct ',rtd 0 ,@(map (lambda (f) | |
726 | (if (memq f field-names) | |
727 | f | |
728 | #f)) | |
729 | (record-type-fields rtd))))))) | |
3bf27608 | 730 | |
0f2d19dd JB |
731 | (define (record-predicate rtd) |
732 | (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj))))) | |
733 | ||
3ba9acb1 | 734 | (define (%record-type-error rtd obj) ;; private helper |
afc4ccd4 KR |
735 | (or (eq? rtd (record-type-descriptor obj)) |
736 | (scm-error 'wrong-type-arg "%record-type-check" | |
9b5a0d84 AW |
737 | "Wrong type record (want `~S'): ~S" |
738 | (list (record-type-name rtd) obj) | |
739 | #f))) | |
afc4ccd4 | 740 | |
0f2d19dd | 741 | (define (record-accessor rtd field-name) |
3bf27608 | 742 | (let ((pos (list-index (record-type-fields rtd) field-name))) |
0f2d19dd | 743 | (if (not pos) |
9b5a0d84 | 744 | (error 'no-such-field field-name)) |
3bf27608 AW |
745 | (lambda (obj) |
746 | (if (eq? (struct-vtable obj) rtd) | |
747 | (struct-ref obj pos) | |
748 | (%record-type-error rtd obj))))) | |
0f2d19dd JB |
749 | |
750 | (define (record-modifier rtd field-name) | |
3bf27608 | 751 | (let ((pos (list-index (record-type-fields rtd) field-name))) |
0f2d19dd | 752 | (if (not pos) |
9b5a0d84 | 753 | (error 'no-such-field field-name)) |
3bf27608 AW |
754 | (lambda (obj val) |
755 | (if (eq? (struct-vtable obj) rtd) | |
756 | (struct-set! obj pos val) | |
757 | (%record-type-error rtd obj))))) | |
0f2d19dd JB |
758 | |
759 | (define (record? obj) | |
760 | (and (struct? obj) (record-type? (struct-vtable obj)))) | |
761 | ||
762 | (define (record-type-descriptor obj) | |
763 | (if (struct? obj) | |
764 | (struct-vtable obj) | |
765 | (error 'not-a-record obj))) | |
766 | ||
21ed9efe MD |
767 | (provide 'record) |
768 | ||
0f2d19dd | 769 | \f |
3d2ada2f | 770 | |
0f2d19dd JB |
771 | ;;; {Booleans} |
772 | ;;; | |
773 | ||
774 | (define (->bool x) (not (not x))) | |
775 | ||
776 | \f | |
3d2ada2f | 777 | |
0f2d19dd JB |
778 | ;;; {Symbols} |
779 | ;;; | |
780 | ||
781 | (define (symbol-append . args) | |
06f0414c | 782 | (string->symbol (apply string-append (map symbol->string args)))) |
0f2d19dd JB |
783 | |
784 | (define (list->symbol . args) | |
785 | (string->symbol (apply list->string args))) | |
786 | ||
787 | (define (symbol . args) | |
788 | (string->symbol (apply string args))) | |
789 | ||
0f2d19dd | 790 | \f |
3d2ada2f | 791 | |
0f2d19dd JB |
792 | ;;; {Lists} |
793 | ;;; | |
794 | ||
795 | (define (list-index l k) | |
796 | (let loop ((n 0) | |
9b5a0d84 | 797 | (l l)) |
0f2d19dd | 798 | (and (not (null? l)) |
9b5a0d84 AW |
799 | (if (eq? (car l) k) |
800 | n | |
801 | (loop (+ n 1) (cdr l)))))) | |
0f2d19dd | 802 | |
1729d8ff | 803 | \f |
3d2ada2f | 804 | |
52cfc69b | 805 | (if (provided? 'posix) |
1e6ebf54 | 806 | (primitive-load-path "ice-9/posix")) |
6fa8995c | 807 | |
52cfc69b | 808 | (if (provided? 'socket) |
1e6ebf54 | 809 | (primitive-load-path "ice-9/networking")) |
3afb28ce | 810 | |
f3197274 | 811 | ;; For reference, Emacs file-exists-p uses stat in this same way. |
6fa8995c | 812 | (define file-exists? |
52cfc69b | 813 | (if (provided? 'posix) |
6fa8995c | 814 | (lambda (str) |
9b5a0d84 | 815 | (->bool (stat str #f))) |
6fa8995c | 816 | (lambda (str) |
9b5a0d84 AW |
817 | (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ)) |
818 | (lambda args #f)))) | |
819 | (if port (begin (close-port port) #t) | |
820 | #f))))) | |
6fa8995c GH |
821 | |
822 | (define file-is-directory? | |
52cfc69b | 823 | (if (provided? 'posix) |
6fa8995c | 824 | (lambda (str) |
9b5a0d84 | 825 | (eq? (stat:type (stat str)) 'directory)) |
6fa8995c | 826 | (lambda (str) |
9b5a0d84 AW |
827 | (let ((port (catch 'system-error |
828 | (lambda () (open-file (string-append str "/.") | |
829 | OPEN_READ)) | |
830 | (lambda args #f)))) | |
831 | (if port (begin (close-port port) #t) | |
832 | #f))))) | |
0f2d19dd JB |
833 | |
834 | (define (has-suffix? str suffix) | |
99f20fb6 | 835 | (string-suffix? suffix str)) |
0f2d19dd | 836 | |
019ac1c9 MV |
837 | (define (system-error-errno args) |
838 | (if (eq? (car args) 'system-error) | |
839 | (car (list-ref args 4)) | |
840 | #f)) | |
841 | ||
0f2d19dd | 842 | \f |
3d2ada2f | 843 | |
0f2d19dd JB |
844 | ;;; {Error Handling} |
845 | ;;; | |
846 | ||
0f2d19dd | 847 | (define (error . args) |
21ed9efe | 848 | (save-stack) |
2194b6f0 | 849 | (if (null? args) |
5552355a | 850 | (scm-error 'misc-error #f "?" #f #f) |
8641dd9e | 851 | (let loop ((msg "~A") |
9b5a0d84 AW |
852 | (rest (cdr args))) |
853 | (if (not (null? rest)) | |
854 | (loop (string-append msg " ~S") | |
855 | (cdr rest)) | |
856 | (scm-error 'misc-error #f msg args #f))))) | |
be2d2c70 | 857 | |
1349bd53 | 858 | ;; bad-throw is the hook that is called upon a throw to a an unhandled |
9a0d70e2 GH |
859 | ;; key (unless the throw has four arguments, in which case |
860 | ;; it's usually interpreted as an error throw.) | |
861 | ;; If the key has a default handler (a throw-handler-default property), | |
0f2d19dd JB |
862 | ;; it is applied to the throw. |
863 | ;; | |
1349bd53 | 864 | (define (bad-throw key . args) |
0f2d19dd JB |
865 | (let ((default (symbol-property key 'throw-handler-default))) |
866 | (or (and default (apply default key args)) | |
9b5a0d84 | 867 | (apply error "unhandled-exception:" key args)))) |
0f2d19dd | 868 | |
0f2d19dd | 869 | \f |
bce074ee | 870 | |
708bf0f3 GH |
871 | (define (tm:sec obj) (vector-ref obj 0)) |
872 | (define (tm:min obj) (vector-ref obj 1)) | |
873 | (define (tm:hour obj) (vector-ref obj 2)) | |
874 | (define (tm:mday obj) (vector-ref obj 3)) | |
875 | (define (tm:mon obj) (vector-ref obj 4)) | |
876 | (define (tm:year obj) (vector-ref obj 5)) | |
877 | (define (tm:wday obj) (vector-ref obj 6)) | |
878 | (define (tm:yday obj) (vector-ref obj 7)) | |
879 | (define (tm:isdst obj) (vector-ref obj 8)) | |
880 | (define (tm:gmtoff obj) (vector-ref obj 9)) | |
881 | (define (tm:zone obj) (vector-ref obj 10)) | |
882 | ||
883 | (define (set-tm:sec obj val) (vector-set! obj 0 val)) | |
884 | (define (set-tm:min obj val) (vector-set! obj 1 val)) | |
885 | (define (set-tm:hour obj val) (vector-set! obj 2 val)) | |
886 | (define (set-tm:mday obj val) (vector-set! obj 3 val)) | |
887 | (define (set-tm:mon obj val) (vector-set! obj 4 val)) | |
888 | (define (set-tm:year obj val) (vector-set! obj 5 val)) | |
889 | (define (set-tm:wday obj val) (vector-set! obj 6 val)) | |
890 | (define (set-tm:yday obj val) (vector-set! obj 7 val)) | |
891 | (define (set-tm:isdst obj val) (vector-set! obj 8 val)) | |
892 | (define (set-tm:gmtoff obj val) (vector-set! obj 9 val)) | |
893 | (define (set-tm:zone obj val) (vector-set! obj 10 val)) | |
894 | ||
6afcd3b2 GH |
895 | (define (tms:clock obj) (vector-ref obj 0)) |
896 | (define (tms:utime obj) (vector-ref obj 1)) | |
897 | (define (tms:stime obj) (vector-ref obj 2)) | |
898 | (define (tms:cutime obj) (vector-ref obj 3)) | |
899 | (define (tms:cstime obj) (vector-ref obj 4)) | |
900 | ||
1334c61a | 901 | (define file-position ftell) |
52c9a338 AW |
902 | (define* (file-set-position port offset #:optional (whence SEEK_SET)) |
903 | (seek port offset whence)) | |
8b13c6b3 | 904 | |
e38303a2 GH |
905 | (define (move->fdes fd/port fd) |
906 | (cond ((integer? fd/port) | |
9b5a0d84 AW |
907 | (dup->fdes fd/port fd) |
908 | (close fd/port) | |
909 | fd) | |
910 | (else | |
911 | (primitive-move->fdes fd/port fd) | |
912 | (set-port-revealed! fd/port 1) | |
913 | fd/port))) | |
8b13c6b3 GH |
914 | |
915 | (define (release-port-handle port) | |
916 | (let ((revealed (port-revealed port))) | |
917 | (if (> revealed 0) | |
9b5a0d84 | 918 | (set-port-revealed! port (- revealed 1))))) |
0f2d19dd | 919 | |
02851b26 AW |
920 | (define dup->port |
921 | (case-lambda | |
922 | ((port/fd mode) | |
923 | (fdopen (dup->fdes port/fd) mode)) | |
924 | ((port/fd mode new-fd) | |
925 | (let ((port (fdopen (dup->fdes port/fd new-fd) mode))) | |
926 | (set-port-revealed! port 1) | |
927 | port)))) | |
928 | ||
929 | (define dup->inport | |
930 | (case-lambda | |
931 | ((port/fd) | |
932 | (dup->port port/fd "r")) | |
933 | ((port/fd new-fd) | |
934 | (dup->port port/fd "r" new-fd)))) | |
935 | ||
936 | (define dup->outport | |
937 | (case-lambda | |
938 | ((port/fd) | |
939 | (dup->port port/fd "w")) | |
940 | ((port/fd new-fd) | |
941 | (dup->port port/fd "w" new-fd)))) | |
942 | ||
943 | (define dup | |
944 | (case-lambda | |
945 | ((port/fd) | |
946 | (if (integer? port/fd) | |
947 | (dup->fdes port/fd) | |
948 | (dup->port port/fd (port-mode port/fd)))) | |
949 | ((port/fd new-fd) | |
950 | (if (integer? port/fd) | |
951 | (dup->fdes port/fd new-fd) | |
952 | (dup->port port/fd (port-mode port/fd) new-fd))))) | |
e38303a2 GH |
953 | |
954 | (define (duplicate-port port modes) | |
955 | (dup->port port modes)) | |
956 | ||
957 | (define (fdes->inport fdes) | |
958 | (let loop ((rest-ports (fdes->ports fdes))) | |
959 | (cond ((null? rest-ports) | |
9b5a0d84 AW |
960 | (let ((result (fdopen fdes "r"))) |
961 | (set-port-revealed! result 1) | |
962 | result)) | |
963 | ((input-port? (car rest-ports)) | |
964 | (set-port-revealed! (car rest-ports) | |
965 | (+ (port-revealed (car rest-ports)) 1)) | |
966 | (car rest-ports)) | |
967 | (else | |
968 | (loop (cdr rest-ports)))))) | |
e38303a2 GH |
969 | |
970 | (define (fdes->outport fdes) | |
971 | (let loop ((rest-ports (fdes->ports fdes))) | |
972 | (cond ((null? rest-ports) | |
9b5a0d84 AW |
973 | (let ((result (fdopen fdes "w"))) |
974 | (set-port-revealed! result 1) | |
975 | result)) | |
976 | ((output-port? (car rest-ports)) | |
977 | (set-port-revealed! (car rest-ports) | |
978 | (+ (port-revealed (car rest-ports)) 1)) | |
979 | (car rest-ports)) | |
980 | (else | |
981 | (loop (cdr rest-ports)))))) | |
e38303a2 GH |
982 | |
983 | (define (port->fdes port) | |
984 | (set-port-revealed! port (+ (port-revealed port) 1)) | |
985 | (fileno port)) | |
986 | ||
956055a9 GH |
987 | (define (setenv name value) |
988 | (if value | |
989 | (putenv (string-append name "=" value)) | |
990 | (putenv name))) | |
991 | ||
5c1254da MV |
992 | (define (unsetenv name) |
993 | "Remove the entry for NAME from the environment." | |
994 | (putenv name)) | |
995 | ||
0f2d19dd | 996 | \f |
3d2ada2f | 997 | |
0f2d19dd JB |
998 | ;;; {Load Paths} |
999 | ;;; | |
1000 | ||
0f2d19dd JB |
1001 | ;;; Here for backward compatability |
1002 | ;; | |
1003 | (define scheme-file-suffix (lambda () ".scm")) | |
1004 | ||
3cab8392 JB |
1005 | (define (in-vicinity vicinity file) |
1006 | (let ((tail (let ((len (string-length vicinity))) | |
9b5a0d84 AW |
1007 | (if (zero? len) |
1008 | #f | |
1009 | (string-ref vicinity (- len 1)))))) | |
3cab8392 | 1010 | (string-append vicinity |
9b5a0d84 AW |
1011 | (if (or (not tail) |
1012 | (eq? tail #\/)) | |
1013 | "" | |
1014 | "/") | |
1015 | file))) | |
02ceadb8 | 1016 | |
0f2d19dd | 1017 | \f |
3d2ada2f | 1018 | |
ef00e7f4 | 1019 | ;;; {Help for scm_shell} |
3d2ada2f | 1020 | ;;; |
ef00e7f4 JB |
1021 | ;;; The argument-processing code used by Guile-based shells generates |
1022 | ;;; Scheme code based on the argument list. This page contains help | |
1023 | ;;; functions for the code it generates. | |
3d2ada2f | 1024 | ;;; |
ef00e7f4 | 1025 | |
ef00e7f4 JB |
1026 | (define (command-line) (program-arguments)) |
1027 | ||
5aa7fe69 JB |
1028 | ;; This is mostly for the internal use of the code generated by |
1029 | ;; scm_compile_shell_switches. | |
eef6519b MV |
1030 | |
1031 | (define (turn-on-debugging) | |
1032 | (debug-enable 'debug) | |
1033 | (debug-enable 'backtrace) | |
1034 | (read-enable 'positions)) | |
4eecfeb7 | 1035 | |
ef00e7f4 | 1036 | (define (load-user-init) |
1f08acd9 | 1037 | (let* ((home (or (getenv "HOME") |
9b5a0d84 AW |
1038 | (false-if-exception (passwd:dir (getpwuid (getuid)))) |
1039 | "/")) ;; fallback for cygwin etc. | |
1040 | (init-file (in-vicinity home ".guile"))) | |
1f08acd9 | 1041 | (if (file-exists? init-file) |
9b5a0d84 | 1042 | (primitive-load init-file)))) |
ef00e7f4 JB |
1043 | |
1044 | \f | |
3d2ada2f | 1045 | |
107139ea AW |
1046 | ;;; {The interpreter stack} |
1047 | ;;; | |
1048 | ||
06dcb9df | 1049 | ;; %stacks defined in stacks.c |
a6cd3555 | 1050 | (define (%start-stack tag thunk) |
8fc43b12 AW |
1051 | (let ((prompt-tag (make-prompt-tag "start-stack"))) |
1052 | (call-with-prompt | |
1053 | prompt-tag | |
1054 | (lambda () | |
1055 | (with-fluids ((%stacks (acons tag prompt-tag | |
1056 | (or (fluid-ref %stacks) '())))) | |
1057 | (thunk))) | |
1058 | (lambda (k . args) | |
a6cd3555 AW |
1059 | (%start-stack tag (lambda () (apply k args))))))) |
1060 | (define-syntax start-stack | |
1061 | (syntax-rules () | |
1062 | ((_ tag exp) | |
1063 | (%start-stack tag (lambda () exp))))) | |
107139ea AW |
1064 | |
1065 | \f | |
1066 | ||
a06181a2 | 1067 | ;;; {Loading by paths} |
3d2ada2f | 1068 | ;;; |
a06181a2 JB |
1069 | |
1070 | ;;; Load a Scheme source file named NAME, searching for it in the | |
1071 | ;;; directories listed in %load-path, and applying each of the file | |
1072 | ;;; name extensions listed in %load-extensions. | |
1073 | (define (load-from-path name) | |
1074 | (start-stack 'load-stack | |
9b5a0d84 | 1075 | (primitive-load-path name))) |
0f2d19dd | 1076 | |
85e95b47 AW |
1077 | (define %load-verbosely #f) |
1078 | (define (assert-load-verbosity v) (set! %load-verbosely v)) | |
1079 | ||
1080 | (define (%load-announce file) | |
1081 | (if %load-verbosely | |
1082 | (with-output-to-port (current-error-port) | |
9b5a0d84 AW |
1083 | (lambda () |
1084 | (display ";;; ") | |
1085 | (display "loading ") | |
1086 | (display file) | |
1087 | (newline) | |
1088 | (force-output))))) | |
85e95b47 AW |
1089 | |
1090 | (set! %load-hook %load-announce) | |
1091 | ||
d6e70467 | 1092 | (define* (load name #:optional reader) |
1ab3976e AW |
1093 | ;; Returns the .go file corresponding to `name'. Does not search load |
1094 | ;; paths, only the fallback path. If the .go file is missing or out of | |
1095 | ;; date, and autocompilation is enabled, will try autocompilation, just | |
1096 | ;; as primitive-load-path does internally. primitive-load is | |
1097 | ;; unaffected. Returns #f if autocompilation failed or was disabled. | |
04af4c4c AW |
1098 | ;; |
1099 | ;; NB: Unless we need to compile the file, this function should not cause | |
1100 | ;; (system base compile) to be loaded up. For that reason compiled-file-name | |
1101 | ;; partially duplicates functionality from (system base compile). | |
1102 | (define (compiled-file-name canon-path) | |
1103 | (and %compile-fallback-path | |
1104 | (string-append | |
1105 | %compile-fallback-path | |
1106 | ;; no need for '/' separator here, canon-path is absolute | |
1107 | canon-path | |
1108 | (cond ((or (null? %load-compiled-extensions) | |
1109 | (string-null? (car %load-compiled-extensions))) | |
1110 | (warn "invalid %load-compiled-extensions" | |
1111 | %load-compiled-extensions) | |
1112 | ".go") | |
1113 | (else (car %load-compiled-extensions)))))) | |
1114 | (define (fresh-compiled-file-name go-path) | |
1ab3976e AW |
1115 | (catch #t |
1116 | (lambda () | |
04af4c4c AW |
1117 | (let* ((scmstat (stat name)) |
1118 | (gostat (stat go-path #f))) | |
1ab3976e | 1119 | (if (and gostat (= (stat:mtime gostat) (stat:mtime scmstat))) |
04af4c4c | 1120 | go-path |
1ab3976e AW |
1121 | (begin |
1122 | (if gostat | |
1123 | (format (current-error-port) | |
1124 | ";;; note: source file ~a\n;;; newer than compiled ~a\n" | |
04af4c4c | 1125 | name go-path)) |
1ab3976e AW |
1126 | (cond |
1127 | (%load-should-autocompile | |
1128 | (%warn-autocompilation-enabled) | |
1129 | (format (current-error-port) ";;; compiling ~a\n" name) | |
1130 | (let ((cfn ((@ (system base compile) compile-file) name | |
1131 | #:env (current-module)))) | |
1132 | (format (current-error-port) ";;; compiled ~a\n" cfn) | |
1133 | cfn)) | |
1134 | (else #f)))))) | |
1135 | (lambda (k . args) | |
1136 | (format (current-error-port) | |
1137 | ";;; WARNING: compilation of ~a failed:\n;;; key ~a, throw_args ~s\n" | |
1138 | name k args) | |
1139 | #f))) | |
d6e70467 | 1140 | (with-fluids ((current-reader reader)) |
eddd16d7 AW |
1141 | (let ((cfn (and=> (and=> (false-if-exception (canonicalize-path name)) |
1142 | compiled-file-name) | |
1143 | fresh-compiled-file-name))) | |
1144 | (if cfn | |
1145 | (load-compiled cfn) | |
1146 | (start-stack 'load-stack | |
1147 | (primitive-load name)))))) | |
5552355a | 1148 | |
0f2d19dd | 1149 | \f |
3d2ada2f | 1150 | |
0f2d19dd JB |
1151 | ;;; {Reader Extensions} |
1152 | ;;; | |
0f2d19dd JB |
1153 | ;;; Reader code for various "#c" forms. |
1154 | ;;; | |
1155 | ||
600c9584 RB |
1156 | (define read-eval? (make-fluid)) |
1157 | (fluid-set! read-eval? #f) | |
1158 | (read-hash-extend #\. | |
1159 | (lambda (c port) | |
1160 | (if (fluid-ref read-eval?) | |
1161 | (eval (read port) (interaction-environment)) | |
1162 | (error | |
71335c0d | 1163 | "#. read expansion found and read-eval? is #f.")))) |
75a97b92 | 1164 | |
0f2d19dd | 1165 | \f |
3d2ada2f | 1166 | |
0f2d19dd JB |
1167 | ;;; {Command Line Options} |
1168 | ;;; | |
1169 | ||
1170 | (define (get-option argv kw-opts kw-args return) | |
1171 | (cond | |
1172 | ((null? argv) | |
1173 | (return #f #f argv)) | |
1174 | ||
1175 | ((or (not (eq? #\- (string-ref (car argv) 0))) | |
9b5a0d84 | 1176 | (eq? (string-length (car argv)) 1)) |
0f2d19dd JB |
1177 | (return 'normal-arg (car argv) (cdr argv))) |
1178 | ||
1179 | ((eq? #\- (string-ref (car argv) 1)) | |
1180 | (let* ((kw-arg-pos (or (string-index (car argv) #\=) | |
9b5a0d84 AW |
1181 | (string-length (car argv)))) |
1182 | (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos))) | |
1183 | (kw-opt? (member kw kw-opts)) | |
1184 | (kw-arg? (member kw kw-args)) | |
1185 | (arg (or (and (not (eq? kw-arg-pos (string-length (car argv)))) | |
1186 | (substring (car argv) | |
1187 | (+ kw-arg-pos 1) | |
1188 | (string-length (car argv)))) | |
1189 | (and kw-arg? | |
1190 | (begin (set! argv (cdr argv)) (car argv)))))) | |
0f2d19dd | 1191 | (if (or kw-opt? kw-arg?) |
9b5a0d84 AW |
1192 | (return kw arg (cdr argv)) |
1193 | (return 'usage-error kw (cdr argv))))) | |
0f2d19dd JB |
1194 | |
1195 | (else | |
1196 | (let* ((char (substring (car argv) 1 2)) | |
9b5a0d84 | 1197 | (kw (symbol->keyword char))) |
0f2d19dd JB |
1198 | (cond |
1199 | ||
1200 | ((member kw kw-opts) | |
9b5a0d84 AW |
1201 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) |
1202 | (new-argv (if (= 0 (string-length rest-car)) | |
1203 | (cdr argv) | |
1204 | (cons (string-append "-" rest-car) (cdr argv))))) | |
1205 | (return kw #f new-argv))) | |
0f2d19dd JB |
1206 | |
1207 | ((member kw kw-args) | |
9b5a0d84 AW |
1208 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) |
1209 | (arg (if (= 0 (string-length rest-car)) | |
1210 | (cadr argv) | |
1211 | rest-car)) | |
1212 | (new-argv (if (= 0 (string-length rest-car)) | |
1213 | (cddr argv) | |
1214 | (cdr argv)))) | |
1215 | (return kw arg new-argv))) | |
0f2d19dd JB |
1216 | |
1217 | (else (return 'usage-error kw argv))))))) | |
1218 | ||
1219 | (define (for-next-option proc argv kw-opts kw-args) | |
1220 | (let loop ((argv argv)) | |
1221 | (get-option argv kw-opts kw-args | |
9b5a0d84 AW |
1222 | (lambda (opt opt-arg argv) |
1223 | (and opt (proc opt opt-arg argv loop)))))) | |
0f2d19dd JB |
1224 | |
1225 | (define (display-usage-report kw-desc) | |
1226 | (for-each | |
1227 | (lambda (kw) | |
1228 | (or (eq? (car kw) #t) | |
9b5a0d84 AW |
1229 | (eq? (car kw) 'else) |
1230 | (let* ((opt-desc kw) | |
1231 | (help (cadr opt-desc)) | |
1232 | (opts (car opt-desc)) | |
1233 | (opts-proper (if (string? (car opts)) (cdr opts) opts)) | |
1234 | (arg-name (if (string? (car opts)) | |
1235 | (string-append "<" (car opts) ">") | |
1236 | "")) | |
1237 | (left-part (string-append | |
1238 | (with-output-to-string | |
1239 | (lambda () | |
1240 | (map (lambda (x) (display (keyword->symbol x)) (display " ")) | |
1241 | opts-proper))) | |
1242 | arg-name)) | |
1243 | (middle-part (if (and (< (string-length left-part) 30) | |
1244 | (< (string-length help) 40)) | |
1245 | (make-string (- 30 (string-length left-part)) #\ ) | |
1246 | "\n\t"))) | |
1247 | (display left-part) | |
1248 | (display middle-part) | |
1249 | (display help) | |
1250 | (newline)))) | |
0f2d19dd | 1251 | kw-desc)) |
0f2d19dd | 1252 | |
20edfbbd TTN |
1253 | |
1254 | ||
0f2d19dd JB |
1255 | (define (transform-usage-lambda cases) |
1256 | (let* ((raw-usage (delq! 'else (map car cases))) | |
9b5a0d84 AW |
1257 | (usage-sans-specials (map (lambda (x) |
1258 | (or (and (not (list? x)) x) | |
1259 | (and (symbol? (car x)) #t) | |
1260 | (and (boolean? (car x)) #t) | |
1261 | x)) | |
1262 | raw-usage)) | |
1263 | (usage-desc (delq! #t usage-sans-specials)) | |
1264 | (kw-desc (map car usage-desc)) | |
1265 | (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc))) | |
1266 | (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc))) | |
1267 | (transmogrified-cases (map (lambda (case) | |
1268 | (cons (let ((opts (car case))) | |
1269 | (if (or (boolean? opts) (eq? 'else opts)) | |
1270 | opts | |
1271 | (cond | |
1272 | ((symbol? (car opts)) opts) | |
1273 | ((boolean? (car opts)) opts) | |
1274 | ((string? (caar opts)) (cdar opts)) | |
1275 | (else (car opts))))) | |
1276 | (cdr case))) | |
1277 | cases))) | |
0f2d19dd JB |
1278 | `(let ((%display-usage (lambda () (display-usage-report ',usage-desc)))) |
1279 | (lambda (%argv) | |
9b5a0d84 AW |
1280 | (let %next-arg ((%argv %argv)) |
1281 | (get-option %argv | |
1282 | ',kw-opts | |
1283 | ',kw-args | |
1284 | (lambda (%opt %arg %new-argv) | |
1285 | (case %opt | |
1286 | ,@ transmogrified-cases)))))))) | |
0f2d19dd JB |
1287 | |
1288 | ||
1289 | \f | |
1290 | ||
1291 | ;;; {Low Level Modules} | |
1292 | ;;; | |
1293 | ;;; These are the low level data structures for modules. | |
1294 | ;;; | |
37f5dfe5 DH |
1295 | ;;; Every module object is of the type 'module-type', which is a record |
1296 | ;;; consisting of the following members: | |
1297 | ;;; | |
1298 | ;;; - eval-closure: the function that defines for its module the strategy that | |
1299 | ;;; shall be followed when looking up symbols in the module. | |
1300 | ;;; | |
1301 | ;;; An eval-closure is a function taking two arguments: the symbol to be | |
1302 | ;;; looked up and a boolean value telling whether a binding for the symbol | |
1303 | ;;; should be created if it does not exist yet. If the symbol lookup | |
1304 | ;;; succeeded (either because an existing binding was found or because a new | |
1305 | ;;; binding was created), a variable object representing the binding is | |
1306 | ;;; returned. Otherwise, the value #f is returned. Note that the eval | |
1307 | ;;; closure does not take the module to be searched as an argument: During | |
1308 | ;;; construction of the eval-closure, the eval-closure has to store the | |
1309 | ;;; module it belongs to in its environment. This means, that any | |
1310 | ;;; eval-closure can belong to only one module. | |
1311 | ;;; | |
1312 | ;;; The eval-closure of a module can be defined arbitrarily. However, three | |
1313 | ;;; special cases of eval-closures are to be distinguished: During startup | |
1314 | ;;; the module system is not yet activated. In this phase, no modules are | |
1315 | ;;; defined and all bindings are automatically stored by the system in the | |
1316 | ;;; pre-modules-obarray. Since no eval-closures exist at this time, the | |
1317 | ;;; functions which require an eval-closure as their argument need to be | |
1318 | ;;; passed the value #f. | |
1319 | ;;; | |
1320 | ;;; The other two special cases of eval-closures are the | |
1321 | ;;; standard-eval-closure and the standard-interface-eval-closure. Both | |
1322 | ;;; behave equally for the case that no new binding is to be created. The | |
1323 | ;;; difference between the two comes in, when the boolean argument to the | |
1324 | ;;; eval-closure indicates that a new binding shall be created if it is not | |
1325 | ;;; found. | |
1326 | ;;; | |
1327 | ;;; Given that no new binding shall be created, both standard eval-closures | |
1328 | ;;; define the following standard strategy of searching bindings in the | |
1329 | ;;; module: First, the module's obarray is searched for the symbol. Second, | |
1330 | ;;; if no binding for the symbol was found in the module's obarray, the | |
1331 | ;;; module's binder procedure is exececuted. If this procedure did not | |
1332 | ;;; return a binding for the symbol, the modules referenced in the module's | |
1333 | ;;; uses list are recursively searched for a binding of the symbol. If the | |
1334 | ;;; binding can not be found in these modules also, the symbol lookup has | |
1335 | ;;; failed. | |
1336 | ;;; | |
1337 | ;;; If a new binding shall be created, the standard-interface-eval-closure | |
1338 | ;;; immediately returns indicating failure. That is, it does not even try | |
1339 | ;;; to look up the symbol. In contrast, the standard-eval-closure would | |
1340 | ;;; first search the obarray, and if no binding was found there, would | |
1341 | ;;; create a new binding in the obarray, therefore not calling the binder | |
1342 | ;;; procedure or searching the modules in the uses list. | |
1343 | ;;; | |
1344 | ;;; The explanation of the following members obarray, binder and uses | |
1345 | ;;; assumes that the symbol lookup follows the strategy that is defined in | |
1346 | ;;; the standard-eval-closure and the standard-interface-eval-closure. | |
1347 | ;;; | |
1348 | ;;; - obarray: a hash table that maps symbols to variable objects. In this | |
1349 | ;;; hash table, the definitions are found that are local to the module (that | |
1350 | ;;; is, not imported from other modules). When looking up bindings in the | |
1351 | ;;; module, this hash table is searched first. | |
1352 | ;;; | |
1353 | ;;; - binder: either #f or a function taking a module and a symbol argument. | |
1354 | ;;; If it is a function it is called after the obarray has been | |
1355 | ;;; unsuccessfully searched for a binding. It then can provide bindings | |
1356 | ;;; that would otherwise not be found locally in the module. | |
1357 | ;;; | |
1358 | ;;; - uses: a list of modules from which non-local bindings can be inherited. | |
1359 | ;;; These modules are the third place queried for bindings after the obarray | |
1360 | ;;; has been unsuccessfully searched and the binder function did not deliver | |
1361 | ;;; a result either. | |
1362 | ;;; | |
1363 | ;;; - transformer: either #f or a function taking a scheme expression as | |
1364 | ;;; delivered by read. If it is a function, it will be called to perform | |
1365 | ;;; syntax transformations (e. g. makro expansion) on the given scheme | |
1366 | ;;; expression. The output of the transformer function will then be passed | |
1367 | ;;; to Guile's internal memoizer. This means that the output must be valid | |
1368 | ;;; scheme code. The only exception is, that the output may make use of the | |
1369 | ;;; syntax extensions provided to identify the modules that a binding | |
1370 | ;;; belongs to. | |
1371 | ;;; | |
1372 | ;;; - name: the name of the module. This is used for all kinds of printing | |
1373 | ;;; outputs. In certain places the module name also serves as a way of | |
1374 | ;;; identification. When adding a module to the uses list of another | |
1375 | ;;; module, it is made sure that the new uses list will not contain two | |
1376 | ;;; modules of the same name. | |
1377 | ;;; | |
1378 | ;;; - kind: classification of the kind of module. The value is (currently?) | |
1379 | ;;; only used for printing. It has no influence on how a module is treated. | |
1380 | ;;; Currently the following values are used when setting the module kind: | |
1381 | ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind | |
1382 | ;;; is set, it defaults to 'module. | |
1383 | ;;; | |
608860a5 LC |
1384 | ;;; - duplicates-handlers: a list of procedures that get called to make a |
1385 | ;;; choice between two duplicate bindings when name clashes occur. See the | |
1386 | ;;; `duplicate-handlers' global variable below. | |
37f5dfe5 | 1387 | ;;; |
608860a5 LC |
1388 | ;;; - observers: a list of procedures that get called when the module is |
1389 | ;;; modified. | |
37f5dfe5 | 1390 | ;;; |
608860a5 LC |
1391 | ;;; - weak-observers: a weak-key hash table of procedures that get called |
1392 | ;;; when the module is modified. See `module-observe-weak' for details. | |
37f5dfe5 DH |
1393 | ;;; |
1394 | ;;; In addition, the module may (must?) contain a binding for | |
608860a5 LC |
1395 | ;;; `%module-public-interface'. This variable should be bound to a module |
1396 | ;;; representing the exported interface of a module. See the | |
1397 | ;;; `module-public-interface' and `module-export!' procedures. | |
37f5dfe5 | 1398 | ;;; |
0f2d19dd JB |
1399 | ;;; !!! warning: The interface to lazy binder procedures is going |
1400 | ;;; to be changed in an incompatible way to permit all the basic | |
1401 | ;;; module ops to be virtualized. | |
1402 | ;;; | |
1403 | ;;; (make-module size use-list lazy-binding-proc) => module | |
1404 | ;;; module-{obarray,uses,binder}[|-set!] | |
1405 | ;;; (module? obj) => [#t|#f] | |
1406 | ;;; (module-locally-bound? module symbol) => [#t|#f] | |
1407 | ;;; (module-bound? module symbol) => [#t|#f] | |
1408 | ;;; (module-symbol-locally-interned? module symbol) => [#t|#f] | |
1409 | ;;; (module-symbol-interned? module symbol) => [#t|#f] | |
1410 | ;;; (module-local-variable module symbol) => [#<variable ...> | #f] | |
1411 | ;;; (module-variable module symbol) => [#<variable ...> | #f] | |
1412 | ;;; (module-symbol-binding module symbol opt-value) | |
9b5a0d84 | 1413 | ;;; => [ <obj> | opt-value | an error occurs ] |
0f2d19dd JB |
1414 | ;;; (module-make-local-var! module symbol) => #<variable...> |
1415 | ;;; (module-add! module symbol var) => unspecified | |
1416 | ;;; (module-remove! module symbol) => unspecified | |
1417 | ;;; (module-for-each proc module) => unspecified | |
1418 | ;;; (make-scm-module) => module ; a lazy copy of the symhash module | |
1419 | ;;; (set-current-module module) => unspecified | |
1420 | ;;; (current-module) => #<module...> | |
1421 | ;;; | |
1422 | ;;; | |
1423 | ||
1424 | \f | |
3d2ada2f | 1425 | |
44cf1f0f | 1426 | ;;; {Printing Modules} |
3d2ada2f DH |
1427 | ;;; |
1428 | ||
44cf1f0f | 1429 | ;; This is how modules are printed. You can re-define it. |
31ac29b6 | 1430 | (define (%print-module mod port) |
0f2d19dd JB |
1431 | (display "#<" port) |
1432 | (display (or (module-kind mod) "module") port) | |
dc1eed52 AW |
1433 | (display " " port) |
1434 | (display (module-name mod) port) | |
0f2d19dd JB |
1435 | (display " " port) |
1436 | (display (number->string (object-address mod) 16) port) | |
1437 | (display ">" port)) | |
1438 | ||
31ac29b6 AW |
1439 | (letrec-syntax |
1440 | ;; Locally extend the syntax to allow record accessors to be defined at | |
1441 | ;; compile-time. Cache the rtd locally to the constructor, the getters and | |
1442 | ;; the setters, in order to allow for redefinition of the record type; not | |
1443 | ;; relevant in the case of modules, but perhaps if we make this public, it | |
1444 | ;; could matter. | |
1445 | ||
1446 | ((define-record-type | |
1447 | (lambda (x) | |
1448 | (define (make-id scope . fragments) | |
1449 | (datum->syntax #'scope | |
1450 | (apply symbol-append | |
1451 | (map (lambda (x) | |
1452 | (if (symbol? x) x (syntax->datum x))) | |
1453 | fragments)))) | |
1454 | ||
1455 | (define (getter rtd type-name field slot) | |
1456 | #`(define #,(make-id rtd type-name '- field) | |
1457 | (let ((rtd #,rtd)) | |
1458 | (lambda (#,type-name) | |
1459 | (if (eq? (struct-vtable #,type-name) rtd) | |
1460 | (struct-ref #,type-name #,slot) | |
1461 | (%record-type-error rtd #,type-name)))))) | |
1462 | ||
1463 | (define (setter rtd type-name field slot) | |
1464 | #`(define #,(make-id rtd 'set- type-name '- field '!) | |
1465 | (let ((rtd #,rtd)) | |
1466 | (lambda (#,type-name val) | |
1467 | (if (eq? (struct-vtable #,type-name) rtd) | |
1468 | (struct-set! #,type-name #,slot val) | |
1469 | (%record-type-error rtd #,type-name)))))) | |
1470 | ||
1471 | (define (accessors rtd type-name fields n exp) | |
1472 | (syntax-case fields () | |
1473 | (() exp) | |
1474 | (((field #:no-accessors) field* ...) (identifier? #'field) | |
1475 | (accessors rtd type-name #'(field* ...) (1+ n) | |
1476 | exp)) | |
1477 | (((field #:no-setter) field* ...) (identifier? #'field) | |
1478 | (accessors rtd type-name #'(field* ...) (1+ n) | |
1479 | #`(begin #,exp | |
1480 | #,(getter rtd type-name #'field n)))) | |
1481 | (((field #:no-getter) field* ...) (identifier? #'field) | |
1482 | (accessors rtd type-name #'(field* ...) (1+ n) | |
1483 | #`(begin #,exp | |
1484 | #,(setter rtd type-name #'field n)))) | |
1485 | ((field field* ...) (identifier? #'field) | |
1486 | (accessors rtd type-name #'(field* ...) (1+ n) | |
1487 | #`(begin #,exp | |
1488 | #,(getter rtd type-name #'field n) | |
1489 | #,(setter rtd type-name #'field n)))))) | |
1490 | ||
1491 | (define (predicate rtd type-name fields exp) | |
1492 | (accessors | |
1493 | rtd type-name fields 0 | |
1494 | #`(begin | |
1495 | #,exp | |
1496 | (define (#,(make-id rtd type-name '?) obj) | |
1497 | (and (struct? obj) (eq? (struct-vtable obj) #,rtd)))))) | |
1498 | ||
1499 | (define (field-list fields) | |
1500 | (syntax-case fields () | |
1501 | (() '()) | |
1502 | (((f . opts) . rest) (identifier? #'f) | |
1503 | (cons #'f (field-list #'rest))) | |
1504 | ((f . rest) (identifier? #'f) | |
1505 | (cons #'f (field-list #'rest))))) | |
1506 | ||
1507 | (define (constructor rtd type-name fields exp) | |
1508 | (let ((ctor (make-id rtd type-name '-constructor)) | |
1509 | (args (field-list fields))) | |
1510 | (predicate rtd type-name fields | |
1511 | #`(begin #,exp | |
1512 | (define #,ctor | |
1513 | (let ((rtd #,rtd)) | |
1514 | (lambda #,args | |
1515 | (make-struct rtd 0 #,@args)))) | |
1516 | (struct-set! #,rtd (+ vtable-offset-user 2) | |
1517 | #,ctor))))) | |
1518 | ||
1519 | (define (type type-name printer fields) | |
1520 | (define (make-layout) | |
1521 | (let lp ((fields fields) (slots '())) | |
1522 | (syntax-case fields () | |
1523 | (() (datum->syntax #'here | |
1524 | (make-struct-layout | |
1525 | (apply string-append slots)))) | |
1526 | ((_ . rest) (lp #'rest (cons "pw" slots)))))) | |
1527 | ||
1528 | (let ((rtd (make-id type-name type-name '-type))) | |
1529 | (constructor rtd type-name fields | |
1530 | #`(begin | |
1531 | (define #,rtd | |
1532 | (make-struct record-type-vtable 0 | |
1533 | '#,(make-layout) | |
1534 | #,printer | |
1535 | '#,type-name | |
1536 | '#,(field-list fields))) | |
1537 | (set-struct-vtable-name! #,rtd '#,type-name))))) | |
1538 | ||
1539 | (syntax-case x () | |
1540 | ((_ type-name printer (field ...)) | |
1541 | (type #'type-name #'printer #'(field ...))))))) | |
1542 | ||
1543 | ;; module-type | |
1544 | ;; | |
1545 | ;; A module is characterized by an obarray in which local symbols | |
1546 | ;; are interned, a list of modules, "uses", from which non-local | |
1547 | ;; bindings can be inherited, and an optional lazy-binder which | |
1548 | ;; is a (CLOSURE module symbol) which, as a last resort, can provide | |
1549 | ;; bindings that would otherwise not be found locally in the module. | |
1550 | ;; | |
1551 | ;; NOTE: If you change the set of fields or their order, you also need to | |
1552 | ;; change the constants in libguile/modules.h. | |
1553 | ;; | |
1554 | ;; NOTE: The getter `module-eval-closure' is used in libguile/modules.c. | |
1555 | ;; NOTE: The getter `module-transfomer' is defined libguile/modules.c. | |
1556 | ;; NOTE: The getter `module-name' is defined later, due to boot reasons. | |
4e48b495 | 1557 | ;; NOTE: The getter `module-public-interface' is used in libguile/modules.c. |
31ac29b6 AW |
1558 | ;; |
1559 | (define-record-type module | |
1560 | (lambda (obj port) (%print-module obj port)) | |
1561 | (obarray | |
1562 | uses | |
1563 | binder | |
1564 | eval-closure | |
1565 | (transformer #:no-getter) | |
1566 | (name #:no-getter) | |
1567 | kind | |
1568 | duplicates-handlers | |
1569 | (import-obarray #:no-setter) | |
1570 | observers | |
1571 | (weak-observers #:no-setter) | |
f905381d | 1572 | version |
81fc66cf | 1573 | submodules |
4e48b495 AW |
1574 | submodule-binder |
1575 | public-interface))) | |
31ac29b6 | 1576 | |
0f2d19dd | 1577 | |
8b718458 | 1578 | ;; make-module &opt size uses binder |
0f2d19dd | 1579 | ;; |
8b718458 JB |
1580 | ;; Create a new module, perhaps with a particular size of obarray, |
1581 | ;; initial uses list, or binding procedure. | |
0f2d19dd | 1582 | ;; |
0f2d19dd JB |
1583 | (define make-module |
1584 | (lambda args | |
0f2d19dd | 1585 | |
8b718458 | 1586 | (define (parse-arg index default) |
9b5a0d84 AW |
1587 | (if (> (length args) index) |
1588 | (list-ref args index) | |
1589 | default)) | |
8b718458 | 1590 | |
608860a5 LC |
1591 | (define %default-import-size |
1592 | ;; Typical number of imported bindings actually used by a module. | |
1593 | 600) | |
1594 | ||
8b718458 | 1595 | (if (> (length args) 3) |
9b5a0d84 | 1596 | (error "Too many args to make-module." args)) |
0f2d19dd | 1597 | |
231a4ea8 | 1598 | (let ((size (parse-arg 0 31)) |
9b5a0d84 AW |
1599 | (uses (parse-arg 1 '())) |
1600 | (binder (parse-arg 2 #f))) | |
1601 | ||
1602 | (if (not (integer? size)) | |
1603 | (error "Illegal size to make-module." size)) | |
1604 | (if (not (and (list? uses) | |
1605 | (and-map module? uses))) | |
1606 | (error "Incorrect use list." uses)) | |
1607 | (if (and binder (not (procedure? binder))) | |
1608 | (error | |
1609 | "Lazy-binder expected to be a procedure or #f." binder)) | |
1610 | ||
1611 | (let ((module (module-constructor (make-hash-table size) | |
4f692ace | 1612 | uses binder #f macroexpand |
13182603 | 1613 | #f #f #f |
9b5a0d84 AW |
1614 | (make-hash-table %default-import-size) |
1615 | '() | |
f905381d | 1616 | (make-weak-key-hash-table 31) #f |
4e48b495 | 1617 | (make-hash-table 7) #f #f))) |
8b718458 | 1618 | |
9b5a0d84 AW |
1619 | ;; We can't pass this as an argument to module-constructor, |
1620 | ;; because we need it to close over a pointer to the module | |
1621 | ;; itself. | |
1622 | (set-module-eval-closure! module (standard-eval-closure module)) | |
8b718458 | 1623 | |
9b5a0d84 | 1624 | module)))) |
0f2d19dd | 1625 | |
8b718458 | 1626 | |
0f2d19dd | 1627 | \f |
3d2ada2f | 1628 | |
1777c18b MD |
1629 | ;;; {Observer protocol} |
1630 | ;;; | |
1631 | ||
1632 | (define (module-observe module proc) | |
1633 | (set-module-observers! module (cons proc (module-observers module))) | |
1634 | (cons module proc)) | |
1635 | ||
608860a5 LC |
1636 | (define (module-observe-weak module observer-id . proc) |
1637 | ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can | |
1638 | ;; be any Scheme object). PROC is invoked and passed MODULE any time | |
1639 | ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd | |
1640 | ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value, | |
1641 | ;; for instance). | |
1642 | ||
1643 | ;; The two-argument version is kept for backward compatibility: when called | |
1644 | ;; with two arguments, the observer gets unregistered when closure PROC | |
1645 | ;; gets GC'd (making it impossible to use an anonymous lambda for PROC). | |
1646 | ||
1647 | (let ((proc (if (null? proc) observer-id (car proc)))) | |
1648 | (hashq-set! (module-weak-observers module) observer-id proc))) | |
1777c18b MD |
1649 | |
1650 | (define (module-unobserve token) | |
1651 | (let ((module (car token)) | |
9b5a0d84 | 1652 | (id (cdr token))) |
1777c18b | 1653 | (if (integer? id) |
9b5a0d84 AW |
1654 | (hash-remove! (module-weak-observers module) id) |
1655 | (set-module-observers! module (delq1! id (module-observers module))))) | |
1777c18b MD |
1656 | *unspecified*) |
1657 | ||
d57da08b | 1658 | (define module-defer-observers #f) |
03d6cddc | 1659 | (define module-defer-observers-mutex (make-mutex 'recursive)) |
d57da08b MD |
1660 | (define module-defer-observers-table (make-hash-table)) |
1661 | ||
1a961d7e | 1662 | (define (module-modified m) |
d57da08b MD |
1663 | (if module-defer-observers |
1664 | (hash-set! module-defer-observers-table m #t) | |
1665 | (module-call-observers m))) | |
1666 | ||
1667 | ;;; This function can be used to delay calls to observers so that they | |
1668 | ;;; can be called once only in the face of massive updating of modules. | |
1669 | ;;; | |
1670 | (define (call-with-deferred-observers thunk) | |
1671 | (dynamic-wind | |
1672 | (lambda () | |
9b5a0d84 AW |
1673 | (lock-mutex module-defer-observers-mutex) |
1674 | (set! module-defer-observers #t)) | |
d57da08b MD |
1675 | thunk |
1676 | (lambda () | |
9b5a0d84 AW |
1677 | (set! module-defer-observers #f) |
1678 | (hash-for-each (lambda (m dummy) | |
1679 | (module-call-observers m)) | |
1680 | module-defer-observers-table) | |
1681 | (hash-clear! module-defer-observers-table) | |
1682 | (unlock-mutex module-defer-observers-mutex)))) | |
d57da08b MD |
1683 | |
1684 | (define (module-call-observers m) | |
1777c18b | 1685 | (for-each (lambda (proc) (proc m)) (module-observers m)) |
608860a5 LC |
1686 | |
1687 | ;; We assume that weak observers don't (un)register themselves as they are | |
1688 | ;; called since this would preclude proper iteration over the hash table | |
1689 | ;; elements. | |
1690 | (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m))) | |
1777c18b MD |
1691 | |
1692 | \f | |
3d2ada2f | 1693 | |
0f2d19dd JB |
1694 | ;;; {Module Searching in General} |
1695 | ;;; | |
1696 | ;;; We sometimes want to look for properties of a symbol | |
1697 | ;;; just within the obarray of one module. If the property | |
1698 | ;;; holds, then it is said to hold ``locally'' as in, ``The symbol | |
1699 | ;;; DISPLAY is locally rebound in the module `safe-guile'.'' | |
1700 | ;;; | |
1701 | ;;; | |
1702 | ;;; Other times, we want to test for a symbol property in the obarray | |
1703 | ;;; of M and, if it is not found there, try each of the modules in the | |
1704 | ;;; uses list of M. This is the normal way of testing for some | |
1705 | ;;; property, so we state these properties without qualification as | |
1706 | ;;; in: ``The symbol 'fnord is interned in module M because it is | |
1707 | ;;; interned locally in module M2 which is a member of the uses list | |
1708 | ;;; of M.'' | |
1709 | ;;; | |
1710 | ||
1711 | ;; module-search fn m | |
20edfbbd | 1712 | ;; |
0f2d19dd JB |
1713 | ;; return the first non-#f result of FN applied to M and then to |
1714 | ;; the modules in the uses of m, and so on recursively. If all applications | |
1715 | ;; return #f, then so does this function. | |
1716 | ;; | |
1717 | (define (module-search fn m v) | |
1718 | (define (loop pos) | |
1719 | (and (pair? pos) | |
9b5a0d84 AW |
1720 | (or (module-search fn (car pos) v) |
1721 | (loop (cdr pos))))) | |
0f2d19dd JB |
1722 | (or (fn m v) |
1723 | (loop (module-uses m)))) | |
1724 | ||
1725 | ||
1726 | ;;; {Is a symbol bound in a module?} | |
1727 | ;;; | |
1728 | ;;; Symbol S in Module M is bound if S is interned in M and if the binding | |
1729 | ;;; of S in M has been set to some well-defined value. | |
1730 | ;;; | |
1731 | ||
1732 | ;; module-locally-bound? module symbol | |
1733 | ;; | |
1734 | ;; Is a symbol bound (interned and defined) locally in a given module? | |
1735 | ;; | |
1736 | (define (module-locally-bound? m v) | |
1737 | (let ((var (module-local-variable m v))) | |
1738 | (and var | |
9b5a0d84 | 1739 | (variable-bound? var)))) |
0f2d19dd JB |
1740 | |
1741 | ;; module-bound? module symbol | |
1742 | ;; | |
1743 | ;; Is a symbol bound (interned and defined) anywhere in a given module | |
1744 | ;; or its uses? | |
1745 | ;; | |
1746 | (define (module-bound? m v) | |
f176c584 AW |
1747 | (let ((var (module-variable m v))) |
1748 | (and var | |
9b5a0d84 | 1749 | (variable-bound? var)))) |
0f2d19dd JB |
1750 | |
1751 | ;;; {Is a symbol interned in a module?} | |
1752 | ;;; | |
20edfbbd | 1753 | ;;; Symbol S in Module M is interned if S occurs in |
0f2d19dd JB |
1754 | ;;; of S in M has been set to some well-defined value. |
1755 | ;;; | |
1756 | ;;; It is possible to intern a symbol in a module without providing | |
1757 | ;;; an initial binding for the corresponding variable. This is done | |
1758 | ;;; with: | |
1759 | ;;; (module-add! module symbol (make-undefined-variable)) | |
1760 | ;;; | |
1761 | ;;; In that case, the symbol is interned in the module, but not | |
1762 | ;;; bound there. The unbound symbol shadows any binding for that | |
1763 | ;;; symbol that might otherwise be inherited from a member of the uses list. | |
1764 | ;;; | |
1765 | ||
1766 | (define (module-obarray-get-handle ob key) | |
1767 | ((if (symbol? key) hashq-get-handle hash-get-handle) ob key)) | |
1768 | ||
1769 | (define (module-obarray-ref ob key) | |
1770 | ((if (symbol? key) hashq-ref hash-ref) ob key)) | |
1771 | ||
1772 | (define (module-obarray-set! ob key val) | |
1773 | ((if (symbol? key) hashq-set! hash-set!) ob key val)) | |
1774 | ||
1775 | (define (module-obarray-remove! ob key) | |
1776 | ((if (symbol? key) hashq-remove! hash-remove!) ob key)) | |
1777 | ||
1778 | ;; module-symbol-locally-interned? module symbol | |
20edfbbd | 1779 | ;; |
0f2d19dd JB |
1780 | ;; is a symbol interned (not neccessarily defined) locally in a given module |
1781 | ;; or its uses? Interned symbols shadow inherited bindings even if | |
1782 | ;; they are not themselves bound to a defined value. | |
1783 | ;; | |
1784 | (define (module-symbol-locally-interned? m v) | |
1785 | (not (not (module-obarray-get-handle (module-obarray m) v)))) | |
1786 | ||
1787 | ;; module-symbol-interned? module symbol | |
20edfbbd | 1788 | ;; |
0f2d19dd JB |
1789 | ;; is a symbol interned (not neccessarily defined) anywhere in a given module |
1790 | ;; or its uses? Interned symbols shadow inherited bindings even if | |
1791 | ;; they are not themselves bound to a defined value. | |
1792 | ;; | |
1793 | (define (module-symbol-interned? m v) | |
1794 | (module-search module-symbol-locally-interned? m v)) | |
1795 | ||
1796 | ||
1797 | ;;; {Mapping modules x symbols --> variables} | |
1798 | ;;; | |
1799 | ||
1800 | ;; module-local-variable module symbol | |
1801 | ;; return the local variable associated with a MODULE and SYMBOL. | |
1802 | ;; | |
1803 | ;;; This function is very important. It is the only function that can | |
1804 | ;;; return a variable from a module other than the mutators that store | |
1805 | ;;; new variables in modules. Therefore, this function is the location | |
1806 | ;;; of the "lazy binder" hack. | |
1807 | ;;; | |
1808 | ;;; If symbol is defined in MODULE, and if the definition binds symbol | |
1809 | ;;; to a variable, return that variable object. | |
1810 | ;;; | |
1811 | ;;; If the symbols is not found at first, but the module has a lazy binder, | |
1812 | ;;; then try the binder. | |
1813 | ;;; | |
1814 | ;;; If the symbol is not found at all, return #f. | |
1815 | ;;; | |
608860a5 LC |
1816 | ;;; (This is now written in C, see `modules.c'.) |
1817 | ;;; | |
0f2d19dd JB |
1818 | |
1819 | ;;; {Mapping modules x symbols --> bindings} | |
1820 | ;;; | |
1821 | ;;; These are similar to the mapping to variables, except that the | |
1822 | ;;; variable is dereferenced. | |
1823 | ;;; | |
1824 | ||
1825 | ;; module-symbol-binding module symbol opt-value | |
20edfbbd | 1826 | ;; |
0f2d19dd JB |
1827 | ;; return the binding of a variable specified by name within |
1828 | ;; a given module, signalling an error if the variable is unbound. | |
1829 | ;; If the OPT-VALUE is passed, then instead of signalling an error, | |
1830 | ;; return OPT-VALUE. | |
1831 | ;; | |
1832 | (define (module-symbol-local-binding m v . opt-val) | |
1833 | (let ((var (module-local-variable m v))) | |
7b07e5ef | 1834 | (if (and var (variable-bound? var)) |
9b5a0d84 AW |
1835 | (variable-ref var) |
1836 | (if (not (null? opt-val)) | |
1837 | (car opt-val) | |
1838 | (error "Locally unbound variable." v))))) | |
0f2d19dd JB |
1839 | |
1840 | ;; module-symbol-binding module symbol opt-value | |
20edfbbd | 1841 | ;; |
0f2d19dd JB |
1842 | ;; return the binding of a variable specified by name within |
1843 | ;; a given module, signalling an error if the variable is unbound. | |
1844 | ;; If the OPT-VALUE is passed, then instead of signalling an error, | |
1845 | ;; return OPT-VALUE. | |
1846 | ;; | |
1847 | (define (module-symbol-binding m v . opt-val) | |
1848 | (let ((var (module-variable m v))) | |
7b07e5ef | 1849 | (if (and var (variable-bound? var)) |
9b5a0d84 AW |
1850 | (variable-ref var) |
1851 | (if (not (null? opt-val)) | |
1852 | (car opt-val) | |
1853 | (error "Unbound variable." v))))) | |
0f2d19dd JB |
1854 | |
1855 | ||
1856 | \f | |
3d2ada2f | 1857 | |
0f2d19dd JB |
1858 | ;;; {Adding Variables to Modules} |
1859 | ;;; | |
0f2d19dd JB |
1860 | |
1861 | ;; module-make-local-var! module symbol | |
20edfbbd | 1862 | ;; |
0f2d19dd JB |
1863 | ;; ensure a variable for V in the local namespace of M. |
1864 | ;; If no variable was already there, then create a new and uninitialzied | |
1865 | ;; variable. | |
1866 | ;; | |
d57da08b MD |
1867 | ;; This function is used in modules.c. |
1868 | ;; | |
0f2d19dd JB |
1869 | (define (module-make-local-var! m v) |
1870 | (or (let ((b (module-obarray-ref (module-obarray m) v))) | |
9b5a0d84 AW |
1871 | (and (variable? b) |
1872 | (begin | |
1873 | ;; Mark as modified since this function is called when | |
1874 | ;; the standard eval closure defines a binding | |
1875 | (module-modified m) | |
1876 | b))) | |
0c5f718b | 1877 | |
608860a5 LC |
1878 | ;; Create a new local variable. |
1879 | (let ((local-var (make-undefined-variable))) | |
1880 | (module-add! m v local-var) | |
1881 | local-var))) | |
0f2d19dd | 1882 | |
89d06712 | 1883 | ;; module-ensure-local-variable! module symbol |
9540368e | 1884 | ;; |
89d06712 MV |
1885 | ;; Ensure that there is a local variable in MODULE for SYMBOL. If |
1886 | ;; there is no binding for SYMBOL, create a new uninitialized | |
1887 | ;; variable. Return the local variable. | |
9540368e | 1888 | ;; |
89d06712 MV |
1889 | (define (module-ensure-local-variable! module symbol) |
1890 | (or (module-local-variable module symbol) | |
9540368e | 1891 | (let ((var (make-undefined-variable))) |
9b5a0d84 AW |
1892 | (module-add! module symbol var) |
1893 | var))) | |
9540368e | 1894 | |
0f2d19dd | 1895 | ;; module-add! module symbol var |
20edfbbd | 1896 | ;; |
0f2d19dd JB |
1897 | ;; ensure a particular variable for V in the local namespace of M. |
1898 | ;; | |
1899 | (define (module-add! m v var) | |
1900 | (if (not (variable? var)) | |
1901 | (error "Bad variable to module-add!" var)) | |
1777c18b | 1902 | (module-obarray-set! (module-obarray m) v var) |
1a961d7e | 1903 | (module-modified m)) |
0f2d19dd | 1904 | |
20edfbbd TTN |
1905 | ;; module-remove! |
1906 | ;; | |
0f2d19dd JB |
1907 | ;; make sure that a symbol is undefined in the local namespace of M. |
1908 | ;; | |
1909 | (define (module-remove! m v) | |
c35738c1 | 1910 | (module-obarray-remove! (module-obarray m) v) |
1a961d7e | 1911 | (module-modified m)) |
0f2d19dd JB |
1912 | |
1913 | (define (module-clear! m) | |
c35738c1 | 1914 | (hash-clear! (module-obarray m)) |
1a961d7e | 1915 | (module-modified m)) |
0f2d19dd JB |
1916 | |
1917 | ;; MODULE-FOR-EACH -- exported | |
20edfbbd | 1918 | ;; |
0f2d19dd JB |
1919 | ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE). |
1920 | ;; | |
1921 | (define (module-for-each proc module) | |
c35738c1 | 1922 | (hash-for-each proc (module-obarray module))) |
0f2d19dd JB |
1923 | |
1924 | (define (module-map proc module) | |
711a9fd7 | 1925 | (hash-map->list proc (module-obarray module))) |
c35738c1 | 1926 | |
0f27ab8a AW |
1927 | ;; Submodules |
1928 | ;; | |
1929 | ;; Modules exist in a separate namespace from values, because you generally do | |
1930 | ;; not want the name of a submodule, which you might not even use, to collide | |
1931 | ;; with local variables that happen to be named the same as the submodule. | |
1932 | ;; | |
1933 | (define (module-ref-submodule module name) | |
81fc66cf AW |
1934 | (or (hashq-ref (module-submodules module) name) |
1935 | (and (module-submodule-binder module) | |
1936 | ((module-submodule-binder module) module name)))) | |
0f27ab8a AW |
1937 | |
1938 | (define (module-define-submodule! module name submodule) | |
f6a5308b | 1939 | (hashq-set! (module-submodules module) name submodule)) |
0f27ab8a | 1940 | |
0f2d19dd JB |
1941 | \f |
1942 | ||
1943 | ;;; {Low Level Bootstrapping} | |
1944 | ;;; | |
1945 | ||
20edfbbd | 1946 | ;; make-root-module |
0f2d19dd | 1947 | |
296ff5e7 MV |
1948 | ;; A root module uses the pre-modules-obarray as its obarray. This |
1949 | ;; special obarray accumulates all bindings that have been established | |
1950 | ;; before the module system is fully booted. | |
0f2d19dd | 1951 | ;; |
296ff5e7 MV |
1952 | ;; (The obarray continues to be used by code that has been closed over |
1953 | ;; before the module system has been booted.) | |
0f2d19dd JB |
1954 | |
1955 | (define (make-root-module) | |
296ff5e7 MV |
1956 | (let ((m (make-module 0))) |
1957 | (set-module-obarray! m (%get-pre-modules-obarray)) | |
1958 | m)) | |
0f2d19dd | 1959 | |
b622dec7 | 1960 | ;; make-scm-module |
0f2d19dd | 1961 | |
296ff5e7 MV |
1962 | ;; The root interface is a module that uses the same obarray as the |
1963 | ;; root module. It does not allow new definitions, tho. | |
0f2d19dd | 1964 | |
6906bd0d | 1965 | (define (make-scm-module) |
296ff5e7 MV |
1966 | (let ((m (make-module 0))) |
1967 | (set-module-obarray! m (%get-pre-modules-obarray)) | |
1968 | (set-module-eval-closure! m (standard-interface-eval-closure m)) | |
1969 | m)) | |
0f2d19dd JB |
1970 | |
1971 | ||
0f2d19dd | 1972 | \f |
3d2ada2f | 1973 | |
0f2d19dd JB |
1974 | ;;; {Module-based Loading} |
1975 | ;;; | |
1976 | ||
1977 | (define (save-module-excursion thunk) | |
1978 | (let ((inner-module (current-module)) | |
9b5a0d84 | 1979 | (outer-module #f)) |
0f2d19dd | 1980 | (dynamic-wind (lambda () |
9b5a0d84 AW |
1981 | (set! outer-module (current-module)) |
1982 | (set-current-module inner-module) | |
1983 | (set! inner-module #f)) | |
1984 | thunk | |
1985 | (lambda () | |
1986 | (set! inner-module (current-module)) | |
1987 | (set-current-module outer-module) | |
1988 | (set! outer-module #f))))) | |
0f2d19dd | 1989 | |
0f2d19dd JB |
1990 | (define basic-load load) |
1991 | ||
ec3a8ace | 1992 | (define (load-module filename . reader) |
c6775c40 MD |
1993 | (save-module-excursion |
1994 | (lambda () | |
1995 | (let ((oldname (and (current-load-port) | |
9b5a0d84 | 1996 | (port-filename (current-load-port))))) |
ec3a8ace | 1997 | (apply basic-load |
9b5a0d84 AW |
1998 | (if (and oldname |
1999 | (> (string-length filename) 0) | |
2000 | (not (char=? (string-ref filename 0) #\/)) | |
2001 | (not (string=? (dirname oldname) "."))) | |
2002 | (string-append (dirname oldname) "/" filename) | |
2003 | filename) | |
2004 | reader))))) | |
0f2d19dd JB |
2005 | |
2006 | ||
2007 | \f | |
3d2ada2f | 2008 | |
44cf1f0f | 2009 | ;;; {MODULE-REF -- exported} |
3d2ada2f DH |
2010 | ;;; |
2011 | ||
0f2d19dd JB |
2012 | ;; Returns the value of a variable called NAME in MODULE or any of its |
2013 | ;; used modules. If there is no such variable, then if the optional third | |
2014 | ;; argument DEFAULT is present, it is returned; otherwise an error is signaled. | |
20edfbbd | 2015 | ;; |
0f2d19dd JB |
2016 | (define (module-ref module name . rest) |
2017 | (let ((variable (module-variable module name))) | |
2018 | (if (and variable (variable-bound? variable)) | |
9b5a0d84 AW |
2019 | (variable-ref variable) |
2020 | (if (null? rest) | |
2021 | (error "No variable named" name 'in module) | |
2022 | (car rest) ; default value | |
2023 | )))) | |
0f2d19dd JB |
2024 | |
2025 | ;; MODULE-SET! -- exported | |
2026 | ;; | |
2027 | ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses) | |
2028 | ;; to VALUE; if there is no such variable, an error is signaled. | |
20edfbbd | 2029 | ;; |
0f2d19dd JB |
2030 | (define (module-set! module name value) |
2031 | (let ((variable (module-variable module name))) | |
2032 | (if variable | |
9b5a0d84 AW |
2033 | (variable-set! variable value) |
2034 | (error "No variable named" name 'in module)))) | |
0f2d19dd JB |
2035 | |
2036 | ;; MODULE-DEFINE! -- exported | |
2037 | ;; | |
2038 | ;; Sets the variable called NAME in MODULE to VALUE; if there is no such | |
2039 | ;; variable, it is added first. | |
20edfbbd | 2040 | ;; |
0f2d19dd JB |
2041 | (define (module-define! module name value) |
2042 | (let ((variable (module-local-variable module name))) | |
2043 | (if variable | |
9b5a0d84 AW |
2044 | (begin |
2045 | (variable-set! variable value) | |
2046 | (module-modified module)) | |
2047 | (let ((variable (make-variable value))) | |
2048 | (module-add! module name variable))))) | |
0f2d19dd | 2049 | |
ed218d98 MV |
2050 | ;; MODULE-DEFINED? -- exported |
2051 | ;; | |
2052 | ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE | |
2053 | ;; uses) | |
2054 | ;; | |
2055 | (define (module-defined? module name) | |
2056 | (let ((variable (module-variable module name))) | |
2057 | (and variable (variable-bound? variable)))) | |
2058 | ||
0f2d19dd JB |
2059 | ;; MODULE-USE! module interface |
2060 | ;; | |
2061 | ;; Add INTERFACE to the list of interfaces used by MODULE. | |
20edfbbd | 2062 | ;; |
0f2d19dd | 2063 | (define (module-use! module interface) |
b1907902 AW |
2064 | (if (not (or (eq? module interface) |
2065 | (memq interface (module-uses module)))) | |
608860a5 LC |
2066 | (begin |
2067 | ;; Newly used modules must be appended rather than consed, so that | |
2068 | ;; `module-variable' traverses the use list starting from the first | |
2069 | ;; used module. | |
2070 | (set-module-uses! module | |
2071 | (append (filter (lambda (m) | |
2072 | (not | |
2073 | (equal? (module-name m) | |
2074 | (module-name interface)))) | |
2075 | (module-uses module)) | |
2076 | (list interface))) | |
8f44138a | 2077 | (hash-clear! (module-import-obarray module)) |
608860a5 | 2078 | (module-modified module)))) |
0f2d19dd | 2079 | |
7b07e5ef MD |
2080 | ;; MODULE-USE-INTERFACES! module interfaces |
2081 | ;; | |
2082 | ;; Same as MODULE-USE! but add multiple interfaces and check for duplicates | |
2083 | ;; | |
2084 | (define (module-use-interfaces! module interfaces) | |
608860a5 LC |
2085 | (set-module-uses! module |
2086 | (append (module-uses module) interfaces)) | |
8f44138a | 2087 | (hash-clear! (module-import-obarray module)) |
608860a5 | 2088 | (module-modified module)) |
7b07e5ef | 2089 | |
0f2d19dd | 2090 | \f |
3d2ada2f | 2091 | |
0f2d19dd JB |
2092 | ;;; {Recursive Namespaces} |
2093 | ;;; | |
0f2d19dd | 2094 | ;;; A hierarchical namespace emerges if we consider some module to be |
b910c4ac | 2095 | ;;; root, and submodules of that module to be nested namespaces. |
0f2d19dd | 2096 | ;;; |
b910c4ac | 2097 | ;;; The routines here manage variable names in hierarchical namespace. |
0f2d19dd JB |
2098 | ;;; Each variable name is a list of elements, looked up in successively nested |
2099 | ;;; modules. | |
2100 | ;;; | |
9b5a0d84 | 2101 | ;;; (nested-ref some-root-module '(foo bar baz)) |
b910c4ac AW |
2102 | ;;; => <value of a variable named baz in the submodule bar of |
2103 | ;;; the submodule foo of some-root-module> | |
0f2d19dd JB |
2104 | ;;; |
2105 | ;;; | |
2106 | ;;; There are: | |
2107 | ;;; | |
9b5a0d84 AW |
2108 | ;;; ;; a-root is a module |
2109 | ;;; ;; name is a list of symbols | |
0f2d19dd | 2110 | ;;; |
9b5a0d84 AW |
2111 | ;;; nested-ref a-root name |
2112 | ;;; nested-set! a-root name val | |
2113 | ;;; nested-define! a-root name val | |
2114 | ;;; nested-remove! a-root name | |
0f2d19dd | 2115 | ;;; |
b910c4ac AW |
2116 | ;;; These functions manipulate values in namespaces. For referencing the |
2117 | ;;; namespaces themselves, use the following: | |
0f2d19dd | 2118 | ;;; |
b910c4ac AW |
2119 | ;;; nested-ref-module a-root name |
2120 | ;;; nested-define-module! a-root name mod | |
2121 | ;;; | |
2122 | ;;; (current-module) is a natural choice for a root so for convenience there are | |
0f2d19dd JB |
2123 | ;;; also: |
2124 | ;;; | |
b910c4ac AW |
2125 | ;;; local-ref name == nested-ref (current-module) name |
2126 | ;;; local-set! name val == nested-set! (current-module) name val | |
2127 | ;;; local-define name val == nested-define! (current-module) name val | |
2128 | ;;; local-remove name == nested-remove! (current-module) name | |
2129 | ;;; local-ref-module name == nested-ref-module (current-module) name | |
2130 | ;;; local-define-module! name m == nested-define-module! (current-module) name m | |
0f2d19dd JB |
2131 | ;;; |
2132 | ||
2133 | ||
0dd5491c | 2134 | (define (nested-ref root names) |
b910c4ac AW |
2135 | (if (null? names) |
2136 | root | |
2137 | (let loop ((cur root) | |
2138 | (head (car names)) | |
2139 | (tail (cdr names))) | |
2140 | (if (null? tail) | |
2141 | (module-ref cur head #f) | |
2142 | (let ((cur (module-ref-submodule cur head))) | |
2143 | (and cur | |
2144 | (loop cur (car tail) (cdr tail)))))))) | |
0f2d19dd | 2145 | |
0dd5491c | 2146 | (define (nested-set! root names val) |
0f2d19dd | 2147 | (let loop ((cur root) |
b910c4ac AW |
2148 | (head (car names)) |
2149 | (tail (cdr names))) | |
2150 | (if (null? tail) | |
2151 | (module-set! cur head val) | |
2152 | (let ((cur (module-ref-submodule cur head))) | |
2153 | (if (not cur) | |
2154 | (error "failed to resolve module" names) | |
2155 | (loop cur (car tail) (cdr tail))))))) | |
0f2d19dd | 2156 | |
0dd5491c | 2157 | (define (nested-define! root names val) |
0f2d19dd | 2158 | (let loop ((cur root) |
b910c4ac AW |
2159 | (head (car names)) |
2160 | (tail (cdr names))) | |
2161 | (if (null? tail) | |
2162 | (module-define! cur head val) | |
2163 | (let ((cur (module-ref-submodule cur head))) | |
2164 | (if (not cur) | |
2165 | (error "failed to resolve module" names) | |
2166 | (loop cur (car tail) (cdr tail))))))) | |
0f2d19dd | 2167 | |
0dd5491c | 2168 | (define (nested-remove! root names) |
0f2d19dd | 2169 | (let loop ((cur root) |
b910c4ac AW |
2170 | (head (car names)) |
2171 | (tail (cdr names))) | |
2172 | (if (null? tail) | |
2173 | (module-remove! cur head) | |
2174 | (let ((cur (module-ref-submodule cur head))) | |
2175 | (if (not cur) | |
2176 | (error "failed to resolve module" names) | |
2177 | (loop cur (car tail) (cdr tail))))))) | |
2178 | ||
2179 | ||
2180 | (define (nested-ref-module root names) | |
2181 | (let loop ((cur root) | |
2182 | (names names)) | |
2183 | (if (null? names) | |
2184 | cur | |
2185 | (let ((cur (module-ref-submodule cur (car names)))) | |
2186 | (and cur | |
2187 | (loop cur (cdr names))))))) | |
2188 | ||
2189 | (define (nested-define-module! root names module) | |
2190 | (if (null? names) | |
2191 | (error "can't redefine root module" root module) | |
2192 | (let loop ((cur root) | |
2193 | (head (car names)) | |
2194 | (tail (cdr names))) | |
2195 | (if (null? tail) | |
2196 | (module-define-submodule! cur head module) | |
2197 | (let ((cur (or (module-ref-submodule cur head) | |
2198 | (let ((m (make-module 31))) | |
2199 | (set-module-kind! m 'directory) | |
2200 | (set-module-name! m (append (module-name cur) | |
2201 | (list head))) | |
2202 | (module-define-submodule! cur head m) | |
2203 | m)))) | |
2204 | (loop cur (car tail) (cdr tail))))))) | |
2205 | ||
0f2d19dd | 2206 | |
0dd5491c MD |
2207 | (define (local-ref names) (nested-ref (current-module) names)) |
2208 | (define (local-set! names val) (nested-set! (current-module) names val)) | |
2209 | (define (local-define names val) (nested-define! (current-module) names val)) | |
2210 | (define (local-remove names) (nested-remove! (current-module) names)) | |
b910c4ac AW |
2211 | (define (local-ref-module names) (nested-ref-module (current-module) names)) |
2212 | (define (local-define-module names mod) (nested-define-module! (current-module) names mod)) | |
2213 | ||
0f2d19dd JB |
2214 | |
2215 | ||
2216 | \f | |
3d2ada2f | 2217 | |
cb67c838 | 2218 | ;;; {The (guile) module} |
0f2d19dd | 2219 | ;;; |
cb67c838 AW |
2220 | ;;; The standard module, which has the core Guile bindings. Also called the |
2221 | ;;; "root module", as it is imported by many other modules, but it is not | |
2222 | ;;; necessarily the root of anything; and indeed, the module named '() might be | |
2223 | ;;; better thought of as a root. | |
0f2d19dd | 2224 | ;;; |
bbd1d133 | 2225 | |
edc185c7 MD |
2226 | (define (set-system-module! m s) |
2227 | (set-procedure-property! (module-eval-closure m) 'system-module s)) | |
0f2d19dd JB |
2228 | (define the-root-module (make-root-module)) |
2229 | (define the-scm-module (make-scm-module)) | |
2230 | (set-module-public-interface! the-root-module the-scm-module) | |
d5504515 MD |
2231 | (set-module-name! the-root-module '(guile)) |
2232 | (set-module-name! the-scm-module '(guile)) | |
2233 | (set-module-kind! the-scm-module 'interface) | |
25d8cd3a AW |
2234 | (set-system-module! the-root-module #t) |
2235 | (set-system-module! the-scm-module #t) | |
0f2d19dd | 2236 | |
bbd1d133 AW |
2237 | |
2238 | \f | |
2239 | ||
2240 | ;; Now that we have a root module, even though modules aren't fully booted, | |
2241 | ;; expand the definition of resolve-module. | |
2242 | ;; | |
2243 | (define (resolve-module name . args) | |
2244 | (if (equal? name '(guile)) | |
2245 | the-root-module | |
2246 | (error "unexpected module to resolve during module boot" name))) | |
2247 | ||
2248 | ;; Cheat. These bindings are needed by modules.c, but we don't want | |
2249 | ;; to move their real definition here because that would be unnatural. | |
2250 | ;; | |
2251 | (define process-define-module #f) | |
2252 | (define process-use-modules #f) | |
2253 | (define module-export! #f) | |
2254 | (define default-duplicate-binding-procedures #f) | |
2255 | ||
2256 | ;; This boots the module system. All bindings needed by modules.c | |
2257 | ;; must have been defined by now. | |
296ff5e7 | 2258 | ;; |
bbd1d133 AW |
2259 | (set-current-module the-root-module) |
2260 | ||
2261 | ||
2262 | \f | |
2263 | ||
2264 | ;; Now that modules are booted, give module-name its final definition. | |
2265 | ;; | |
2266 | (define module-name | |
2267 | (let ((accessor (record-accessor module-type 'name))) | |
2268 | (lambda (mod) | |
2269 | (or (accessor mod) | |
2270 | (let ((name (list (gensym)))) | |
cb67c838 AW |
2271 | ;; Name MOD and bind it in the module root so that it's visible to |
2272 | ;; `resolve-module'. This is important as `psyntax' stores module | |
2273 | ;; names and relies on being able to `resolve-module' them. | |
bbd1d133 | 2274 | (set-module-name! mod name) |
9e0bfdba | 2275 | (nested-define-module! (resolve-module '() #f) name mod) |
bbd1d133 AW |
2276 | (accessor mod)))))) |
2277 | ||
296ff5e7 | 2278 | (define (make-modules-in module name) |
9e0bfdba AW |
2279 | (or (nested-ref-module module name) |
2280 | (let ((m (make-module 31))) | |
2281 | (set-module-kind! m 'directory) | |
2282 | (set-module-name! m (append (module-name module) name)) | |
2283 | (nested-define-module! module name m) | |
2284 | m))) | |
0f2d19dd | 2285 | |
296ff5e7 MV |
2286 | (define (beautify-user-module! module) |
2287 | (let ((interface (module-public-interface module))) | |
2288 | (if (or (not interface) | |
9b5a0d84 AW |
2289 | (eq? interface module)) |
2290 | (let ((interface (make-module 31))) | |
2291 | (set-module-name! interface (module-name module)) | |
dca14012 | 2292 | (set-module-version! interface (module-version module)) |
9b5a0d84 AW |
2293 | (set-module-kind! interface 'interface) |
2294 | (set-module-public-interface! module interface)))) | |
296ff5e7 | 2295 | (if (and (not (memq the-scm-module (module-uses module))) |
9b5a0d84 | 2296 | (not (eq? module the-root-module))) |
608860a5 LC |
2297 | ;; Import the default set of bindings (from the SCM module) in MODULE. |
2298 | (module-use! module the-scm-module))) | |
432558b9 | 2299 | |
dca14012 JG |
2300 | (define (version-matches? version-ref target) |
2301 | (define (any pred lst) | |
2302 | (and (not (null? lst)) (or (pred (car lst)) (any pred (cdr lst))))) | |
2303 | (define (every pred lst) | |
2304 | (or (null? lst) (and (pred (car lst)) (every pred (cdr lst))))) | |
2305 | (define (sub-versions-match? v-refs t) | |
2306 | (define (sub-version-matches? v-ref t) | |
2307 | (define (curried-sub-version-matches? v) | |
2308 | (sub-version-matches? v t)) | |
2309 | (cond ((number? v-ref) (eqv? v-ref t)) | |
2310 | ((list? v-ref) | |
2311 | (let ((cv (car v-ref))) | |
2312 | (cond ((eq? cv '>=) (>= t (cadr v-ref))) | |
2313 | ((eq? cv '<=) (<= t (cadr v-ref))) | |
2314 | ((eq? cv 'and) | |
2315 | (every curried-sub-version-matches? (cdr v-ref))) | |
2316 | ((eq? cv 'or) | |
2317 | (any curried-sub-version-matches? (cdr v-ref))) | |
2318 | ((eq? cv 'not) (not (sub-version-matches? (cadr v-ref) t))) | |
2319 | (else (error "Incompatible sub-version reference" cv))))) | |
2320 | (else (error "Incompatible sub-version reference" v-ref)))) | |
2321 | (or (null? v-refs) | |
2322 | (and (not (null? t)) | |
2323 | (sub-version-matches? (car v-refs) (car t)) | |
2324 | (sub-versions-match? (cdr v-refs) (cdr t))))) | |
2325 | (define (curried-version-matches? v) | |
2326 | (version-matches? v target)) | |
2327 | (or (null? version-ref) | |
2328 | (let ((cv (car version-ref))) | |
2329 | (cond ((eq? cv 'and) (every curried-version-matches? (cdr version-ref))) | |
2330 | ((eq? cv 'or) (any curried-version-matches? (cdr version-ref))) | |
0dfe0e75 | 2331 | ((eq? cv 'not) (not (version-matches? (cadr version-ref) target))) |
dca14012 JG |
2332 | (else (sub-versions-match? version-ref target)))))) |
2333 | ||
2334 | (define (find-versioned-module dir-hint name version-ref roots) | |
2335 | (define (subdir-pair-less pair1 pair2) | |
2336 | (define (numlist-less lst1 lst2) | |
2337 | (or (null? lst2) | |
2338 | (and (not (null? lst1)) | |
2339 | (cond ((> (car lst1) (car lst2)) #t) | |
2340 | ((< (car lst1) (car lst2)) #f) | |
2341 | (else (numlist-less (cdr lst1) (cdr lst2))))))) | |
2342 | (numlist-less (car pair1) (car pair2))) | |
2343 | (define (match-version-and-file pair) | |
2344 | (and (version-matches? version-ref (car pair)) | |
2345 | (let ((filenames | |
2346 | (filter (lambda (file) | |
2347 | (let ((s (false-if-exception (stat file)))) | |
2348 | (and s (eq? (stat:type s) 'regular)))) | |
2349 | (map (lambda (ext) | |
2350 | (string-append (cdr pair) "/" name ext)) | |
2351 | %load-extensions)))) | |
2352 | (and (not (null? filenames)) | |
2353 | (cons (car pair) (car filenames)))))) | |
2354 | ||
2355 | (define (match-version-recursive root-pairs leaf-pairs) | |
2356 | (define (filter-subdirs root-pairs ret) | |
2357 | (define (filter-subdir root-pair dstrm subdir-pairs) | |
2358 | (let ((entry (readdir dstrm))) | |
2359 | (if (eof-object? entry) | |
2360 | subdir-pairs | |
2361 | (let* ((subdir (string-append (cdr root-pair) "/" entry)) | |
2362 | (num (string->number entry)) | |
2363 | (num (and num (append (car root-pair) (list num))))) | |
2364 | (if (and num (eq? (stat:type (stat subdir)) 'directory)) | |
2365 | (filter-subdir | |
2366 | root-pair dstrm (cons (cons num subdir) subdir-pairs)) | |
2367 | (filter-subdir root-pair dstrm subdir-pairs)))))) | |
2368 | ||
2369 | (or (and (null? root-pairs) ret) | |
2370 | (let* ((rp (car root-pairs)) | |
2371 | (dstrm (false-if-exception (opendir (cdr rp))))) | |
2372 | (if dstrm | |
2373 | (let ((subdir-pairs (filter-subdir rp dstrm '()))) | |
2374 | (closedir dstrm) | |
2375 | (filter-subdirs (cdr root-pairs) | |
2376 | (or (and (null? subdir-pairs) ret) | |
2377 | (append ret subdir-pairs)))) | |
2378 | (filter-subdirs (cdr root-pairs) ret))))) | |
2379 | ||
2380 | (or (and (null? root-pairs) leaf-pairs) | |
2381 | (let ((matching-subdir-pairs (filter-subdirs root-pairs '()))) | |
2382 | (match-version-recursive | |
2383 | matching-subdir-pairs | |
2384 | (append leaf-pairs (filter pair? (map match-version-and-file | |
2385 | matching-subdir-pairs))))))) | |
2386 | (define (make-root-pair root) | |
2387 | (cons '() (string-append root "/" dir-hint))) | |
2388 | ||
2389 | (let* ((root-pairs (map make-root-pair roots)) | |
2390 | (matches (if (null? version-ref) | |
2391 | (filter pair? (map match-version-and-file root-pairs)) | |
2392 | '())) | |
2393 | (matches (append matches (match-version-recursive root-pairs '())))) | |
2394 | (and (null? matches) (error "No matching modules found.")) | |
2395 | (cdar (sort matches subdir-pair-less)))) | |
2396 | ||
f95f82f8 AW |
2397 | (define (make-fresh-user-module) |
2398 | (let ((m (make-module))) | |
2399 | (beautify-user-module! m) | |
2400 | m)) | |
2401 | ||
1f60d9d2 MD |
2402 | ;; NOTE: This binding is used in libguile/modules.c. |
2403 | ;; | |
53f84bc8 | 2404 | (define resolve-module |
cb67c838 AW |
2405 | (let ((root (make-module))) |
2406 | (set-module-name! root '()) | |
2407 | ;; Define the-root-module as '(guile). | |
d58ccc66 | 2408 | (module-define-submodule! root 'guile the-root-module) |
cb67c838 | 2409 | |
dbbbc2a1 | 2410 | (lambda (name . args) ;; #:optional (autoload #t) (version #f) |
d58ccc66 | 2411 | (let* ((already (nested-ref-module root name)) |
cb67c838 AW |
2412 | (numargs (length args)) |
2413 | (autoload (or (= numargs 0) (car args))) | |
2414 | (version (and (> numargs 1) (cadr args)))) | |
2415 | (cond | |
d58ccc66 | 2416 | ((and already |
cb67c838 AW |
2417 | (or (not autoload) (module-public-interface already))) |
2418 | ;; A hit, a palpable hit. | |
2419 | (if (and version | |
2420 | (not (version-matches? version (module-version already)))) | |
2421 | (error "incompatible module version already loaded" name)) | |
2422 | already) | |
2423 | (autoload | |
2424 | ;; Try to autoload the module, and recurse. | |
2425 | (try-load-module name version) | |
2426 | (resolve-module name #f)) | |
2427 | (else | |
51b22dbb AW |
2428 | ;; No module found (or if one was, it had no public interface), and |
2429 | ;; we're not autoloading. Here's the weird semantics: we ensure | |
2430 | ;; there's an empty module. | |
2431 | (or already (make-modules-in root name)))))))) | |
cb67c838 | 2432 | |
296ff5e7 | 2433 | |
dca14012 JG |
2434 | (define (try-load-module name version) |
2435 | (try-module-autoload name version)) | |
0f2d19dd | 2436 | |
90847923 MD |
2437 | (define (purify-module! module) |
2438 | "Removes bindings in MODULE which are inherited from the (guile) module." | |
2439 | (let ((use-list (module-uses module))) | |
2440 | (if (and (pair? use-list) | |
9b5a0d84 AW |
2441 | (eq? (car (last-pair use-list)) the-scm-module)) |
2442 | (set-module-uses! module (reverse (cdr (reverse use-list))))))) | |
90847923 | 2443 | |
4eecfeb7 | 2444 | ;; Return a module that is an interface to the module designated by |
532cf805 MV |
2445 | ;; NAME. |
2446 | ;; | |
c614a00b | 2447 | ;; `resolve-interface' takes four keyword arguments: |
532cf805 MV |
2448 | ;; |
2449 | ;; #:select SELECTION | |
2450 | ;; | |
2451 | ;; SELECTION is a list of binding-specs to be imported; A binding-spec | |
2452 | ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG | |
2453 | ;; is the name in the used module and SEEN is the name in the using | |
2454 | ;; module. Note that SEEN is also passed through RENAMER, below. The | |
2455 | ;; default is to select all bindings. If you specify no selection but | |
4eecfeb7 | 2456 | ;; a renamer, only the bindings that already exist in the used module |
532cf805 MV |
2457 | ;; are made available in the interface. Bindings that are added later |
2458 | ;; are not picked up. | |
2459 | ;; | |
c614a00b | 2460 | ;; #:hide BINDINGS |
532cf805 | 2461 | ;; |
c614a00b | 2462 | ;; BINDINGS is a list of bindings which should not be imported. |
f595ccfe MD |
2463 | ;; |
2464 | ;; #:prefix PREFIX | |
2465 | ;; | |
2466 | ;; PREFIX is a symbol that will be appended to each exported name. | |
2467 | ;; The default is to not perform any renaming. | |
532cf805 | 2468 | ;; |
c614a00b MD |
2469 | ;; #:renamer RENAMER |
2470 | ;; | |
2471 | ;; RENAMER is a procedure that takes a symbol and returns its new | |
2472 | ;; name. The default is not perform any renaming. | |
2473 | ;; | |
532cf805 MV |
2474 | ;; Signal "no code for module" error if module name is not resolvable |
2475 | ;; or its public interface is not available. Signal "no binding" | |
2476 | ;; error if selected binding does not exist in the used module. | |
2477 | ;; | |
2478 | (define (resolve-interface name . args) | |
2479 | ||
2480 | (define (get-keyword-arg args kw def) | |
2481 | (cond ((memq kw args) | |
9b5a0d84 AW |
2482 | => (lambda (kw-arg) |
2483 | (if (null? (cdr kw-arg)) | |
2484 | (error "keyword without value: " kw)) | |
2485 | (cadr kw-arg))) | |
2486 | (else | |
2487 | def))) | |
532cf805 MV |
2488 | |
2489 | (let* ((select (get-keyword-arg args #:select #f)) | |
9b5a0d84 AW |
2490 | (hide (get-keyword-arg args #:hide '())) |
2491 | (renamer (or (get-keyword-arg args #:renamer #f) | |
2492 | (let ((prefix (get-keyword-arg args #:prefix #f))) | |
2493 | (and prefix (symbol-prefix-proc prefix))) | |
2494 | identity)) | |
dca14012 JG |
2495 | (version (get-keyword-arg args #:version #f)) |
2496 | (module (resolve-module name #t version)) | |
b622dec7 TTN |
2497 | (public-i (and module (module-public-interface module)))) |
2498 | (and (or (not module) (not public-i)) | |
2499 | (error "no code for module" name)) | |
c614a00b | 2500 | (if (and (not select) (null? hide) (eq? renamer identity)) |
b622dec7 | 2501 | public-i |
532cf805 | 2502 | (let ((selection (or select (module-map (lambda (sym var) sym) |
9b5a0d84 | 2503 | public-i))) |
b622dec7 | 2504 | (custom-i (make-module 31))) |
c614a00b | 2505 | (set-module-kind! custom-i 'custom-interface) |
9b5a0d84 AW |
2506 | (set-module-name! custom-i name) |
2507 | ;; XXX - should use a lazy binder so that changes to the | |
2508 | ;; used module are picked up automatically. | |
2509 | (for-each (lambda (bspec) | |
2510 | (let* ((direct? (symbol? bspec)) | |
2511 | (orig (if direct? bspec (car bspec))) | |
2512 | (seen (if direct? bspec (cdr bspec))) | |
2513 | (var (or (module-local-variable public-i orig) | |
2514 | (module-local-variable module orig) | |
2515 | (error | |
2516 | ;; fixme: format manually for now | |
2517 | (simple-format | |
2518 | #f "no binding `~A' in module ~A" | |
2519 | orig name))))) | |
2520 | (if (memq orig hide) | |
2521 | (set! hide (delq! orig hide)) | |
2522 | (module-add! custom-i | |
2523 | (renamer seen) | |
2524 | var)))) | |
2525 | selection) | |
2526 | ;; Check that we are not hiding bindings which don't exist | |
2527 | (for-each (lambda (binding) | |
2528 | (if (not (module-local-variable public-i binding)) | |
2529 | (error | |
2530 | (simple-format | |
2531 | #f "no binding `~A' to hide in module ~A" | |
2532 | binding name)))) | |
2533 | hide) | |
b622dec7 | 2534 | custom-i)))) |
fb1b76f4 TTN |
2535 | |
2536 | (define (symbol-prefix-proc prefix) | |
2537 | (lambda (symbol) | |
2538 | (symbol-append prefix symbol))) | |
0f2d19dd | 2539 | |
482a28f9 MV |
2540 | ;; This function is called from "modules.c". If you change it, be |
2541 | ;; sure to update "modules.c" as well. | |
2542 | ||
0f2d19dd | 2543 | (define (process-define-module args) |
f8a502cb TTN |
2544 | (let* ((module-id (car args)) |
2545 | (module (resolve-module module-id #f)) | |
2546 | (kws (cdr args)) | |
2547 | (unrecognized (lambda (arg) | |
2548 | (error "unrecognized define-module argument" arg)))) | |
0f2d19dd | 2549 | (beautify-user-module! module) |
0209ca9a | 2550 | (let loop ((kws kws) |
1b92d94c AW |
2551 | (reversed-interfaces '()) |
2552 | (exports '()) | |
2553 | (re-exports '()) | |
2554 | (replacements '()) | |
608860a5 | 2555 | (autoloads '())) |
e4da0740 | 2556 | |
0209ca9a | 2557 | (if (null? kws) |
1b92d94c AW |
2558 | (call-with-deferred-observers |
2559 | (lambda () | |
2560 | (module-use-interfaces! module (reverse reversed-interfaces)) | |
2561 | (module-export! module exports) | |
2562 | (module-replace! module replacements) | |
2563 | (module-re-export! module re-exports) | |
608860a5 LC |
2564 | (if (not (null? autoloads)) |
2565 | (apply module-autoload! module autoloads)))) | |
1b92d94c AW |
2566 | (case (car kws) |
2567 | ((#:use-module #:use-syntax) | |
2568 | (or (pair? (cdr kws)) | |
2569 | (unrecognized kws)) | |
13182603 AW |
2570 | (cond |
2571 | ((equal? (caadr kws) '(ice-9 syncase)) | |
2572 | (issue-deprecation-warning | |
2573 | "(ice-9 syncase) is deprecated. Support for syntax-case is now in Guile core.") | |
1b92d94c | 2574 | (loop (cddr kws) |
13182603 | 2575 | reversed-interfaces |
1b92d94c AW |
2576 | exports |
2577 | re-exports | |
2578 | replacements | |
13182603 AW |
2579 | autoloads)) |
2580 | (else | |
2581 | (let* ((interface-args (cadr kws)) | |
2582 | (interface (apply resolve-interface interface-args))) | |
2583 | (and (eq? (car kws) #:use-syntax) | |
2584 | (or (symbol? (caar interface-args)) | |
2585 | (error "invalid module name for use-syntax" | |
2586 | (car interface-args))) | |
2587 | (set-module-transformer! | |
2588 | module | |
2589 | (module-ref interface | |
2590 | (car (last-pair (car interface-args))) | |
2591 | #f))) | |
2592 | (loop (cddr kws) | |
2593 | (cons interface reversed-interfaces) | |
2594 | exports | |
2595 | re-exports | |
2596 | replacements | |
2597 | autoloads))))) | |
1b92d94c AW |
2598 | ((#:autoload) |
2599 | (or (and (pair? (cdr kws)) (pair? (cddr kws))) | |
2600 | (unrecognized kws)) | |
2601 | (loop (cdddr kws) | |
608860a5 | 2602 | reversed-interfaces |
1b92d94c AW |
2603 | exports |
2604 | re-exports | |
2605 | replacements | |
608860a5 LC |
2606 | (let ((name (cadr kws)) |
2607 | (bindings (caddr kws))) | |
2608 | (cons* name bindings autoloads)))) | |
1b92d94c AW |
2609 | ((#:no-backtrace) |
2610 | (set-system-module! module #t) | |
2611 | (loop (cdr kws) reversed-interfaces exports re-exports | |
608860a5 | 2612 | replacements autoloads)) |
1b92d94c AW |
2613 | ((#:pure) |
2614 | (purify-module! module) | |
2615 | (loop (cdr kws) reversed-interfaces exports re-exports | |
608860a5 | 2616 | replacements autoloads)) |
dca14012 JG |
2617 | ((#:version) |
2618 | (or (pair? (cdr kws)) | |
2619 | (unrecognized kws)) | |
2620 | (let ((version (cadr kws))) | |
2621 | (set-module-version! module version) | |
2622 | (set-module-version! (module-public-interface module) version)) | |
2623 | (loop (cddr kws) reversed-interfaces exports re-exports | |
2624 | replacements autoloads)) | |
1b92d94c AW |
2625 | ((#:duplicates) |
2626 | (if (not (pair? (cdr kws))) | |
2627 | (unrecognized kws)) | |
2628 | (set-module-duplicates-handlers! | |
2629 | module | |
2630 | (lookup-duplicates-handlers (cadr kws))) | |
2631 | (loop (cddr kws) reversed-interfaces exports re-exports | |
608860a5 | 2632 | replacements autoloads)) |
1b92d94c AW |
2633 | ((#:export #:export-syntax) |
2634 | (or (pair? (cdr kws)) | |
2635 | (unrecognized kws)) | |
2636 | (loop (cddr kws) | |
2637 | reversed-interfaces | |
2638 | (append (cadr kws) exports) | |
2639 | re-exports | |
2640 | replacements | |
608860a5 | 2641 | autoloads)) |
1b92d94c AW |
2642 | ((#:re-export #:re-export-syntax) |
2643 | (or (pair? (cdr kws)) | |
2644 | (unrecognized kws)) | |
2645 | (loop (cddr kws) | |
2646 | reversed-interfaces | |
2647 | exports | |
2648 | (append (cadr kws) re-exports) | |
2649 | replacements | |
608860a5 | 2650 | autoloads)) |
1b92d94c AW |
2651 | ((#:replace #:replace-syntax) |
2652 | (or (pair? (cdr kws)) | |
2653 | (unrecognized kws)) | |
2654 | (loop (cddr kws) | |
2655 | reversed-interfaces | |
2656 | exports | |
2657 | re-exports | |
2658 | (append (cadr kws) replacements) | |
608860a5 | 2659 | autoloads)) |
1b92d94c AW |
2660 | (else |
2661 | (unrecognized kws))))) | |
db853761 | 2662 | (run-hook module-defined-hook module) |
0f2d19dd | 2663 | module)) |
71225060 | 2664 | |
db853761 NJ |
2665 | ;; `module-defined-hook' is a hook that is run whenever a new module |
2666 | ;; is defined. Its members are called with one argument, the new | |
2667 | ;; module. | |
2668 | (define module-defined-hook (make-hook 1)) | |
2669 | ||
3d2ada2f DH |
2670 | \f |
2671 | ||
71225060 | 2672 | ;;; {Autoload} |
3d2ada2f | 2673 | ;;; |
71225060 MD |
2674 | |
2675 | (define (make-autoload-interface module name bindings) | |
2676 | (let ((b (lambda (a sym definep) | |
9b5a0d84 AW |
2677 | (and (memq sym bindings) |
2678 | (let ((i (module-public-interface (resolve-module name)))) | |
2679 | (if (not i) | |
2680 | (error "missing interface for module" name)) | |
2681 | (let ((autoload (memq a (module-uses module)))) | |
2682 | ;; Replace autoload-interface with actual interface if | |
2683 | ;; that has not happened yet. | |
2684 | (if (pair? autoload) | |
2685 | (set-car! autoload i))) | |
2686 | (module-local-variable i sym)))))) | |
608860a5 | 2687 | (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f |
f905381d | 2688 | (make-hash-table 0) '() (make-weak-value-hash-table 31) #f |
4e48b495 | 2689 | (make-hash-table 0) #f #f))) |
608860a5 LC |
2690 | |
2691 | (define (module-autoload! module . args) | |
2692 | "Have @var{module} automatically load the module named @var{name} when one | |
2693 | of the symbols listed in @var{bindings} is looked up. @var{args} should be a | |
2694 | list of module-name/binding-list pairs, e.g., as in @code{(module-autoload! | |
2695 | module '(ice-9 q) '(make-q q-length))}." | |
2696 | (let loop ((args args)) | |
2697 | (cond ((null? args) | |
2698 | #t) | |
2699 | ((null? (cdr args)) | |
2700 | (error "invalid name+binding autoload list" args)) | |
2701 | (else | |
2702 | (let ((name (car args)) | |
2703 | (bindings (cadr args))) | |
2704 | (module-use! module (make-autoload-interface module | |
2705 | name bindings)) | |
2706 | (loop (cddr args))))))) | |
2707 | ||
71225060 | 2708 | |
0f2d19dd | 2709 | \f |
3d2ada2f | 2710 | |
44cf1f0f | 2711 | ;;; {Autoloading modules} |
3d2ada2f | 2712 | ;;; |
0f2d19dd JB |
2713 | |
2714 | (define autoloads-in-progress '()) | |
2715 | ||
482a28f9 MV |
2716 | ;; This function is called from "modules.c". If you change it, be |
2717 | ;; sure to update "modules.c" as well. | |
2718 | ||
dca14012 | 2719 | (define (try-module-autoload module-name . args) |
0f2d19dd | 2720 | (let* ((reverse-name (reverse module-name)) |
9b5a0d84 | 2721 | (name (symbol->string (car reverse-name))) |
dca14012 | 2722 | (version (and (not (null? args)) (car args))) |
9b5a0d84 AW |
2723 | (dir-hint-module-name (reverse (cdr reverse-name))) |
2724 | (dir-hint (apply string-append | |
2725 | (map (lambda (elt) | |
2726 | (string-append (symbol->string elt) "/")) | |
2727 | dir-hint-module-name)))) | |
0209ca9a | 2728 | (resolve-module dir-hint-module-name #f) |
0f2d19dd | 2729 | (and (not (autoload-done-or-in-progress? dir-hint name)) |
9b5a0d84 AW |
2730 | (let ((didit #f)) |
2731 | (dynamic-wind | |
2732 | (lambda () (autoload-in-progress! dir-hint name)) | |
2733 | (lambda () | |
eddd16d7 AW |
2734 | (with-fluids ((current-reader #f)) |
2735 | (save-module-excursion | |
2736 | (lambda () | |
2737 | (if version | |
2738 | (load (find-versioned-module | |
2739 | dir-hint name version %load-path)) | |
2740 | (primitive-load-path (in-vicinity dir-hint name) #f)) | |
2741 | (set! didit #t))))) | |
9b5a0d84 AW |
2742 | (lambda () (set-autoloaded! dir-hint name didit))) |
2743 | didit)))) | |
0f2d19dd | 2744 | |
71225060 | 2745 | \f |
3d2ada2f DH |
2746 | |
2747 | ;;; {Dynamic linking of modules} | |
2748 | ;;; | |
d0cbd20c | 2749 | |
0f2d19dd JB |
2750 | (define autoloads-done '((guile . guile))) |
2751 | ||
2752 | (define (autoload-done-or-in-progress? p m) | |
2753 | (let ((n (cons p m))) | |
2754 | (->bool (or (member n autoloads-done) | |
9b5a0d84 | 2755 | (member n autoloads-in-progress))))) |
0f2d19dd JB |
2756 | |
2757 | (define (autoload-done! p m) | |
2758 | (let ((n (cons p m))) | |
2759 | (set! autoloads-in-progress | |
9b5a0d84 | 2760 | (delete! n autoloads-in-progress)) |
0f2d19dd | 2761 | (or (member n autoloads-done) |
9b5a0d84 | 2762 | (set! autoloads-done (cons n autoloads-done))))) |
0f2d19dd JB |
2763 | |
2764 | (define (autoload-in-progress! p m) | |
2765 | (let ((n (cons p m))) | |
2766 | (set! autoloads-done | |
9b5a0d84 | 2767 | (delete! n autoloads-done)) |
0f2d19dd JB |
2768 | (set! autoloads-in-progress (cons n autoloads-in-progress)))) |
2769 | ||
2770 | (define (set-autoloaded! p m done?) | |
2771 | (if done? | |
2772 | (autoload-done! p m) | |
2773 | (let ((n (cons p m))) | |
9b5a0d84 AW |
2774 | (set! autoloads-done (delete! n autoloads-done)) |
2775 | (set! autoloads-in-progress (delete! n autoloads-in-progress))))) | |
0f2d19dd | 2776 | |
0f2d19dd JB |
2777 | \f |
2778 | ||
83b38198 | 2779 | ;;; {Run-time options} |
3d2ada2f | 2780 | ;;; |
83b38198 | 2781 | |
27af6bc2 | 2782 | (defmacro define-option-interface (option-group) |
9ea12179 | 2783 | (let* ((option-name 'car) |
9b5a0d84 AW |
2784 | (option-value 'cadr) |
2785 | (option-documentation 'caddr) | |
e9bab9df | 2786 | |
9b5a0d84 | 2787 | ;; Below follow the macros defining the run-time option interfaces. |
e9bab9df | 2788 | |
9b5a0d84 AW |
2789 | (make-options (lambda (interface) |
2790 | `(lambda args | |
2791 | (cond ((null? args) (,interface)) | |
2792 | ((list? (car args)) | |
2793 | (,interface (car args)) (,interface)) | |
2794 | (else (for-each | |
27af6bc2 | 2795 | (lambda (option) |
9ea12179 | 2796 | (display (,option-name option)) |
27af6bc2 | 2797 | (if (< (string-length |
9ea12179 | 2798 | (symbol->string (,option-name option))) |
27af6bc2 AW |
2799 | 8) |
2800 | (display #\tab)) | |
2801 | (display #\tab) | |
9ea12179 | 2802 | (display (,option-value option)) |
27af6bc2 | 2803 | (display #\tab) |
9ea12179 | 2804 | (display (,option-documentation option)) |
27af6bc2 AW |
2805 | (newline)) |
2806 | (,interface #t))))))) | |
e9bab9df | 2807 | |
9b5a0d84 AW |
2808 | (make-enable (lambda (interface) |
2809 | `(lambda flags | |
2810 | (,interface (append flags (,interface))) | |
2811 | (,interface)))) | |
2812 | ||
2813 | (make-disable (lambda (interface) | |
2814 | `(lambda flags | |
2815 | (let ((options (,interface))) | |
2816 | (for-each (lambda (flag) | |
2817 | (set! options (delq! flag options))) | |
2818 | flags) | |
2819 | (,interface options) | |
2820 | (,interface)))))) | |
27af6bc2 AW |
2821 | (let* ((interface (car option-group)) |
2822 | (options/enable/disable (cadr option-group))) | |
2823 | `(begin | |
2824 | (define ,(car options/enable/disable) | |
2825 | ,(make-options interface)) | |
2826 | (define ,(cadr options/enable/disable) | |
2827 | ,(make-enable interface)) | |
2828 | (define ,(caddr options/enable/disable) | |
2829 | ,(make-disable interface)) | |
2830 | (defmacro ,(caaddr option-group) (opt val) | |
2831 | `(,',(car options/enable/disable) | |
2832 | (append (,',(car options/enable/disable)) | |
2833 | (list ',opt ,val)))))))) | |
e9bab9df DH |
2834 | |
2835 | (define-option-interface | |
2836 | (eval-options-interface | |
2837 | (eval-options eval-enable eval-disable) | |
2838 | (eval-set!))) | |
2839 | ||
2840 | (define-option-interface | |
2841 | (debug-options-interface | |
2842 | (debug-options debug-enable debug-disable) | |
2843 | (debug-set!))) | |
2844 | ||
2845 | (define-option-interface | |
2846 | (evaluator-traps-interface | |
2847 | (traps trap-enable trap-disable) | |
2848 | (trap-set!))) | |
2849 | ||
2850 | (define-option-interface | |
2851 | (read-options-interface | |
2852 | (read-options read-enable read-disable) | |
2853 | (read-set!))) | |
2854 | ||
2855 | (define-option-interface | |
2856 | (print-options-interface | |
2857 | (print-options print-enable print-disable) | |
2858 | (print-set!))) | |
83b38198 MD |
2859 | |
2860 | \f | |
2861 | ||
0f2d19dd JB |
2862 | ;;; {Running Repls} |
2863 | ;;; | |
2864 | ||
2865 | (define (repl read evaler print) | |
75a97b92 | 2866 | (let loop ((source (read (current-input-port)))) |
0f2d19dd | 2867 | (print (evaler source)) |
75a97b92 | 2868 | (loop (read (current-input-port))))) |
0f2d19dd JB |
2869 | |
2870 | ;; A provisional repl that acts like the SCM repl: | |
2871 | ;; | |
2872 | (define scm-repl-silent #f) | |
2873 | (define (assert-repl-silence v) (set! scm-repl-silent v)) | |
2874 | ||
21ed9efe MD |
2875 | (define *unspecified* (if #f #f)) |
2876 | (define (unspecified? v) (eq? v *unspecified*)) | |
2877 | ||
2878 | (define scm-repl-print-unspecified #f) | |
2879 | (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v)) | |
2880 | ||
79451588 | 2881 | (define scm-repl-verbose #f) |
0f2d19dd JB |
2882 | (define (assert-repl-verbosity v) (set! scm-repl-verbose v)) |
2883 | ||
e6875011 | 2884 | (define scm-repl-prompt "guile> ") |
0f2d19dd | 2885 | |
e6875011 MD |
2886 | (define (set-repl-prompt! v) (set! scm-repl-prompt v)) |
2887 | ||
9f0e9918 | 2888 | (define (default-pre-unwind-handler key . args) |
06dcb9df AW |
2889 | ;; Narrow by two more frames: this one, and the throw handler. |
2890 | (save-stack 2) | |
d5d34fa1 MD |
2891 | (apply throw key args)) |
2892 | ||
1351c2db AW |
2893 | (begin-deprecated |
2894 | (define (pre-unwind-handler-dispatch key . args) | |
2895 | (apply default-pre-unwind-handler key args))) | |
0f2d19dd | 2896 | |
3e3cec45 | 2897 | (define abort-hook (make-hook)) |
59e1116d | 2898 | |
28d8ab3c GH |
2899 | ;; these definitions are used if running a script. |
2900 | ;; otherwise redefined in error-catching-loop. | |
2901 | (define (set-batch-mode?! arg) #t) | |
2902 | (define (batch-mode?) #t) | |
4bbbcd5c | 2903 | |
0f2d19dd | 2904 | (define (error-catching-loop thunk) |
4bbbcd5c | 2905 | (let ((status #f) |
9b5a0d84 | 2906 | (interactive #t)) |
8e44e7a0 | 2907 | (define (loop first) |
20edfbbd | 2908 | (let ((next |
9b5a0d84 AW |
2909 | (catch #t |
2910 | ||
2911 | (lambda () | |
2912 | (call-with-unblocked-asyncs | |
2913 | (lambda () | |
2914 | (with-traps | |
2915 | (lambda () | |
2916 | (first) | |
2917 | ||
2918 | ;; This line is needed because mark | |
2919 | ;; doesn't do closures quite right. | |
2920 | ;; Unreferenced locals should be | |
2921 | ;; collected. | |
2922 | (set! first #f) | |
2923 | (let loop ((v (thunk))) | |
2924 | (loop (thunk))) | |
2925 | #f))))) | |
2926 | ||
2927 | (lambda (key . args) | |
2928 | (case key | |
2929 | ((quit) | |
2930 | (set! status args) | |
2931 | #f) | |
2932 | ||
2933 | ((switch-repl) | |
2934 | (apply throw 'switch-repl args)) | |
2935 | ||
2936 | ((abort) | |
2937 | ;; This is one of the closures that require | |
2938 | ;; (set! first #f) above | |
2939 | ;; | |
2940 | (lambda () | |
2941 | (run-hook abort-hook) | |
2942 | (force-output (current-output-port)) | |
2943 | (display "ABORT: " (current-error-port)) | |
2944 | (write args (current-error-port)) | |
2945 | (newline (current-error-port)) | |
2946 | (if interactive | |
2947 | (begin | |
2948 | (if (and | |
2949 | (not has-shown-debugger-hint?) | |
2950 | (not (memq 'backtrace | |
2951 | (debug-options-interface))) | |
2952 | (stack? (fluid-ref the-last-stack))) | |
2953 | (begin | |
2954 | (newline (current-error-port)) | |
2955 | (display | |
2956 | "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n" | |
2957 | (current-error-port)) | |
2958 | (set! has-shown-debugger-hint? #t))) | |
2959 | (force-output (current-error-port))) | |
2960 | (begin | |
2961 | (primitive-exit 1))) | |
2962 | (set! stack-saved? #f))) | |
2963 | ||
2964 | (else | |
2965 | ;; This is the other cons-leak closure... | |
2966 | (lambda () | |
2967 | (cond ((= (length args) 4) | |
2968 | (apply handle-system-error key args)) | |
2969 | (else | |
2970 | (apply bad-throw key args))))))) | |
56658166 | 2971 | |
1351c2db | 2972 | default-pre-unwind-handler))) |
56658166 | 2973 | |
9b5a0d84 | 2974 | (if next (loop next) status))) |
5f5f2642 | 2975 | (set! set-batch-mode?! (lambda (arg) |
9b5a0d84 AW |
2976 | (cond (arg |
2977 | (set! interactive #f) | |
2978 | (restore-signals)) | |
2979 | (#t | |
2980 | (error "sorry, not implemented"))))) | |
5f5f2642 | 2981 | (set! batch-mode? (lambda () (not interactive))) |
bb00edfa MV |
2982 | (call-with-blocked-asyncs |
2983 | (lambda () (loop (lambda () #t)))))) | |
0f2d19dd | 2984 | |
8bb7f646 | 2985 | ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace () |
8087b6be | 2986 | (define before-signal-stack (make-fluid)) |
06dcb9df | 2987 | ;; FIXME: stack-saved? is broken in the presence of threads. |
21ed9efe MD |
2988 | (define stack-saved? #f) |
2989 | ||
2990 | (define (save-stack . narrowing) | |
06dcb9df AW |
2991 | (if (not stack-saved?) |
2992 | (begin | |
2993 | (let ((stacks (fluid-ref %stacks))) | |
2994 | (fluid-set! the-last-stack | |
2995 | ;; (make-stack obj inner outer inner outer ...) | |
2996 | ;; | |
2997 | ;; In this case, cut away the make-stack frame, the | |
2998 | ;; save-stack frame, and then narrow as specified by the | |
2999 | ;; user, delimited by the nearest start-stack invocation, | |
3000 | ;; if any. | |
3001 | (apply make-stack #t | |
3002 | 2 | |
3003 | (if (pair? stacks) (cdar stacks) 0) | |
3004 | narrowing))) | |
3005 | (set! stack-saved? #t)))) | |
1c6cd8e8 | 3006 | |
3e3cec45 MD |
3007 | (define before-error-hook (make-hook)) |
3008 | (define after-error-hook (make-hook)) | |
3009 | (define before-backtrace-hook (make-hook)) | |
3010 | (define after-backtrace-hook (make-hook)) | |
1c6cd8e8 | 3011 | |
21ed9efe MD |
3012 | (define has-shown-debugger-hint? #f) |
3013 | ||
35c5db87 GH |
3014 | (define (handle-system-error key . args) |
3015 | (let ((cep (current-error-port))) | |
8bb7f646 | 3016 | (cond ((not (stack? (fluid-ref the-last-stack)))) |
9b5a0d84 AW |
3017 | ((memq 'backtrace (debug-options-interface)) |
3018 | (let ((highlights (if (or (eq? key 'wrong-type-arg) | |
3019 | (eq? key 'out-of-range)) | |
3020 | (list-ref args 3) | |
3021 | '()))) | |
3022 | (run-hook before-backtrace-hook) | |
3023 | (newline cep) | |
3024 | (display "Backtrace:\n") | |
3025 | (display-backtrace (fluid-ref the-last-stack) cep | |
3026 | #f #f highlights) | |
3027 | (newline cep) | |
3028 | (run-hook after-backtrace-hook)))) | |
04efd24d | 3029 | (run-hook before-error-hook) |
8bb7f646 | 3030 | (apply display-error (fluid-ref the-last-stack) cep args) |
04efd24d | 3031 | (run-hook after-error-hook) |
35c5db87 GH |
3032 | (force-output cep) |
3033 | (throw 'abort key))) | |
21ed9efe | 3034 | |
0f2d19dd JB |
3035 | (define (quit . args) |
3036 | (apply throw 'quit args)) | |
3037 | ||
7950df7c GH |
3038 | (define exit quit) |
3039 | ||
d590bbf6 MD |
3040 | ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace () |
3041 | ||
3042 | ;; Replaced by C code: | |
3043 | ;;(define (backtrace) | |
8bb7f646 | 3044 | ;; (if (fluid-ref the-last-stack) |
d590bbf6 | 3045 | ;; (begin |
9b5a0d84 AW |
3046 | ;; (newline) |
3047 | ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port)) | |
3048 | ;; (newline) | |
3049 | ;; (if (and (not has-shown-backtrace-hint?) | |
3050 | ;; (not (memq 'backtrace (debug-options-interface)))) | |
3051 | ;; (begin | |
3052 | ;; (display | |
d590bbf6 MD |
3053 | ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace |
3054 | ;;automatically if an error occurs in the future.\n") | |
9b5a0d84 | 3055 | ;; (set! has-shown-backtrace-hint? #t)))) |
d590bbf6 | 3056 | ;; (display "No backtrace available.\n"))) |
21ed9efe | 3057 | |
0f2d19dd | 3058 | (define (error-catching-repl r e p) |
5f89fb13 MV |
3059 | (error-catching-loop |
3060 | (lambda () | |
3061 | (call-with-values (lambda () (e (r))) | |
3062 | (lambda the-values (for-each p the-values)))))) | |
0f2d19dd JB |
3063 | |
3064 | (define (gc-run-time) | |
3065 | (cdr (assq 'gc-time-taken (gc-stats)))) | |
3066 | ||
3e3cec45 MD |
3067 | (define before-read-hook (make-hook)) |
3068 | (define after-read-hook (make-hook)) | |
870777d7 KN |
3069 | (define before-eval-hook (make-hook 1)) |
3070 | (define after-eval-hook (make-hook 1)) | |
3071 | (define before-print-hook (make-hook 1)) | |
3072 | (define after-print-hook (make-hook 1)) | |
1c6cd8e8 | 3073 | |
dc5c2038 MD |
3074 | ;;; The default repl-reader function. We may override this if we've |
3075 | ;;; the readline library. | |
3076 | (define repl-reader | |
a58b7fbb | 3077 | (lambda (prompt . reader) |
0becb8f3 AW |
3078 | (if (not (char-ready?)) |
3079 | (display (if (string? prompt) prompt (prompt)))) | |
dc5c2038 | 3080 | (force-output) |
04efd24d | 3081 | (run-hook before-read-hook) |
a58b7fbb AW |
3082 | ((or (and (pair? reader) (car reader)) |
3083 | (fluid-ref current-reader) | |
3084 | read) | |
3085 | (current-input-port)))) | |
dc5c2038 | 3086 | |
0f2d19dd | 3087 | (define (scm-style-repl) |
9d774814 | 3088 | |
0f2d19dd | 3089 | (letrec ( |
9b5a0d84 AW |
3090 | (start-gc-rt #f) |
3091 | (start-rt #f) | |
3092 | (repl-report-start-timing (lambda () | |
3093 | (set! start-gc-rt (gc-run-time)) | |
3094 | (set! start-rt (get-internal-run-time)))) | |
3095 | (repl-report (lambda () | |
3096 | (display ";;; ") | |
3097 | (display (inexact->exact | |
3098 | (* 1000 (/ (- (get-internal-run-time) start-rt) | |
3099 | internal-time-units-per-second)))) | |
3100 | (display " msec (") | |
3101 | (display (inexact->exact | |
3102 | (* 1000 (/ (- (gc-run-time) start-gc-rt) | |
3103 | internal-time-units-per-second)))) | |
3104 | (display " msec in gc)\n"))) | |
3105 | ||
3106 | (consume-trailing-whitespace | |
3107 | (lambda () | |
3108 | (let ((ch (peek-char))) | |
3109 | (cond | |
3110 | ((eof-object? ch)) | |
3111 | ((or (char=? ch #\space) (char=? ch #\tab)) | |
3112 | (read-char) | |
3113 | (consume-trailing-whitespace)) | |
3114 | ((char=? ch #\newline) | |
3115 | (read-char)))))) | |
3116 | (-read (lambda () | |
3117 | (let ((val | |
3118 | (let ((prompt (cond ((string? scm-repl-prompt) | |
3119 | scm-repl-prompt) | |
3120 | ((thunk? scm-repl-prompt) | |
3121 | (scm-repl-prompt)) | |
3122 | (scm-repl-prompt "> ") | |
3123 | (else "")))) | |
3124 | (repl-reader prompt)))) | |
3125 | ||
3126 | ;; As described in R4RS, the READ procedure updates the | |
3127 | ;; port to point to the first character past the end of | |
3128 | ;; the external representation of the object. This | |
3129 | ;; means that it doesn't consume the newline typically | |
3130 | ;; found after an expression. This means that, when | |
3131 | ;; debugging Guile with GDB, GDB gets the newline, which | |
3132 | ;; it often interprets as a "continue" command, making | |
3133 | ;; breakpoints kind of useless. So, consume any | |
3134 | ;; trailing newline here, as well as any whitespace | |
3135 | ;; before it. | |
3136 | ;; But not if EOF, for control-D. | |
3137 | (if (not (eof-object? val)) | |
3138 | (consume-trailing-whitespace)) | |
3139 | (run-hook after-read-hook) | |
3140 | (if (eof-object? val) | |
3141 | (begin | |
3142 | (repl-report-start-timing) | |
3143 | (if scm-repl-verbose | |
3144 | (begin | |
3145 | (newline) | |
3146 | (display ";;; EOF -- quitting") | |
3147 | (newline))) | |
3148 | (quit 0))) | |
3149 | val))) | |
3150 | ||
3151 | (-eval (lambda (sourc) | |
3152 | (repl-report-start-timing) | |
3153 | (run-hook before-eval-hook sourc) | |
3154 | (let ((val (start-stack 'repl-stack | |
3155 | ;; If you change this procedure | |
3156 | ;; (primitive-eval), please also | |
3157 | ;; modify the repl-stack case in | |
3158 | ;; save-stack so that stack cutting | |
3159 | ;; continues to work. | |
3160 | (primitive-eval sourc)))) | |
3161 | (run-hook after-eval-hook sourc) | |
3162 | val))) | |
3163 | ||
3164 | ||
3165 | (-print (let ((maybe-print (lambda (result) | |
3166 | (if (or scm-repl-print-unspecified | |
3167 | (not (unspecified? result))) | |
3168 | (begin | |
3169 | (write result) | |
3170 | (newline)))))) | |
3171 | (lambda (result) | |
3172 | (if (not scm-repl-silent) | |
3173 | (begin | |
3174 | (run-hook before-print-hook result) | |
3175 | (maybe-print result) | |
3176 | (run-hook after-print-hook result) | |
3177 | (if scm-repl-verbose | |
3178 | (repl-report)) | |
3179 | (force-output)))))) | |
3180 | ||
3181 | (-quit (lambda (args) | |
3182 | (if scm-repl-verbose | |
3183 | (begin | |
3184 | (display ";;; QUIT executed, repl exitting") | |
3185 | (newline) | |
3186 | (repl-report))) | |
3187 | args))) | |
0f2d19dd | 3188 | |
8e44e7a0 | 3189 | (let ((status (error-catching-repl -read |
9b5a0d84 AW |
3190 | -eval |
3191 | -print))) | |
8e44e7a0 | 3192 | (-quit status)))) |
20edfbbd | 3193 | |
0f2d19dd | 3194 | |
0f2d19dd | 3195 | \f |
3d2ada2f | 3196 | |
44cf1f0f | 3197 | ;;; {IOTA functions: generating lists of numbers} |
3d2ada2f | 3198 | ;;; |
0f2d19dd | 3199 | |
e69cd299 MD |
3200 | (define (iota n) |
3201 | (let loop ((count (1- n)) (result '())) | |
3202 | (if (< count 0) result | |
3203 | (loop (1- count) (cons count result))))) | |
0f2d19dd JB |
3204 | |
3205 | \f | |
3d2ada2f | 3206 | |
7398c2c2 MD |
3207 | ;;; {collect} |
3208 | ;;; | |
3209 | ;;; Similar to `begin' but returns a list of the results of all constituent | |
3210 | ;;; forms instead of the result of the last form. | |
3211 | ;;; (The definition relies on the current left-to-right | |
3212 | ;;; order of evaluation of operands in applications.) | |
3d2ada2f | 3213 | ;;; |
7398c2c2 MD |
3214 | |
3215 | (defmacro collect forms | |
3216 | (cons 'list forms)) | |
0f2d19dd | 3217 | |
3d2ada2f DH |
3218 | \f |
3219 | ||
773abfbb KR |
3220 | ;;; {While} |
3221 | ;;; | |
3222 | ;;; with `continue' and `break'. | |
3223 | ;;; | |
3224 | ||
3225 | ;; The inner `do' loop avoids re-establishing a catch every iteration, | |
5578a53f KR |
3226 | ;; that's only necessary if continue is actually used. A new key is |
3227 | ;; generated every time, so break and continue apply to their originating | |
972c33e5 | 3228 | ;; `while' even when recursing. |
c8fc38b1 | 3229 | ;; |
972c33e5 AW |
3230 | ;; FIXME: This macro is unintentionally unhygienic with respect to let, |
3231 | ;; make-symbol, do, throw, catch, lambda, and not. | |
c8fc38b1 | 3232 | ;; |
773abfbb | 3233 | (define-macro (while cond . body) |
972c33e5 AW |
3234 | (let ((keyvar (make-symbol "while-keyvar"))) |
3235 | `(let ((,keyvar (make-symbol "while-key"))) | |
3236 | (do () | |
3237 | ((catch ,keyvar | |
3238 | (lambda () | |
3239 | (let ((break (lambda () (throw ,keyvar #t))) | |
3240 | (continue (lambda () (throw ,keyvar #f)))) | |
3241 | (do () | |
3242 | ((not ,cond)) | |
3243 | ,@body) | |
3244 | #t)) | |
3245 | (lambda (key arg) | |
3246 | arg))))))) | |
5578a53f | 3247 | |
773abfbb | 3248 | |
0f2d19dd | 3249 | \f |
3d2ada2f | 3250 | |
0f2d19dd JB |
3251 | ;;; {Module System Macros} |
3252 | ;;; | |
3253 | ||
532cf805 MV |
3254 | ;; Return a list of expressions that evaluate to the appropriate |
3255 | ;; arguments for resolve-interface according to SPEC. | |
3256 | ||
b15dea68 | 3257 | (eval-when |
25d8cd3a AW |
3258 | (compile) |
3259 | (if (memq 'prefix (read-options)) | |
3260 | (error "boot-9 must be compiled with #:kw, not :kw"))) | |
1a1a10d3 | 3261 | |
532cf805 MV |
3262 | (define (keyword-like-symbol->keyword sym) |
3263 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) | |
3264 | ||
074e036e AW |
3265 | ;; FIXME: we really need to clean up the guts of the module system. |
3266 | ;; We can compile to something better than process-define-module. | |
3267 | (define-syntax define-module | |
3268 | (lambda (x) | |
3269 | (define (keyword-like? stx) | |
3270 | (let ((dat (syntax->datum stx))) | |
3271 | (and (symbol? dat) | |
3272 | (eqv? (string-ref (symbol->string dat) 0) #\:)))) | |
3273 | (define (->keyword sym) | |
3274 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) | |
3275 | ||
3276 | (define (quotify-iface args) | |
3277 | (let loop ((in args) (out '())) | |
3278 | (syntax-case in () | |
3279 | (() (reverse! out)) | |
3280 | ;; The user wanted #:foo, but wrote :foo. Fix it. | |
3281 | ((sym . in) (keyword-like? #'sym) | |
3282 | (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) | |
3283 | ((kw . in) (not (keyword? (syntax->datum #'kw))) | |
3284 | (syntax-violation 'define-module "expected keyword arg" x #'kw)) | |
3285 | ((#:renamer renamer . in) | |
3286 | (loop #'in (cons* #'renamer #:renamer out))) | |
3287 | ((kw val . in) | |
3288 | (loop #'in (cons* #''val #'kw out)))))) | |
3289 | ||
3290 | (define (quotify args) | |
3291 | ;; Just quote everything except #:use-module and #:use-syntax. We | |
3292 | ;; need to know about all arguments regardless since we want to turn | |
3293 | ;; symbols that look like keywords into real keywords, and the | |
3294 | ;; keyword args in a define-module form are not regular | |
3295 | ;; (i.e. no-backtrace doesn't take a value). | |
3296 | (let loop ((in args) (out '())) | |
3297 | (syntax-case in () | |
3298 | (() (reverse! out)) | |
3299 | ;; The user wanted #:foo, but wrote :foo. Fix it. | |
3300 | ((sym . in) (keyword-like? #'sym) | |
3301 | (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) | |
3302 | ((kw . in) (not (keyword? (syntax->datum #'kw))) | |
3303 | (syntax-violation 'define-module "expected keyword arg" x #'kw)) | |
3304 | ((#:no-backtrace . in) | |
3305 | (loop #'in (cons #:no-backtrace out))) | |
3306 | ((#:pure . in) | |
3307 | (loop #'in (cons #:pure out))) | |
3308 | ((kw) | |
3309 | (syntax-violation 'define-module "keyword arg without value" x #'kw)) | |
3310 | ((use-module (name name* ...) . in) | |
3311 | (and (memq (syntax->datum #'use-module) '(#:use-module #:use-syntax)) | |
3312 | (and-map symbol? (syntax->datum #'(name name* ...)))) | |
3313 | (loop #'in | |
3314 | (cons* #''((name name* ...)) | |
3315 | #'use-module | |
3316 | out))) | |
3317 | ((use-module ((name name* ...) arg ...) . in) | |
3318 | (and (memq (syntax->datum #'use-module) '(#:use-module #:use-syntax)) | |
3319 | (and-map symbol? (syntax->datum #'(name name* ...)))) | |
3320 | (loop #'in | |
3321 | (cons* #`(list '(name name* ...) #,@(quotify-iface #'(arg ...))) | |
3322 | #'use-module | |
3323 | out))) | |
3324 | ((#:autoload name bindings . in) | |
3325 | (loop #'in (cons* #''bindings #''name #:autoload out))) | |
3326 | ((kw val . in) | |
3327 | (loop #'in (cons* #''val #'kw out)))))) | |
3328 | ||
3329 | (syntax-case x () | |
3330 | ((_ (name name* ...) arg ...) | |
3331 | (with-syntax (((quoted-arg ...) (quotify #'(arg ...)))) | |
d9b1c71a | 3332 | #'(eval-when (eval load compile expand) |
074e036e AW |
3333 | (let ((m (process-define-module |
3334 | (list '(name name* ...) quoted-arg ...)))) | |
3335 | (set-current-module m) | |
3336 | m))))))) | |
0f2d19dd | 3337 | |
532cf805 MV |
3338 | ;; The guts of the use-modules macro. Add the interfaces of the named |
3339 | ;; modules to the use-list of the current module, in order. | |
3340 | ||
482a28f9 MV |
3341 | ;; This function is called by "modules.c". If you change it, be sure |
3342 | ;; to change scm_c_use_module as well. | |
3343 | ||
532cf805 | 3344 | (define (process-use-modules module-interface-args) |
d57da08b | 3345 | (let ((interfaces (map (lambda (mif-args) |
9b5a0d84 AW |
3346 | (or (apply resolve-interface mif-args) |
3347 | (error "no such module" mif-args))) | |
3348 | module-interface-args))) | |
d57da08b MD |
3349 | (call-with-deferred-observers |
3350 | (lambda () | |
3351 | (module-use-interfaces! (current-module) interfaces))))) | |
89da9036 | 3352 | |
4e3328ce AW |
3353 | (define-syntax use-modules |
3354 | (lambda (x) | |
3355 | (define (keyword-like? stx) | |
3356 | (let ((dat (syntax->datum stx))) | |
3357 | (and (symbol? dat) | |
3358 | (eqv? (string-ref (symbol->string dat) 0) #\:)))) | |
3359 | (define (->keyword sym) | |
3360 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) | |
3361 | ||
3362 | (define (quotify-iface args) | |
3363 | (let loop ((in args) (out '())) | |
3364 | (syntax-case in () | |
3365 | (() (reverse! out)) | |
3366 | ;; The user wanted #:foo, but wrote :foo. Fix it. | |
3367 | ((sym . in) (keyword-like? #'sym) | |
3368 | (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) | |
3369 | ((kw . in) (not (keyword? (syntax->datum #'kw))) | |
3370 | (syntax-violation 'define-module "expected keyword arg" x #'kw)) | |
3371 | ((#:renamer renamer . in) | |
3372 | (loop #'in (cons* #'renamer #:renamer out))) | |
3373 | ((kw val . in) | |
3374 | (loop #'in (cons* #''val #'kw out)))))) | |
3375 | ||
3376 | (define (quotify specs) | |
3377 | (let lp ((in specs) (out '())) | |
3378 | (syntax-case in () | |
3379 | (() (reverse out)) | |
3380 | (((name name* ...) . in) | |
3381 | (and-map symbol? (syntax->datum #'(name name* ...))) | |
3382 | (lp #'in (cons #''((name name* ...)) out))) | |
3383 | ((((name name* ...) arg ...) . in) | |
3384 | (and-map symbol? (syntax->datum #'(name name* ...))) | |
3385 | (with-syntax (((quoted-arg ...) (quotify-iface #'(arg ...)))) | |
3386 | (lp #'in (cons #`(list '(name name* ...) quoted-arg ...) | |
3387 | out))))))) | |
3388 | ||
3389 | (syntax-case x () | |
3390 | ((_ spec ...) | |
3391 | (with-syntax (((quoted-args ...) (quotify #'(spec ...)))) | |
520caaeb | 3392 | #'(eval-when (eval load compile expand) |
4e3328ce AW |
3393 | (process-use-modules (list quoted-args ...)) |
3394 | *unspecified*)))))) | |
3395 | ||
3396 | (define-syntax use-syntax | |
3397 | (syntax-rules () | |
3398 | ((_ spec ...) | |
3399 | (begin | |
520caaeb | 3400 | (eval-when (eval load compile expand) |
4e3328ce AW |
3401 | (issue-deprecation-warning |
3402 | "`use-syntax' is deprecated. Please contact guile-devel for more info.")) | |
3403 | (use-modules spec ...))))) | |
0f2d19dd | 3404 | |
b1e4c7cc JG |
3405 | (include-from-path "ice-9/r6rs-libraries") |
3406 | ||
13182603 AW |
3407 | (define-syntax define-private |
3408 | (syntax-rules () | |
3409 | ((_ foo bar) | |
3410 | (define foo bar)))) | |
3411 | ||
3412 | (define-syntax define-public | |
3413 | (syntax-rules () | |
3414 | ((_ (name . args) . body) | |
3415 | (define-public name (lambda args . body))) | |
3416 | ((_ name val) | |
3417 | (begin | |
3418 | (define name val) | |
3419 | (export name))))) | |
3420 | ||
3421 | (define-syntax defmacro-public | |
3422 | (syntax-rules () | |
3423 | ((_ name args . body) | |
3424 | (begin | |
3425 | (defmacro name args . body) | |
3426 | (export-syntax name))))) | |
0f2d19dd | 3427 | |
87e00370 LC |
3428 | ;; And now for the most important macro. |
3429 | (define-syntax λ | |
3430 | (syntax-rules () | |
3431 | ((_ formals body ...) | |
3432 | (lambda formals body ...)))) | |
3433 | ||
3434 | \f | |
89d06712 | 3435 | ;; Export a local variable |
482a28f9 MV |
3436 | |
3437 | ;; This function is called from "modules.c". If you change it, be | |
3438 | ;; sure to update "modules.c" as well. | |
3439 | ||
90847923 MD |
3440 | (define (module-export! m names) |
3441 | (let ((public-i (module-public-interface m))) | |
3442 | (for-each (lambda (name) | |
78c22f5e JG |
3443 | (let* ((internal-name (if (pair? name) (car name) name)) |
3444 | (external-name (if (pair? name) (cdr name) name)) | |
3445 | (var (module-ensure-local-variable! m internal-name))) | |
3446 | (module-add! public-i external-name var))) | |
9b5a0d84 | 3447 | names))) |
89d06712 | 3448 | |
f595ccfe MD |
3449 | (define (module-replace! m names) |
3450 | (let ((public-i (module-public-interface m))) | |
3451 | (for-each (lambda (name) | |
78c22f5e JG |
3452 | (let* ((internal-name (if (pair? name) (car name) name)) |
3453 | (external-name (if (pair? name) (cdr name) name)) | |
3454 | (var (module-ensure-local-variable! m internal-name))) | |
9b5a0d84 | 3455 | (set-object-property! var 'replace #t) |
78c22f5e | 3456 | (module-add! public-i external-name var))) |
9b5a0d84 | 3457 | names))) |
f595ccfe | 3458 | |
d2b7b761 AW |
3459 | ;; Export all local variables from a module |
3460 | ;; | |
3461 | (define (module-export-all! mod) | |
3462 | (define (fresh-interface!) | |
3463 | (let ((iface (make-module))) | |
3464 | (set-module-name! iface (module-name mod)) | |
3465 | ;; for guile 2: (set-module-version! iface (module-version mod)) | |
3466 | (set-module-kind! iface 'interface) | |
3467 | (set-module-public-interface! mod iface) | |
3468 | iface)) | |
3469 | (let ((iface (or (module-public-interface mod) | |
3470 | (fresh-interface!)))) | |
3471 | (set-module-obarray! iface (module-obarray mod)))) | |
3472 | ||
89d06712 MV |
3473 | ;; Re-export a imported variable |
3474 | ;; | |
3475 | (define (module-re-export! m names) | |
3476 | (let ((public-i (module-public-interface m))) | |
3477 | (for-each (lambda (name) | |
78c22f5e JG |
3478 | (let* ((internal-name (if (pair? name) (car name) name)) |
3479 | (external-name (if (pair? name) (cdr name) name)) | |
3480 | (var (module-variable m internal-name))) | |
9b5a0d84 | 3481 | (cond ((not var) |
78c22f5e JG |
3482 | (error "Undefined variable:" internal-name)) |
3483 | ((eq? var (module-local-variable m internal-name)) | |
3484 | (error "re-exporting local variable:" internal-name)) | |
9b5a0d84 | 3485 | (else |
78c22f5e | 3486 | (module-add! public-i external-name var))))) |
9b5a0d84 | 3487 | names))) |
90847923 | 3488 | |
d6151723 AW |
3489 | (define-syntax export |
3490 | (syntax-rules () | |
3491 | ((_ name ...) | |
520caaeb | 3492 | (eval-when (eval load compile expand) |
d6151723 AW |
3493 | (call-with-deferred-observers |
3494 | (lambda () | |
3495 | (module-export! (current-module) '(name ...)))))))) | |
a0cc0a01 | 3496 | |
d6151723 AW |
3497 | (define-syntax re-export |
3498 | (syntax-rules () | |
3499 | ((_ name ...) | |
520caaeb | 3500 | (eval-when (eval load compile expand) |
d6151723 AW |
3501 | (call-with-deferred-observers |
3502 | (lambda () | |
3503 | (module-re-export! (current-module) '(name ...)))))))) | |
89d06712 | 3504 | |
d6151723 AW |
3505 | (define-syntax export-syntax |
3506 | (syntax-rules () | |
3507 | ((_ name ...) | |
3508 | (export name ...)))) | |
a0cc0a01 | 3509 | |
d6151723 AW |
3510 | (define-syntax re-export-syntax |
3511 | (syntax-rules () | |
3512 | ((_ name ...) | |
3513 | (re-export name ...)))) | |
a0cc0a01 | 3514 | |
0f2d19dd JB |
3515 | (define load load-module) |
3516 | ||
3de80ed5 AW |
3517 | \f |
3518 | ||
f595ccfe MD |
3519 | ;;; {Parameters} |
3520 | ;;; | |
3521 | ||
3522 | (define make-mutable-parameter | |
3523 | (let ((make (lambda (fluid converter) | |
9b5a0d84 AW |
3524 | (lambda args |
3525 | (if (null? args) | |
3526 | (fluid-ref fluid) | |
3527 | (fluid-set! fluid (converter (car args)))))))) | |
f595ccfe MD |
3528 | (lambda (init . converter) |
3529 | (let ((fluid (make-fluid)) | |
9b5a0d84 AW |
3530 | (converter (if (null? converter) |
3531 | identity | |
3532 | (car converter)))) | |
3533 | (fluid-set! fluid (converter init)) | |
3534 | (make fluid converter))))) | |
f595ccfe MD |
3535 | |
3536 | \f | |
3d2ada2f | 3537 | |
7b07e5ef MD |
3538 | ;;; {Handling of duplicate imported bindings} |
3539 | ;;; | |
3540 | ||
3541 | ;; Duplicate handlers take the following arguments: | |
3542 | ;; | |
3543 | ;; module importing module | |
9b5a0d84 AW |
3544 | ;; name conflicting name |
3545 | ;; int1 old interface where name occurs | |
3546 | ;; val1 value of binding in old interface | |
3547 | ;; int2 new interface where name occurs | |
3548 | ;; val2 value of binding in new interface | |
3549 | ;; var previous resolution or #f | |
3550 | ;; val value of previous resolution | |
7b07e5ef MD |
3551 | ;; |
3552 | ;; A duplicate handler can take three alternative actions: | |
3553 | ;; | |
3554 | ;; 1. return #f => leave responsibility to next handler | |
3555 | ;; 2. exit with an error | |
3556 | ;; 3. return a variable resolving the conflict | |
3557 | ;; | |
3558 | ||
3559 | (define duplicate-handlers | |
3560 | (let ((m (make-module 7))) | |
f595ccfe MD |
3561 | |
3562 | (define (check module name int1 val1 int2 val2 var val) | |
3563 | (scm-error 'misc-error | |
9b5a0d84 AW |
3564 | #f |
3565 | "~A: `~A' imported from both ~A and ~A" | |
3566 | (list (module-name module) | |
3567 | name | |
3568 | (module-name int1) | |
3569 | (module-name int2)) | |
3570 | #f)) | |
f595ccfe | 3571 | |
65bed4aa | 3572 | (define (warn module name int1 val1 int2 val2 var val) |
d7c0c26d | 3573 | (format (current-error-port) |
9b5a0d84 AW |
3574 | "WARNING: ~A: `~A' imported from both ~A and ~A\n" |
3575 | (module-name module) | |
3576 | name | |
3577 | (module-name int1) | |
3578 | (module-name int2)) | |
65bed4aa | 3579 | #f) |
f595ccfe MD |
3580 | |
3581 | (define (replace module name int1 val1 int2 val2 var val) | |
3582 | (let ((old (or (and var (object-property var 'replace) var) | |
9b5a0d84 AW |
3583 | (module-variable int1 name))) |
3584 | (new (module-variable int2 name))) | |
3585 | (if (object-property old 'replace) | |
3586 | (and (or (eq? old new) | |
3587 | (not (object-property new 'replace))) | |
3588 | old) | |
3589 | (and (object-property new 'replace) | |
3590 | new)))) | |
f595ccfe | 3591 | |
65bed4aa MD |
3592 | (define (warn-override-core module name int1 val1 int2 val2 var val) |
3593 | (and (eq? int1 the-scm-module) | |
9b5a0d84 AW |
3594 | (begin |
3595 | (format (current-error-port) | |
3596 | "WARNING: ~A: imported module ~A overrides core binding `~A'\n" | |
3597 | (module-name module) | |
3598 | (module-name int2) | |
3599 | name) | |
3600 | (module-local-variable int2 name)))) | |
f595ccfe | 3601 | |
65bed4aa MD |
3602 | (define (first module name int1 val1 int2 val2 var val) |
3603 | (or var (module-local-variable int1 name))) | |
f595ccfe | 3604 | |
65bed4aa MD |
3605 | (define (last module name int1 val1 int2 val2 var val) |
3606 | (module-local-variable int2 name)) | |
f595ccfe | 3607 | |
65bed4aa MD |
3608 | (define (noop module name int1 val1 int2 val2 var val) |
3609 | #f) | |
3610 | ||
7b07e5ef MD |
3611 | (set-module-name! m 'duplicate-handlers) |
3612 | (set-module-kind! m 'interface) | |
f595ccfe MD |
3613 | (module-define! m 'check check) |
3614 | (module-define! m 'warn warn) | |
3615 | (module-define! m 'replace replace) | |
3616 | (module-define! m 'warn-override-core warn-override-core) | |
3617 | (module-define! m 'first first) | |
3618 | (module-define! m 'last last) | |
65bed4aa MD |
3619 | (module-define! m 'merge-generics noop) |
3620 | (module-define! m 'merge-accessors noop) | |
7b07e5ef MD |
3621 | m)) |
3622 | ||
f595ccfe | 3623 | (define (lookup-duplicates-handlers handler-names) |
109c2c9f MD |
3624 | (and handler-names |
3625 | (map (lambda (handler-name) | |
9b5a0d84 AW |
3626 | (or (module-symbol-local-binding |
3627 | duplicate-handlers handler-name #f) | |
3628 | (error "invalid duplicate handler name:" | |
3629 | handler-name))) | |
3630 | (if (list? handler-names) | |
3631 | handler-names | |
3632 | (list handler-names))))) | |
f595ccfe | 3633 | |
70a459e3 MD |
3634 | (define default-duplicate-binding-procedures |
3635 | (make-mutable-parameter #f)) | |
3636 | ||
3637 | (define default-duplicate-binding-handler | |
6496a663 | 3638 | (make-mutable-parameter '(replace warn-override-core warn last) |
9b5a0d84 AW |
3639 | (lambda (handler-names) |
3640 | (default-duplicate-binding-procedures | |
3641 | (lookup-duplicates-handlers handler-names)) | |
3642 | handler-names))) | |
f595ccfe | 3643 | |
7b07e5ef | 3644 | \f |
7f24bc58 MG |
3645 | |
3646 | ;;; {`cond-expand' for SRFI-0 support.} | |
3647 | ;;; | |
3648 | ;;; This syntactic form expands into different commands or | |
3649 | ;;; definitions, depending on the features provided by the Scheme | |
3650 | ;;; implementation. | |
3651 | ;;; | |
3652 | ;;; Syntax: | |
3653 | ;;; | |
3654 | ;;; <cond-expand> | |
3655 | ;;; --> (cond-expand <cond-expand-clause>+) | |
3656 | ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>)) | |
3657 | ;;; <cond-expand-clause> | |
3658 | ;;; --> (<feature-requirement> <command-or-definition>*) | |
3659 | ;;; <feature-requirement> | |
3660 | ;;; --> <feature-identifier> | |
3661 | ;;; | (and <feature-requirement>*) | |
3662 | ;;; | (or <feature-requirement>*) | |
3663 | ;;; | (not <feature-requirement>) | |
3664 | ;;; <feature-identifier> | |
3665 | ;;; --> <a symbol which is the name or alias of a SRFI> | |
3666 | ;;; | |
3667 | ;;; Additionally, this implementation provides the | |
3668 | ;;; <feature-identifier>s `guile' and `r5rs', so that programs can | |
3669 | ;;; determine the implementation type and the supported standard. | |
3670 | ;;; | |
3671 | ;;; Currently, the following feature identifiers are supported: | |
3672 | ;;; | |
08b609aa | 3673 | ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61 |
7f24bc58 MG |
3674 | ;;; |
3675 | ;;; Remember to update the features list when adding more SRFIs. | |
3d2ada2f | 3676 | ;;; |
7f24bc58 | 3677 | |
b9b8f9da | 3678 | (define %cond-expand-features |
f41be016 | 3679 | ;; Adjust the above comment when changing this. |
018733ff | 3680 | '(guile |
60c8ad9e | 3681 | guile-2 |
018733ff KR |
3682 | r5rs |
3683 | srfi-0 ;; cond-expand itself | |
85acb35f | 3684 | srfi-4 ;; homogenous numeric vectors |
018733ff | 3685 | srfi-6 ;; open-input-string etc, in the guile core |
4a276c08 MV |
3686 | srfi-13 ;; string library |
3687 | srfi-14 ;; character sets | |
344d68d5 | 3688 | srfi-55 ;; require-extension |
08b609aa | 3689 | srfi-61 ;; general cond clause |
018733ff | 3690 | )) |
1d00af09 | 3691 | |
b9b8f9da MG |
3692 | ;; This table maps module public interfaces to the list of features. |
3693 | ;; | |
3694 | (define %cond-expand-table (make-hash-table 31)) | |
3695 | ||
3696 | ;; Add one or more features to the `cond-expand' feature list of the | |
3697 | ;; module `module'. | |
3698 | ;; | |
3699 | (define (cond-expand-provide module features) | |
3700 | (let ((mod (module-public-interface module))) | |
3701 | (and mod | |
9b5a0d84 AW |
3702 | (hashq-set! %cond-expand-table mod |
3703 | (append (hashq-ref %cond-expand-table mod '()) | |
3704 | features))))) | |
b9b8f9da | 3705 | |
f4bf64b4 LC |
3706 | (define-macro (cond-expand . clauses) |
3707 | (let ((syntax-error (lambda (cl) | |
3708 | (error "invalid clause in `cond-expand'" cl)))) | |
3709 | (letrec | |
3710 | ((test-clause | |
3711 | (lambda (clause) | |
3712 | (cond | |
3713 | ((symbol? clause) | |
3714 | (or (memq clause %cond-expand-features) | |
3715 | (let lp ((uses (module-uses (current-module)))) | |
3716 | (if (pair? uses) | |
3717 | (or (memq clause | |
3718 | (hashq-ref %cond-expand-table | |
3719 | (car uses) '())) | |
3720 | (lp (cdr uses))) | |
3721 | #f)))) | |
3722 | ((pair? clause) | |
3723 | (cond | |
3724 | ((eq? 'and (car clause)) | |
3725 | (let lp ((l (cdr clause))) | |
3726 | (cond ((null? l) | |
3727 | #t) | |
3728 | ((pair? l) | |
3729 | (and (test-clause (car l)) (lp (cdr l)))) | |
3730 | (else | |
3731 | (syntax-error clause))))) | |
3732 | ((eq? 'or (car clause)) | |
3733 | (let lp ((l (cdr clause))) | |
3734 | (cond ((null? l) | |
3735 | #f) | |
3736 | ((pair? l) | |
3737 | (or (test-clause (car l)) (lp (cdr l)))) | |
3738 | (else | |
3739 | (syntax-error clause))))) | |
3740 | ((eq? 'not (car clause)) | |
3741 | (cond ((not (pair? (cdr clause))) | |
3742 | (syntax-error clause)) | |
3743 | ((pair? (cddr clause)) | |
3744 | ((syntax-error clause)))) | |
3745 | (not (test-clause (cadr clause)))) | |
3746 | (else | |
3747 | (syntax-error clause)))) | |
3748 | (else | |
3749 | (syntax-error clause)))))) | |
3750 | (let lp ((c clauses)) | |
3751 | (cond | |
3752 | ((null? c) | |
3753 | (error "Unfulfilled `cond-expand'")) | |
3754 | ((not (pair? c)) | |
3755 | (syntax-error c)) | |
3756 | ((not (pair? (car c))) | |
3757 | (syntax-error (car c))) | |
3758 | ((test-clause (caar c)) | |
3759 | `(begin ,@(cdar c))) | |
3760 | ((eq? (caar c) 'else) | |
3761 | (if (pair? (cdr c)) | |
3762 | (syntax-error c)) | |
3763 | `(begin ,@(cdar c))) | |
3764 | (else | |
3765 | (lp (cdr c)))))))) | |
0f2d19dd | 3766 | |
f41be016 MG |
3767 | ;; This procedure gets called from the startup code with a list of |
3768 | ;; numbers, which are the numbers of the SRFIs to be loaded on startup. | |
3769 | ;; | |
3770 | (define (use-srfis srfis) | |
9a18d8d4 KR |
3771 | (process-use-modules |
3772 | (map (lambda (num) | |
9b5a0d84 AW |
3773 | (list (list 'srfi (string->symbol |
3774 | (string-append "srfi-" (number->string num)))))) | |
3775 | srfis))) | |
f8a502cb | 3776 | |
0f2d19dd | 3777 | \f |
9d774814 | 3778 | |
344d68d5 RB |
3779 | ;;; srfi-55: require-extension |
3780 | ;;; | |
3781 | ||
3782 | (define-macro (require-extension extension-spec) | |
3783 | ;; This macro only handles the srfi extension, which, at present, is | |
3784 | ;; the only one defined by the standard. | |
3785 | (if (not (pair? extension-spec)) | |
3786 | (scm-error 'wrong-type-arg "require-extension" | |
3787 | "Not an extension: ~S" (list extension-spec) #f)) | |
3788 | (let ((extension (car extension-spec)) | |
3789 | (extension-args (cdr extension-spec))) | |
3790 | (case extension | |
3791 | ((srfi) | |
3792 | (let ((use-list '())) | |
3793 | (for-each | |
3794 | (lambda (i) | |
3795 | (if (not (integer? i)) | |
3796 | (scm-error 'wrong-type-arg "require-extension" | |
3797 | "Invalid srfi name: ~S" (list i) #f)) | |
3798 | (let ((srfi-sym (string->symbol | |
3799 | (string-append "srfi-" (number->string i))))) | |
3800 | (if (not (memq srfi-sym %cond-expand-features)) | |
3801 | (set! use-list (cons `(use-modules (srfi ,srfi-sym)) | |
3802 | use-list))))) | |
3803 | extension-args) | |
3804 | (if (pair? use-list) | |
3805 | ;; i.e. (begin (use-modules x) (use-modules y) (use-modules z)) | |
3806 | `(begin ,@(reverse! use-list))))) | |
3807 | (else | |
3808 | (scm-error | |
3809 | 'wrong-type-arg "require-extension" | |
3810 | "Not a recognized extension type: ~S" (list extension) #f))))) | |
3811 | ||
3812 | \f | |
3813 | ||
9aca88c3 | 3814 | ;;; {Load emacs interface support if emacs option is given.} |
3d2ada2f | 3815 | ;;; |
9aca88c3 | 3816 | |
645e38d9 | 3817 | (define (named-module-use! user usee) |
89d06712 | 3818 | (module-use! (resolve-module user) (resolve-interface usee))) |
645e38d9 | 3819 | |
9aca88c3 | 3820 | (define (load-emacs-interface) |
fb1b76f4 TTN |
3821 | (and (provided? 'debug-extensions) |
3822 | (debug-enable 'backtrace)) | |
645e38d9 | 3823 | (named-module-use! '(guile-user) '(ice-9 emacs))) |
9aca88c3 JB |
3824 | |
3825 | \f | |
0f2d19dd | 3826 | |
755457ec MD |
3827 | (define using-readline? |
3828 | (let ((using-readline? (make-fluid))) | |
3829 | (make-procedure-with-setter | |
3830 | (lambda () (fluid-ref using-readline?)) | |
3831 | (lambda (v) (fluid-set! using-readline? v))))) | |
3832 | ||
20edfbbd | 3833 | (define (top-repl) |
615bfe72 MV |
3834 | (let ((guile-user-module (resolve-module '(guile-user)))) |
3835 | ||
3836 | ;; Load emacs interface support if emacs option is given. | |
454b82f4 | 3837 | (if (and (module-defined? guile-user-module 'use-emacs-interface) |
9b5a0d84 AW |
3838 | (module-ref guile-user-module 'use-emacs-interface)) |
3839 | (load-emacs-interface)) | |
615bfe72 MV |
3840 | |
3841 | ;; Use some convenient modules (in reverse order) | |
bbf5a913 | 3842 | |
9a18d8d4 KR |
3843 | (set-current-module guile-user-module) |
3844 | (process-use-modules | |
3845 | (append | |
3846 | '(((ice-9 r5rs)) | |
9b5a0d84 AW |
3847 | ((ice-9 session)) |
3848 | ((ice-9 debug))) | |
9a18d8d4 | 3849 | (if (provided? 'regex) |
9b5a0d84 AW |
3850 | '(((ice-9 regex))) |
3851 | '()) | |
9a18d8d4 | 3852 | (if (provided? 'threads) |
9b5a0d84 AW |
3853 | '(((ice-9 threads))) |
3854 | '()))) | |
615bfe72 | 3855 | ;; load debugger on demand |
42ee0d00 | 3856 | (module-autoload! guile-user-module '(system vm debug) '(debug)) |
615bfe72 | 3857 | |
9a18d8d4 KR |
3858 | ;; Note: SIGFPE, SIGSEGV and SIGBUS are actually "query-only" (see |
3859 | ;; scmsigs.c scm_sigaction_for_thread), so the handlers setup here have | |
3860 | ;; no effect. | |
615bfe72 | 3861 | (let ((old-handlers #f) |
6a01fabf AW |
3862 | (start-repl (module-ref (resolve-interface '(system repl repl)) |
3863 | 'start-repl)) | |
9b5a0d84 AW |
3864 | (signals (if (provided? 'posix) |
3865 | `((,SIGINT . "User interrupt") | |
3866 | (,SIGFPE . "Arithmetic error") | |
3867 | (,SIGSEGV | |
3868 | . "Bad memory access (Segmentation violation)")) | |
3869 | '()))) | |
9a18d8d4 KR |
3870 | ;; no SIGBUS on mingw |
3871 | (if (defined? 'SIGBUS) | |
9b5a0d84 AW |
3872 | (set! signals (acons SIGBUS "Bad memory access (bus error)" |
3873 | signals))) | |
615bfe72 MV |
3874 | |
3875 | (dynamic-wind | |
3876 | ||
9b5a0d84 AW |
3877 | ;; call at entry |
3878 | (lambda () | |
3879 | (let ((make-handler (lambda (msg) | |
3880 | (lambda (sig) | |
3881 | ;; Make a backup copy of the stack | |
3882 | (fluid-set! before-signal-stack | |
3883 | (fluid-ref the-last-stack)) | |
3884 | (save-stack 2) | |
3885 | (scm-error 'signal | |
3886 | #f | |
3887 | msg | |
3888 | #f | |
3889 | (list sig)))))) | |
3890 | (set! old-handlers | |
3891 | (map (lambda (sig-msg) | |
3892 | (sigaction (car sig-msg) | |
3893 | (make-handler (cdr sig-msg)))) | |
3894 | signals)))) | |
3895 | ||
3896 | ;; the protected thunk. | |
3897 | (lambda () | |
6a01fabf | 3898 | (let ((status (start-repl 'scheme))) |
9b5a0d84 AW |
3899 | (run-hook exit-hook) |
3900 | status)) | |
3901 | ||
3902 | ;; call at exit. | |
3903 | (lambda () | |
3904 | (map (lambda (sig-msg old-handler) | |
3905 | (if (not (car old-handler)) | |
3906 | ;; restore original C handler. | |
3907 | (sigaction (car sig-msg) #f) | |
3908 | ;; restore Scheme handler, SIG_IGN or SIG_DFL. | |
3909 | (sigaction (car sig-msg) | |
3910 | (car old-handler) | |
3911 | (cdr old-handler)))) | |
3912 | signals old-handlers)))))) | |
0f2d19dd | 3913 | |
2055a1bc MD |
3914 | ;;; This hook is run at the very end of an interactive session. |
3915 | ;;; | |
3e3cec45 | 3916 | (define exit-hook (make-hook)) |
2055a1bc | 3917 | |
4d31f0da | 3918 | \f |
3d2ada2f DH |
3919 | |
3920 | ;;; {Deprecated stuff} | |
3921 | ;;; | |
3922 | ||
3d2ada2f | 3923 | (begin-deprecated |
0ea72faa | 3924 | (module-use! the-scm-module (resolve-interface '(ice-9 deprecated)))) |
3d2ada2f DH |
3925 | |
3926 | \f | |
3927 | ||
3928 | ;;; Place the user in the guile-user module. | |
3929 | ;;; | |
6eb396fe | 3930 | |
13182603 AW |
3931 | ;;; FIXME: annotate ? |
3932 | ;; (define (syncase exp) | |
3933 | ;; (with-fluids ((expansion-eval-closure | |
9b5a0d84 | 3934 | ;; (module-eval-closure (current-module)))) |
8a73a6d2 | 3935 | ;; (deannotate/source-properties (macroexpand (annotate exp))))) |
13182603 | 3936 | |
a2689737 AW |
3937 | ;; FIXME: |
3938 | (module-use! the-scm-module (resolve-interface '(srfi srfi-4))) | |
3939 | ||
68623e8e AW |
3940 | (define-module (guile-user) |
3941 | #:autoload (system base compile) (compile)) | |
6d36532c | 3942 | |
7385dc12 LC |
3943 | ;; Remain in the `(guile)' module at compilation-time so that the |
3944 | ;; `-Wunused-toplevel' warning works as expected. | |
3945 | (eval-when (compile) (set-current-module the-root-module)) | |
3946 | ||
20edfbbd | 3947 | ;;; boot-9.scm ends here |