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