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