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