Merge branch 'master' into boehm-demers-weiser-gc
[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,2008,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 (define (load name . reader)
871 (with-fluid* current-reader (and (pair? reader) (car reader))
872 (lambda ()
873 (start-stack 'load-stack
874 (primitive-load name)))))
875
876 \f
877
878 ;;; {Transcendental Functions}
879 ;;;
880 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
881 ;;; Written by Jerry D. Hedden, (C) FSF.
882 ;;; See the file `COPYING' for terms applying to this program.
883 ;;;
884
885 (define expt
886 (let ((integer-expt integer-expt))
887 (lambda (z1 z2)
888 (cond ((and (exact? z2) (integer? z2))
889 (integer-expt z1 z2))
890 ((and (real? z2) (real? z1) (>= z1 0))
891 ($expt z1 z2))
892 (else
893 (exp (* z2 (log z1))))))))
894
895 (define (sinh z)
896 (if (real? z) ($sinh z)
897 (let ((x (real-part z)) (y (imag-part z)))
898 (make-rectangular (* ($sinh x) ($cos y))
899 (* ($cosh x) ($sin y))))))
900 (define (cosh z)
901 (if (real? z) ($cosh z)
902 (let ((x (real-part z)) (y (imag-part z)))
903 (make-rectangular (* ($cosh x) ($cos y))
904 (* ($sinh x) ($sin y))))))
905 (define (tanh z)
906 (if (real? z) ($tanh z)
907 (let* ((x (* 2 (real-part z)))
908 (y (* 2 (imag-part z)))
909 (w (+ ($cosh x) ($cos y))))
910 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
911
912 (define (asinh z)
913 (if (real? z) ($asinh z)
914 (log (+ z (sqrt (+ (* z z) 1))))))
915
916 (define (acosh z)
917 (if (and (real? z) (>= z 1))
918 ($acosh z)
919 (log (+ z (sqrt (- (* z z) 1))))))
920
921 (define (atanh z)
922 (if (and (real? z) (> z -1) (< z 1))
923 ($atanh z)
924 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
925
926 (define (sin z)
927 (if (real? z) ($sin z)
928 (let ((x (real-part z)) (y (imag-part z)))
929 (make-rectangular (* ($sin x) ($cosh y))
930 (* ($cos x) ($sinh y))))))
931 (define (cos z)
932 (if (real? z) ($cos z)
933 (let ((x (real-part z)) (y (imag-part z)))
934 (make-rectangular (* ($cos x) ($cosh y))
935 (- (* ($sin x) ($sinh y)))))))
936 (define (tan z)
937 (if (real? z) ($tan z)
938 (let* ((x (* 2 (real-part z)))
939 (y (* 2 (imag-part z)))
940 (w (+ ($cos x) ($cosh y))))
941 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
942
943 (define (asin z)
944 (if (and (real? z) (>= z -1) (<= z 1))
945 ($asin z)
946 (* -i (asinh (* +i z)))))
947
948 (define (acos z)
949 (if (and (real? z) (>= z -1) (<= z 1))
950 ($acos z)
951 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
952
953 (define (atan z . y)
954 (if (null? y)
955 (if (real? z) ($atan z)
956 (/ (log (/ (- +i z) (+ +i z))) +2i))
957 ($atan2 z (car y))))
958
959 \f
960
961 ;;; {Reader Extensions}
962 ;;;
963 ;;; Reader code for various "#c" forms.
964 ;;;
965
966 (define read-eval? (make-fluid))
967 (fluid-set! read-eval? #f)
968 (read-hash-extend #\.
969 (lambda (c port)
970 (if (fluid-ref read-eval?)
971 (eval (read port) (interaction-environment))
972 (error
973 "#. read expansion found and read-eval? is #f."))))
974
975 \f
976
977 ;;; {Command Line Options}
978 ;;;
979
980 (define (get-option argv kw-opts kw-args return)
981 (cond
982 ((null? argv)
983 (return #f #f argv))
984
985 ((or (not (eq? #\- (string-ref (car argv) 0)))
986 (eq? (string-length (car argv)) 1))
987 (return 'normal-arg (car argv) (cdr argv)))
988
989 ((eq? #\- (string-ref (car argv) 1))
990 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
991 (string-length (car argv))))
992 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
993 (kw-opt? (member kw kw-opts))
994 (kw-arg? (member kw kw-args))
995 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
996 (substring (car argv)
997 (+ kw-arg-pos 1)
998 (string-length (car argv))))
999 (and kw-arg?
1000 (begin (set! argv (cdr argv)) (car argv))))))
1001 (if (or kw-opt? kw-arg?)
1002 (return kw arg (cdr argv))
1003 (return 'usage-error kw (cdr argv)))))
1004
1005 (else
1006 (let* ((char (substring (car argv) 1 2))
1007 (kw (symbol->keyword char)))
1008 (cond
1009
1010 ((member kw kw-opts)
1011 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1012 (new-argv (if (= 0 (string-length rest-car))
1013 (cdr argv)
1014 (cons (string-append "-" rest-car) (cdr argv)))))
1015 (return kw #f new-argv)))
1016
1017 ((member kw kw-args)
1018 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1019 (arg (if (= 0 (string-length rest-car))
1020 (cadr argv)
1021 rest-car))
1022 (new-argv (if (= 0 (string-length rest-car))
1023 (cddr argv)
1024 (cdr argv))))
1025 (return kw arg new-argv)))
1026
1027 (else (return 'usage-error kw argv)))))))
1028
1029 (define (for-next-option proc argv kw-opts kw-args)
1030 (let loop ((argv argv))
1031 (get-option argv kw-opts kw-args
1032 (lambda (opt opt-arg argv)
1033 (and opt (proc opt opt-arg argv loop))))))
1034
1035 (define (display-usage-report kw-desc)
1036 (for-each
1037 (lambda (kw)
1038 (or (eq? (car kw) #t)
1039 (eq? (car kw) 'else)
1040 (let* ((opt-desc kw)
1041 (help (cadr opt-desc))
1042 (opts (car opt-desc))
1043 (opts-proper (if (string? (car opts)) (cdr opts) opts))
1044 (arg-name (if (string? (car opts))
1045 (string-append "<" (car opts) ">")
1046 ""))
1047 (left-part (string-append
1048 (with-output-to-string
1049 (lambda ()
1050 (map (lambda (x) (display (keyword->symbol x)) (display " "))
1051 opts-proper)))
1052 arg-name))
1053 (middle-part (if (and (< (string-length left-part) 30)
1054 (< (string-length help) 40))
1055 (make-string (- 30 (string-length left-part)) #\ )
1056 "\n\t")))
1057 (display left-part)
1058 (display middle-part)
1059 (display help)
1060 (newline))))
1061 kw-desc))
1062
1063
1064
1065 (define (transform-usage-lambda cases)
1066 (let* ((raw-usage (delq! 'else (map car cases)))
1067 (usage-sans-specials (map (lambda (x)
1068 (or (and (not (list? x)) x)
1069 (and (symbol? (car x)) #t)
1070 (and (boolean? (car x)) #t)
1071 x))
1072 raw-usage))
1073 (usage-desc (delq! #t usage-sans-specials))
1074 (kw-desc (map car usage-desc))
1075 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1076 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1077 (transmogrified-cases (map (lambda (case)
1078 (cons (let ((opts (car case)))
1079 (if (or (boolean? opts) (eq? 'else opts))
1080 opts
1081 (cond
1082 ((symbol? (car opts)) opts)
1083 ((boolean? (car opts)) opts)
1084 ((string? (caar opts)) (cdar opts))
1085 (else (car opts)))))
1086 (cdr case)))
1087 cases)))
1088 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1089 (lambda (%argv)
1090 (let %next-arg ((%argv %argv))
1091 (get-option %argv
1092 ',kw-opts
1093 ',kw-args
1094 (lambda (%opt %arg %new-argv)
1095 (case %opt
1096 ,@ transmogrified-cases))))))))
1097
1098
1099 \f
1100
1101 ;;; {Low Level Modules}
1102 ;;;
1103 ;;; These are the low level data structures for modules.
1104 ;;;
1105 ;;; Every module object is of the type 'module-type', which is a record
1106 ;;; consisting of the following members:
1107 ;;;
1108 ;;; - eval-closure: the function that defines for its module the strategy that
1109 ;;; shall be followed when looking up symbols in the module.
1110 ;;;
1111 ;;; An eval-closure is a function taking two arguments: the symbol to be
1112 ;;; looked up and a boolean value telling whether a binding for the symbol
1113 ;;; should be created if it does not exist yet. If the symbol lookup
1114 ;;; succeeded (either because an existing binding was found or because a new
1115 ;;; binding was created), a variable object representing the binding is
1116 ;;; returned. Otherwise, the value #f is returned. Note that the eval
1117 ;;; closure does not take the module to be searched as an argument: During
1118 ;;; construction of the eval-closure, the eval-closure has to store the
1119 ;;; module it belongs to in its environment. This means, that any
1120 ;;; eval-closure can belong to only one module.
1121 ;;;
1122 ;;; The eval-closure of a module can be defined arbitrarily. However, three
1123 ;;; special cases of eval-closures are to be distinguished: During startup
1124 ;;; the module system is not yet activated. In this phase, no modules are
1125 ;;; defined and all bindings are automatically stored by the system in the
1126 ;;; pre-modules-obarray. Since no eval-closures exist at this time, the
1127 ;;; functions which require an eval-closure as their argument need to be
1128 ;;; passed the value #f.
1129 ;;;
1130 ;;; The other two special cases of eval-closures are the
1131 ;;; standard-eval-closure and the standard-interface-eval-closure. Both
1132 ;;; behave equally for the case that no new binding is to be created. The
1133 ;;; difference between the two comes in, when the boolean argument to the
1134 ;;; eval-closure indicates that a new binding shall be created if it is not
1135 ;;; found.
1136 ;;;
1137 ;;; Given that no new binding shall be created, both standard eval-closures
1138 ;;; define the following standard strategy of searching bindings in the
1139 ;;; module: First, the module's obarray is searched for the symbol. Second,
1140 ;;; if no binding for the symbol was found in the module's obarray, the
1141 ;;; module's binder procedure is exececuted. If this procedure did not
1142 ;;; return a binding for the symbol, the modules referenced in the module's
1143 ;;; uses list are recursively searched for a binding of the symbol. If the
1144 ;;; binding can not be found in these modules also, the symbol lookup has
1145 ;;; failed.
1146 ;;;
1147 ;;; If a new binding shall be created, the standard-interface-eval-closure
1148 ;;; immediately returns indicating failure. That is, it does not even try
1149 ;;; to look up the symbol. In contrast, the standard-eval-closure would
1150 ;;; first search the obarray, and if no binding was found there, would
1151 ;;; create a new binding in the obarray, therefore not calling the binder
1152 ;;; procedure or searching the modules in the uses list.
1153 ;;;
1154 ;;; The explanation of the following members obarray, binder and uses
1155 ;;; assumes that the symbol lookup follows the strategy that is defined in
1156 ;;; the standard-eval-closure and the standard-interface-eval-closure.
1157 ;;;
1158 ;;; - obarray: a hash table that maps symbols to variable objects. In this
1159 ;;; hash table, the definitions are found that are local to the module (that
1160 ;;; is, not imported from other modules). When looking up bindings in the
1161 ;;; module, this hash table is searched first.
1162 ;;;
1163 ;;; - binder: either #f or a function taking a module and a symbol argument.
1164 ;;; If it is a function it is called after the obarray has been
1165 ;;; unsuccessfully searched for a binding. It then can provide bindings
1166 ;;; that would otherwise not be found locally in the module.
1167 ;;;
1168 ;;; - uses: a list of modules from which non-local bindings can be inherited.
1169 ;;; These modules are the third place queried for bindings after the obarray
1170 ;;; has been unsuccessfully searched and the binder function did not deliver
1171 ;;; a result either.
1172 ;;;
1173 ;;; - transformer: either #f or a function taking a scheme expression as
1174 ;;; delivered by read. If it is a function, it will be called to perform
1175 ;;; syntax transformations (e. g. makro expansion) on the given scheme
1176 ;;; expression. The output of the transformer function will then be passed
1177 ;;; to Guile's internal memoizer. This means that the output must be valid
1178 ;;; scheme code. The only exception is, that the output may make use of the
1179 ;;; syntax extensions provided to identify the modules that a binding
1180 ;;; belongs to.
1181 ;;;
1182 ;;; - name: the name of the module. This is used for all kinds of printing
1183 ;;; outputs. In certain places the module name also serves as a way of
1184 ;;; identification. When adding a module to the uses list of another
1185 ;;; module, it is made sure that the new uses list will not contain two
1186 ;;; modules of the same name.
1187 ;;;
1188 ;;; - kind: classification of the kind of module. The value is (currently?)
1189 ;;; only used for printing. It has no influence on how a module is treated.
1190 ;;; Currently the following values are used when setting the module kind:
1191 ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind
1192 ;;; is set, it defaults to 'module.
1193 ;;;
1194 ;;; - duplicates-handlers: a list of procedures that get called to make a
1195 ;;; choice between two duplicate bindings when name clashes occur. See the
1196 ;;; `duplicate-handlers' global variable below.
1197 ;;;
1198 ;;; - observers: a list of procedures that get called when the module is
1199 ;;; modified.
1200 ;;;
1201 ;;; - weak-observers: a weak-key hash table of procedures that get called
1202 ;;; when the module is modified. See `module-observe-weak' for details.
1203 ;;;
1204 ;;; In addition, the module may (must?) contain a binding for
1205 ;;; `%module-public-interface'. This variable should be bound to a module
1206 ;;; representing the exported interface of a module. See the
1207 ;;; `module-public-interface' and `module-export!' procedures.
1208 ;;;
1209 ;;; !!! warning: The interface to lazy binder procedures is going
1210 ;;; to be changed in an incompatible way to permit all the basic
1211 ;;; module ops to be virtualized.
1212 ;;;
1213 ;;; (make-module size use-list lazy-binding-proc) => module
1214 ;;; module-{obarray,uses,binder}[|-set!]
1215 ;;; (module? obj) => [#t|#f]
1216 ;;; (module-locally-bound? module symbol) => [#t|#f]
1217 ;;; (module-bound? module symbol) => [#t|#f]
1218 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1219 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1220 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1221 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1222 ;;; (module-symbol-binding module symbol opt-value)
1223 ;;; => [ <obj> | opt-value | an error occurs ]
1224 ;;; (module-make-local-var! module symbol) => #<variable...>
1225 ;;; (module-add! module symbol var) => unspecified
1226 ;;; (module-remove! module symbol) => unspecified
1227 ;;; (module-for-each proc module) => unspecified
1228 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1229 ;;; (set-current-module module) => unspecified
1230 ;;; (current-module) => #<module...>
1231 ;;;
1232 ;;;
1233
1234 \f
1235
1236 ;;; {Printing Modules}
1237 ;;;
1238
1239 ;; This is how modules are printed. You can re-define it.
1240 ;; (Redefining is actually more complicated than simply redefining
1241 ;; %print-module because that would only change the binding and not
1242 ;; the value stored in the vtable that determines how record are
1243 ;; printed. Sigh.)
1244
1245 (define (%print-module mod port) ; unused args: depth length style table)
1246 (display "#<" port)
1247 (display (or (module-kind mod) "module") port)
1248 (display " " port)
1249 (display (module-name mod) port)
1250 (display " " port)
1251 (display (number->string (object-address mod) 16) port)
1252 (display ">" port))
1253
1254 ;; module-type
1255 ;;
1256 ;; A module is characterized by an obarray in which local symbols
1257 ;; are interned, a list of modules, "uses", from which non-local
1258 ;; bindings can be inherited, and an optional lazy-binder which
1259 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1260 ;; bindings that would otherwise not be found locally in the module.
1261 ;;
1262 ;; NOTE: If you change anything here, you also need to change
1263 ;; libguile/modules.h.
1264 ;;
1265 (define module-type
1266 (make-record-type 'module
1267 '(obarray uses binder eval-closure transformer name kind
1268 duplicates-handlers import-obarray
1269 observers weak-observers)
1270 %print-module))
1271
1272 ;; make-module &opt size uses binder
1273 ;;
1274 ;; Create a new module, perhaps with a particular size of obarray,
1275 ;; initial uses list, or binding procedure.
1276 ;;
1277 (define make-module
1278 (lambda args
1279
1280 (define (parse-arg index default)
1281 (if (> (length args) index)
1282 (list-ref args index)
1283 default))
1284
1285 (define %default-import-size
1286 ;; Typical number of imported bindings actually used by a module.
1287 600)
1288
1289 (if (> (length args) 3)
1290 (error "Too many args to make-module." args))
1291
1292 (let ((size (parse-arg 0 31))
1293 (uses (parse-arg 1 '()))
1294 (binder (parse-arg 2 #f)))
1295
1296 (if (not (integer? size))
1297 (error "Illegal size to make-module." size))
1298 (if (not (and (list? uses)
1299 (and-map module? uses)))
1300 (error "Incorrect use list." uses))
1301 (if (and binder (not (procedure? binder)))
1302 (error
1303 "Lazy-binder expected to be a procedure or #f." binder))
1304
1305 (let ((module (module-constructor (make-hash-table size)
1306 uses binder #f %pre-modules-transformer
1307 #f #f #f
1308 (make-hash-table %default-import-size)
1309 '()
1310 (make-weak-key-hash-table 31))))
1311
1312 ;; We can't pass this as an argument to module-constructor,
1313 ;; because we need it to close over a pointer to the module
1314 ;; itself.
1315 (set-module-eval-closure! module (standard-eval-closure module))
1316
1317 module))))
1318
1319 (define module-constructor (record-constructor module-type))
1320 (define module-obarray (record-accessor module-type 'obarray))
1321 (define set-module-obarray! (record-modifier module-type 'obarray))
1322 (define module-uses (record-accessor module-type 'uses))
1323 (define set-module-uses! (record-modifier module-type 'uses))
1324 (define module-binder (record-accessor module-type 'binder))
1325 (define set-module-binder! (record-modifier module-type 'binder))
1326
1327 ;; NOTE: This binding is used in libguile/modules.c.
1328 (define module-eval-closure (record-accessor module-type 'eval-closure))
1329
1330 (define module-transformer (record-accessor module-type 'transformer))
1331 (define set-module-transformer! (record-modifier module-type 'transformer))
1332 ;; (define module-name (record-accessor module-type 'name)) wait until mods are booted
1333 (define set-module-name! (record-modifier module-type 'name))
1334 (define module-kind (record-accessor module-type 'kind))
1335 (define set-module-kind! (record-modifier module-type 'kind))
1336 (define module-duplicates-handlers
1337 (record-accessor module-type 'duplicates-handlers))
1338 (define set-module-duplicates-handlers!
1339 (record-modifier module-type 'duplicates-handlers))
1340 (define module-observers (record-accessor module-type 'observers))
1341 (define set-module-observers! (record-modifier module-type 'observers))
1342 (define module-weak-observers (record-accessor module-type 'weak-observers))
1343 (define module? (record-predicate module-type))
1344
1345 (define module-import-obarray (record-accessor module-type 'import-obarray))
1346
1347 (define set-module-eval-closure!
1348 (let ((setter (record-modifier module-type 'eval-closure)))
1349 (lambda (module closure)
1350 (setter module closure)
1351 ;; Make it possible to lookup the module from the environment.
1352 ;; This implementation is correct since an eval closure can belong
1353 ;; to maximally one module.
1354
1355 ;; XXX: The following line introduces a circular reference that
1356 ;; precludes garbage collection of modules with the current weak hash
1357 ;; table semantics (see
1358 ;; http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/2465
1359 ;; for details). Since it doesn't appear to be used (only in
1360 ;; `scm_lookup_closure_module ()', which has 1 caller), we just comment
1361 ;; it out.
1362
1363 ;(set-procedure-property! closure 'module module)
1364 )))
1365
1366 \f
1367
1368 ;;; {Observer protocol}
1369 ;;;
1370
1371 (define (module-observe module proc)
1372 (set-module-observers! module (cons proc (module-observers module)))
1373 (cons module proc))
1374
1375 (define (module-observe-weak module observer-id . proc)
1376 ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can
1377 ;; be any Scheme object). PROC is invoked and passed MODULE any time
1378 ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd
1379 ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value,
1380 ;; for instance).
1381
1382 ;; The two-argument version is kept for backward compatibility: when called
1383 ;; with two arguments, the observer gets unregistered when closure PROC
1384 ;; gets GC'd (making it impossible to use an anonymous lambda for PROC).
1385
1386 (let ((proc (if (null? proc) observer-id (car proc))))
1387 (hashq-set! (module-weak-observers module) observer-id proc)))
1388
1389 (define (module-unobserve token)
1390 (let ((module (car token))
1391 (id (cdr token)))
1392 (if (integer? id)
1393 (hash-remove! (module-weak-observers module) id)
1394 (set-module-observers! module (delq1! id (module-observers module)))))
1395 *unspecified*)
1396
1397 (define module-defer-observers #f)
1398 (define module-defer-observers-mutex (make-mutex 'recursive))
1399 (define module-defer-observers-table (make-hash-table))
1400
1401 (define (module-modified m)
1402 (if module-defer-observers
1403 (hash-set! module-defer-observers-table m #t)
1404 (module-call-observers m)))
1405
1406 ;;; This function can be used to delay calls to observers so that they
1407 ;;; can be called once only in the face of massive updating of modules.
1408 ;;;
1409 (define (call-with-deferred-observers thunk)
1410 (dynamic-wind
1411 (lambda ()
1412 (lock-mutex module-defer-observers-mutex)
1413 (set! module-defer-observers #t))
1414 thunk
1415 (lambda ()
1416 (set! module-defer-observers #f)
1417 (hash-for-each (lambda (m dummy)
1418 (module-call-observers m))
1419 module-defer-observers-table)
1420 (hash-clear! module-defer-observers-table)
1421 (unlock-mutex module-defer-observers-mutex))))
1422
1423 (define (module-call-observers m)
1424 (for-each (lambda (proc) (proc m)) (module-observers m))
1425
1426 ;; We assume that weak observers don't (un)register themselves as they are
1427 ;; called since this would preclude proper iteration over the hash table
1428 ;; elements.
1429 (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m)))
1430
1431 \f
1432
1433 ;;; {Module Searching in General}
1434 ;;;
1435 ;;; We sometimes want to look for properties of a symbol
1436 ;;; just within the obarray of one module. If the property
1437 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1438 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1439 ;;;
1440 ;;;
1441 ;;; Other times, we want to test for a symbol property in the obarray
1442 ;;; of M and, if it is not found there, try each of the modules in the
1443 ;;; uses list of M. This is the normal way of testing for some
1444 ;;; property, so we state these properties without qualification as
1445 ;;; in: ``The symbol 'fnord is interned in module M because it is
1446 ;;; interned locally in module M2 which is a member of the uses list
1447 ;;; of M.''
1448 ;;;
1449
1450 ;; module-search fn m
1451 ;;
1452 ;; return the first non-#f result of FN applied to M and then to
1453 ;; the modules in the uses of m, and so on recursively. If all applications
1454 ;; return #f, then so does this function.
1455 ;;
1456 (define (module-search fn m v)
1457 (define (loop pos)
1458 (and (pair? pos)
1459 (or (module-search fn (car pos) v)
1460 (loop (cdr pos)))))
1461 (or (fn m v)
1462 (loop (module-uses m))))
1463
1464
1465 ;;; {Is a symbol bound in a module?}
1466 ;;;
1467 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1468 ;;; of S in M has been set to some well-defined value.
1469 ;;;
1470
1471 ;; module-locally-bound? module symbol
1472 ;;
1473 ;; Is a symbol bound (interned and defined) locally in a given module?
1474 ;;
1475 (define (module-locally-bound? m v)
1476 (let ((var (module-local-variable m v)))
1477 (and var
1478 (variable-bound? var))))
1479
1480 ;; module-bound? module symbol
1481 ;;
1482 ;; Is a symbol bound (interned and defined) anywhere in a given module
1483 ;; or its uses?
1484 ;;
1485 (define (module-bound? m v)
1486 (let ((var (module-variable m v)))
1487 (and var
1488 (variable-bound? var))))
1489
1490 ;;; {Is a symbol interned in a module?}
1491 ;;;
1492 ;;; Symbol S in Module M is interned if S occurs in
1493 ;;; of S in M has been set to some well-defined value.
1494 ;;;
1495 ;;; It is possible to intern a symbol in a module without providing
1496 ;;; an initial binding for the corresponding variable. This is done
1497 ;;; with:
1498 ;;; (module-add! module symbol (make-undefined-variable))
1499 ;;;
1500 ;;; In that case, the symbol is interned in the module, but not
1501 ;;; bound there. The unbound symbol shadows any binding for that
1502 ;;; symbol that might otherwise be inherited from a member of the uses list.
1503 ;;;
1504
1505 (define (module-obarray-get-handle ob key)
1506 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1507
1508 (define (module-obarray-ref ob key)
1509 ((if (symbol? key) hashq-ref hash-ref) ob key))
1510
1511 (define (module-obarray-set! ob key val)
1512 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1513
1514 (define (module-obarray-remove! ob key)
1515 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1516
1517 ;; module-symbol-locally-interned? module symbol
1518 ;;
1519 ;; is a symbol interned (not neccessarily defined) locally in a given module
1520 ;; or its uses? Interned symbols shadow inherited bindings even if
1521 ;; they are not themselves bound to a defined value.
1522 ;;
1523 (define (module-symbol-locally-interned? m v)
1524 (not (not (module-obarray-get-handle (module-obarray m) v))))
1525
1526 ;; module-symbol-interned? module symbol
1527 ;;
1528 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1529 ;; or its uses? Interned symbols shadow inherited bindings even if
1530 ;; they are not themselves bound to a defined value.
1531 ;;
1532 (define (module-symbol-interned? m v)
1533 (module-search module-symbol-locally-interned? m v))
1534
1535
1536 ;;; {Mapping modules x symbols --> variables}
1537 ;;;
1538
1539 ;; module-local-variable module symbol
1540 ;; return the local variable associated with a MODULE and SYMBOL.
1541 ;;
1542 ;;; This function is very important. It is the only function that can
1543 ;;; return a variable from a module other than the mutators that store
1544 ;;; new variables in modules. Therefore, this function is the location
1545 ;;; of the "lazy binder" hack.
1546 ;;;
1547 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1548 ;;; to a variable, return that variable object.
1549 ;;;
1550 ;;; If the symbols is not found at first, but the module has a lazy binder,
1551 ;;; then try the binder.
1552 ;;;
1553 ;;; If the symbol is not found at all, return #f.
1554 ;;;
1555 ;;; (This is now written in C, see `modules.c'.)
1556 ;;;
1557
1558 ;;; {Mapping modules x symbols --> bindings}
1559 ;;;
1560 ;;; These are similar to the mapping to variables, except that the
1561 ;;; variable is dereferenced.
1562 ;;;
1563
1564 ;; module-symbol-binding module symbol opt-value
1565 ;;
1566 ;; return the binding of a variable specified by name within
1567 ;; a given module, signalling an error if the variable is unbound.
1568 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1569 ;; return OPT-VALUE.
1570 ;;
1571 (define (module-symbol-local-binding m v . opt-val)
1572 (let ((var (module-local-variable m v)))
1573 (if (and var (variable-bound? var))
1574 (variable-ref var)
1575 (if (not (null? opt-val))
1576 (car opt-val)
1577 (error "Locally unbound variable." v)))))
1578
1579 ;; module-symbol-binding module symbol opt-value
1580 ;;
1581 ;; return the binding of a variable specified by name within
1582 ;; a given module, signalling an error if the variable is unbound.
1583 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1584 ;; return OPT-VALUE.
1585 ;;
1586 (define (module-symbol-binding m v . opt-val)
1587 (let ((var (module-variable m v)))
1588 (if (and var (variable-bound? var))
1589 (variable-ref var)
1590 (if (not (null? opt-val))
1591 (car opt-val)
1592 (error "Unbound variable." v)))))
1593
1594
1595 \f
1596
1597 ;;; {Adding Variables to Modules}
1598 ;;;
1599
1600 ;; module-make-local-var! module symbol
1601 ;;
1602 ;; ensure a variable for V in the local namespace of M.
1603 ;; If no variable was already there, then create a new and uninitialzied
1604 ;; variable.
1605 ;;
1606 ;; This function is used in modules.c.
1607 ;;
1608 (define (module-make-local-var! m v)
1609 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1610 (and (variable? b)
1611 (begin
1612 ;; Mark as modified since this function is called when
1613 ;; the standard eval closure defines a binding
1614 (module-modified m)
1615 b)))
1616
1617 ;; Create a new local variable.
1618 (let ((local-var (make-undefined-variable)))
1619 (module-add! m v local-var)
1620 local-var)))
1621
1622 ;; module-ensure-local-variable! module symbol
1623 ;;
1624 ;; Ensure that there is a local variable in MODULE for SYMBOL. If
1625 ;; there is no binding for SYMBOL, create a new uninitialized
1626 ;; variable. Return the local variable.
1627 ;;
1628 (define (module-ensure-local-variable! module symbol)
1629 (or (module-local-variable module symbol)
1630 (let ((var (make-undefined-variable)))
1631 (module-add! module symbol var)
1632 var)))
1633
1634 ;; module-add! module symbol var
1635 ;;
1636 ;; ensure a particular variable for V in the local namespace of M.
1637 ;;
1638 (define (module-add! m v var)
1639 (if (not (variable? var))
1640 (error "Bad variable to module-add!" var))
1641 (module-obarray-set! (module-obarray m) v var)
1642 (module-modified m))
1643
1644 ;; module-remove!
1645 ;;
1646 ;; make sure that a symbol is undefined in the local namespace of M.
1647 ;;
1648 (define (module-remove! m v)
1649 (module-obarray-remove! (module-obarray m) v)
1650 (module-modified m))
1651
1652 (define (module-clear! m)
1653 (hash-clear! (module-obarray m))
1654 (module-modified m))
1655
1656 ;; MODULE-FOR-EACH -- exported
1657 ;;
1658 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1659 ;;
1660 (define (module-for-each proc module)
1661 (hash-for-each proc (module-obarray module)))
1662
1663 (define (module-map proc module)
1664 (hash-map->list proc (module-obarray module)))
1665
1666 \f
1667
1668 ;;; {Low Level Bootstrapping}
1669 ;;;
1670
1671 ;; make-root-module
1672
1673 ;; A root module uses the pre-modules-obarray as its obarray. This
1674 ;; special obarray accumulates all bindings that have been established
1675 ;; before the module system is fully booted.
1676 ;;
1677 ;; (The obarray continues to be used by code that has been closed over
1678 ;; before the module system has been booted.)
1679
1680 (define (make-root-module)
1681 (let ((m (make-module 0)))
1682 (set-module-obarray! m (%get-pre-modules-obarray))
1683 m))
1684
1685 ;; make-scm-module
1686
1687 ;; The root interface is a module that uses the same obarray as the
1688 ;; root module. It does not allow new definitions, tho.
1689
1690 (define (make-scm-module)
1691 (let ((m (make-module 0)))
1692 (set-module-obarray! m (%get-pre-modules-obarray))
1693 (set-module-eval-closure! m (standard-interface-eval-closure m))
1694 m))
1695
1696
1697 \f
1698
1699 ;;; {Module-based Loading}
1700 ;;;
1701
1702 (define (save-module-excursion thunk)
1703 (let ((inner-module (current-module))
1704 (outer-module #f))
1705 (dynamic-wind (lambda ()
1706 (set! outer-module (current-module))
1707 (set-current-module inner-module)
1708 (set! inner-module #f))
1709 thunk
1710 (lambda ()
1711 (set! inner-module (current-module))
1712 (set-current-module outer-module)
1713 (set! outer-module #f)))))
1714
1715 (define basic-load load)
1716
1717 (define (load-module filename . reader)
1718 (save-module-excursion
1719 (lambda ()
1720 (let ((oldname (and (current-load-port)
1721 (port-filename (current-load-port)))))
1722 (apply basic-load
1723 (if (and oldname
1724 (> (string-length filename) 0)
1725 (not (char=? (string-ref filename 0) #\/))
1726 (not (string=? (dirname oldname) ".")))
1727 (string-append (dirname oldname) "/" filename)
1728 filename)
1729 reader)))))
1730
1731
1732 \f
1733
1734 ;;; {MODULE-REF -- exported}
1735 ;;;
1736
1737 ;; Returns the value of a variable called NAME in MODULE or any of its
1738 ;; used modules. If there is no such variable, then if the optional third
1739 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1740 ;;
1741 (define (module-ref module name . rest)
1742 (let ((variable (module-variable module name)))
1743 (if (and variable (variable-bound? variable))
1744 (variable-ref variable)
1745 (if (null? rest)
1746 (error "No variable named" name 'in module)
1747 (car rest) ; default value
1748 ))))
1749
1750 ;; MODULE-SET! -- exported
1751 ;;
1752 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1753 ;; to VALUE; if there is no such variable, an error is signaled.
1754 ;;
1755 (define (module-set! module name value)
1756 (let ((variable (module-variable module name)))
1757 (if variable
1758 (variable-set! variable value)
1759 (error "No variable named" name 'in module))))
1760
1761 ;; MODULE-DEFINE! -- exported
1762 ;;
1763 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1764 ;; variable, it is added first.
1765 ;;
1766 (define (module-define! module name value)
1767 (let ((variable (module-local-variable module name)))
1768 (if variable
1769 (begin
1770 (variable-set! variable value)
1771 (module-modified module))
1772 (let ((variable (make-variable value)))
1773 (module-add! module name variable)))))
1774
1775 ;; MODULE-DEFINED? -- exported
1776 ;;
1777 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1778 ;; uses)
1779 ;;
1780 (define (module-defined? module name)
1781 (let ((variable (module-variable module name)))
1782 (and variable (variable-bound? variable))))
1783
1784 ;; MODULE-USE! module interface
1785 ;;
1786 ;; Add INTERFACE to the list of interfaces used by MODULE.
1787 ;;
1788 (define (module-use! module interface)
1789 (if (not (or (eq? module interface)
1790 (memq interface (module-uses module))))
1791 (begin
1792 ;; Newly used modules must be appended rather than consed, so that
1793 ;; `module-variable' traverses the use list starting from the first
1794 ;; used module.
1795 (set-module-uses! module
1796 (append (filter (lambda (m)
1797 (not
1798 (equal? (module-name m)
1799 (module-name interface))))
1800 (module-uses module))
1801 (list interface)))
1802
1803 (module-modified module))))
1804
1805 ;; MODULE-USE-INTERFACES! module interfaces
1806 ;;
1807 ;; Same as MODULE-USE! but add multiple interfaces and check for duplicates
1808 ;;
1809 (define (module-use-interfaces! module interfaces)
1810 (set-module-uses! module
1811 (append (module-uses module) interfaces))
1812 (module-modified module))
1813
1814 \f
1815
1816 ;;; {Recursive Namespaces}
1817 ;;;
1818 ;;; A hierarchical namespace emerges if we consider some module to be
1819 ;;; root, and variables bound to modules as nested namespaces.
1820 ;;;
1821 ;;; The routines in this file manage variable names in hierarchical namespace.
1822 ;;; Each variable name is a list of elements, looked up in successively nested
1823 ;;; modules.
1824 ;;;
1825 ;;; (nested-ref some-root-module '(foo bar baz))
1826 ;;; => <value of a variable named baz in the module bound to bar in
1827 ;;; the module bound to foo in some-root-module>
1828 ;;;
1829 ;;;
1830 ;;; There are:
1831 ;;;
1832 ;;; ;; a-root is a module
1833 ;;; ;; name is a list of symbols
1834 ;;;
1835 ;;; nested-ref a-root name
1836 ;;; nested-set! a-root name val
1837 ;;; nested-define! a-root name val
1838 ;;; nested-remove! a-root name
1839 ;;;
1840 ;;;
1841 ;;; (current-module) is a natural choice for a-root so for convenience there are
1842 ;;; also:
1843 ;;;
1844 ;;; local-ref name == nested-ref (current-module) name
1845 ;;; local-set! name val == nested-set! (current-module) name val
1846 ;;; local-define! name val == nested-define! (current-module) name val
1847 ;;; local-remove! name == nested-remove! (current-module) name
1848 ;;;
1849
1850
1851 (define (nested-ref root names)
1852 (let loop ((cur root)
1853 (elts names))
1854 (cond
1855 ((null? elts) cur)
1856 ((not (module? cur)) #f)
1857 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1858
1859 (define (nested-set! root names val)
1860 (let loop ((cur root)
1861 (elts names))
1862 (if (null? (cdr elts))
1863 (module-set! cur (car elts) val)
1864 (loop (module-ref cur (car elts)) (cdr elts)))))
1865
1866 (define (nested-define! root names val)
1867 (let loop ((cur root)
1868 (elts names))
1869 (if (null? (cdr elts))
1870 (module-define! cur (car elts) val)
1871 (loop (module-ref cur (car elts)) (cdr elts)))))
1872
1873 (define (nested-remove! root names)
1874 (let loop ((cur root)
1875 (elts names))
1876 (if (null? (cdr elts))
1877 (module-remove! cur (car elts))
1878 (loop (module-ref cur (car elts)) (cdr elts)))))
1879
1880 (define (local-ref names) (nested-ref (current-module) names))
1881 (define (local-set! names val) (nested-set! (current-module) names val))
1882 (define (local-define names val) (nested-define! (current-module) names val))
1883 (define (local-remove names) (nested-remove! (current-module) names))
1884
1885
1886 \f
1887
1888 ;;; {The (%app) module}
1889 ;;;
1890 ;;; The root of conventionally named objects not directly in the top level.
1891 ;;;
1892 ;;; (%app modules)
1893 ;;; (%app modules guile)
1894 ;;;
1895 ;;; The directory of all modules and the standard root module.
1896 ;;;
1897
1898 ;; module-public-interface is defined in C.
1899 (define (set-module-public-interface! m i)
1900 (module-define! m '%module-public-interface i))
1901 (define (set-system-module! m s)
1902 (set-procedure-property! (module-eval-closure m) 'system-module s))
1903 (define the-root-module (make-root-module))
1904 (define the-scm-module (make-scm-module))
1905 (set-module-public-interface! the-root-module the-scm-module)
1906 (set-module-name! the-root-module '(guile))
1907 (set-module-name! the-scm-module '(guile))
1908 (set-module-kind! the-scm-module 'interface)
1909 (set-system-module! the-root-module #t)
1910 (set-system-module! the-scm-module #t)
1911
1912 ;; NOTE: This binding is used in libguile/modules.c.
1913 ;;
1914 (define (make-modules-in module name)
1915 (if (null? name)
1916 module
1917 (make-modules-in
1918 (let* ((var (module-local-variable module (car name)))
1919 (val (and var (variable-bound? var) (variable-ref var))))
1920 (if (module? val)
1921 val
1922 (let ((m (make-module 31)))
1923 (set-module-kind! m 'directory)
1924 (set-module-name! m (append (module-name module)
1925 (list (car name))))
1926 (module-define! module (car name) m)
1927 m)))
1928 (cdr name))))
1929
1930 (define (beautify-user-module! module)
1931 (let ((interface (module-public-interface module)))
1932 (if (or (not interface)
1933 (eq? interface module))
1934 (let ((interface (make-module 31)))
1935 (set-module-name! interface (module-name module))
1936 (set-module-kind! interface 'interface)
1937 (set-module-public-interface! module interface))))
1938 (if (and (not (memq the-scm-module (module-uses module)))
1939 (not (eq? module the-root-module)))
1940 ;; Import the default set of bindings (from the SCM module) in MODULE.
1941 (module-use! module the-scm-module)))
1942
1943 ;; NOTE: This binding is used in libguile/modules.c.
1944 ;;
1945 (define resolve-module
1946 (let ((the-root-module the-root-module))
1947 (lambda (name . maybe-autoload)
1948 (if (equal? name '(guile))
1949 the-root-module
1950 (let ((full-name (append '(%app modules) name)))
1951 (let ((already (nested-ref the-root-module full-name))
1952 (autoload (or (null? maybe-autoload) (car maybe-autoload))))
1953 (cond
1954 ((and already (module? already)
1955 (or (not autoload) (module-public-interface already)))
1956 ;; A hit, a palpable hit.
1957 already)
1958 (autoload
1959 ;; Try to autoload the module, and recurse.
1960 (try-load-module name)
1961 (resolve-module name #f))
1962 (else
1963 ;; A module is not bound (but maybe something else is),
1964 ;; we're not autoloading -- here's the weird semantics,
1965 ;; we create an empty module.
1966 (make-modules-in the-root-module full-name)))))))))
1967
1968 ;; Cheat. These bindings are needed by modules.c, but we don't want
1969 ;; to move their real definition here because that would be unnatural.
1970 ;;
1971 (define try-module-autoload #f)
1972 (define process-define-module #f)
1973 (define process-use-modules #f)
1974 (define module-export! #f)
1975 (define default-duplicate-binding-procedures #f)
1976
1977 (define %app (make-module 31))
1978 (set-module-name! %app '(%app))
1979 (define app %app) ;; for backwards compatability
1980
1981 (let ((m (make-module 31)))
1982 (set-module-name! m '())
1983 (local-define '(%app modules) m))
1984 (local-define '(%app modules guile) the-root-module)
1985
1986 ;; This boots the module system. All bindings needed by modules.c
1987 ;; must have been defined by now.
1988 ;;
1989 (set-current-module the-root-module)
1990 ;; definition deferred for syncase's benefit.
1991 (define module-name
1992 (let ((accessor (record-accessor module-type 'name)))
1993 (lambda (mod)
1994 (or (accessor mod)
1995 (let ((name (list (gensym))))
1996 ;; Name MOD and bind it in THE-ROOT-MODULE so that it's visible
1997 ;; to `resolve-module'. This is important as `psyntax' stores
1998 ;; module names and relies on being able to `resolve-module'
1999 ;; them.
2000 (set-module-name! mod name)
2001 (nested-define! the-root-module `(%app modules ,@name) mod)
2002 (accessor mod))))))
2003
2004 ;; (define-special-value '(%app modules new-ws) (lambda () (make-scm-module)))
2005
2006 (define (try-load-module name)
2007 (try-module-autoload name))
2008
2009 (define (purify-module! module)
2010 "Removes bindings in MODULE which are inherited from the (guile) module."
2011 (let ((use-list (module-uses module)))
2012 (if (and (pair? use-list)
2013 (eq? (car (last-pair use-list)) the-scm-module))
2014 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
2015
2016 ;; Return a module that is an interface to the module designated by
2017 ;; NAME.
2018 ;;
2019 ;; `resolve-interface' takes four keyword arguments:
2020 ;;
2021 ;; #:select SELECTION
2022 ;;
2023 ;; SELECTION is a list of binding-specs to be imported; A binding-spec
2024 ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG
2025 ;; is the name in the used module and SEEN is the name in the using
2026 ;; module. Note that SEEN is also passed through RENAMER, below. The
2027 ;; default is to select all bindings. If you specify no selection but
2028 ;; a renamer, only the bindings that already exist in the used module
2029 ;; are made available in the interface. Bindings that are added later
2030 ;; are not picked up.
2031 ;;
2032 ;; #:hide BINDINGS
2033 ;;
2034 ;; BINDINGS is a list of bindings which should not be imported.
2035 ;;
2036 ;; #:prefix PREFIX
2037 ;;
2038 ;; PREFIX is a symbol that will be appended to each exported name.
2039 ;; The default is to not perform any renaming.
2040 ;;
2041 ;; #:renamer RENAMER
2042 ;;
2043 ;; RENAMER is a procedure that takes a symbol and returns its new
2044 ;; name. The default is not perform any renaming.
2045 ;;
2046 ;; Signal "no code for module" error if module name is not resolvable
2047 ;; or its public interface is not available. Signal "no binding"
2048 ;; error if selected binding does not exist in the used module.
2049 ;;
2050 (define (resolve-interface name . args)
2051
2052 (define (get-keyword-arg args kw def)
2053 (cond ((memq kw args)
2054 => (lambda (kw-arg)
2055 (if (null? (cdr kw-arg))
2056 (error "keyword without value: " kw))
2057 (cadr kw-arg)))
2058 (else
2059 def)))
2060
2061 (let* ((select (get-keyword-arg args #:select #f))
2062 (hide (get-keyword-arg args #:hide '()))
2063 (renamer (or (get-keyword-arg args #:renamer #f)
2064 (let ((prefix (get-keyword-arg args #:prefix #f)))
2065 (and prefix (symbol-prefix-proc prefix)))
2066 identity))
2067 (module (resolve-module name))
2068 (public-i (and module (module-public-interface module))))
2069 (and (or (not module) (not public-i))
2070 (error "no code for module" name))
2071 (if (and (not select) (null? hide) (eq? renamer identity))
2072 public-i
2073 (let ((selection (or select (module-map (lambda (sym var) sym)
2074 public-i)))
2075 (custom-i (make-module 31)))
2076 (set-module-kind! custom-i 'custom-interface)
2077 (set-module-name! custom-i name)
2078 ;; XXX - should use a lazy binder so that changes to the
2079 ;; used module are picked up automatically.
2080 (for-each (lambda (bspec)
2081 (let* ((direct? (symbol? bspec))
2082 (orig (if direct? bspec (car bspec)))
2083 (seen (if direct? bspec (cdr bspec)))
2084 (var (or (module-local-variable public-i orig)
2085 (module-local-variable module orig)
2086 (error
2087 ;; fixme: format manually for now
2088 (simple-format
2089 #f "no binding `~A' in module ~A"
2090 orig name)))))
2091 (if (memq orig hide)
2092 (set! hide (delq! orig hide))
2093 (module-add! custom-i
2094 (renamer seen)
2095 var))))
2096 selection)
2097 ;; Check that we are not hiding bindings which don't exist
2098 (for-each (lambda (binding)
2099 (if (not (module-local-variable public-i binding))
2100 (error
2101 (simple-format
2102 #f "no binding `~A' to hide in module ~A"
2103 binding name))))
2104 hide)
2105 custom-i))))
2106
2107 (define (symbol-prefix-proc prefix)
2108 (lambda (symbol)
2109 (symbol-append prefix symbol)))
2110
2111 ;; This function is called from "modules.c". If you change it, be
2112 ;; sure to update "modules.c" as well.
2113
2114 (define (process-define-module args)
2115 (let* ((module-id (car args))
2116 (module (resolve-module module-id #f))
2117 (kws (cdr args))
2118 (unrecognized (lambda (arg)
2119 (error "unrecognized define-module argument" arg))))
2120 (beautify-user-module! module)
2121 (let loop ((kws kws)
2122 (reversed-interfaces '())
2123 (exports '())
2124 (re-exports '())
2125 (replacements '())
2126 (autoloads '()))
2127
2128 (if (null? kws)
2129 (call-with-deferred-observers
2130 (lambda ()
2131 (module-use-interfaces! module (reverse reversed-interfaces))
2132 (module-export! module exports)
2133 (module-replace! module replacements)
2134 (module-re-export! module re-exports)
2135 (if (not (null? autoloads))
2136 (apply module-autoload! module autoloads))))
2137 (case (car kws)
2138 ((#:use-module #:use-syntax)
2139 (or (pair? (cdr kws))
2140 (unrecognized kws))
2141 (cond
2142 ((equal? (caadr kws) '(ice-9 syncase))
2143 (issue-deprecation-warning
2144 "(ice-9 syncase) is deprecated. Support for syntax-case is now in Guile core.")
2145 (loop (cddr kws)
2146 reversed-interfaces
2147 exports
2148 re-exports
2149 replacements
2150 autoloads))
2151 (else
2152 (let* ((interface-args (cadr kws))
2153 (interface (apply resolve-interface interface-args)))
2154 (and (eq? (car kws) #:use-syntax)
2155 (or (symbol? (caar interface-args))
2156 (error "invalid module name for use-syntax"
2157 (car interface-args)))
2158 (set-module-transformer!
2159 module
2160 (module-ref interface
2161 (car (last-pair (car interface-args)))
2162 #f)))
2163 (loop (cddr kws)
2164 (cons interface reversed-interfaces)
2165 exports
2166 re-exports
2167 replacements
2168 autoloads)))))
2169 ((#:autoload)
2170 (or (and (pair? (cdr kws)) (pair? (cddr kws)))
2171 (unrecognized kws))
2172 (loop (cdddr kws)
2173 reversed-interfaces
2174 exports
2175 re-exports
2176 replacements
2177 (let ((name (cadr kws))
2178 (bindings (caddr kws)))
2179 (cons* name bindings autoloads))))
2180 ((#:no-backtrace)
2181 (set-system-module! module #t)
2182 (loop (cdr kws) reversed-interfaces exports re-exports
2183 replacements autoloads))
2184 ((#:pure)
2185 (purify-module! module)
2186 (loop (cdr kws) reversed-interfaces exports re-exports
2187 replacements autoloads))
2188 ((#:duplicates)
2189 (if (not (pair? (cdr kws)))
2190 (unrecognized kws))
2191 (set-module-duplicates-handlers!
2192 module
2193 (lookup-duplicates-handlers (cadr kws)))
2194 (loop (cddr kws) reversed-interfaces exports re-exports
2195 replacements autoloads))
2196 ((#:export #:export-syntax)
2197 (or (pair? (cdr kws))
2198 (unrecognized kws))
2199 (loop (cddr kws)
2200 reversed-interfaces
2201 (append (cadr kws) exports)
2202 re-exports
2203 replacements
2204 autoloads))
2205 ((#:re-export #:re-export-syntax)
2206 (or (pair? (cdr kws))
2207 (unrecognized kws))
2208 (loop (cddr kws)
2209 reversed-interfaces
2210 exports
2211 (append (cadr kws) re-exports)
2212 replacements
2213 autoloads))
2214 ((#:replace #:replace-syntax)
2215 (or (pair? (cdr kws))
2216 (unrecognized kws))
2217 (loop (cddr kws)
2218 reversed-interfaces
2219 exports
2220 re-exports
2221 (append (cadr kws) replacements)
2222 autoloads))
2223 (else
2224 (unrecognized kws)))))
2225 (run-hook module-defined-hook module)
2226 module))
2227
2228 ;; `module-defined-hook' is a hook that is run whenever a new module
2229 ;; is defined. Its members are called with one argument, the new
2230 ;; module.
2231 (define module-defined-hook (make-hook 1))
2232
2233 \f
2234
2235 ;;; {Autoload}
2236 ;;;
2237
2238 (define (make-autoload-interface module name bindings)
2239 (let ((b (lambda (a sym definep)
2240 (and (memq sym bindings)
2241 (let ((i (module-public-interface (resolve-module name))))
2242 (if (not i)
2243 (error "missing interface for module" name))
2244 (let ((autoload (memq a (module-uses module))))
2245 ;; Replace autoload-interface with actual interface if
2246 ;; that has not happened yet.
2247 (if (pair? autoload)
2248 (set-car! autoload i)))
2249 (module-local-variable i sym))))))
2250 (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f
2251 (make-hash-table 0) '() (make-weak-value-hash-table 31))))
2252
2253 (define (module-autoload! module . args)
2254 "Have @var{module} automatically load the module named @var{name} when one
2255 of the symbols listed in @var{bindings} is looked up. @var{args} should be a
2256 list of module-name/binding-list pairs, e.g., as in @code{(module-autoload!
2257 module '(ice-9 q) '(make-q q-length))}."
2258 (let loop ((args args))
2259 (cond ((null? args)
2260 #t)
2261 ((null? (cdr args))
2262 (error "invalid name+binding autoload list" args))
2263 (else
2264 (let ((name (car args))
2265 (bindings (cadr args)))
2266 (module-use! module (make-autoload-interface module
2267 name bindings))
2268 (loop (cddr args)))))))
2269
2270
2271 \f
2272
2273 ;;; {Autoloading modules}
2274 ;;;
2275
2276 (define autoloads-in-progress '())
2277
2278 ;; This function is called from "modules.c". If you change it, be
2279 ;; sure to update "modules.c" as well.
2280
2281 (define (try-module-autoload module-name)
2282 (let* ((reverse-name (reverse module-name))
2283 (name (symbol->string (car reverse-name)))
2284 (dir-hint-module-name (reverse (cdr reverse-name)))
2285 (dir-hint (apply string-append
2286 (map (lambda (elt)
2287 (string-append (symbol->string elt) "/"))
2288 dir-hint-module-name))))
2289 (resolve-module dir-hint-module-name #f)
2290 (and (not (autoload-done-or-in-progress? dir-hint name))
2291 (let ((didit #f))
2292 (dynamic-wind
2293 (lambda () (autoload-in-progress! dir-hint name))
2294 (lambda ()
2295 (with-fluid* current-reader #f
2296 (lambda ()
2297 (save-module-excursion
2298 (lambda ()
2299 (primitive-load-path (in-vicinity dir-hint name) #f)
2300 (set! didit #t))))))
2301 (lambda () (set-autoloaded! dir-hint name didit)))
2302 didit))))
2303
2304 \f
2305
2306 ;;; {Dynamic linking of modules}
2307 ;;;
2308
2309 (define autoloads-done '((guile . guile)))
2310
2311 (define (autoload-done-or-in-progress? p m)
2312 (let ((n (cons p m)))
2313 (->bool (or (member n autoloads-done)
2314 (member n autoloads-in-progress)))))
2315
2316 (define (autoload-done! p m)
2317 (let ((n (cons p m)))
2318 (set! autoloads-in-progress
2319 (delete! n autoloads-in-progress))
2320 (or (member n autoloads-done)
2321 (set! autoloads-done (cons n autoloads-done)))))
2322
2323 (define (autoload-in-progress! p m)
2324 (let ((n (cons p m)))
2325 (set! autoloads-done
2326 (delete! n autoloads-done))
2327 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2328
2329 (define (set-autoloaded! p m done?)
2330 (if done?
2331 (autoload-done! p m)
2332 (let ((n (cons p m)))
2333 (set! autoloads-done (delete! n autoloads-done))
2334 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2335
2336 \f
2337
2338 ;;; {Run-time options}
2339 ;;;
2340
2341 (defmacro define-option-interface (option-group)
2342 (let* ((option-name 'car)
2343 (option-value 'cadr)
2344 (option-documentation 'caddr)
2345
2346 ;; Below follow the macros defining the run-time option interfaces.
2347
2348 (make-options (lambda (interface)
2349 `(lambda args
2350 (cond ((null? args) (,interface))
2351 ((list? (car args))
2352 (,interface (car args)) (,interface))
2353 (else (for-each
2354 (lambda (option)
2355 (display (,option-name option))
2356 (if (< (string-length
2357 (symbol->string (,option-name option)))
2358 8)
2359 (display #\tab))
2360 (display #\tab)
2361 (display (,option-value option))
2362 (display #\tab)
2363 (display (,option-documentation option))
2364 (newline))
2365 (,interface #t)))))))
2366
2367 (make-enable (lambda (interface)
2368 `(lambda flags
2369 (,interface (append flags (,interface)))
2370 (,interface))))
2371
2372 (make-disable (lambda (interface)
2373 `(lambda flags
2374 (let ((options (,interface)))
2375 (for-each (lambda (flag)
2376 (set! options (delq! flag options)))
2377 flags)
2378 (,interface options)
2379 (,interface))))))
2380 (let* ((interface (car option-group))
2381 (options/enable/disable (cadr option-group)))
2382 `(begin
2383 (define ,(car options/enable/disable)
2384 ,(make-options interface))
2385 (define ,(cadr options/enable/disable)
2386 ,(make-enable interface))
2387 (define ,(caddr options/enable/disable)
2388 ,(make-disable interface))
2389 (defmacro ,(caaddr option-group) (opt val)
2390 `(,',(car options/enable/disable)
2391 (append (,',(car options/enable/disable))
2392 (list ',opt ,val))))))))
2393
2394 (define-option-interface
2395 (eval-options-interface
2396 (eval-options eval-enable eval-disable)
2397 (eval-set!)))
2398
2399 (define-option-interface
2400 (debug-options-interface
2401 (debug-options debug-enable debug-disable)
2402 (debug-set!)))
2403
2404 (define-option-interface
2405 (evaluator-traps-interface
2406 (traps trap-enable trap-disable)
2407 (trap-set!)))
2408
2409 (define-option-interface
2410 (read-options-interface
2411 (read-options read-enable read-disable)
2412 (read-set!)))
2413
2414 (define-option-interface
2415 (print-options-interface
2416 (print-options print-enable print-disable)
2417 (print-set!)))
2418
2419 \f
2420
2421 ;;; {Running Repls}
2422 ;;;
2423
2424 (define (repl read evaler print)
2425 (let loop ((source (read (current-input-port))))
2426 (print (evaler source))
2427 (loop (read (current-input-port)))))
2428
2429 ;; A provisional repl that acts like the SCM repl:
2430 ;;
2431 (define scm-repl-silent #f)
2432 (define (assert-repl-silence v) (set! scm-repl-silent v))
2433
2434 (define *unspecified* (if #f #f))
2435 (define (unspecified? v) (eq? v *unspecified*))
2436
2437 (define scm-repl-print-unspecified #f)
2438 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2439
2440 (define scm-repl-verbose #f)
2441 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2442
2443 (define scm-repl-prompt "guile> ")
2444
2445 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2446
2447 (define (default-pre-unwind-handler key . args)
2448 (save-stack 1)
2449 (apply throw key args))
2450
2451 (begin-deprecated
2452 (define (pre-unwind-handler-dispatch key . args)
2453 (apply default-pre-unwind-handler key args)))
2454
2455 (define abort-hook (make-hook))
2456
2457 ;; these definitions are used if running a script.
2458 ;; otherwise redefined in error-catching-loop.
2459 (define (set-batch-mode?! arg) #t)
2460 (define (batch-mode?) #t)
2461
2462 (define (error-catching-loop thunk)
2463 (let ((status #f)
2464 (interactive #t))
2465 (define (loop first)
2466 (let ((next
2467 (catch #t
2468
2469 (lambda ()
2470 (call-with-unblocked-asyncs
2471 (lambda ()
2472 (with-traps
2473 (lambda ()
2474 (first)
2475
2476 ;; This line is needed because mark
2477 ;; doesn't do closures quite right.
2478 ;; Unreferenced locals should be
2479 ;; collected.
2480 (set! first #f)
2481 (let loop ((v (thunk)))
2482 (loop (thunk)))
2483 #f)))))
2484
2485 (lambda (key . args)
2486 (case key
2487 ((quit)
2488 (set! status args)
2489 #f)
2490
2491 ((switch-repl)
2492 (apply throw 'switch-repl args))
2493
2494 ((abort)
2495 ;; This is one of the closures that require
2496 ;; (set! first #f) above
2497 ;;
2498 (lambda ()
2499 (run-hook abort-hook)
2500 (force-output (current-output-port))
2501 (display "ABORT: " (current-error-port))
2502 (write args (current-error-port))
2503 (newline (current-error-port))
2504 (if interactive
2505 (begin
2506 (if (and
2507 (not has-shown-debugger-hint?)
2508 (not (memq 'backtrace
2509 (debug-options-interface)))
2510 (stack? (fluid-ref the-last-stack)))
2511 (begin
2512 (newline (current-error-port))
2513 (display
2514 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2515 (current-error-port))
2516 (set! has-shown-debugger-hint? #t)))
2517 (force-output (current-error-port)))
2518 (begin
2519 (primitive-exit 1)))
2520 (set! stack-saved? #f)))
2521
2522 (else
2523 ;; This is the other cons-leak closure...
2524 (lambda ()
2525 (cond ((= (length args) 4)
2526 (apply handle-system-error key args))
2527 (else
2528 (apply bad-throw key args)))))))
2529
2530 default-pre-unwind-handler)))
2531
2532 (if next (loop next) status)))
2533 (set! set-batch-mode?! (lambda (arg)
2534 (cond (arg
2535 (set! interactive #f)
2536 (restore-signals))
2537 (#t
2538 (error "sorry, not implemented")))))
2539 (set! batch-mode? (lambda () (not interactive)))
2540 (call-with-blocked-asyncs
2541 (lambda () (loop (lambda () #t))))))
2542
2543 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2544 (define before-signal-stack (make-fluid))
2545 (define stack-saved? #f)
2546
2547 (define (save-stack . narrowing)
2548 (or stack-saved?
2549 (cond ((not (memq 'debug (debug-options-interface)))
2550 (fluid-set! the-last-stack #f)
2551 (set! stack-saved? #t))
2552 (else
2553 (fluid-set!
2554 the-last-stack
2555 (case (stack-id #t)
2556 ((repl-stack)
2557 (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
2558 ((load-stack)
2559 (apply make-stack #t save-stack 0 #t 0 narrowing))
2560 ((tk-stack)
2561 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2562 ((#t)
2563 (apply make-stack #t save-stack 0 1 narrowing))
2564 (else
2565 (let ((id (stack-id #t)))
2566 (and (procedure? id)
2567 (apply make-stack #t save-stack id #t 0 narrowing))))))
2568 (set! stack-saved? #t)))))
2569
2570 (define before-error-hook (make-hook))
2571 (define after-error-hook (make-hook))
2572 (define before-backtrace-hook (make-hook))
2573 (define after-backtrace-hook (make-hook))
2574
2575 (define has-shown-debugger-hint? #f)
2576
2577 (define (handle-system-error key . args)
2578 (let ((cep (current-error-port)))
2579 (cond ((not (stack? (fluid-ref the-last-stack))))
2580 ((memq 'backtrace (debug-options-interface))
2581 (let ((highlights (if (or (eq? key 'wrong-type-arg)
2582 (eq? key 'out-of-range))
2583 (list-ref args 3)
2584 '())))
2585 (run-hook before-backtrace-hook)
2586 (newline cep)
2587 (display "Backtrace:\n")
2588 (display-backtrace (fluid-ref the-last-stack) cep
2589 #f #f highlights)
2590 (newline cep)
2591 (run-hook after-backtrace-hook))))
2592 (run-hook before-error-hook)
2593 (apply display-error (fluid-ref the-last-stack) cep args)
2594 (run-hook after-error-hook)
2595 (force-output cep)
2596 (throw 'abort key)))
2597
2598 (define (quit . args)
2599 (apply throw 'quit args))
2600
2601 (define exit quit)
2602
2603 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2604
2605 ;; Replaced by C code:
2606 ;;(define (backtrace)
2607 ;; (if (fluid-ref the-last-stack)
2608 ;; (begin
2609 ;; (newline)
2610 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2611 ;; (newline)
2612 ;; (if (and (not has-shown-backtrace-hint?)
2613 ;; (not (memq 'backtrace (debug-options-interface))))
2614 ;; (begin
2615 ;; (display
2616 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2617 ;;automatically if an error occurs in the future.\n")
2618 ;; (set! has-shown-backtrace-hint? #t))))
2619 ;; (display "No backtrace available.\n")))
2620
2621 (define (error-catching-repl r e p)
2622 (error-catching-loop
2623 (lambda ()
2624 (call-with-values (lambda () (e (r)))
2625 (lambda the-values (for-each p the-values))))))
2626
2627 (define (gc-run-time)
2628 (cdr (assq 'gc-time-taken (gc-stats))))
2629
2630 (define before-read-hook (make-hook))
2631 (define after-read-hook (make-hook))
2632 (define before-eval-hook (make-hook 1))
2633 (define after-eval-hook (make-hook 1))
2634 (define before-print-hook (make-hook 1))
2635 (define after-print-hook (make-hook 1))
2636
2637 ;;; The default repl-reader function. We may override this if we've
2638 ;;; the readline library.
2639 (define repl-reader
2640 (lambda (prompt)
2641 (display (if (string? prompt) prompt (prompt)))
2642 (force-output)
2643 (run-hook before-read-hook)
2644 ((or (fluid-ref current-reader) read) (current-input-port))))
2645
2646 (define (scm-style-repl)
2647
2648 (letrec (
2649 (start-gc-rt #f)
2650 (start-rt #f)
2651 (repl-report-start-timing (lambda ()
2652 (set! start-gc-rt (gc-run-time))
2653 (set! start-rt (get-internal-run-time))))
2654 (repl-report (lambda ()
2655 (display ";;; ")
2656 (display (inexact->exact
2657 (* 1000 (/ (- (get-internal-run-time) start-rt)
2658 internal-time-units-per-second))))
2659 (display " msec (")
2660 (display (inexact->exact
2661 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2662 internal-time-units-per-second))))
2663 (display " msec in gc)\n")))
2664
2665 (consume-trailing-whitespace
2666 (lambda ()
2667 (let ((ch (peek-char)))
2668 (cond
2669 ((eof-object? ch))
2670 ((or (char=? ch #\space) (char=? ch #\tab))
2671 (read-char)
2672 (consume-trailing-whitespace))
2673 ((char=? ch #\newline)
2674 (read-char))))))
2675 (-read (lambda ()
2676 (let ((val
2677 (let ((prompt (cond ((string? scm-repl-prompt)
2678 scm-repl-prompt)
2679 ((thunk? scm-repl-prompt)
2680 (scm-repl-prompt))
2681 (scm-repl-prompt "> ")
2682 (else ""))))
2683 (repl-reader prompt))))
2684
2685 ;; As described in R4RS, the READ procedure updates the
2686 ;; port to point to the first character past the end of
2687 ;; the external representation of the object. This
2688 ;; means that it doesn't consume the newline typically
2689 ;; found after an expression. This means that, when
2690 ;; debugging Guile with GDB, GDB gets the newline, which
2691 ;; it often interprets as a "continue" command, making
2692 ;; breakpoints kind of useless. So, consume any
2693 ;; trailing newline here, as well as any whitespace
2694 ;; before it.
2695 ;; But not if EOF, for control-D.
2696 (if (not (eof-object? val))
2697 (consume-trailing-whitespace))
2698 (run-hook after-read-hook)
2699 (if (eof-object? val)
2700 (begin
2701 (repl-report-start-timing)
2702 (if scm-repl-verbose
2703 (begin
2704 (newline)
2705 (display ";;; EOF -- quitting")
2706 (newline)))
2707 (quit 0)))
2708 val)))
2709
2710 (-eval (lambda (sourc)
2711 (repl-report-start-timing)
2712 (run-hook before-eval-hook sourc)
2713 (let ((val (start-stack 'repl-stack
2714 ;; If you change this procedure
2715 ;; (primitive-eval), please also
2716 ;; modify the repl-stack case in
2717 ;; save-stack so that stack cutting
2718 ;; continues to work.
2719 (primitive-eval sourc))))
2720 (run-hook after-eval-hook sourc)
2721 val)))
2722
2723
2724 (-print (let ((maybe-print (lambda (result)
2725 (if (or scm-repl-print-unspecified
2726 (not (unspecified? result)))
2727 (begin
2728 (write result)
2729 (newline))))))
2730 (lambda (result)
2731 (if (not scm-repl-silent)
2732 (begin
2733 (run-hook before-print-hook result)
2734 (maybe-print result)
2735 (run-hook after-print-hook result)
2736 (if scm-repl-verbose
2737 (repl-report))
2738 (force-output))))))
2739
2740 (-quit (lambda (args)
2741 (if scm-repl-verbose
2742 (begin
2743 (display ";;; QUIT executed, repl exitting")
2744 (newline)
2745 (repl-report)))
2746 args))
2747
2748 (-abort (lambda ()
2749 (if scm-repl-verbose
2750 (begin
2751 (display ";;; ABORT executed.")
2752 (newline)
2753 (repl-report)))
2754 (repl -read -eval -print))))
2755
2756 (let ((status (error-catching-repl -read
2757 -eval
2758 -print)))
2759 (-quit status))))
2760
2761
2762 \f
2763
2764 ;;; {IOTA functions: generating lists of numbers}
2765 ;;;
2766
2767 (define (iota n)
2768 (let loop ((count (1- n)) (result '()))
2769 (if (< count 0) result
2770 (loop (1- count) (cons count result)))))
2771
2772 \f
2773
2774 ;;; {collect}
2775 ;;;
2776 ;;; Similar to `begin' but returns a list of the results of all constituent
2777 ;;; forms instead of the result of the last form.
2778 ;;; (The definition relies on the current left-to-right
2779 ;;; order of evaluation of operands in applications.)
2780 ;;;
2781
2782 (defmacro collect forms
2783 (cons 'list forms))
2784
2785 \f
2786
2787 ;;; {with-fluids}
2788 ;;;
2789
2790 ;; with-fluids is a convenience wrapper for the builtin procedure
2791 ;; `with-fluids*'. The syntax is just like `let':
2792 ;;
2793 ;; (with-fluids ((fluid val)
2794 ;; ...)
2795 ;; body)
2796
2797 (defmacro with-fluids (bindings . body)
2798 (let ((fluids (map car bindings))
2799 (values (map cadr bindings)))
2800 (if (and (= (length fluids) 1) (= (length values) 1))
2801 `(with-fluid* ,(car fluids) ,(car values) (lambda () ,@body))
2802 `(with-fluids* (list ,@fluids) (list ,@values)
2803 (lambda () ,@body)))))
2804
2805 ;;; {While}
2806 ;;;
2807 ;;; with `continue' and `break'.
2808 ;;;
2809
2810 ;; The inner `do' loop avoids re-establishing a catch every iteration,
2811 ;; that's only necessary if continue is actually used. A new key is
2812 ;; generated every time, so break and continue apply to their originating
2813 ;; `while' even when recursing.
2814 ;;
2815 ;; FIXME: This macro is unintentionally unhygienic with respect to let,
2816 ;; make-symbol, do, throw, catch, lambda, and not.
2817 ;;
2818 (define-macro (while cond . body)
2819 (let ((keyvar (make-symbol "while-keyvar")))
2820 `(let ((,keyvar (make-symbol "while-key")))
2821 (do ()
2822 ((catch ,keyvar
2823 (lambda ()
2824 (let ((break (lambda () (throw ,keyvar #t)))
2825 (continue (lambda () (throw ,keyvar #f))))
2826 (do ()
2827 ((not ,cond))
2828 ,@body)
2829 #t))
2830 (lambda (key arg)
2831 arg)))))))
2832
2833
2834 \f
2835
2836 ;;; {Module System Macros}
2837 ;;;
2838
2839 ;; Return a list of expressions that evaluate to the appropriate
2840 ;; arguments for resolve-interface according to SPEC.
2841
2842 (eval-when
2843 (compile)
2844 (if (memq 'prefix (read-options))
2845 (error "boot-9 must be compiled with #:kw, not :kw")))
2846
2847 (define (compile-interface-spec spec)
2848 (define (make-keyarg sym key quote?)
2849 (cond ((or (memq sym spec)
2850 (memq key spec))
2851 => (lambda (rest)
2852 (if quote?
2853 (list key (list 'quote (cadr rest)))
2854 (list key (cadr rest)))))
2855 (else
2856 '())))
2857 (define (map-apply func list)
2858 (map (lambda (args) (apply func args)) list))
2859 (define keys
2860 ;; sym key quote?
2861 '((:select #:select #t)
2862 (:hide #:hide #t)
2863 (:prefix #:prefix #t)
2864 (:renamer #:renamer #f)))
2865 (if (not (pair? (car spec)))
2866 `(',spec)
2867 `(',(car spec)
2868 ,@(apply append (map-apply make-keyarg keys)))))
2869
2870 (define (keyword-like-symbol->keyword sym)
2871 (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
2872
2873 (define (compile-define-module-args args)
2874 ;; Just quote everything except #:use-module and #:use-syntax. We
2875 ;; need to know about all arguments regardless since we want to turn
2876 ;; symbols that look like keywords into real keywords, and the
2877 ;; keyword args in a define-module form are not regular
2878 ;; (i.e. no-backtrace doesn't take a value).
2879 (let loop ((compiled-args `((quote ,(car args))))
2880 (args (cdr args)))
2881 (cond ((null? args)
2882 (reverse! compiled-args))
2883 ;; symbol in keyword position
2884 ((symbol? (car args))
2885 (loop compiled-args
2886 (cons (keyword-like-symbol->keyword (car args)) (cdr args))))
2887 ((memq (car args) '(#:no-backtrace #:pure))
2888 (loop (cons (car args) compiled-args)
2889 (cdr args)))
2890 ((null? (cdr args))
2891 (error "keyword without value:" (car args)))
2892 ((memq (car args) '(#:use-module #:use-syntax))
2893 (loop (cons* `(list ,@(compile-interface-spec (cadr args)))
2894 (car args)
2895 compiled-args)
2896 (cddr args)))
2897 ((eq? (car args) #:autoload)
2898 (loop (cons* `(quote ,(caddr args))
2899 `(quote ,(cadr args))
2900 (car args)
2901 compiled-args)
2902 (cdddr args)))
2903 (else
2904 (loop (cons* `(quote ,(cadr args))
2905 (car args)
2906 compiled-args)
2907 (cddr args))))))
2908
2909 (defmacro define-module args
2910 `(eval-when
2911 (eval load compile)
2912 (let ((m (process-define-module
2913 (list ,@(compile-define-module-args args)))))
2914 (set-current-module m)
2915 m)))
2916
2917 ;; The guts of the use-modules macro. Add the interfaces of the named
2918 ;; modules to the use-list of the current module, in order.
2919
2920 ;; This function is called by "modules.c". If you change it, be sure
2921 ;; to change scm_c_use_module as well.
2922
2923 (define (process-use-modules module-interface-args)
2924 (let ((interfaces (map (lambda (mif-args)
2925 (or (apply resolve-interface mif-args)
2926 (error "no such module" mif-args)))
2927 module-interface-args)))
2928 (call-with-deferred-observers
2929 (lambda ()
2930 (module-use-interfaces! (current-module) interfaces)))))
2931
2932 (defmacro use-modules modules
2933 `(eval-when
2934 (eval load compile)
2935 (process-use-modules
2936 (list ,@(map (lambda (m)
2937 `(list ,@(compile-interface-spec m)))
2938 modules)))
2939 *unspecified*))
2940
2941 (defmacro use-syntax (spec)
2942 `(eval-when
2943 (eval load compile)
2944 (issue-deprecation-warning
2945 "`use-syntax' is deprecated. Please contact guile-devel for more info.")
2946 (process-use-modules (list (list ,@(compile-interface-spec spec))))
2947 *unspecified*))
2948
2949 (define-syntax define-private
2950 (syntax-rules ()
2951 ((_ foo bar)
2952 (define foo bar))))
2953
2954 (define-syntax define-public
2955 (syntax-rules ()
2956 ((_ (name . args) . body)
2957 (define-public name (lambda args . body)))
2958 ((_ name val)
2959 (begin
2960 (define name val)
2961 (export name)))))
2962
2963 (define-syntax defmacro-public
2964 (syntax-rules ()
2965 ((_ name args . body)
2966 (begin
2967 (defmacro name args . body)
2968 (export-syntax name)))))
2969
2970 ;; Export a local variable
2971
2972 ;; This function is called from "modules.c". If you change it, be
2973 ;; sure to update "modules.c" as well.
2974
2975 (define (module-export! m names)
2976 (let ((public-i (module-public-interface m)))
2977 (for-each (lambda (name)
2978 (let ((var (module-ensure-local-variable! m name)))
2979 (module-add! public-i name var)))
2980 names)))
2981
2982 (define (module-replace! m names)
2983 (let ((public-i (module-public-interface m)))
2984 (for-each (lambda (name)
2985 (let ((var (module-ensure-local-variable! m name)))
2986 (set-object-property! var 'replace #t)
2987 (module-add! public-i name var)))
2988 names)))
2989
2990 ;; Re-export a imported variable
2991 ;;
2992 (define (module-re-export! m names)
2993 (let ((public-i (module-public-interface m)))
2994 (for-each (lambda (name)
2995 (let ((var (module-variable m name)))
2996 (cond ((not var)
2997 (error "Undefined variable:" name))
2998 ((eq? var (module-local-variable m name))
2999 (error "re-exporting local variable:" name))
3000 (else
3001 (module-add! public-i name var)))))
3002 names)))
3003
3004 (defmacro export names
3005 `(call-with-deferred-observers
3006 (lambda ()
3007 (module-export! (current-module) ',names))))
3008
3009 (defmacro re-export names
3010 `(call-with-deferred-observers
3011 (lambda ()
3012 (module-re-export! (current-module) ',names))))
3013
3014 (defmacro export-syntax names
3015 `(export ,@names))
3016
3017 (defmacro re-export-syntax names
3018 `(re-export ,@names))
3019
3020 (define load load-module)
3021
3022 \f
3023
3024 ;;; {Parameters}
3025 ;;;
3026
3027 (define make-mutable-parameter
3028 (let ((make (lambda (fluid converter)
3029 (lambda args
3030 (if (null? args)
3031 (fluid-ref fluid)
3032 (fluid-set! fluid (converter (car args))))))))
3033 (lambda (init . converter)
3034 (let ((fluid (make-fluid))
3035 (converter (if (null? converter)
3036 identity
3037 (car converter))))
3038 (fluid-set! fluid (converter init))
3039 (make fluid converter)))))
3040
3041 \f
3042
3043 ;;; {Handling of duplicate imported bindings}
3044 ;;;
3045
3046 ;; Duplicate handlers take the following arguments:
3047 ;;
3048 ;; module importing module
3049 ;; name conflicting name
3050 ;; int1 old interface where name occurs
3051 ;; val1 value of binding in old interface
3052 ;; int2 new interface where name occurs
3053 ;; val2 value of binding in new interface
3054 ;; var previous resolution or #f
3055 ;; val value of previous resolution
3056 ;;
3057 ;; A duplicate handler can take three alternative actions:
3058 ;;
3059 ;; 1. return #f => leave responsibility to next handler
3060 ;; 2. exit with an error
3061 ;; 3. return a variable resolving the conflict
3062 ;;
3063
3064 (define duplicate-handlers
3065 (let ((m (make-module 7)))
3066
3067 (define (check module name int1 val1 int2 val2 var val)
3068 (scm-error 'misc-error
3069 #f
3070 "~A: `~A' imported from both ~A and ~A"
3071 (list (module-name module)
3072 name
3073 (module-name int1)
3074 (module-name int2))
3075 #f))
3076
3077 (define (warn module name int1 val1 int2 val2 var val)
3078 (format (current-error-port)
3079 "WARNING: ~A: `~A' imported from both ~A and ~A\n"
3080 (module-name module)
3081 name
3082 (module-name int1)
3083 (module-name int2))
3084 #f)
3085
3086 (define (replace module name int1 val1 int2 val2 var val)
3087 (let ((old (or (and var (object-property var 'replace) var)
3088 (module-variable int1 name)))
3089 (new (module-variable int2 name)))
3090 (if (object-property old 'replace)
3091 (and (or (eq? old new)
3092 (not (object-property new 'replace)))
3093 old)
3094 (and (object-property new 'replace)
3095 new))))
3096
3097 (define (warn-override-core module name int1 val1 int2 val2 var val)
3098 (and (eq? int1 the-scm-module)
3099 (begin
3100 (format (current-error-port)
3101 "WARNING: ~A: imported module ~A overrides core binding `~A'\n"
3102 (module-name module)
3103 (module-name int2)
3104 name)
3105 (module-local-variable int2 name))))
3106
3107 (define (first module name int1 val1 int2 val2 var val)
3108 (or var (module-local-variable int1 name)))
3109
3110 (define (last module name int1 val1 int2 val2 var val)
3111 (module-local-variable int2 name))
3112
3113 (define (noop module name int1 val1 int2 val2 var val)
3114 #f)
3115
3116 (set-module-name! m 'duplicate-handlers)
3117 (set-module-kind! m 'interface)
3118 (module-define! m 'check check)
3119 (module-define! m 'warn warn)
3120 (module-define! m 'replace replace)
3121 (module-define! m 'warn-override-core warn-override-core)
3122 (module-define! m 'first first)
3123 (module-define! m 'last last)
3124 (module-define! m 'merge-generics noop)
3125 (module-define! m 'merge-accessors noop)
3126 m))
3127
3128 (define (lookup-duplicates-handlers handler-names)
3129 (and handler-names
3130 (map (lambda (handler-name)
3131 (or (module-symbol-local-binding
3132 duplicate-handlers handler-name #f)
3133 (error "invalid duplicate handler name:"
3134 handler-name)))
3135 (if (list? handler-names)
3136 handler-names
3137 (list handler-names)))))
3138
3139 (define default-duplicate-binding-procedures
3140 (make-mutable-parameter #f))
3141
3142 (define default-duplicate-binding-handler
3143 (make-mutable-parameter '(replace warn-override-core warn last)
3144 (lambda (handler-names)
3145 (default-duplicate-binding-procedures
3146 (lookup-duplicates-handlers handler-names))
3147 handler-names)))
3148
3149 \f
3150
3151 ;;; {`cond-expand' for SRFI-0 support.}
3152 ;;;
3153 ;;; This syntactic form expands into different commands or
3154 ;;; definitions, depending on the features provided by the Scheme
3155 ;;; implementation.
3156 ;;;
3157 ;;; Syntax:
3158 ;;;
3159 ;;; <cond-expand>
3160 ;;; --> (cond-expand <cond-expand-clause>+)
3161 ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
3162 ;;; <cond-expand-clause>
3163 ;;; --> (<feature-requirement> <command-or-definition>*)
3164 ;;; <feature-requirement>
3165 ;;; --> <feature-identifier>
3166 ;;; | (and <feature-requirement>*)
3167 ;;; | (or <feature-requirement>*)
3168 ;;; | (not <feature-requirement>)
3169 ;;; <feature-identifier>
3170 ;;; --> <a symbol which is the name or alias of a SRFI>
3171 ;;;
3172 ;;; Additionally, this implementation provides the
3173 ;;; <feature-identifier>s `guile' and `r5rs', so that programs can
3174 ;;; determine the implementation type and the supported standard.
3175 ;;;
3176 ;;; Currently, the following feature identifiers are supported:
3177 ;;;
3178 ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61
3179 ;;;
3180 ;;; Remember to update the features list when adding more SRFIs.
3181 ;;;
3182
3183 (define %cond-expand-features
3184 ;; Adjust the above comment when changing this.
3185 '(guile
3186 r5rs
3187 srfi-0 ;; cond-expand itself
3188 srfi-4 ;; homogenous numeric vectors
3189 srfi-6 ;; open-input-string etc, in the guile core
3190 srfi-13 ;; string library
3191 srfi-14 ;; character sets
3192 srfi-55 ;; require-extension
3193 srfi-61 ;; general cond clause
3194 ))
3195
3196 ;; This table maps module public interfaces to the list of features.
3197 ;;
3198 (define %cond-expand-table (make-hash-table 31))
3199
3200 ;; Add one or more features to the `cond-expand' feature list of the
3201 ;; module `module'.
3202 ;;
3203 (define (cond-expand-provide module features)
3204 (let ((mod (module-public-interface module)))
3205 (and mod
3206 (hashq-set! %cond-expand-table mod
3207 (append (hashq-ref %cond-expand-table mod '())
3208 features)))))
3209
3210 (define-macro (cond-expand . clauses)
3211 (let ((syntax-error (lambda (cl)
3212 (error "invalid clause in `cond-expand'" cl))))
3213 (letrec
3214 ((test-clause
3215 (lambda (clause)
3216 (cond
3217 ((symbol? clause)
3218 (or (memq clause %cond-expand-features)
3219 (let lp ((uses (module-uses (current-module))))
3220 (if (pair? uses)
3221 (or (memq clause
3222 (hashq-ref %cond-expand-table
3223 (car uses) '()))
3224 (lp (cdr uses)))
3225 #f))))
3226 ((pair? clause)
3227 (cond
3228 ((eq? 'and (car clause))
3229 (let lp ((l (cdr clause)))
3230 (cond ((null? l)
3231 #t)
3232 ((pair? l)
3233 (and (test-clause (car l)) (lp (cdr l))))
3234 (else
3235 (syntax-error clause)))))
3236 ((eq? 'or (car clause))
3237 (let lp ((l (cdr clause)))
3238 (cond ((null? l)
3239 #f)
3240 ((pair? l)
3241 (or (test-clause (car l)) (lp (cdr l))))
3242 (else
3243 (syntax-error clause)))))
3244 ((eq? 'not (car clause))
3245 (cond ((not (pair? (cdr clause)))
3246 (syntax-error clause))
3247 ((pair? (cddr clause))
3248 ((syntax-error clause))))
3249 (not (test-clause (cadr clause))))
3250 (else
3251 (syntax-error clause))))
3252 (else
3253 (syntax-error clause))))))
3254 (let lp ((c clauses))
3255 (cond
3256 ((null? c)
3257 (error "Unfulfilled `cond-expand'"))
3258 ((not (pair? c))
3259 (syntax-error c))
3260 ((not (pair? (car c)))
3261 (syntax-error (car c)))
3262 ((test-clause (caar c))
3263 `(begin ,@(cdar c)))
3264 ((eq? (caar c) 'else)
3265 (if (pair? (cdr c))
3266 (syntax-error c))
3267 `(begin ,@(cdar c)))
3268 (else
3269 (lp (cdr c))))))))
3270
3271 ;; This procedure gets called from the startup code with a list of
3272 ;; numbers, which are the numbers of the SRFIs to be loaded on startup.
3273 ;;
3274 (define (use-srfis srfis)
3275 (process-use-modules
3276 (map (lambda (num)
3277 (list (list 'srfi (string->symbol
3278 (string-append "srfi-" (number->string num))))))
3279 srfis)))
3280
3281 \f
3282
3283 ;;; srfi-55: require-extension
3284 ;;;
3285
3286 (define-macro (require-extension extension-spec)
3287 ;; This macro only handles the srfi extension, which, at present, is
3288 ;; the only one defined by the standard.
3289 (if (not (pair? extension-spec))
3290 (scm-error 'wrong-type-arg "require-extension"
3291 "Not an extension: ~S" (list extension-spec) #f))
3292 (let ((extension (car extension-spec))
3293 (extension-args (cdr extension-spec)))
3294 (case extension
3295 ((srfi)
3296 (let ((use-list '()))
3297 (for-each
3298 (lambda (i)
3299 (if (not (integer? i))
3300 (scm-error 'wrong-type-arg "require-extension"
3301 "Invalid srfi name: ~S" (list i) #f))
3302 (let ((srfi-sym (string->symbol
3303 (string-append "srfi-" (number->string i)))))
3304 (if (not (memq srfi-sym %cond-expand-features))
3305 (set! use-list (cons `(use-modules (srfi ,srfi-sym))
3306 use-list)))))
3307 extension-args)
3308 (if (pair? use-list)
3309 ;; i.e. (begin (use-modules x) (use-modules y) (use-modules z))
3310 `(begin ,@(reverse! use-list)))))
3311 (else
3312 (scm-error
3313 'wrong-type-arg "require-extension"
3314 "Not a recognized extension type: ~S" (list extension) #f)))))
3315
3316 \f
3317
3318 ;;; {Load emacs interface support if emacs option is given.}
3319 ;;;
3320
3321 (define (named-module-use! user usee)
3322 (module-use! (resolve-module user) (resolve-interface usee)))
3323
3324 (define (load-emacs-interface)
3325 (and (provided? 'debug-extensions)
3326 (debug-enable 'backtrace))
3327 (named-module-use! '(guile-user) '(ice-9 emacs)))
3328
3329 \f
3330
3331 (define using-readline?
3332 (let ((using-readline? (make-fluid)))
3333 (make-procedure-with-setter
3334 (lambda () (fluid-ref using-readline?))
3335 (lambda (v) (fluid-set! using-readline? v)))))
3336
3337 (define (top-repl)
3338 (let ((guile-user-module (resolve-module '(guile-user))))
3339
3340 ;; Load emacs interface support if emacs option is given.
3341 (if (and (module-defined? guile-user-module 'use-emacs-interface)
3342 (module-ref guile-user-module 'use-emacs-interface))
3343 (load-emacs-interface))
3344
3345 ;; Use some convenient modules (in reverse order)
3346
3347 (set-current-module guile-user-module)
3348 (process-use-modules
3349 (append
3350 '(((ice-9 r5rs))
3351 ((ice-9 session))
3352 ((ice-9 debug)))
3353 (if (provided? 'regex)
3354 '(((ice-9 regex)))
3355 '())
3356 (if (provided? 'threads)
3357 '(((ice-9 threads)))
3358 '())))
3359 ;; load debugger on demand
3360 (module-autoload! guile-user-module '(ice-9 debugger) '(debug))
3361
3362 ;; Note: SIGFPE, SIGSEGV and SIGBUS are actually "query-only" (see
3363 ;; scmsigs.c scm_sigaction_for_thread), so the handlers setup here have
3364 ;; no effect.
3365 (let ((old-handlers #f)
3366 (start-repl (module-ref (resolve-interface '(system repl repl))
3367 'start-repl))
3368 (signals (if (provided? 'posix)
3369 `((,SIGINT . "User interrupt")
3370 (,SIGFPE . "Arithmetic error")
3371 (,SIGSEGV
3372 . "Bad memory access (Segmentation violation)"))
3373 '())))
3374 ;; no SIGBUS on mingw
3375 (if (defined? 'SIGBUS)
3376 (set! signals (acons SIGBUS "Bad memory access (bus error)"
3377 signals)))
3378
3379 (dynamic-wind
3380
3381 ;; call at entry
3382 (lambda ()
3383 (let ((make-handler (lambda (msg)
3384 (lambda (sig)
3385 ;; Make a backup copy of the stack
3386 (fluid-set! before-signal-stack
3387 (fluid-ref the-last-stack))
3388 (save-stack 2)
3389 (scm-error 'signal
3390 #f
3391 msg
3392 #f
3393 (list sig))))))
3394 (set! old-handlers
3395 (map (lambda (sig-msg)
3396 (sigaction (car sig-msg)
3397 (make-handler (cdr sig-msg))))
3398 signals))))
3399
3400 ;; the protected thunk.
3401 (lambda ()
3402 (let ((status (start-repl 'scheme)))
3403 (run-hook exit-hook)
3404 status))
3405
3406 ;; call at exit.
3407 (lambda ()
3408 (map (lambda (sig-msg old-handler)
3409 (if (not (car old-handler))
3410 ;; restore original C handler.
3411 (sigaction (car sig-msg) #f)
3412 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
3413 (sigaction (car sig-msg)
3414 (car old-handler)
3415 (cdr old-handler))))
3416 signals old-handlers))))))
3417
3418 ;;; This hook is run at the very end of an interactive session.
3419 ;;;
3420 (define exit-hook (make-hook))
3421
3422 \f
3423
3424 ;;; {Deprecated stuff}
3425 ;;;
3426
3427 (begin-deprecated
3428 (define (feature? sym)
3429 (issue-deprecation-warning
3430 "`feature?' is deprecated. Use `provided?' instead.")
3431 (provided? sym)))
3432
3433 (begin-deprecated
3434 (primitive-load-path "ice-9/deprecated"))
3435
3436 \f
3437
3438 ;;; Place the user in the guile-user module.
3439 ;;;
3440
3441 ;;; FIXME: annotate ?
3442 ;; (define (syncase exp)
3443 ;; (with-fluids ((expansion-eval-closure
3444 ;; (module-eval-closure (current-module))))
3445 ;; (deannotate/source-properties (sc-expand (annotate exp)))))
3446
3447 (define-module (guile-user)
3448 #:autoload (system base compile) (compile))
3449
3450 ;;; boot-9.scm ends here