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