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