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