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