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