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