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