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