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