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