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