Describe gtcltk-lib.
[bpt/guile.git] / ice-9 / boot-9.scm
CommitLineData
0f2d19dd
JB
1;;; installed-scm-file
2
3;;;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4;;;;
5;;;; This program is free software; you can redistribute it and/or modify
6;;;; it under the terms of the GNU General Public License as published by
7;;;; the Free Software Foundation; either version 2, or (at your option)
8;;;; any later version.
9;;;;
10;;;; This program is distributed in the hope that it will be useful,
11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;;;; GNU General Public License for more details.
14;;;;
15;;;; You should have received a copy of the GNU General Public License
16;;;; along with this software; see the file COPYING. If not, write to
17;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18;;;;
19\f
20
21;;; This file is the first thing loaded into Guile. It adds many mundane
22;;; definitions and a few that are interesting.
23;;;
24;;; The module system (hence the hierarchical namespace) are defined in this
25;;; file.
26;;;
27
28\f
29
30;; {Simple Debugging Tools}
31;;
32
33
34;; peek takes any number of arguments, writes them to the
35;; current ouput port, and returns the last argument.
36;; It is handy to wrap around an expression to look at
37;; a value each time is evaluated, e.g.:
38;;
39;; (+ 10 (troublesome-fn))
40;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
41;;
42
43(define (peek . stuff)
44 (newline)
45 (display ";;; ")
46 (write stuff)
47 (newline)
48 (car (last-pair stuff)))
49
50(define pk peek)
51
52(define (warn . stuff)
53 (with-output-to-port (current-error-port)
54 (lambda ()
55 (newline)
56 (display ";;; WARNING ")
57 (print stuff)
58 (newline)
59 (car (last-pair stuff)))))
60
61\f
62;;; {apply and call-with-current-continuation}
63;;;
64;;; These turn syntax, @apply and @call-with-current-continuation,
65;;; into procedures.
66;;;
67
68(set! apply (lambda (fun . args) (@apply fun (apply:nconc2last args))))
69(define (call-with-current-continuation proc) (@call-with-current-continuation proc))
70
71
72\f
73;;; {apply-to-args}
74;;;
75;;; apply-to-args is functionally redunant with apply and, worse,
76;;; is less general than apply since it only takes two arguments.
77;;;
78;;; On the other hand, apply-to-args is a syntacticly convenient way to
79;;; perform binding in many circumstances when the "let" family of
80;;; of forms don't cut it. E.g.:
81;;;
82;;; (apply-to-args (return-3d-mouse-coords)
83;;; (lambda (x y z)
84;;; ...))
85;;;
86
87(define (apply-to-args args fn) (apply fn args))
88
89\f
90;;; {Silly Naming Cleanups and Trivial Functions}
91;;;
92
0f2d19dd
JB
93(define (id x) x)
94(define < <?)
95(define <= <=?)
96(define = =?)
97(define > >?)
98(define >= >=?)
99(define (1+ n) (+ n 1))
100(define (-1+ n) (+ n -1))
101(define 1- -1+)
102(define return-it noop)
103(define (and=> value thunk) (and value (thunk value)))
104(define (make-hash-table k) (make-vector k '()))
105\f
106;;; {Integer Math}
107;;;
108
109(define (integer? x) (and (number? x) (= x (inexact->exact x))))
110
111(define (ipow-by-squaring x k acc proc)
112 (cond ((zero? k) acc)
113 ((= 1 k) (proc acc x))
114 (else (logical:ipow-by-squaring (proc x x)
115 (quotient k 2)
116 (if (even? k) acc (proc acc x))
117 proc))))
118
119(define string-character-length string-length)
120
121
122
123;; A convenience function for combining flag bits. Like logior, but
124;; handles the cases of 0 and 1 arguments.
125;;
126(define (flags . args)
127 (cond
128 ((null? args) 0)
129 ((null? (cdr args)) (car args))
130 (else (apply logior args))))
131
132\f
133;;; {Basic Port Code}
134;;;
135;;; Specificly, the parts of the low-level port code that are written in
136;;; Scheme rather than C.
137;;;
138;;; WARNING: the parts of this interface that refer to file ports
139;;; is going away. It would be gone already except that it is used
140;;; "internally" in a few places.
141;;;
142
143
144;; OPEN_READ, OPEN_WRITE, and OPEN_BOTH are used to request the proper
145;; mode to open files in. MSDOS does carraige return - newline
146;; translation if not opened in `b' mode.
147;;
148(define OPEN_READ (case (software-type)
149 ((MS-DOS WINDOWS ATARIST) "rb")
150 (else "r")))
151(define OPEN_WRITE (case (software-type)
152 ((MS-DOS WINDOWS ATARIST) "wb")
153 (else "w")))
154(define OPEN_BOTH (case (software-type)
155 ((MS-DOS WINDOWS ATARIST) "r+b")
156 (else "r+")))
157
8b13c6b3
GH
158(define *null-device* "/dev/null")
159
0f2d19dd 160(define (open-input-file str)
8b13c6b3 161 (open-file str OPEN_READ))
0f2d19dd
JB
162
163(define (open-output-file str)
8b13c6b3 164 (open-file str OPEN_WRITE))
0f2d19dd
JB
165
166(define (open-io-file str) (open-file str OPEN_BOTH))
167(define close-input-port close-port)
168(define close-output-port close-port)
169(define close-io-port close-port)
170
171(define (call-with-input-file str proc)
172 (let* ((file (open-input-file str))
173 (ans (proc file)))
174 (close-input-port file)
175 ans))
176
177(define (call-with-output-file str proc)
178 (let* ((file (open-output-file str))
179 (ans (proc file)))
180 (close-output-port file)
181 ans))
182
183(define (with-input-from-port port thunk)
184 (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
185 (dynamic-wind swaports thunk swaports)))
186
187(define (with-output-to-port port thunk)
188 (let* ((swaports (lambda () (set! port (set-current-output-port port)))))
189 (dynamic-wind swaports thunk swaports)))
190
191(define (with-error-to-port port thunk)
192 (let* ((swaports (lambda () (set! port (set-current-error-port port)))))
193 (dynamic-wind swaports thunk swaports)))
194
195(define (with-input-from-file file thunk)
196 (let* ((nport (open-input-file file))
197 (ans (with-input-from-port nport thunk)))
198 (close-port nport)
199 ans))
200
201(define (with-output-to-file file thunk)
202 (let* ((nport (open-output-file file))
203 (ans (with-output-to-port nport thunk)))
204 (close-port nport)
205 ans))
206
207(define (with-error-to-file file thunk)
208 (let* ((nport (open-output-file file))
209 (ans (with-error-to-port nport thunk)))
210 (close-port nport)
211 ans))
212
213(define (with-input-from-string string thunk)
214 (call-with-input-string string
215 (lambda (p) (with-input-from-port p thunk))))
216
217(define (with-output-to-string thunk)
218 (call-with-output-string
219 (lambda (p) (with-output-to-port p thunk))))
220
221(define (with-error-to-string thunk)
222 (call-with-output-string
223 (lambda (p) (with-error-to-port p thunk))))
224
225(define the-eof-object (call-with-input-string "" (lambda (p) (read-char p))))
226
227
228\f
229;;; {Symbol Properties}
230;;;
231
232(define (symbol-property sym prop)
233 (let ((pair (assoc prop (symbol-pref sym))))
234 (and pair (cdr pair))))
235
236(define (set-symbol-property! sym prop val)
237 (let ((pair (assoc prop (symbol-pref sym))))
238 (if pair
239 (set-cdr! pair val)
240 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
241
242(define (symbol-property-remove! sym prop)
243 (let ((pair (assoc prop (symbol-pref sym))))
244 (if pair
245 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
246
247\f
248;;; {Arrays}
249;;;
250
251(begin
252 (define uniform-vector? array?)
253 (define make-uniform-vector dimensions->uniform-array)
254 ; (define uniform-vector-ref array-ref)
255 (define (uniform-vector-set! u i o)
256 (uniform-vector-set1! u o i))
257 (define uniform-vector-fill! array-fill!)
258 (define uniform-vector-read! uniform-array-read!)
259 (define uniform-vector-write uniform-array-write)
260
261 (define (make-array fill . args)
262 (dimensions->uniform-array args () fill))
263 (define (make-uniform-array prot . args)
264 (dimensions->uniform-array args prot))
265 (define (list->array ndim lst)
266 (list->uniform-array ndim '() lst))
267 (define (list->uniform-vector prot lst)
268 (list->uniform-array 1 prot lst))
269 (define (array-shape a)
270 (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
271 (array-dimensions a))))
272
273\f
274;;; {Keywords}
275;;;
276
277(define (symbol->keyword symbol)
278 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
279
280(define (keyword->symbol kw)
281 (let ((sym (keyword-dash-symbol kw)))
282 (string->symbol (substring sym 1 (length sym)))))
283
284(define (kw-arg-ref args kw)
285 (let ((rem (member kw args)))
286 (and rem (pair? (cdr rem)) (cadr rem))))
287
288\f
289
290;;; {Print}
291;;;
292
293(define (print obj . args)
294 (let ((default-args (list (current-output-port) 0 0 default-print-style #f)))
295 (apply-to-args (append args (list-cdr-ref default-args (length args)))
296 (lambda (port depth length style table)
297 (cond
298 ((and table (print-table-ref table obj)) ((print-style-tag-hook style 'eq-val)
299 obj port depth length style table))
300 (else
301 (and table (print-table-add! table obj))
302 (cond
303 ((print-style-max-depth? style depth) ((print-style-excess-depth-hook style)))
304 ((print-style-max-length? style length) ((print-style-excess-length-hook style)))
305 (else ((print-style-hook style obj)
306 obj port depth length style table)))))))))
307
308(define (make-print-style) (make-vector 59))
309
310(define (extend-print-style! style utag printer) (hashq-set! style utag printer))
311
312(define (print-style-hook style obj)
313 (let ((type-tag (tag obj)))
314 (or (hashq-ref style type-tag)
315 (hashq-ref style (logand type-tag 255))
316 print-obj)))
317
318(define (print-style-tag-hook style type-tag)
319 (or (hashq-ref style type-tag)
320 print-obj))
321
322(define (print-style-max-depth? style d) #f)
323(define (print-style-max-length? style l) #f)
324(define (print-style-excess-length-hook style) (hashq-ref style 'excess-length-hook))
325(define (print-style-excess-depth-hook style) (hashq-ref style 'excess-depth-hook))
326
327(define (make-print-table) (make-vector 59))
328(define (print-table-ref table obj) (hashq-ref table obj))
329(define (print-table-add! table obj) (hashq-set! table obj (gensym 'ref)))
330
331(define (print-obj obj port depth length style table) (write obj port))
332
333(define (print-pair pair port depth length style table)
334 (if (= 0 length)
335 (display #\( port))
336
337 (print (car pair) port (+ 1 depth) 0 style table)
338
339 (cond
340 ((and (pair? (cdr pair))
341 (or (not table)
342 (not (print-table-ref table (cdr pair)))))
343
344 (display #\space port)
345 (print (cdr pair) port depth (+ 1 length) style table))
346
347 ((null? (cdr pair)) (display #\) port))
348
349 (else (display " . " port)
350 (print (cdr pair) port (+ 1 depth) 0 style table)
351 (display #\) port))))
352
353(define (print-vector obj port depth length style table)
354 (if (= 0 length)
355 (cond
074fa9cf 356 ((weak-key-hash-table? obj) (display "#wh(" port))
0f2d19dd
JB
357 ((weak-value-hash-table? obj) (display "#whv(" port))
358 ((doubly-weak-hash-table? obj) (display "#whd(" port))
359 (else (display "#(" port))))
360
361 (if (< length (vector-length obj))
362 (print (vector-ref obj length) port (+ 1 depth) 0 style table))
363
364 (cond
365 ((>= (+ 1 length) (vector-length obj)) (display #\) port))
366 (else (display #\space port)
367 (print obj port depth (+ 1 length) style table))))
368
369(define default-print-style (make-print-style))
370
371(extend-print-style! default-print-style utag_vector print-vector)
372(extend-print-style! default-print-style utag_wvect print-vector)
373(extend-print-style! default-print-style utag_pair print-pair)
374(extend-print-style! default-print-style 'eq-val
375 (lambda (obj port depth length style table)
376 (if (symbol? obj)
377 (display obj)
378 (begin
379 (display "##" port)
380 (display (print-table-ref table obj))))))
381
382\f
383;;; {Records}
384;;;
385
386(define record-type-vtable (make-vtable-vtable "prpr" 0))
387
388(define (record-type? obj)
389 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
390
391(define (make-record-type type-name fields . opt)
392 (let ((printer-fn (and opt (car opt))))
393 (let ((struct (make-struct record-type-vtable 0
394 (make-struct-layout (apply symbol-append (map (lambda (f) "pw") fields)))
395 type-name
396 (copy-tree fields))))
397 ;; !!! leaks printer functions
398 (if printer-fn
399 (extend-print-style! default-print-style
400 (logior utag_struct_base (ash (struct-vtable-tag struct) 8))
401 printer-fn))
402 struct)))
403
404(define (record-type-name obj)
405 (if (record-type? obj)
406 (struct-ref obj struct-vtable-offset)
407 (error 'not-a-record-type obj)))
408
409(define (record-type-fields obj)
410 (if (record-type? obj)
411 (struct-ref obj (+ 1 struct-vtable-offset))
412 (error 'not-a-record-type obj)))
413
414(define (record-constructor rtd . opt)
415 (let ((field-names (if opt (car opt) (record-type-fields rtd))))
416 (eval `(lambda ,field-names
417 (make-struct ',rtd 0 ,@(map (lambda (f)
418 (if (memq f field-names)
419 f
420 #f))
421 (record-type-fields rtd)))))))
422
423(define (record-predicate rtd)
424 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
425
426(define (record-accessor rtd field-name)
427 (let* ((pos (list-index (record-type-fields rtd) field-name)))
428 (if (not pos)
429 (error 'no-such-field field-name))
430 (eval `(lambda (obj)
431 (and (eq? ',rtd (record-type-descriptor obj))
432 (struct-ref obj ,pos))))))
433
434(define (record-modifier rtd field-name)
435 (let* ((pos (list-index (record-type-fields rtd) field-name)))
436 (if (not pos)
437 (error 'no-such-field field-name))
438 (eval `(lambda (obj val)
439 (and (eq? ',rtd (record-type-descriptor obj))
440 (struct-set! obj ,pos val))))))
441
442
443(define (record? obj)
444 (and (struct? obj) (record-type? (struct-vtable obj))))
445
446(define (record-type-descriptor obj)
447 (if (struct? obj)
448 (struct-vtable obj)
449 (error 'not-a-record obj)))
450
451\f
452;;; {Booleans}
453;;;
454
455(define (->bool x) (not (not x)))
456
457\f
458;;; {Symbols}
459;;;
460
461(define (symbol-append . args)
462 (string->symbol (apply string-append args)))
463
464(define (list->symbol . args)
465 (string->symbol (apply list->string args)))
466
467(define (symbol . args)
468 (string->symbol (apply string args)))
469
470(define (obarray-symbol-append ob . args)
471 (string->obarray-symbol (apply string-append ob args)))
472
473(define obarray-gensym
474 (let ((n -1))
475 (lambda (obarray . opt)
476 (if (null? opt)
477 (set! opt '(%%gensym)))
478 (let loop ((proposed-name (apply string-append opt)))
479 (if (string->obarray-symbol obarray proposed-name #t)
480 (loop (apply string-append (append opt (begin (set! n (1+ n)) (list (number->string n))))))
481 (string->obarray-symbol obarray proposed-name))))))
482
483(define (gensym . args) (apply obarray-gensym #f args))
484
485\f
486;;; {Lists}
487;;;
488
489(define (list-index l k)
490 (let loop ((n 0)
491 (l l))
492 (and (not (null? l))
493 (if (eq? (car l) k)
494 n
495 (loop (+ n 1) (cdr l))))))
496
497(define (make-list n init)
498 (let loop ((answer '())
499 (n n))
500 (if (<= n 0)
501 answer
502 (loop (cons init answer) (- n 1)))))
503
504
505\f
506;;; {and-map, or-map, and map-in-order}
507;;;
508;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
509;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
510;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
511;;;
512
513;; and-map f l
514;;
515;; Apply f to successive elements of l until exhaustion or f returns #f.
516;; If returning early, return #f. Otherwise, return the last value returned
517;; by f. If f has never been called because l is empty, return #t.
518;;
519(define (and-map f lst)
520 (let loop ((result #t)
521 (l lst))
522 (and result
523 (or (and (null? l)
524 result)
525 (loop (f (car l)) (cdr l))))))
526
527;; or-map f l
528;;
529;; Apply f to successive elements of l until exhaustion or while f returns #f.
530;; If returning early, return the return value of f.
531;;
532(define (or-map f lst)
533 (let loop ((result #f)
534 (l lst))
535 (or result
536 (and (not (null? l))
537 (loop (f (car l)) (cdr l))))))
538
539;; map-in-order
540;;
541;; Like map, but guaranteed to process the list in order.
542;;
543(define (map-in-order fn l)
544 (if (null? l)
545 '()
546 (cons (fn (car l))
547 (map-in-order fn (cdr l)))))
548
549\f
550;;; {Files}
551;;; !!!! these should be implemented using Tcl commands, not fports.
552;;;
553
554(define (file-exists? str)
8b13c6b3
GH
555 ;; we don't have false-if-exception (or defmacro) yet.
556 (let ((port (catch #t (lambda () (open-file str OPEN_READ))
557 (lambda args #f))))
0f2d19dd
JB
558 (if port (begin (close-port port) #t)
559 #f)))
560
561(define (file-is-directory? str)
8b13c6b3
GH
562 (let ((port (catch #t (lambda () (open-file (string-append str "/.")
563 OPEN_READ))
564 (lambda args #f))))
0f2d19dd
JB
565 (if port (begin (close-port port) #t)
566 #f)))
567
568(define (has-suffix? str suffix)
569 (let ((sufl (string-length suffix))
570 (sl (string-length str)))
571 (and (> sl sufl)
572 (string=? (substring str (- sl sufl) sl) suffix))))
573
574
575
576\f
577;;; {Error Handling}
578;;;
579
580
581;; (error . args) is short for (throw (quote error) . args)
582;;
583(define (error . args)
584 (apply throw 'error args))
585
586
587
588
589;; Error handling a la SCM.
590;;
591(define (%%default-error-handler tag . args)
592 (define cep (current-error-port))
593 (perror "ERROR")
594 (errno 0)
595 (display "ERROR: " cep)
596 (if (not (null? args))
597 (begin (display (car args) cep)
598 (for-each (lambda (x) (display #\ cep) (write x cep))
599 (cdr args))))
600 (newline cep)
601 (force-output cep)
602 (apply throw 'abort tag args))
603
604
605
606;; Install SCM error handling as the default.
607;;
608(set-symbol-property! 'error
609 'throw-handler-default
610 %%default-error-handler)
611
612
613
614
615
616;; %%bad-throw is the hook that is called upon a throw to a an unhandled
617;; key. If the key has a default handler (a throw-handler-default property),
618;; it is applied to the throw.
619;;
620(define (%%bad-throw key . args)
621 (let ((default (symbol-property key 'throw-handler-default)))
622 (or (and default (apply default key args))
623 (throw 'error 'unhandled-exception key args))))
624
625
626
627
628
629;; A number of internally defined error types are represented
630;; as integers. Here is the mapping to symbolic names
631;; and error messages.
632;;
633(define %%system-errors
634 '((-1 UNKNOWN "Unknown error")
635 (0 ARGn "Wrong type argument to ")
636 (1 ARG1 "Wrong type argument in position 1 to ")
637 (2 ARG2 "Wrong type argument in position 2 to ")
638 (3 ARG3 "Wrong type argument in position 3 to ")
639 (4 ARG4 "Wrong type argument in position 4 to ")
640 (5 ARG5 "Wrong type argument in position 5 to ")
641 (6 ARG5 "Wrong type argument in position 5 to ")
642 (7 ARG5 "Wrong type argument in position 5 to ")
643 (8 WNA "Wrong number of arguments to ")
644 (9 OVFLOW "Numerical overflow to ")
645 (10 OUTOFRANGE "Argument out of range to ")
646 (11 NALLOC "Could not allocate to ")
647 (12 STACK_OVFLOW "Stack overflow")
648 (13 EXIT "Exit (internal error?).")
649 (14 HUP_SIGNAL "hang-up")
650 (15 INT_SIGNAL "user interrupt")
651 (16 FPE_SIGNAL "arithmetic error")
652 (17 BUS_SIGNAL "bus error")
653 (18 SEGV_SIGNAL "segmentation violation")
654 (19 ALRM_SIGNAL "alarm")
655 (20 GC_SIGNAL "gc")
656 (21 TICK_SIGNAL "tick")))
657
658
659(define (timer-thunk) #t)
660(define (gc-thunk) #t)
661(define (alarm-thunk) #t)
662
663(define (signal-handler n)
664 (cond
665 ((= n 21) (unmask-signals) (timer-thunk))
666 ((= n 20) (unmask-signals) (gc-thunk))
667 ((= n 19) (unmask-signals) (alarm-thunk))
668 (else (unmask-signals) (throw '%%system-error n #f))))
669
670
671;; The default handler for built-in error types when
672;; thrown by their symbolic name. The action is to
673;; convert the error into a generic error, building
674;; a descriptive message for the error.
675;;
676(define (%%handle-system-error ignored desc proc . args)
677 (let* ((b (assoc desc %%system-errors))
678 (msghead (cond
679 (b (caddr b))
680 ((or (symbol? desc) (string? desc))
681 (string-append desc " "))
682 (#t "Unknown error")))
683 (msg (if (symbol? proc)
684 (string-append msghead proc ":")
685 msghead))
686 (rest (if (and proc (not (symbol? proc)))
687 (cons proc args)
688 args))
689 (fixed-args (cons msg rest)))
690 (apply error fixed-args)))
691
692
693(set-symbol-property! '%%system-error
694 'throw-handler-default
695 %%handle-system-error)
696
697
698;; Install default handlers for built-in errors.
699;;
700(map (lambda (err)
701 (set-symbol-property! (cadr err)
702 'throw-handler-default
703 %%handle-system-error))
704 (cdr %%system-errors))
705
706
707\f
0f2d19dd
JB
708(begin
709 (define (syserror key fn err . args)
710 (errno err)
711 (apply error (cons fn args)))
712 (set-symbol-property! 'syserror 'throw-handler-default syserror))
713
714\f
02b754d3
GH
715(define (getgrnam name) (getgr name))
716(define (getgrgid id) (getgr id))
717(define (gethostbyaddr addr) (gethost addr))
718(define (gethostbyname name) (gethost name))
719(define (getnetbyaddr addr) (getnet addr))
720(define (getnetbyname name) (getnet name))
721(define (getprotobyname name) (getproto name))
722(define (getprotobynumber addr) (getproto addr))
723(define (getpwnam name) (getpw name))
724(define (getpwuid uid) (getpw uid))
725(define (getservbyname name proto) (%getserv name proto))
726(define (getservbyport port proto) (%getserv port proto))
0f2d19dd
JB
727(define (endgrent) (setgr))
728(define (endhostent) (sethost))
729(define (endnetent) (setnet))
730(define (endprotoent) (setproto))
731(define (endpwent) (setpw))
732(define (endservent) (setserv))
733(define (file-position . args) (apply ftell args))
734(define (file-set-position . args) (apply fseek args))
02b754d3
GH
735(define (getgrent) (getgr))
736(define (gethostent) (gethost))
737(define (getnetent) (getnet))
738(define (getprotoent) (getproto))
739(define (getpwent) (getpw))
740(define (getservent) (getserv))
0f2d19dd
JB
741(define (reopen-file . args) (apply freopen args))
742(define (setgrent arg) (setgr arg))
743(define (sethostent arg) (sethost arg))
744(define (setnetent arg) (setnet arg))
745(define (setprotoent arg) (setproto arg))
746(define (setpwent arg) (setpw arg))
747(define (setservent arg) (setserv arg))
8b13c6b3 748
02b754d3 749(define (move->fdes port fd)
8b13c6b3
GH
750 (primitive-move->fdes port fd)
751 (set-port-revealed! port 1)
752 port)
753
754(define (release-port-handle port)
755 (let ((revealed (port-revealed port)))
756 (if (> revealed 0)
757 (set-port-revealed! port (- revealed 1)))))
0f2d19dd
JB
758
759\f
760;;; {Load Paths}
761;;;
762
763(define implementation-vicinity compiled-library-path)
764
765;;; Here for backward compatability
766;;
767(define scheme-file-suffix (lambda () ".scm"))
768
769(define in-vicinity string-append)
770
771(define (parse-path env_path)
772 (cond ((string? env_path)
773 (let loop ((curdir "") (env env_path) (path '()))
774 (cond ((= (string-length env) 0)
775 (if (> (string-length curdir) 0)
776 (append path (list curdir))
777 path))
778 ((char=? (string-ref env 0) #\:)
779 (loop ""
780 (substring env 1 (string-length env))
781 (append path (list curdir))))
782 (#t
783 (loop (string-append curdir (substring env 0 1))
784 (substring env 1 (string-length env))
785 path)))))
786 (#t '())))
787
02b754d3
GH
788;;; we don't have false-if-exception (or defmacro) yet.
789(define %load-path
790 (let ((lp (catch #t (lambda () (getenv "SCHEME_LOAD_PATH"))
791 (lambda args #f))))
02ceadb8
JB
792 (append
793 (parse-path lp)
794 (cons "./"
795 (map (lambda (dir) (in-vicinity (implementation-vicinity) dir "/"))
796 (cons "site"
797 (map (lambda (dir) (string-append (version) "/" dir))
798 '("." "gls/guile" "gls" "slib"))))))))
799
0f2d19dd
JB
800\f
801;;; {try-load}
802;;;
803
804(define (try-load-with-path file-name path)
805 (or-map (lambda (d)
806 (let ((f (in-vicinity d file-name)))
807 (and (not (file-is-directory? f))
808 (%try-load f #t read-sharp))))
809 path))
810
811(define (try-load name)
812 (if (eval '(defined? %load-path))
813 (try-load-with-path name (eval '%load-path))
814 (%try-load name #t read-sharp)))
815\f
816;;; {Load}
817;;;
818
819(define %load-verbosely #t)
820(define (assert-load-verbosity v) (set! %load-verbosely v))
821(define %load-indent -2)
822
823(define (%load f)
824 (current-module)
825 (or (and (not (file-is-directory? f))
826 (%try-load f #t read-sharp))
827 (and (not (has-suffix? f (scheme-file-suffix)))
828 (%try-load (string-append f (scheme-file-suffix)) #t read-sharp))))
829
830(define (%load-announce file)
831 (if %load-verbosely
832 (with-output-to-port (current-error-port)
833 (lambda ()
834 (display ";;; ")
835 (display (make-string %load-indent #\ ))
836 (display "loading ")
837 (display file)
838 (display "...")
839 (newline)
840 (force-output)))))
841
842(define (%load-announce-win file)
843 (if %load-verbosely
844 (with-output-to-port (current-error-port)
845 (lambda ()
846 (display ";;; ")
847 (display (make-string %load-indent #\ ))
848 (display "...loaded ")
849 (display file)
850 (display ".")
851 (newline)
852 (force-output)))))
853
854(define (%load-announce-lossage file path)
855 (if %load-verbosely
856 (with-output-to-port (current-error-port)
857 (lambda ()
858 (display ";;; ")
859 (display (make-string %load-indent #\ ))
860 (display "...COULD NOT LOAD ")
861 (display file)
862 (display " from ")
863 (write path)
864 (newline)
865 (force-output))))
866 (throw 'could-not-load file path))
867
868
869(define (load-with-path name path)
870 (define (do-load)
871 (%load-announce name)
872 (if (not (or-map (lambda (d)
873 (if (%load (in-vicinity d name))
874 (begin
875 (%load-announce-win (in-vicinity d name))
876 #t)
877 #f))
878 path))
879 (%load-announce-lossage name path)))
880
881 (let ((indent %load-indent))
882 (dynamic-wind
883 (lambda () (set! %load-indent (modulo (+ indent 2) 16)))
884 do-load
885 (lambda () (set! %load-indent indent))))
886 #t)
887
888
889(define (load name)
890 (if (eval '(defined? %load-path))
891 (load-with-path name (eval '%load-path))
892 (load-with-path name '())))
893
894
895\f
896;;; {Transcendental Functions}
897;;;
898;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
899;;; Copyright (C) 1992, 1993 Jerry D. Hedden.
900;;; See the file `COPYING' for terms applying to this program.
901;;;
902
903(define (exp z)
904 (if (real? z) ($exp z)
905 (make-polar ($exp (real-part z)) (imag-part z))))
906
907(define (log z)
908 (if (and (real? z) (>= z 0))
909 ($log z)
910 (make-rectangular ($log (magnitude z)) (angle z))))
911
912(define (sqrt z)
913 (if (real? z)
914 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
915 ($sqrt z))
916 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
917
918(define expt
919 (let ((integer-expt integer-expt))
920 (lambda (z1 z2)
921 (cond ((exact? z2)
922 (integer-expt z1 z2))
923 ((and (real? z2) (real? z1) (>= z1 0))
924 ($expt z1 z2))
925 (else
926 (exp (* z2 (log z1))))))))
927
928(define (sinh z)
929 (if (real? z) ($sinh z)
930 (let ((x (real-part z)) (y (imag-part z)))
931 (make-rectangular (* ($sinh x) ($cos y))
932 (* ($cosh x) ($sin y))))))
933(define (cosh z)
934 (if (real? z) ($cosh z)
935 (let ((x (real-part z)) (y (imag-part z)))
936 (make-rectangular (* ($cosh x) ($cos y))
937 (* ($sinh x) ($sin y))))))
938(define (tanh z)
939 (if (real? z) ($tanh z)
940 (let* ((x (* 2 (real-part z)))
941 (y (* 2 (imag-part z)))
942 (w (+ ($cosh x) ($cos y))))
943 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
944
945(define (asinh z)
946 (if (real? z) ($asinh z)
947 (log (+ z (sqrt (+ (* z z) 1))))))
948
949(define (acosh z)
950 (if (and (real? z) (>= z 1))
951 ($acosh z)
952 (log (+ z (sqrt (- (* z z) 1))))))
953
954(define (atanh z)
955 (if (and (real? z) (> z -1) (< z 1))
956 ($atanh z)
957 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
958
959(define (sin z)
960 (if (real? z) ($sin z)
961 (let ((x (real-part z)) (y (imag-part z)))
962 (make-rectangular (* ($sin x) ($cosh y))
963 (* ($cos x) ($sinh y))))))
964(define (cos z)
965 (if (real? z) ($cos z)
966 (let ((x (real-part z)) (y (imag-part z)))
967 (make-rectangular (* ($cos x) ($cosh y))
968 (- (* ($sin x) ($sinh y)))))))
969(define (tan z)
970 (if (real? z) ($tan z)
971 (let* ((x (* 2 (real-part z)))
972 (y (* 2 (imag-part z)))
973 (w (+ ($cos x) ($cosh y))))
974 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
975
976(define (asin z)
977 (if (and (real? z) (>= z -1) (<= z 1))
978 ($asin z)
979 (* -i (asinh (* +i z)))))
980
981(define (acos z)
982 (if (and (real? z) (>= z -1) (<= z 1))
983 ($acos z)
984 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
985
986(define (atan z . y)
987 (if (null? y)
988 (if (real? z) ($atan z)
989 (/ (log (/ (- +i z) (+ +i z))) +2i))
990 ($atan2 z (car y))))
991
992(set! abs magnitude)
993
994\f
995;;; {User Settable Hooks}
996;;;
997;;; Parts of the C code check the bindings of these variables.
998;;;
999
1000(define ticks-interrupt #f)
1001(define user-interrupt #f)
1002(define alarm-interrupt #f)
1003(define out-of-storage #f)
1004(define could-not-open #f)
1005(define end-of-program #f)
1006(define hang-up #f)
1007(define arithmetic-error #f)
1008(define read-sharp #f)
1009
1010\f
1011
1012;;; {Reader Extensions}
1013;;;
1014
1015;;; Reader code for various "#c" forms.
1016;;;
1017
1018(define (parse-path-symbol s)
1019 (define (seperate-fields-discarding-char ch str ret)
1020 (let loop ((fields '())
1021 (str str))
1022 (cond
1023 ((string-rindex str ch)
1024 => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 pos)) fields)
1025 (make-shared-substring str 0 pos))))
1026 (else (ret (cons str fields))))))
1027 (seperate-fields-discarding-char #\/
1028 s
1029 (lambda (fields)
1030 (map string->symbol fields))))
1031
1032
1033(define (%read-sharp c port)
1034 (define (barf)
1035 (error "unknown # object" c))
1036
1037 (case c
1038 ((#\/) (let ((look (peek-char port)))
1039 (if (or (eof-object? look)
1040 (and (char? look)
1041 (or (char-whitespace? look)
1042 (string-index ")" look))))
1043 '()
1044 (parse-path-symbol (read port #t read-sharp)))))
1045 ((#\') (read port #t read-sharp))
1046 ((#\.) (eval (read port #t read-sharp)))
1047 ((#\b) (read:uniform-vector #t port))
1048 ((#\a) (read:uniform-vector #\a port))
1049 ((#\u) (read:uniform-vector 1 port))
1050 ((#\e) (read:uniform-vector -1 port))
1051 ((#\s) (read:uniform-vector 1.0 port))
1052 ((#\i) (read:uniform-vector 1/3 port))
1053 ((#\c) (read:uniform-vector 0+i port))
1054 ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
1055 (read:array c port))
1056 ((#\!) (if (= 1 (line-number))
1057 (let skip () (if (eq? #\newline (peek-char port))
1058 (read port #t read-sharp)
1059 (begin (read-char port) (skip))))
1060 (barf)))
1061 (else (barf))))
1062
1063(define (read:array digit port)
1064 (define chr0 (char->integer #\0))
1065 (let ((rank (let readnum ((val (- (char->integer digit) chr0)))
1066 (if (char-numeric? (peek-char port))
1067 (readnum (+ (* 10 val)
1068 (- (char->integer (read-char port)) chr0)))
1069 val)))
1070 (prot (if (eq? #\( (peek-char port))
1071 '()
1072 (let ((c (read-char port)))
1073 (case c ((#\b) #t)
1074 ((#\a) #\a)
1075 ((#\u) 1)
1076 ((#\e) -1)
1077 ((#\s) 1.0)
1078 ((#\i) 1/3)
1079 ((#\c) 0+i)
1080 (else (error "read:array unknown option " c)))))))
1081 (if (eq? (peek-char port) #\()
1082 (list->uniform-array rank prot (read port #t read-sharp))
1083 (error "read:array list not found"))))
1084
1085(define (read:uniform-vector proto port)
1086 (if (eq? #\( (peek-char port))
1087 (list->uniform-array 1 proto (read port #t read-sharp))
1088 (error "read:uniform-vector list not found")))
1089
1090
1091(define read-sharp (lambda a (apply %read-sharp a)))
1092
1093
1094\f
1095;;; {Dynamic Roots}
1096;;;
1097
1098; mystery integers passed dynamic root error handlers
1099(define repl-quit -1)
1100(define repl-abort -2)
1101
1102
1103\f
1104;;; {Command Line Options}
1105;;;
1106
1107(define (get-option argv kw-opts kw-args return)
1108 (cond
1109 ((null? argv)
1110 (return #f #f argv))
1111
1112 ((or (not (eq? #\- (string-ref (car argv) 0)))
1113 (eq? (string-length (car argv)) 1))
1114 (return 'normal-arg (car argv) (cdr argv)))
1115
1116 ((eq? #\- (string-ref (car argv) 1))
1117 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
1118 (string-length (car argv))))
1119 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
1120 (kw-opt? (member kw kw-opts))
1121 (kw-arg? (member kw kw-args))
1122 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
1123 (substring (car argv)
1124 (+ kw-arg-pos 1)
1125 (string-length (car argv))))
1126 (and kw-arg?
1127 (begin (set! argv (cdr argv)) (car argv))))))
1128 (if (or kw-opt? kw-arg?)
1129 (return kw arg (cdr argv))
1130 (return 'usage-error kw (cdr argv)))))
1131
1132 (else
1133 (let* ((char (substring (car argv) 1 2))
1134 (kw (symbol->keyword char)))
1135 (cond
1136
1137 ((member kw kw-opts)
1138 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1139 (new-argv (if (= 0 (string-length rest-car))
1140 (cdr argv)
1141 (cons (string-append "-" rest-car) (cdr argv)))))
1142 (return kw #f new-argv)))
1143
1144 ((member kw kw-args)
1145 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1146 (arg (if (= 0 (string-length rest-car))
1147 (cadr argv)
1148 rest-car))
1149 (new-argv (if (= 0 (string-length rest-car))
1150 (cddr argv)
1151 (cdr argv))))
1152 (return kw arg new-argv)))
1153
1154 (else (return 'usage-error kw argv)))))))
1155
1156(define (for-next-option proc argv kw-opts kw-args)
1157 (let loop ((argv argv))
1158 (get-option argv kw-opts kw-args
1159 (lambda (opt opt-arg argv)
1160 (and opt (proc opt opt-arg argv loop))))))
1161
1162(define (display-usage-report kw-desc)
1163 (for-each
1164 (lambda (kw)
1165 (or (eq? (car kw) #t)
1166 (eq? (car kw) 'else)
1167 (let* ((opt-desc kw)
1168 (help (cadr opt-desc))
1169 (opts (car opt-desc))
1170 (opts-proper (if (string? (car opts)) (cdr opts) opts))
1171 (arg-name (if (string? (car opts))
1172 (string-append "<" (car opts) ">")
1173 ""))
1174 (left-part (string-append
1175 (with-output-to-string
1176 (lambda ()
1177 (map (lambda (x) (display (keyword-symbol x)) (display " "))
1178 opts-proper)))
1179 arg-name))
1180 (middle-part (if (and (< (length left-part) 30)
1181 (< (length help) 40))
1182 (make-string (- 30 (length left-part)) #\ )
1183 "\n\t")))
1184 (display left-part)
1185 (display middle-part)
1186 (display help)
1187 (newline))))
1188 kw-desc))
1189
1190
1191
1192(define (delq-all! obj l)
1193 (let ((answer (cons '() l)))
1194 (let loop ((pos answer))
1195 (cond
1196 ((null? (cdr pos)) (cdr answer))
1197 ((eq? (cadr pos) obj) (set-cdr! pos (cddr pos))
1198 (loop pos))
1199 (else (loop (cdr pos)))))))
1200
1201(define (transform-usage-lambda cases)
1202 (let* ((raw-usage (delq! 'else (map car cases)))
1203 (usage-sans-specials (map (lambda (x)
1204 (or (and (not (list? x)) x)
1205 (and (symbol? (car x)) #t)
1206 (and (boolean? (car x)) #t)
1207 x))
1208 raw-usage))
1209 (usage-desc (delq-all! #t usage-sans-specials))
1210 (kw-desc (map car usage-desc))
1211 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1212 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1213 (transmogrified-cases (map (lambda (case)
1214 (cons (let ((opts (car case)))
1215 (if (or (boolean? opts) (eq? 'else opts))
1216 opts
1217 (cond
1218 ((symbol? (car opts)) opts)
1219 ((boolean? (car opts)) opts)
1220 ((string? (caar opts)) (cdar opts))
1221 (else (car opts)))))
1222 (cdr case)))
1223 cases)))
1224 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1225 (lambda (%argv)
1226 (let %next-arg ((%argv %argv))
1227 (get-option %argv
1228 ',kw-opts
1229 ',kw-args
1230 (lambda (%opt %arg %new-argv)
1231 (case %opt
1232 ,@ transmogrified-cases))))))))
1233
1234
1235\f
1236
1237;;; {Low Level Modules}
1238;;;
1239;;; These are the low level data structures for modules.
1240;;;
1241;;; !!! warning: The interface to lazy binder procedures is going
1242;;; to be changed in an incompatible way to permit all the basic
1243;;; module ops to be virtualized.
1244;;;
1245;;; (make-module size use-list lazy-binding-proc) => module
1246;;; module-{obarray,uses,binder}[|-set!]
1247;;; (module? obj) => [#t|#f]
1248;;; (module-locally-bound? module symbol) => [#t|#f]
1249;;; (module-bound? module symbol) => [#t|#f]
1250;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1251;;; (module-symbol-interned? module symbol) => [#t|#f]
1252;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1253;;; (module-variable module symbol) => [#<variable ...> | #f]
1254;;; (module-symbol-binding module symbol opt-value)
1255;;; => [ <obj> | opt-value | an error occurs ]
1256;;; (module-make-local-var! module symbol) => #<variable...>
1257;;; (module-add! module symbol var) => unspecified
1258;;; (module-remove! module symbol) => unspecified
1259;;; (module-for-each proc module) => unspecified
1260;;; (make-scm-module) => module ; a lazy copy of the symhash module
1261;;; (set-current-module module) => unspecified
1262;;; (current-module) => #<module...>
1263;;;
1264;;;
1265
1266\f
1267;; This is how modules are printed.
1268;; You can re-define it.
1269;;
1270(define (%print-module mod port depth length style table)
1271 (display "#<" port)
1272 (display (or (module-kind mod) "module") port)
1273 (let ((name (module-name mod)))
1274 (if name
1275 (begin
1276 (display " " port)
1277 (display name port))))
1278 (display " " port)
1279 (display (number->string (object-address mod) 16) port)
1280 (display ">" port))
1281
1282;; module-type
1283;;
1284;; A module is characterized by an obarray in which local symbols
1285;; are interned, a list of modules, "uses", from which non-local
1286;; bindings can be inherited, and an optional lazy-binder which
1287;; is a (THUNK module symbol) which, as a last resort, can provide
1288;; bindings that would otherwise not be found locally in the module.
1289;;
1290(define module-type
1291 (make-record-type 'module '(obarray uses binder eval-thunk name kind) %print-module))
1292
1293;; make-module &opt size uses
1294;;
1295;; Create a new module, perhaps with a particular size of obarray
1296;; or initial uses list.
1297;;
1298(define module-constructor (record-constructor module-type))
1299
1300(define make-module
1301 (lambda args
1302 (let* ((size 1021)
1303 (uses '())
1304 (binder #f)
1305 (answer #f)
1306 (eval-thunk
1307 (lambda (symbol define?)
1308 (if define?
1309 (module-make-local-var! answer symbol)
1310 (module-variable answer symbol)))))
1311
1312 (if (> (length args) 0)
1313 (begin
1314 (set! size (or (car args) size))
1315 (set! args (cdr args))))
1316
1317 (if (> (length args) 0)
1318 (begin
1319 (set! uses (or (car args) uses))
1320 (set! args (cdr args))))
1321
1322 (if (> (length args) 0)
1323 (begin
1324 (set! binder (or (car args) binder))
1325 (set! args (cdr args))))
1326
1327 (if (not (null? args))
1328 (error "Too many args to make-module." args))
1329
1330 (if (not (integer? size))
1331 (error "Illegal size to make-module." size))
1332
1333 (and (list? uses)
1334 (or (and-map module? uses)
1335 (error "Incorrect use list." uses)))
1336
1337 (if (and binder (not (procedure? binder)))
1338 (error
1339 "Lazy-binder expected to be a procedure or #f." binder))
1340
1341 (set! answer
1342 (module-constructor (make-vector size '())
1343 uses
1344 binder
1345 eval-thunk
1346 #f
1347 #f))
1348 answer)))
1349
1350(define module-obarray (record-accessor module-type 'obarray))
1351(define set-module-obarray! (record-modifier module-type 'obarray))
1352(define module-uses (record-accessor module-type 'uses))
1353(define set-module-uses! (record-modifier module-type 'uses))
1354(define module-binder (record-accessor module-type 'binder))
1355(define set-module-binder! (record-modifier module-type 'binder))
1356(define module-eval-thunk (record-accessor module-type 'eval-thunk))
1357(define set-module-eval-thunk! (record-modifier module-type 'eval-thunk))
1358(define module-name (record-accessor module-type 'name))
1359(define set-module-name! (record-modifier module-type 'name))
1360(define module-kind (record-accessor module-type 'kind))
1361(define set-module-kind! (record-modifier module-type 'kind))
1362(define module? (record-predicate module-type))
1363
1364(define (eval-in-module exp module)
1365 (eval2 exp (module-eval-thunk module)))
1366
1367\f
1368;;; {Module Searching in General}
1369;;;
1370;;; We sometimes want to look for properties of a symbol
1371;;; just within the obarray of one module. If the property
1372;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1373;;; DISPLAY is locally rebound in the module `safe-guile'.''
1374;;;
1375;;;
1376;;; Other times, we want to test for a symbol property in the obarray
1377;;; of M and, if it is not found there, try each of the modules in the
1378;;; uses list of M. This is the normal way of testing for some
1379;;; property, so we state these properties without qualification as
1380;;; in: ``The symbol 'fnord is interned in module M because it is
1381;;; interned locally in module M2 which is a member of the uses list
1382;;; of M.''
1383;;;
1384
1385;; module-search fn m
1386;;
1387;; return the first non-#f result of FN applied to M and then to
1388;; the modules in the uses of m, and so on recursively. If all applications
1389;; return #f, then so does this function.
1390;;
1391(define (module-search fn m v)
1392 (define (loop pos)
1393 (and (pair? pos)
1394 (or (module-search fn (car pos) v)
1395 (loop (cdr pos)))))
1396 (or (fn m v)
1397 (loop (module-uses m))))
1398
1399
1400;;; {Is a symbol bound in a module?}
1401;;;
1402;;; Symbol S in Module M is bound if S is interned in M and if the binding
1403;;; of S in M has been set to some well-defined value.
1404;;;
1405
1406;; module-locally-bound? module symbol
1407;;
1408;; Is a symbol bound (interned and defined) locally in a given module?
1409;;
1410(define (module-locally-bound? m v)
1411 (let ((var (module-local-variable m v)))
1412 (and var
1413 (variable-bound? var))))
1414
1415;; module-bound? module symbol
1416;;
1417;; Is a symbol bound (interned and defined) anywhere in a given module
1418;; or its uses?
1419;;
1420(define (module-bound? m v)
1421 (module-search module-locally-bound? m v))
1422
1423;;; {Is a symbol interned in a module?}
1424;;;
1425;;; Symbol S in Module M is interned if S occurs in
1426;;; of S in M has been set to some well-defined value.
1427;;;
1428;;; It is possible to intern a symbol in a module without providing
1429;;; an initial binding for the corresponding variable. This is done
1430;;; with:
1431;;; (module-add! module symbol (make-undefined-variable))
1432;;;
1433;;; In that case, the symbol is interned in the module, but not
1434;;; bound there. The unbound symbol shadows any binding for that
1435;;; symbol that might otherwise be inherited from a member of the uses list.
1436;;;
1437
1438(define (module-obarray-get-handle ob key)
1439 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1440
1441(define (module-obarray-ref ob key)
1442 ((if (symbol? key) hashq-ref hash-ref) ob key))
1443
1444(define (module-obarray-set! ob key val)
1445 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1446
1447(define (module-obarray-remove! ob key)
1448 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1449
1450;; module-symbol-locally-interned? module symbol
1451;;
1452;; is a symbol interned (not neccessarily defined) locally in a given module
1453;; or its uses? Interned symbols shadow inherited bindings even if
1454;; they are not themselves bound to a defined value.
1455;;
1456(define (module-symbol-locally-interned? m v)
1457 (not (not (module-obarray-get-handle (module-obarray m) v))))
1458
1459;; module-symbol-interned? module symbol
1460;;
1461;; is a symbol interned (not neccessarily defined) anywhere in a given module
1462;; or its uses? Interned symbols shadow inherited bindings even if
1463;; they are not themselves bound to a defined value.
1464;;
1465(define (module-symbol-interned? m v)
1466 (module-search module-symbol-locally-interned? m v))
1467
1468
1469;;; {Mapping modules x symbols --> variables}
1470;;;
1471
1472;; module-local-variable module symbol
1473;; return the local variable associated with a MODULE and SYMBOL.
1474;;
1475;;; This function is very important. It is the only function that can
1476;;; return a variable from a module other than the mutators that store
1477;;; new variables in modules. Therefore, this function is the location
1478;;; of the "lazy binder" hack.
1479;;;
1480;;; If symbol is defined in MODULE, and if the definition binds symbol
1481;;; to a variable, return that variable object.
1482;;;
1483;;; If the symbols is not found at first, but the module has a lazy binder,
1484;;; then try the binder.
1485;;;
1486;;; If the symbol is not found at all, return #f.
1487;;;
1488(define (module-local-variable m v)
1489 (caddr
1490 (list m v
1491 (let ((b (module-obarray-ref (module-obarray m) v)))
1492 (or (and (variable? b) b)
1493 (and (module-binder m)
1494 ((module-binder m) m v #f)))))))
1495
1496;; module-variable module symbol
1497;;
1498;; like module-local-variable, except search the uses in the
1499;; case V is not found in M.
1500;;
1501(define (module-variable m v)
1502 (module-search module-local-variable m v))
1503
1504
1505;;; {Mapping modules x symbols --> bindings}
1506;;;
1507;;; These are similar to the mapping to variables, except that the
1508;;; variable is dereferenced.
1509;;;
1510
1511;; module-symbol-binding module symbol opt-value
1512;;
1513;; return the binding of a variable specified by name within
1514;; a given module, signalling an error if the variable is unbound.
1515;; If the OPT-VALUE is passed, then instead of signalling an error,
1516;; return OPT-VALUE.
1517;;
1518(define (module-symbol-local-binding m v . opt-val)
1519 (let ((var (module-local-variable m v)))
1520 (if var
1521 (variable-ref var)
1522 (if (not (null? opt-val))
1523 (car opt-val)
1524 (error "Locally unbound variable." v)))))
1525
1526;; module-symbol-binding module symbol opt-value
1527;;
1528;; return the binding of a variable specified by name within
1529;; a given module, signalling an error if the variable is unbound.
1530;; If the OPT-VALUE is passed, then instead of signalling an error,
1531;; return OPT-VALUE.
1532;;
1533(define (module-symbol-binding m v . opt-val)
1534 (let ((var (module-variable m v)))
1535 (if var
1536 (variable-ref var)
1537 (if (not (null? opt-val))
1538 (car opt-val)
1539 (error "Unbound variable." v)))))
1540
1541
1542\f
1543;;; {Adding Variables to Modules}
1544;;;
1545;;;
1546
1547
1548;; module-make-local-var! module symbol
1549;;
1550;; ensure a variable for V in the local namespace of M.
1551;; If no variable was already there, then create a new and uninitialzied
1552;; variable.
1553;;
1554(define (module-make-local-var! m v)
1555 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1556 (and (variable? b) b))
1557 (and (module-binder m)
1558 ((module-binder m) m v #t))
1559 (begin
1560 (let ((answer (make-undefined-variable v)))
1561 (module-obarray-set! (module-obarray m) v answer)
1562 answer))))
1563
1564;; module-add! module symbol var
1565;;
1566;; ensure a particular variable for V in the local namespace of M.
1567;;
1568(define (module-add! m v var)
1569 (if (not (variable? var))
1570 (error "Bad variable to module-add!" var))
1571 (module-obarray-set! (module-obarray m) v var))
1572
1573;; module-remove!
1574;;
1575;; make sure that a symbol is undefined in the local namespace of M.
1576;;
1577(define (module-remove! m v)
1578 (module-obarray-remove! (module-obarray m) v))
1579
1580(define (module-clear! m)
1581 (vector-fill! (module-obarray m) '()))
1582
1583;; MODULE-FOR-EACH -- exported
1584;;
1585;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1586;;
1587(define (module-for-each proc module)
1588 (let ((obarray (module-obarray module)))
1589 (do ((index 0 (+ index 1))
1590 (end (vector-length obarray)))
1591 ((= index end))
1592 (for-each
1593 (lambda (bucket)
1594 (proc (car bucket) (cdr bucket)))
1595 (vector-ref obarray index)))))
1596
1597
1598(define (module-map proc module)
1599 (let* ((obarray (module-obarray module))
1600 (end (vector-length obarray)))
1601
1602 (let loop ((i 0)
1603 (answer '()))
1604 (if (= i end)
1605 answer
1606 (loop (+ 1 i)
1607 (append!
1608 (map (lambda (bucket)
1609 (proc (car bucket) (cdr bucket)))
1610 (vector-ref obarray i))
1611 answer))))))
1612\f
1613
1614;;; {Low Level Bootstrapping}
1615;;;
1616
1617;; make-root-module
1618
1619:; A root module uses the symhash table (the system's privileged
1620;; obarray). Being inside a root module is like using SCM without
1621;; any module system.
1622;;
1623
1624
1625(define (root-module-thunk m s define?)
1626 (let ((bi (and (symbol-interned? #f s)
1627 (builtin-variable s))))
1628 (and bi
1629 (or define? (variable-bound? bi))
1630 (begin
1631 (module-add! m s bi)
1632 bi))))
1633
1634(define (make-root-module)
1635 (make-module 1019 #f root-module-thunk))
1636
1637
1638;; make-scm-module
1639
1640;; An scm module is a module into which the lazy binder copies
1641;; variable bindings from the system symhash table. The mapping is
1642;; one way only; newly introduced bindings in an scm module are not
1643;; copied back into the system symhash table (and can be used to override
1644;; bindings from the symhash table).
1645;;
1646
1647(define (make-scm-module)
1648 (make-module 1019 #f
1649 (lambda (m s define?)
1650 (let ((bi (and (symbol-interned? #f s)
1651 (builtin-variable s))))
1652 (and bi
1653 (variable-bound? bi)
1654 (begin
1655 (module-add! m s bi)
1656 bi))))))
1657
1658
1659
1660
1661;; the-module
1662;;
1663(define the-module #f)
1664
1665;; set-current-module module
1666;;
1667;; set the current module as viewed by the normalizer.
1668;;
1669(define (set-current-module m)
1670 (set! the-module m)
1671 (if m
1672 (set! *top-level-lookup-thunk* (module-eval-thunk the-module))
1673 (set! *top-level-lookup-thunk* #f)))
1674
1675
1676;; current-module
1677;;
1678;; return the current module as viewed by the normalizer.
1679;;
1680(define (current-module) the-module)
1681\f
1682;;; {Module-based Loading}
1683;;;
1684
1685(define (save-module-excursion thunk)
1686 (let ((inner-module (current-module))
1687 (outer-module #f))
1688 (dynamic-wind (lambda ()
1689 (set! outer-module (current-module))
1690 (set-current-module inner-module)
1691 (set! inner-module #f))
1692 thunk
1693 (lambda ()
1694 (set! inner-module (current-module))
1695 (set-current-module outer-module)
1696 (set! outer-module #f)))))
1697
1698(define basic-try-load-with-path try-load-with-path)
1699(define basic-try-load try-load)
1700(define basic-load-with-path load-with-path)
1701(define basic-load load)
1702
1703
1704(define (try-load-module-with-path . args)
1705 (save-module-excursion (lambda () (apply basic-try-load-with-path args))))
1706
1707(define (try-load-module . args)
1708 (save-module-excursion (lambda () (apply basic-try-load args))))
1709
1710(define (load-module-with-path . args)
1711 (save-module-excursion (lambda () (apply basic-load-with-path args))))
1712
1713(define (load-module . args)
1714 (save-module-excursion (lambda () (apply basic-load args))))
1715
1716
1717\f
1718
1719;; MODULE-REF -- exported
1720;;
1721;; Returns the value of a variable called NAME in MODULE or any of its
1722;; used modules. If there is no such variable, then if the optional third
1723;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1724;;
1725(define (module-ref module name . rest)
1726 (let ((variable (module-variable module name)))
1727 (if (and variable (variable-bound? variable))
1728 (variable-ref variable)
1729 (if (null? rest)
1730 (error "No variable named" name 'in module)
1731 (car rest) ; default value
1732 ))))
1733
1734;; MODULE-SET! -- exported
1735;;
1736;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1737;; to VALUE; if there is no such variable, an error is signaled.
1738;;
1739(define (module-set! module name value)
1740 (let ((variable (module-variable module name)))
1741 (if variable
1742 (variable-set! variable value)
1743 (error "No variable named" name 'in module))))
1744
1745;; MODULE-DEFINE! -- exported
1746;;
1747;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1748;; variable, it is added first.
1749;;
1750(define (module-define! module name value)
1751 (let ((variable (module-local-variable module name)))
1752 (if variable
1753 (variable-set! variable value)
1754 (module-add! module name (make-variable value name)))))
1755
1756;; MODULE-USE! module interface
1757;;
1758;; Add INTERFACE to the list of interfaces used by MODULE.
1759;;
1760(define (module-use! module interface)
1761 (set-module-uses! module
1762 (cons interface (delq! interface (module-uses module)))))
1763
1764\f
1765
1766
1767;;;;
1768;;; {Recursive Namespaces}
1769;;;
1770;;;
1771;;; A hierarchical namespace emerges if we consider some module to be
1772;;; root, and variables bound to modules as nested namespaces.
1773;;;
1774;;; The routines in this file manage variable names in hierarchical namespace.
1775;;; Each variable name is a list of elements, looked up in successively nested
1776;;; modules.
1777;;;
1778;;; (resolved-ref some-root-module '(foo bar baz))
1779;;; => <value of a variable named baz in the module bound to bar in
1780;;; the module bound to foo in some-root-module>
1781;;;
1782;;;
1783;;; There are:
1784;;;
1785;;; ;; a-root is a module
1786;;; ;; name is a list of symbols
1787;;;
1788;;; resolved-ref a-root name
1789;;; resolved-set! a-root name val
1790;;; resolved-define! a-root name val
1791;;; resolved-remove! a-root name
1792;;;
1793;;;
1794;;; (current-module) is a natural choice for a-root so for convenience there are
1795;;; also:
1796;;;
1797;;; value-ref name == resolved-ref (current-module) name
1798;;; value-set! name val == resolved-set! (current-module) name val
1799;;; value-define! name val == resolved-define! (current-module) name val
1800;;; value-remove! name == resolved-remove! (current-module) name
1801;;;
1802
1803
1804(define (resolved-ref root names)
1805 (let loop ((cur root)
1806 (elts names))
1807 (cond
1808 ((null? elts) cur)
1809 ((not (module? cur)) #f)
1810 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1811
1812(define (resolved-set! root names val)
1813 (let loop ((cur root)
1814 (elts names))
1815 (if (null? (cdr elts))
1816 (module-set! cur (car elts) val)
1817 (loop (module-ref cur (car elts)) (cdr elts)))))
1818
1819(define (resolved-define! root names val)
1820 (let loop ((cur root)
1821 (elts names))
1822 (if (null? (cdr elts))
1823 (module-define! cur (car elts) val)
1824 (loop (module-ref cur (car elts)) (cdr elts)))))
1825
1826(define (resolved-remove! root names)
1827 (let loop ((cur root)
1828 (elts names))
1829 (if (null? (cdr elts))
1830 (module-remove! cur (car elts))
1831 (loop (module-ref cur (car elts)) (cdr elts)))))
1832
1833(define (value-ref names) (resolved-ref (current-module) names))
1834(define (value-set! names val) (resolved-set! (current-module) names val))
1835(define (value-define names val) (resolved-define! (current-module) names val))
1836(define (value-remove names) (resolved-remove! (current-module) names))
1837
1838
1839\f
1840
1841;;;;
1842;;; #/app
1843;;;
1844;;; The root of conventionally named objects not directly in the top level.
1845;;;
1846;;; #/app/modules
1847;;; #/app/modules/guile
1848;;;
1849;;; The directory of all modules and the standard root module.
1850;;;
1851
1852(define (module-public-interface m) (module-ref m '%module-public-interface #f))
1853(define (set-module-public-interface! m i) (module-define! m '%module-public-interface i))
1854(define the-root-module (make-root-module))
1855(define the-scm-module (make-scm-module))
1856(set-module-public-interface! the-root-module the-scm-module)
1857(set-module-name! the-root-module 'the-root-module)
1858(set-module-name! the-scm-module 'the-scm-module)
1859
1860(set-current-module the-root-module)
1861
1862(define app (make-module 31))
1863(value-define '(app modules) (make-module 31))
1864(value-define '(app modules guile) the-root-module)
1865
1866;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1867
1868(define (resolve-module name)
1869 (let ((full-name (append '(app modules) name)))
1870 (let ((already (value-ref full-name)))
1871 (or already
1872 (begin
1873 (try-module-autoload name)
1874 (make-modules-in (current-module) full-name))))))
1875
1876(define (beautify-user-module! module)
1877 (if (not (module-public-interface module))
1878 (let ((interface (make-module 31)))
1879 (set-module-name! interface (module-name module))
1880 (set-module-kind! interface 'interface)
1881 (set-module-public-interface! module interface)))
1882 (if (not (memq the-scm-module (module-uses module)))
1883 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1884
1885(define (make-modules-in module name)
1886 (if (null? name)
1887 module
1888 (cond
1889 ((module-ref module (car name) #f) => (lambda (m) (make-modules-in m (cdr name))))
1890 (else (let ((m (make-module 31)))
1891 (set-module-kind! m 'directory)
1892 (set-module-name! m (car name))
1893 (module-define! module (car name) m)
1894 (make-modules-in m (cdr name)))))))
1895
1896(define (resolve-interface name)
1897 (let ((module (resolve-module name)))
1898 (and module (module-public-interface module))))
1899
1900
1901(define %autoloader-developer-mode #t)
1902
1903(define (process-define-module args)
1904 (let* ((module-id (car args))
1905 (module (resolve-module module-id))
1906 (kws (cdr args)))
1907 (beautify-user-module! module)
1908 (let loop ((kws kws))
1909 (and (not (null? kws))
1910 (case (car kws)
1911 ((:use-module)
1912 (if (not (pair? (cdr kws)))
1913 (error "unrecognized defmodule argument" kws))
1914 (let* ((used-name (cadr kws))
1915 (used-module (resolve-module used-name)))
1916 (if (not (module-ref used-module '%module-public-interface #f))
1917 (begin
1918 ((if %autoloader-developer-mode warn error) "no code for module" used-module)
1919 (beautify-user-module! used-module)))
1920 (let ((interface (module-ref used-module '%module-public-interface #f)))
1921 (if (not interface)
1922 (error "missing interface for use-module" used-module))
1923 (set-module-uses! module
1924 (append! (delq! interface (module-uses module))
1925 (list interface)))))
1926 (loop (cddr kws)))
1927
1928 (else (error "unrecognized defmodule argument" kws)))))
1929 module))
1930\f
1931
1932(define autoloads-in-progress '())
1933
1934(define (try-module-autoload module-name)
1935
1936 (define (sfx name) (string-append name (scheme-file-suffix)))
1937 (let* ((reverse-name (reverse module-name))
1938 (name (car reverse-name))
1939 (dir-hint-module-name (reverse (cdr reverse-name)))
1940 (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
1941 (resolve-module dir-hint-module-name)
1942 (and (not (autoload-done-or-in-progress? dir-hint name))
1943 (let ((didit #f))
1944 (dynamic-wind
1945 (lambda () (autoload-in-progress! dir-hint name))
1946 (lambda ()
1947 (let loop ((dirs %load-path))
1948 (and (not (null? dirs))
1949 (or
1950 (let ((d (car dirs))
1951 (trys (list
1952 dir-hint
1953 (sfx dir-hint)
1954 (in-vicinity dir-hint name)
1955 (in-vicinity dir-hint (sfx name)))))
1956 (and (or-map (lambda (f)
1957 (let ((full (in-vicinity d f)))
1958 full
1959 (and (not (file-is-directory? full))
1960 (file-exists? full)
1961 (begin
1962 (save-module-excursion
1963 (lambda ()
1964 (list f d)
1965 (load-with-path f (list d))))
1966 #t))))
1967 trys)
1968 (begin
1969 (set! didit #t)
1970 #t)))
1971 (loop (cdr dirs))))))
1972 (lambda () (set-autoloaded! dir-hint name didit)))
1973 didit))))
1974
1975(define autoloads-done '((guile . guile)))
1976
1977(define (autoload-done-or-in-progress? p m)
1978 (let ((n (cons p m)))
1979 (->bool (or (member n autoloads-done)
1980 (member n autoloads-in-progress)))))
1981
1982(define (autoload-done! p m)
1983 (let ((n (cons p m)))
1984 (set! autoloads-in-progress
1985 (delete! n autoloads-in-progress))
1986 (or (member n autoloads-done)
1987 (set! autoloads-done (cons n autoloads-done)))))
1988
1989(define (autoload-in-progress! p m)
1990 (let ((n (cons p m)))
1991 (set! autoloads-done
1992 (delete! n autoloads-done))
1993 (set! autoloads-in-progress (cons n autoloads-in-progress))))
1994
1995(define (set-autoloaded! p m done?)
1996 (if done?
1997 (autoload-done! p m)
1998 (let ((n (cons p m)))
1999 (set! autoloads-done (delete! n autoloads-done))
2000 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2001
2002
2003
2004
2005\f
2006;;; {Macros}
2007;;;
2008
9591db87
MD
2009(define macro-table (make-weak-key-hash-table 523))
2010(define xformer-table (make-weak-key-hash-table 523))
0f2d19dd
JB
2011
2012(define (defmacro? m) (hashq-ref macro-table m))
2013(define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2014(define (defmacro-transformer m) (hashq-ref xformer-table m))
2015(define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2016
2017(define defmacro:transformer
2018 (lambda (f)
2019 (let* ((xform (lambda (exp env)
2020 (copy-tree (apply f (cdr exp)))))
2021 (a (procedure->memoizing-macro xform)))
2022 (assert-defmacro?! a)
2023 (set-defmacro-transformer! a f)
2024 a)))
2025
2026
2027(define defmacro
2028 (let ((defmacro-transformer
2029 (lambda (name parms . body)
2030 (let ((transformer `(lambda ,parms ,@body)))
2031 `(define ,name
2032 (,(lambda (transformer)
2033 (defmacro:transformer transformer))
2034 ,transformer))))))
2035 (defmacro:transformer defmacro-transformer)))
2036
2037(define defmacro:syntax-transformer
2038 (lambda (f)
2039 (procedure->syntax
2040 (lambda (exp env)
2041 (copy-tree (apply f (cdr exp)))))))
2042
2043(define (macroexpand-1 e)
2044 (cond
2045 ((pair? e) (let* ((a (car e))
2046 (val (and (symbol? a) (eval `(defined? ,a)) (eval a))))
2047 (if (defmacro? val)
2048 (apply (defmacro-transformer val) (cdr e))
2049 e)))
2050 (#t e)))
2051
2052(define (macroexpand e)
2053 (cond
2054 ((pair? e) (let* ((a (car e))
2055 (val (and (symbol? a) (eval `(defined? ,a)) (eval a))))
2056 (if (defmacro? val)
2057 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2058 e)))
2059 (#t e)))
2060
2061(define gentemp
2062 (let ((*gensym-counter* -1))
2063 (lambda ()
2064 (set! *gensym-counter* (+ *gensym-counter* 1))
2065 (string->symbol
2066 (string-append "scm:G" (number->string *gensym-counter*))))))
2067
2068
2069\f
2070
2071;;; {Running Repls}
2072;;;
2073
2074(define (repl read evaler print)
2075 (let loop ((source (read (current-input-port) #t read-sharp)))
2076 (print (evaler source))
2077 (loop (read (current-input-port) #t read-sharp))))
2078
2079;; A provisional repl that acts like the SCM repl:
2080;;
2081(define scm-repl-silent #f)
2082(define (assert-repl-silence v) (set! scm-repl-silent v))
2083
2084(define scm-repl-verbose #t)
2085(define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2086
2087(define scm-repl-prompt #t)
2088(define (assert-repl-prompt v) (set! scm-repl-prompt v))
2089
2090(define the-prompt-string "guile> ")
2091
2092(define (error-catching-loop thunk)
2093 (define (loop first)
2094 (let ((next
2095 (catch #t
2096 (lambda ()
2097 (dynamic-wind
2098 (lambda () (unmask-signals))
2099 (lambda ()
2100 (first)
2101
2102 ;; This line is needed because mark doesn't do closures quite right.
2103 ;; Unreferenced locals should be collected.
2104 ;;
2105 (set! first #f)
2106 (let loop ((v (thunk)))
2107 (loop (thunk)))
2108 #f)
2109 (lambda () (mask-signals))))
2110
2111 (lambda (key . args)
2112 (case key
2113 ((quit) (force-output)
2114 (pk 'quit args)
2115 #f)
2116
2117 ((abort) ;; This is one of the closures that require (set! first #f)
2118 ;; above
2119 ;;
2120 (lambda ()
2121 (force-output)
2122 (display "ABORT: " (current-error-port))
2123 (write args (current-error-port))
2124 (newline (current-error-port))))
2125
2126 (else ;; This is the other cons-leak closure...
2127 (lambda ()
2128 (apply %%bad-throw key args))))))))
2129 (and next (loop next))))
2130 (loop (lambda () #t)))
2131
2132(define (quit . args)
2133 (apply throw 'quit args))
2134
2135(define (error-catching-repl r e p)
2136 (error-catching-loop (lambda () (p (e (r))))))
2137
2138(define (gc-run-time)
2139 (cdr (assq 'gc-time-taken (gc-stats))))
2140
2141(define (scm-style-repl)
2142 (letrec (
2143 (start-gc-rt #f)
2144 (start-rt #f)
2145 (repl-report-reset (lambda () #f))
2146 (repl-report-start-timing (lambda ()
2147 (set! start-gc-rt (gc-run-time))
2148 (set! start-rt (get-internal-run-time))))
2149 (repl-report (lambda ()
2150 (display ";;; ")
2151 (display (inexact->exact
2152 (* 1000 (/ (- (get-internal-run-time) start-rt)
2153 internal-time-units-per-second))))
2154 (display " msec (")
2155 (display (inexact->exact
2156 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2157 internal-time-units-per-second))))
2158 (display " msec in gc)\n")))
2159 (-read (lambda ()
2160 (if scm-repl-prompt
2161 (begin
2162 (display the-prompt-string)
2163 (force-output)
2164 (repl-report-reset)))
2165 (let ((val (read (current-input-port) #t read-sharp)))
2166 (if (eof-object? val)
2167 (begin
2168 (if scm-repl-verbose
2169 (begin
2170 (newline)
2171 (display ";;; EOF -- quitting")
2172 (newline)))
2173 (quit 0)))
2174 val)))
2175
2176 (-eval (lambda (sourc)
2177 (repl-report-start-timing)
2178 (eval sourc)))
2179
2180 (-print (lambda (result)
2181 (if (not scm-repl-silent)
2182 (begin
2183 (print result)
2184 (newline)
2185 (if scm-repl-verbose
2186 (repl-report))
2187 (force-output)))))
2188
2189 (-quit (lambda ()
2190 (if scm-repl-verbose
2191 (begin
2192 (display ";;; QUIT executed, repl exitting")
2193 (newline)
2194 (repl-report)))
2195 #t))
2196
2197 (-abort (lambda ()
2198 (if scm-repl-verbose
2199 (begin
2200 (display ";;; ABORT executed.")
2201 (newline)
2202 (repl-report)))
2203 (repl -read -eval -print))))
2204
2205 (error-catching-repl -read
2206 -eval
2207 -print)))
2208
2209(define (stand-alone-repl)
2210 (let ((oport (current-input-port)))
2211 (set-current-input-port *stdin*)
2212 (scm-style-repl)
2213 (set-current-input-port oport)))
2214
2215
2216\f
2217
2218
2219(define (reverse-iota n) (if (> n 0) (cons (1- n) (reverse-iota (1- n))) '()))
2220(define (iota n) (list-reverse! (reverse-iota n)))
2221
2222\f
2223;;; {While}
2224;;;
2225;;; with `continue' and `break'.
2226;;;
2227
2228(defmacro while (cond . body)
2229 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2230 (break (lambda val (apply throw 'break val))))
2231 (catch 'break
2232 (lambda () (continue))
2233 (lambda v (cadr v)))))
2234
2235
2236\f
2237
2238;;; {Macros}
2239;;;
2240
2241;; actually....hobbit might be able to hack these with a little
2242;; coaxing
2243;;
2244
2245(defmacro define-macro (first . rest)
2246 (let ((name (if (symbol? first) first (car first)))
2247 (transformer
2248 (if (symbol? first)
2249 (car rest)
2250 `(lambda ,(cdr first) ,@rest))))
2251 `(define ,name (defmacro:transformer ,transformer))))
2252
2253
2254(defmacro define-syntax-macro (first . rest)
2255 (let ((name (if (symbol? first) first (car first)))
2256 (transformer
2257 (if (symbol? first)
2258 (car rest)
2259 `(lambda ,(cdr first) ,@rest))))
2260 `(define ,name (defmacro:syntax-transformer ,transformer))))
2261\f
2262;;; {Module System Macros}
2263;;;
2264
2265(defmacro define-module args
2266 `(let* ((process-define-module process-define-module)
2267 (set-current-module set-current-module)
2268 (module (process-define-module ',args)))
2269 (set-current-module module)
2270 module))
2271
2272(define define-private define)
2273
2274(defmacro define-public args
2275 (define (syntax)
2276 (error "bad syntax" (list 'define-public args)))
2277 (define (defined-name n)
2278 (cond
2279 ((symbol? n) n)
2280 ((pair? n) (defined-name (car n)))
2281 (else (syntax))))
2282 (cond
2283 ((null? args) (syntax))
2284
2285 (#t (let ((name (defined-name (car args))))
2286 `(begin
2287 (let ((public-i (module-public-interface (current-module))))
2288 ;; Make sure there is a local variable:
2289 ;;
2290 (module-define! (current-module)
2291 ',name
2292 (module-ref (current-module) ',name #f))
2293
2294 ;; Make sure that local is exported:
2295 ;;
2296 (module-add! public-i ',name (module-variable (current-module) ',name)))
2297
2298 ;; Now (re)define the var normally.
2299 ;;
2300 (define-private ,@ args))))))
2301
2302
2303
2304(defmacro defmacro-public args
2305 (define (syntax)
2306 (error "bad syntax" (list 'defmacro-public args)))
2307 (define (defined-name n)
2308 (cond
2309 ((symbol? n) n)
2310 (else (syntax))))
2311 (cond
2312 ((null? args) (syntax))
2313
2314 (#t (let ((name (defined-name (car args))))
2315 `(begin
2316 (let ((public-i (module-public-interface (current-module))))
2317 ;; Make sure there is a local variable:
2318 ;;
2319 (module-define! (current-module)
2320 ',name
2321 (module-ref (current-module) ',name #f))
2322
2323 ;; Make sure that local is exported:
2324 ;;
2325 (module-add! public-i ',name (module-variable (current-module) ',name)))
2326
2327 ;; Now (re)define the var normally.
2328 ;;
2329 (defmacro ,@ args))))))
2330
2331
2332
2333
2334(define try-load-with-path try-load-module-with-path)
2335(define try-load try-load-module)
2336(define load-with-path load-module-with-path)
2337(define load load-module)
2338
2339
2340\f
2341
2342;; (define in-ch (get-standard-channel TCL_STDIN))
2343;; (define out-ch (get-standard-channel TCL_STDOUT))
2344;; (define err-ch (get-standard-channel TCL_STDERR))
2345;;
2346;; (define inp (%make-channel-port in-ch "r"))
2347;; (define outp (%make-channel-port out-ch "w"))
2348;; (define errp (%make-channel-port err-ch "w"))
2349;;
2350;; (define %system-char-ready? char-ready?)
2351;;
2352;; (define (char-ready? p)
2353;; (if (not (channel-port? p))
2354;; (%system-char-ready? p)
2355;; (let* ((channel (%channel-port-channel p))
2356;; (old-blocking (channel-option-ref channel :blocking)))
2357;; (dynamic-wind
2358;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking "0"))
2359;; (lambda () (not (eof-object? (peek-char p))))
2360;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking old-blocking))))))
2361;;
2362;; (define (top-repl)
2363;; (with-input-from-port inp
2364;; (lambda ()
2365;; (with-output-to-port outp
2366;; (lambda ()
2367;; (with-error-to-port errp
2368;; (lambda ()
2369;; (scm-style-repl))))))))
2370;;
2371;; (set-current-input-port inp)
2372;; (set-current-output-port outp)
2373;; (set-current-error-port errp)
2374
2375(define (top-repl) (scm-style-repl))
2376
02b754d3
GH
2377(defmacro false-if-exception (expr)
2378 `(catch #t (lambda () ,expr)
2379 (lambda args #f)))
2380
0f2d19dd
JB
2381\f
2382(define-module (ice-9 calling))
2383
2384;;;;
2385;;; {Calling Conventions}
2386;;;
2387;;; This file contains a number of macros that support
2388;;; common calling conventions.
2389
2390;;;
2391;;; with-excursion-function <vars> proc
2392;;; <vars> is an unevaluated list of names that are bound in the caller.
2393;;; proc is a procedure, called:
2394;;; (proc excursion)
2395;;;
2396;;; excursion is a procedure isolates all changes to <vars>
2397;;; in the dynamic scope of the call to proc. In other words,
2398;;; the values of <vars> are saved when proc is entered, and when
2399;;; proc returns, those values are restored. Values are also restored
2400;;; entering and leaving the call to proc non-locally, such as using
2401;;; call-with-current-continuation, error, or throw.
2402;;;
2403(defmacro-public with-excursion-function (vars proc)
2404 `(,proc ,(excursion-function-syntax vars)))
2405
2406
2407
2408;;; with-getter-and-setter <vars> proc
2409;;; <vars> is an unevaluated list of names that are bound in the caller.
2410;;; proc is a procedure, called:
2411;;; (proc getter setter)
2412;;;
2413;;; getter and setter are procedures used to access
2414;;; or modify <vars>.
2415;;;
2416;;; setter, called with keywords arguments, modifies the named
2417;;; values. If "foo" and "bar" are among <vars>, then:
2418;;;
2419;;; (setter :foo 1 :bar 2)
2420;;; == (set! foo 1 bar 2)
2421;;;
2422;;; getter, called with just keywords, returns
2423;;; a list of the corresponding values. For example,
2424;;; if "foo" and "bar" are among the <vars>, then
2425;;;
2426;;; (getter :foo :bar)
2427;;; => (<value-of-foo> <value-of-bar>)
2428;;;
2429;;; getter, called with no arguments, returns a list of all accepted
2430;;; keywords and the corresponding values. If "foo" and "bar" are
2431;;; the *only* <vars>, then:
2432;;;
2433;;; (getter)
2434;;; => (:foo <value-of-bar> :bar <value-of-foo>)
2435;;;
2436;;; The unusual calling sequence of a getter supports too handy
2437;;; idioms:
2438;;;
2439;;; (apply setter (getter)) ;; save and restore
2440;;;
2441;;; (apply-to-args (getter :foo :bar) ;; fetch and bind
2442;;; (lambda (foo bar) ....))
2443;;;
2444;;; ;; [ "apply-to-args" is just like two-argument "apply" except that it
2445;;; ;; takes its arguments in a different order.
2446;;;
2447;;;
2448(defmacro-public with-getter-and-setter (vars proc)
2449 `(,proc ,@ (getter-and-setter-syntax vars)))
2450
2451;;; with-getter vars proc
2452;;; A short-hand for a call to with-getter-and-setter.
2453;;; The procedure is called:
2454;;; (proc getter)
2455;;;
2456(defmacro-public with-getter (vars proc)
2457 `(,proc ,(car (getter-and-setter-syntax vars))))
2458
2459
2460;;; with-delegating-getter-and-setter <vars> get-delegate set-delegate proc
2461;;; Compose getters and setters.
2462;;;
2463;;; <vars> is an unevaluated list of names that are bound in the caller.
2464;;;
2465;;; get-delegate is called by the new getter to extend the set of
2466;;; gettable variables beyond just <vars>
2467;;; set-delegate is called by the new setter to extend the set of
2468;;; gettable variables beyond just <vars>
2469;;;
2470;;; proc is a procedure that is called
2471;;; (proc getter setter)
2472;;;
2473(defmacro-public with-delegating-getter-and-setter (vars get-delegate set-delegate proc)
2474 `(,proc ,@ (delegating-getter-and-setter-syntax vars get-delegate set-delegate)))
2475
2476
2477;;; with-delegating-getter-and-setter <vars> get-delegate set-delegate proc
2478;;; <vars> is an unevaluated list of names that are bound in the caller.
2479;;; proc is called:
2480;;;
2481;;; (proc excursion getter setter)
2482;;;
2483;;; See also:
2484;;; with-getter-and-setter
2485;;; with-excursion-function
2486;;;
2487(defmacro-public with-excursion-getter-and-setter (vars proc)
2488 `(,proc ,(excursion-function-syntax vars)
2489 ,@ (getter-and-setter-syntax vars)))
2490
2491
2492(define (excursion-function-syntax vars)
2493 (let ((saved-value-names (map gensym vars))
2494 (tmp-var-name (gensym 'temp))
2495 (swap-fn-name (gensym 'swap))
2496 (thunk-name (gensym 'thunk)))
2497 `(lambda (,thunk-name)
2498 (letrec ((,tmp-var-name #f)
2499 (,swap-fn-name
2500 (lambda () ,@ (map (lambda (n sn) `(set! ,tmp-var-name ,n ,n ,sn ,sn ,tmp-var-name))
2501 vars saved-value-names)))
2502 ,@ (map (lambda (sn n) `(,sn ,n)) saved-value-names vars))
2503 (dynamic-wind
2504 ,swap-fn-name
2505 ,thunk-name
2506 ,swap-fn-name)))))
2507
2508
2509(define (getter-and-setter-syntax vars)
2510 (let ((args-name (gensym 'args))
2511 (an-arg-name (gensym 'an-arg))
2512 (new-val-name (gensym 'new-value))
2513 (loop-name (gensym 'loop))
2514 (kws (map symbol->keyword vars)))
2515 (list `(lambda ,args-name
2516 (let ,loop-name ((,args-name ,args-name))
2517 (if (null? ,args-name)
2518 ,(if (null? kws)
2519 ''()
2520 `(let ((all-vals (,loop-name ',kws)))
2521 (let ,loop-name ((vals all-vals)
2522 (kws ',kws))
2523 (if (null? vals)
2524 '()
2525 `(,(car kws) ,(car vals) ,@(,loop-name (cdr vals) (cdr kws)))))))
2526 (map (lambda (,an-arg-name)
2527 (case ,an-arg-name
2528 ,@ (append
2529 (map (lambda (kw v) `((,kw) ,v)) kws vars)
2530 `((else (throw 'bad-get-option ,an-arg-name))))))
2531 ,args-name))))
2532
2533 `(lambda ,args-name
2534 (let ,loop-name ((,args-name ,args-name))
2535 (or (null? ,args-name)
2536 (null? (cdr ,args-name))
2537 (let ((,an-arg-name (car ,args-name))
2538 (,new-val-name (cadr ,args-name)))
2539 (case ,an-arg-name
2540 ,@ (append
2541 (map (lambda (kw v) `((,kw) (set! ,v ,new-val-name))) kws vars)
2542 `((else (throw 'bad-set-option ,an-arg-name)))))
2543 (,loop-name (cddr ,args-name)))))))))
2544
2545(define (delegating-getter-and-setter-syntax vars get-delegate set-delegate)
2546 (let ((args-name (gensym 'args))
2547 (an-arg-name (gensym 'an-arg))
2548 (new-val-name (gensym 'new-value))
2549 (loop-name (gensym 'loop))
2550 (kws (map symbol->keyword vars)))
2551 (list `(lambda ,args-name
2552 (let ,loop-name ((,args-name ,args-name))
2553 (if (null? ,args-name)
2554 (append!
2555 ,(if (null? kws)
2556 ''()
2557 `(let ((all-vals (,loop-name ',kws)))
2558 (let ,loop-name ((vals all-vals)
2559 (kws ',kws))
2560 (if (null? vals)
2561 '()
2562 `(,(car kws) ,(car vals) ,@(,loop-name (cdr vals) (cdr kws)))))))
2563 (,get-delegate))
2564 (map (lambda (,an-arg-name)
2565 (case ,an-arg-name
2566 ,@ (append
2567 (map (lambda (kw v) `((,kw) ,v)) kws vars)
2568 `((else (car (,get-delegate ,an-arg-name)))))))
2569 ,args-name))))
2570
2571 `(lambda ,args-name
2572 (let ,loop-name ((,args-name ,args-name))
2573 (or (null? ,args-name)
2574 (null? (cdr ,args-name))
2575 (let ((,an-arg-name (car ,args-name))
2576 (,new-val-name (cadr ,args-name)))
2577 (case ,an-arg-name
2578 ,@ (append
2579 (map (lambda (kw v) `((,kw) (set! ,v ,new-val-name))) kws vars)
2580 `((else (,set-delegate ,an-arg-name ,new-val-name)))))
2581 (,loop-name (cddr ,args-name)))))))))
2582
2583
2584
2585
2586;;; with-configuration-getter-and-setter <vars-etc> proc
2587;;;
2588;;; Create a getter and setter that can trigger arbitrary computation.
2589;;;
2590;;; <vars-etc> is a list of variable specifiers, explained below.
2591;;; proc is called:
2592;;;
2593;;; (proc getter setter)
2594;;;
2595;;; Each element of the <vars-etc> list is of the form:
2596;;;
2597;;; (<var> getter-hook setter-hook)
2598;;;
2599;;; Both hook elements are evaluated; the variable name is not.
2600;;; Either hook may be #f or procedure.
2601;;;
2602;;; A getter hook is a thunk that returns a value for the corresponding
2603;;; variable. If omitted (#f is passed), the binding of <var> is
2604;;; returned.
2605;;;
2606;;; A setter hook is a procedure of one argument that accepts a new value
2607;;; for the corresponding variable. If omitted, the binding of <var>
2608;;; is simply set using set!.
2609;;;
2610(defmacro-public with-configuration-getter-and-setter (vars-etc proc)
2611 `((lambda (simpler-get simpler-set body-proc)
2612 (with-delegating-getter-and-setter ()
2613 simpler-get simpler-set body-proc))
2614
2615 (lambda (kw)
2616 (case kw
2617 ,@(map (lambda (v) `((,(symbol->keyword (car v)))
2618 ,(cond
2619 ((cadr v) => list)
2620 (else `(list ,(car v))))))
2621 vars-etc)))
2622
2623 (lambda (kw new-val)
2624 (case kw
2625 ,@(map (lambda (v) `((,(symbol->keyword (car v)))
2626 ,(cond
2627 ((caddr v) => (lambda (proc) `(,proc new-val)))
2628 (else `(set! ,(car v) new-val)))))
2629 vars-etc)))
2630
2631 ,proc))
2632
2633(defmacro-public with-delegating-configuration-getter-and-setter (vars-etc delegate-get delegate-set proc)
2634 `((lambda (simpler-get simpler-set body-proc)
2635 (with-delegating-getter-and-setter ()
2636 simpler-get simpler-set body-proc))
2637
2638 (lambda (kw)
2639 (case kw
2640 ,@(append! (map (lambda (v) `((,(symbol->keyword (car v)))
2641 ,(cond
2642 ((cadr v) => list)
2643 (else `(list ,(car v))))))
2644 vars-etc)
2645 `((else (,delegate-get kw))))))
2646
2647 (lambda (kw new-val)
2648 (case kw
2649 ,@(append! (map (lambda (v) `((,(symbol->keyword (car v)))
2650 ,(cond
2651 ((caddr v) => (lambda (proc) `(,proc new-val)))
2652 (else `(set! ,(car v) new-val)))))
2653 vars-etc)
2654 `((else (,delegate-set kw new-val))))))
2655
2656 ,proc))
2657
2658
2659;;; let-configuration-getter-and-setter <vars-etc> proc
2660;;;
2661;;; This procedure is like with-configuration-getter-and-setter (q.v.)
2662;;; except that each element of <vars-etc> is:
2663;;;
2664;;; (<var> initial-value getter-hook setter-hook)
2665;;;
2666;;; Unlike with-configuration-getter-and-setter, let-configuration-getter-and-setter
2667;;; introduces bindings for the variables named in <vars-etc>.
2668;;; It is short-hand for:
2669;;;
2670;;; (let ((<var1> initial-value-1)
2671;;; (<var2> initial-value-2)
2672;;; ...)
2673;;; (with-configuration-getter-and-setter ((<var1> v1-get v1-set) ...) proc))
2674;;;
2675(defmacro-public let-with-configuration-getter-and-setter (vars-etc proc)
2676 `(let ,(map (lambda (v) `(,(car v) ,(cadr v))) vars-etc)
2677 (with-configuration-getter-and-setter ,(map (lambda (v) `(,(car v) ,(caddr v) ,(cadddr v))) vars-etc)
2678 ,proc)))
2679
2680
2681
2682\f
2683(define-module (ice-9 common-list))
2684
2685;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
2686; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
2687;
2688;Permission to copy this software, to redistribute it, and to use it
2689;for any purpose is granted, subject to the following restrictions and
2690;understandings.
2691;
2692;1. Any copy made of this software must include this copyright notice
2693;in full.
2694;
2695;2. I have made no warrantee or representation that the operation of
2696;this software will be error-free, and I am under no obligation to
2697;provide any services, by way of maintenance, update, or otherwise.
2698;
2699;3. In conjunction with products arising from the use of this
2700;material, there shall be no use of my name in any advertising,
2701;promotional, or sales literature without prior written consent in
2702;each case.
2703
2704\f
2705
2706
2707;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
2708(define-public (make-list k . init)
2709 (set! init (if (pair? init) (car init)))
2710 (do ((k k (+ -1 k))
2711 (result '() (cons init result)))
2712 ((<= k 0) result)))
2713
2714(define-public (adjoin e l) (if (memq e l) l (cons e l)))
2715
2716(define-public (union l1 l2)
2717 (cond ((null? l1) l2)
2718 ((null? l2) l1)
2719 (else (union (cdr l1) (adjoin (car l1) l2)))))
2720
2721(define-public (intersection l1 l2)
2722 (cond ((null? l1) l1)
2723 ((null? l2) l2)
2724 ((memv (car l1) l2) (cons (car l1) (intersection (cdr l1) l2)))
2725 (else (intersection (cdr l1) l2))))
2726
2727(define-public (set-difference l1 l2)
2728 (cond ((null? l1) l1)
2729 ((memv (car l1) l2) (set-difference (cdr l1) l2))
2730 (else (cons (car l1) (set-difference (cdr l1) l2)))))
2731
2732(define-public (reduce-init p init l)
2733 (if (null? l)
2734 init
2735 (reduce-init p (p init (car l)) (cdr l))))
2736
2737(define-public (reduce p l)
2738 (cond ((null? l) l)
2739 ((null? (cdr l)) (car l))
2740 (else (reduce-init p (car l) (cdr l)))))
2741
2742(define-public (some pred l . rest)
2743 (cond ((null? rest)
2744 (let mapf ((l l))
2745 (and (not (null? l))
2746 (or (pred (car l)) (mapf (cdr l))))))
2747 (else (let mapf ((l l) (rest rest))
2748 (and (not (null? l))
2749 (or (apply pred (car l) (map car rest))
2750 (mapf (cdr l) (map cdr rest))))))))
2751
2752(define-public (every pred l . rest)
2753 (cond ((null? rest)
2754 (let mapf ((l l))
2755 (or (null? l)
2756 (and (pred (car l)) (mapf (cdr l))))))
2757 (else (let mapf ((l l) (rest rest))
2758 (or (null? l)
2759 (and (apply pred (car l) (map car rest))
2760 (mapf (cdr l) (map cdr rest))))))))
2761
2762(define-public (notany pred . ls) (not (apply some pred ls)))
2763
2764(define-public (notevery pred . ls) (not (apply every pred ls)))
2765
2766(define-public (find-if t l)
2767 (cond ((null? l) #f)
2768 ((t (car l)) (car l))
2769 (else (find-if t (cdr l)))))
2770
2771(define-public (member-if t l)
2772 (cond ((null? l) #f)
2773 ((t (car l)) l)
2774 (else (member-if t (cdr l)))))
2775
2776(define-public (remove-if p l)
2777 (cond ((null? l) '())
2778 ((p (car l)) (remove-if p (cdr l)))
2779 (else (cons (car l) (remove-if p (cdr l))))))
2780
2781(define-public (delete-if! pred list)
2782 (let delete-if ((list list))
2783 (cond ((null? list) '())
2784 ((pred (car list)) (delete-if (cdr list)))
2785 (else
2786 (set-cdr! list (delete-if (cdr list)))
2787 list))))
2788
2789(define-public (delete-if-not! pred list)
2790 (let delete-if ((list list))
2791 (cond ((null? list) '())
2792 ((not (pred (car list))) (delete-if (cdr list)))
2793 (else
2794 (set-cdr! list (delete-if (cdr list)))
2795 list))))
2796
2797(define-public (butlast lst n)
2798 (letrec ((l (- (length lst) n))
2799 (bl (lambda (lst n)
2800 (cond ((null? lst) lst)
2801 ((positive? n)
2802 (cons (car lst) (bl (cdr lst) (+ -1 n))))
2803 (else '())))))
2804 (bl lst (if (negative? n)
2805 (slib:error "negative argument to butlast" n)
2806 l))))
2807
2808(define-public (and? . args)
2809 (cond ((null? args) #t)
2810 ((car args) (apply and? (cdr args)))
2811 (else #f)))
2812
2813(define-public (or? . args)
2814 (cond ((null? args) #f)
2815 ((car args) #t)
2816 (else (apply or? (cdr args)))))
2817
2818(define-public (has-duplicates? lst)
2819 (cond ((null? lst) #f)
2820 ((member (car lst) (cdr lst)) #t)
2821 (else (has-duplicates? (cdr lst)))))
2822
2823(define-public (list* x . y)
2824 (define (list*1 x)
2825 (if (null? (cdr x))
2826 (car x)
2827 (cons (car x) (list*1 (cdr x)))))
2828 (if (null? y)
2829 x
2830 (cons x (list*1 y))))
2831
2832;; pick p l
2833;; Apply P to each element of L, returning a list of elts
2834;; for which P returns a non-#f value.
2835;;
2836(define-public (pick p l)
2837 (let loop ((s '())
2838 (l l))
2839 (cond
2840 ((null? l) s)
2841 ((p (car l)) (loop (cons (car l) s) (cdr l)))
2842 (else (loop s (cdr l))))))
2843
2844;; pick p l
2845;; Apply P to each element of L, returning a list of the
2846;; non-#f return values of P.
2847;;
2848(define-public (pick-mappings p l)
2849 (let loop ((s '())
2850 (l l))
2851 (cond
2852 ((null? l) s)
2853 ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l))))
2854 (else (loop s (cdr l))))))
2855
2856(define-public (uniq l)
2857 (if (null? l)
2858 '()
2859 (let ((u (uniq (cdr l))))
2860 (if (memq (car l) u)
2861 u
2862 (cons (car l) u)))))
2863
2864\f
2865(define-module (ice-9 ls)
2866 :use-module (ice-9 common-list))
2867
2868\f
2869
2870;;;;
2871;;; local-definitions-in root name
2872;;; Returns a list of names defined locally in the named subdirectory of root.
2873;;; definitions-in root name
2874;;; Returns a list of all names defined in the named subdirectory of root.
2875;;; The list includes alll locally defined names as well as all names inherited
2876;;; from a member of a use-list.
2877;;;
2878;;; A convenient interface for examining the nature of things:
2879;;;
2880;;; ls . various-names
2881;;;
2882;;; With just one argument, interpret that argument as the name of a subdirectory
2883;;; of the current module and return a list of names defined there.
2884;;;
2885;;; With more than one argument, still compute subdirectory lists, but
2886;;; return a list:
2887;;; ((<subdir-name> . <names-defined-there>)
2888;;; (<subdir-name> . <names-defined-there>)
2889;;; ...)
2890;;;
2891
2892(define-public (local-definitions-in root names)
2893 (let ((m (resolved-ref root names))
2894 (answer '()))
2895 (if (not (module? m))
2896 (set! answer m)
2897 (module-for-each (lambda (k v) (set! answer (cons k answer))) m))
2898 answer))
2899
2900(define-public (definitions-in root names)
2901 (let ((m (resolved-ref root names)))
2902 (if (not (module? m))
2903 m
2904 (reduce union
2905 (cons (local-definitions-in m '())
2906 (map (lambda (m2) (definitions-in m2 '())) (module-uses m)))))))
2907
2908(define-public (ls . various-refs)
2909 (and various-refs
2910 (if (cdr various-refs)
2911 (map (lambda (ref)
2912 (cons ref (definitions-in (current-module) ref)))
2913 various-refs)
2914 (definitions-in (current-module) (car various-refs)))))
2915
2916(define-public (lls . various-refs)
2917 (and various-refs
2918 (if (cdr various-refs)
2919 (map (lambda (ref)
2920 (cons ref (local-definitions-in (current-module) ref)))
2921 various-refs)
2922 (local-definitions-in (current-module) (car various-refs)))))
2923
2924(define-public (recursive-value-define name value)
2925 (let ((parent (reverse! (cdr (reverse name)))))
2926 (and parent (make-modules-in (current-module) parent))
2927 (value-define name value)))
2928\f
2929(define-module (ice-9 q))
2930
2931;;;; Copyright (C) 1995 Free Software Foundation, Inc.
2932;;;;
2933;;;; This program is free software; you can redistribute it and/or modify
2934;;;; it under the terms of the GNU General Public License as published by
2935;;;; the Free Software Foundation; either version 2, or (at your option)
2936;;;; any later version.
2937;;;;
2938;;;; This program is distributed in the hope that it will be useful,
2939;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2940;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2941;;;; GNU General Public License for more details.
2942;;;;
2943;;;; You should have received a copy of the GNU General Public License
2944;;;; along with this software; see the file COPYING. If not, write to
2945;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
2946;;;;
2947
2948\f
2949;;;;
2950;;; Q: Based on the interface to
2951;;;
2952;;; "queue.scm" Queues/Stacks for Scheme
2953;;; Written by Andrew Wilcox (awilcox@astro.psu.edu) on April 1, 1992.
2954;;;
2955
2956\f
2957;;;;
2958;;; {Q}
2959;;;
2960;;; A list is just a bunch of cons pairs that follows some constrains, right?
2961;;; Association lists are the same. Hash tables are just vectors and association
2962;;; lists. You can print them, read them, write them as constants, pun them off as other data
2963;;; structures etc. This is good. This is lisp. These structures are fast and compact
2964;;; and easy to manipulate arbitrarily because of their simple, regular structure and
2965;;; non-disjointedness (associations being lists and so forth).
2966;;;
2967;;; So I figured, queues should be the same -- just a "subtype" of cons-pair
2968;;; structures in general.
2969;;;
2970;;; A queue is a cons pair:
2971;;; ( <the-q> . <last-pair> )
2972;;;
2973;;; <the-q> is a list of things in the q. New elements go at the end of that list.
2974;;;
2975;;; <last-pair> is #f if the q is empty, and otherwise is the last pair of <the-q>.
2976;;;
2977;;; q's print nicely, but alas, they do not read well because the eq?-ness of
2978;;; <last-pair> and (last-pair <the-q>) is lost by read. The procedure
2979;;;
2980;;; (sync-q! q)
2981;;;
2982;;; recomputes and resets the <last-pair> component of a queue.
2983;;;
2984
2985(define-public (sync-q! obj) (set-cdr! obj (and (car obj) (last-pair (car obj)))))
2986
2987;;; make-q
2988;;; return a new q.
2989;;;
2990(define-public (make-q) (cons '() '()))
2991
2992;;; q? obj
2993;;; Return true if obj is a Q.
2994;;; An object is a queue if it is equal? to '(#f . #f) or
2995;;; if it is a pair P with (list? (car P)) and (eq? (cdr P) (last-pair P)).
2996;;;
2997(define-public (q? obj) (and (pair? obj)
2998 (or (and (null? (car obj))
2999 (null? (cdr obj)))
3000 (and
3001 (list? (car obj))
3002 (eq? (cdr obj) (last-pair (car obj)))))))
3003
3004;;; q-empty? obj
3005;;;
3006(define-public (q-empty? obj) (null? (car obj)))
3007
3008;;; q-empty-check q
3009;;; Throw a q-empty exception if Q is empty.
3010(define-public (q-empty-check q) (if (q-empty? q) (throw 'q-empty q)))
3011
3012
3013;;; q-front q
3014;;; Return the first element of Q.
3015(define-public (q-front q) (q-empty-check q) (caar q))
3016
3017;;; q-front q
3018;;; Return the last element of Q.
3019(define-public (q-rear q) (q-empty-check q) (cadr q))
3020
3021;;; q-remove! q obj
3022;;; Remove all occurences of obj from Q.
3023(define-public (q-remove! q obj)
3024 (while (memq obj (car q))
3025 (set-car! q (delq! obj (car q))))
3026 (set-cdr! q (last-pair (car q))))
3027
3028;;; q-push! q obj
3029;;; Add obj to the front of Q
3030(define-public (q-push! q d)
3031 (let ((h (cons d (car q))))
3032 (set-car! q h)
3033 (if (null? (cdr q))
3034 (set-cdr! q h))))
3035
3036;;; enq! q obj
3037;;; Add obj to the rear of Q
3038(define-public (enq! q d)
3039 (let ((h (cons d '())))
3040 (if (not (null? (cdr q)))
3041 (set-cdr! (cdr q) h)
3042 (set-car! q h))
3043 (set-cdr! q h)))
3044
3045;;; q-pop! q
3046;;; Take the front of Q and return it.
3047(define-public (q-pop! q)
3048 (q-empty-check q)
3049 (let ((it (caar q))
3050 (next (cdar q)))
3051 (if (not next)
3052 (set-cdr! q #f))
3053 (set-car! q next)
3054 it))
3055
3056;;; deq! q
3057;;; Take the front of Q and return it.
3058(define-public deq! q-pop!)
3059
3060;;; q-length q
3061;;; Return the number of enqueued elements.
3062;;;
3063(define-public (q-length q) (length (car q)))
3064
3065
3066
3067\f
3068;;; installed-scm-file
3069(define-module (ice-9 runq)
3070 :use-module (ice-9 q))
3071
3072\f
3073
3074;;;; Copyright (C) 1996 Free Software Foundation, Inc.
3075;;;;
3076;;;; This program is free software; you can redistribute it and/or modify
3077;;;; it under the terms of the GNU General Public License as published by
3078;;;; the Free Software Foundation; either version 2, or (at your option)
3079;;;; any later version.
3080;;;;
3081;;;; This program is distributed in the hope that it will be useful,
3082;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
3083;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3084;;;; GNU General Public License for more details.
3085;;;;
3086;;;; You should have received a copy of the GNU General Public License
3087;;;; along with this software; see the file COPYING. If not, write to
3088;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
3089;;;;
3090
3091
3092\f
3093;;;;
3094;;; {The runq data structure}
3095;;;
3096;;; One way to schedule parallel computations in a serial environment is
3097;;; to explicitly divide each task up into small, finite execution time,
3098;;; strips. Then you interleave the execution of strips from various
3099;;; tasks to achieve a kind of parallelism. Runqs are a handy data
3100;;; structure for this style of programming.
3101;;;
3102;;; We use thunks (nullary procedures) and lists of thunks to represent
3103;;; strips. By convention, the return value of a strip-thunk must either
3104;;; be another strip or the value #f.
3105;;;
3106;;; A runq is a procedure that manages a queue of strips. Called with no
3107;;; arguments, it processes one strip from the queue. Called with
3108;;; arguments, the arguments form a control message for the queue. The
3109;;; first argument is a symbol which is the message selector.
3110;;;
3111;;; A strip is processed this way: If the strip is a thunk, the thunk is
3112;;; called -- if it returns a strip, that strip is added back to the
3113;;; queue. To process a strip which is a list of thunks, the CAR of that
3114;;; list is called. After a call to that CAR, there are 0, 1, or 2 strips
3115;;; -- perhaps one returned by the thunk, and perhaps the CDR of the
3116;;; original strip if that CDR is not nil. The runq puts whichever of
3117;;; these strips exist back on the queue. (The exact order in which
3118;;; strips are put back on the queue determines the scheduling behavior of
3119;;; a particular queue -- it's a parameter.)
3120;;;
3121;;;
3122
3123
3124
3125;;;;
3126;;; (runq-control q msg . args)
3127;;;
3128;;; processes in the default way the control messages that
3129;;; can be sent to a runq. Q should be an ordinary
3130;;; Q (see utils/q.scm).
3131;;;
3132;;; The standard runq messages are:
3133;;;
3134;;; 'add! strip0 strip1... ;; to enqueue one or more strips
3135;;; 'enqueue! strip0 strip1... ;; to enqueue one or more strips
3136;;; 'push! strip0 ... ;; add strips to the front of the queue
3137;;; 'empty? ;; true if it is
3138;;; 'length ;; how many strips in the queue?
3139;;; 'kill! ;; empty the queue
3140;;; else ;; throw 'not-understood
3141;;;
3142(define-public (runq-control q msg . args)
3143 (case msg
3144 ((add!) (for-each (lambda (t) (enq! q t)) args) '*unspecified*)
3145 ((enque!) (for-each (lambda (t) (enq! q t)) args) '*unspecified*)
3146 ((push!) (for-each (lambda (t) (q-push! q t)) args) '*unspecified*)
3147 ((empty?) (q-empty? q))
3148 ((length) (q-length q))
3149 ((kill!) (set! q (make-q)))
3150 (else (throw 'not-understood msg args))))
3151
3152(define (run-strip thunk) (catch #t thunk (lambda ign (warn 'runq-strip thunk ign) #f)))
3153
3154;;;;
3155;;; make-void-runq
3156;;;
3157;;; Make a runq that discards all messages except "length", for which
3158;;; it returns 0.
3159;;;
3160(define-public (make-void-runq)
3161 (lambda opts
3162 (and opts
3163 (apply-to-args opts
3164 (lambda (msg . args)
3165 (case msg
3166 ((length) 0)
3167 (else #f)))))))
3168
3169;;;;
3170;;; (make-fair-runq)
3171;;;
3172;;; Returns a runq procedure.
3173;;; Called with no arguments, the procedure processes one strip from the queue.
3174;;; Called with arguments, it uses runq-control.
3175;;;
3176;;; In a fair runq, if a strip returns a new strip X, X is added
3177;;; to the end of the queue, meaning it will be the last to execute
3178;;; of all the remaining procedures.
3179;;;
3180(define-public (make-fair-runq)
3181 (letrec ((q (make-q))
3182 (self
3183 (lambda ctl
3184 (if ctl
3185 (apply runq-control q ctl)
3186 (and (not (q-empty? q))
3187 (let ((next-strip (deq! q)))
3188 (cond
3189 ((procedure? next-strip) (let ((k (run-strip next-strip)))
3190 (and k (enq! q k))))
3191 ((pair? next-strip) (let ((k (run-strip (car next-strip))))
3192 (and k (enq! q k)))
3193 (if (not (null? (cdr next-strip)))
3194 (enq! q (cdr next-strip)))))
3195 self))))))
3196 self))
3197
3198
3199;;;;
3200;;; (make-exclusive-runq)
3201;;;
3202;;; Returns a runq procedure.
3203;;; Called with no arguments, the procedure processes one strip from the queue.
3204;;; Called with arguments, it uses runq-control.
3205;;;
3206;;; In an exclusive runq, if a strip W returns a new strip X, X is added
3207;;; to the front of the queue, meaning it will be the next to execute
3208;;; of all the remaining procedures.
3209;;;
3210;;; An exception to this occurs if W was the CAR of a list of strips.
3211;;; In that case, after the return value of W is pushed onto the front
3212;;; of the queue, the CDR of the list of strips is pushed in front
3213;;; of that (if the CDR is not nil). This way, the rest of the thunks
3214;;; in the list that contained W have priority over the return value of W.
3215;;;
3216(define-public (make-exclusive-runq)
3217 (letrec ((q (make-q))
3218 (self
3219 (lambda ctl
3220 (if ctl
3221 (apply runq-control q ctl)
3222 (and (not (q-empty? q))
3223 (let ((next-strip (deq! q)))
3224 (cond
3225 ((procedure? next-strip) (let ((k (run-strip next-strip)))
3226 (and k (q-push! q k))))
3227 ((pair? next-strip) (let ((k (run-strip (car next-strip))))
3228 (and k (q-push! q k)))
3229 (if (not (null? (cdr next-strip)))
3230 (q-push! q (cdr next-strip)))))
3231 self))))))
3232 self))
3233
3234
3235;;;;
3236;;; (make-subordinate-runq-to superior basic-inferior)
3237;;;
3238;;; Returns a runq proxy for the runq basic-inferior.
3239;;;
3240;;; The proxy watches for operations on the basic-inferior that cause
3241;;; a transition from a queue length of 0 to a non-zero length and
3242;;; vice versa. While the basic-inferior queue is not empty,
3243;;; the proxy installs a task on the superior runq. Each strip
3244;;; of that task processes N strips from the basic-inferior where
3245;;; N is the length of the basic-inferior queue when the proxy
3246;;; strip is entered. [Countless scheduling variations are possible.]
3247;;;
3248(define-public (make-subordinate-runq-to superior-runq basic-runq)
3249 (let ((runq-task (cons #f #f)))
3250 (set-car! runq-task
3251 (lambda ()
3252 (if (basic-runq 'empty?)
3253 (set-cdr! runq-task #f)
3254 (do ((n (basic-runq 'length) (1- n)))
3255 ((<= n 0) #f)
3256 (basic-runq)))))
3257 (letrec ((self
3258 (lambda ctl
3259 (if (not ctl)
3260 (let ((answer (basic-runq)))
3261 (self 'empty?)
3262 answer)
3263 (begin
3264 (case (car ctl)
3265 ((suspend) (set-cdr! runq-task #f))
3266 (else (let ((answer (apply basic-runq ctl)))
3267 (if (and (not (cdr runq-task)) (not (basic-runq 'empty?)))
3268 (begin
3269 (set-cdr! runq-task runq-task)
3270 (superior-runq 'add! runq-task)))
3271 answer))))))))
3272 self)))
3273
3274;;;;
3275;;; (define fork-strips (lambda args args))
3276;;; Return a strip that starts several strips in
3277;;; parallel. If this strip is enqueued on a fair
3278;;; runq, strips of the parallel subtasks will run
3279;;; round-robin style.
3280;;;
3281(define fork-strips (lambda args args))
3282
3283
3284;;;;
3285;;; (strip-sequence . strips)
3286;;;
3287;;; Returns a new strip which is the concatenation of the argument strips.
3288;;;
3289(define-public ((strip-sequence . strips))
3290 (let loop ((st (let ((a strips)) (set! strips #f) a)))
3291 (and (not (null? st))
3292 (let ((then ((car st))))
3293 (if then
3294 (lambda () (loop (cons then (cdr st))))
3295 (lambda () (loop (cdr st))))))))
3296
3297
3298;;;;
3299;;; (fair-strip-subtask . initial-strips)
3300;;;
3301;;; Returns a new strip which is the synchronos, fair,
3302;;; parallel execution of the argument strips.
3303;;;
3304;;;
3305;;;
3306(define-public (fair-strip-subtask . initial-strips)
3307 (let ((st (make-fair-runq)))
3308 (apply st 'add! initial-strips)
3309 st))
3310
3311\f
3312
3313;;; installed-scm-file
3314\f
3315(define-module (ice-9 string-fun))
3316
3317\f
3318;;;;
3319;;; {String Fun}
3320;;;
3321;;; Various string funcitons, particularly those that take
3322;;; advantage of the "shared substring" capability.
3323;;;
3324\f
3325;;;;
3326;;; {Dividing Strings Into Fields}
3327;;;
3328;;; The names of these functions are very regular.
3329;;; Here is a grammar of a call to one of these:
3330;;;
3331;;; <string-function-invocation>
3332;;; := (<action>-<seperator-disposition>-<seperator-determination> <seperator-param> <str> <ret>)
3333;;;
3334;;; <str> = the string
3335;;;
3336;;; <ret> = The continuation. String functions generally return
3337;;; multiple values by passing them to this procedure.
3338;;;
3339;;; <action> = split
3340;;; | separate-fields
3341;;;
3342;;; "split" means to divide a string into two parts.
3343;;; <ret> will be called with two arguments.
3344;;;
3345;;; "separate-fields" means to divide a string into as many
3346;;; parts as possible. <ret> will be called with
3347;;; however many fields are found.
3348;;;
3349;;; <seperator-disposition> = before
3350;;; | after
3351;;; | discarding
3352;;;
3353;;; "before" means to leave the seperator attached to
3354;;; the beginning of the field to its right.
3355;;; "after" means to leave the seperator attached to
3356;;; the end of the field to its left.
3357;;; "discarding" means to discard seperators.
3358;;;
3359;;; Other dispositions might be handy. For example, "isolate"
3360;;; could mean to treat the separator as a field unto itself.
3361;;;
3362;;; <seperator-determination> = char
3363;;; | predicate
3364;;;
3365;;; "char" means to use a particular character as field seperator.
3366;;; "predicate" means to check each character using a particular predicate.
3367;;;
3368;;; Other determinations might be handy. For example, "character-set-member".
3369;;;
3370;;; <seperator-param> = A parameter that completes the meaning of the determinations.
3371;;; For example, if the determination is "char", then this parameter
3372;;; says which character. If it is "predicate", the parameter is the
3373;;; predicate.
3374;;;
3375;;;
3376;;; For example:
3377;;;
3378;;; (separate-fields-discarding-char #\, "foo, bar, baz, , bat" list)
3379;;; => ("foo" " bar" " baz" " " " bat")
3380;;;
3381;;; (split-after-char #\- 'an-example-of-split list)
3382;;; => ("an-" "example-of-split")
3383;;;
3384;;; As an alternative to using a determination "predicate", or to trying to do anything
3385;;; complicated with these functions, consider using regular expressions.
3386;;;
3387
3388(define-public (split-after-char char str ret)
3389 (let ((end (cond
3390 ((string-index str char) => 1+)
3391 (else (string-length str)))))
3392 (ret (make-shared-substring str 0 end)
3393 (make-shared-substring str end))))
3394
3395(define-public (split-before-char char str ret)
3396 (let ((end (or (string-index str char)
3397 (string-length str))))
3398 (ret (make-shared-substring str 0 end)
3399 (make-shared-substring str end))))
3400
3401(define-public (split-discarding-char char str ret)
3402 (let ((end (string-index str char)))
3403 (if (not end)
3404 (ret str "")
3405 (ret (make-shared-substring str 0 end)
3406 (make-shared-substring str (1+ end))))))
3407
3408(define-public (split-after-char-last char str ret)
3409 (let ((end (cond
3410 ((string-rindex str char) => 1+)
3411 (else 0))))
3412 (ret (make-shared-substring str 0 end)
3413 (make-shared-substring str end))))
3414
3415(define-public (split-before-char-last char str ret)
3416 (let ((end (or (string-rindex str char) 0)))
3417 (ret (make-shared-substring str 0 end)
3418 (make-shared-substring str end))))
3419
3420(define-public (split-discarding-char-last char str ret)
3421 (let ((end (string-rindex str char)))
3422 (if (not end)
3423 (ret str "")
3424 (ret (make-shared-substring str 0 end)
3425 (make-shared-substring str (1+ end))))))
3426
3427(define (split-before-predicate pred str ret)
3428 (let loop ((n 0))
3429 (cond
3430 ((= n (length str)) (ret str ""))
3431 ((not (pred (string-ref str n))) (loop (1+ n)))
3432 (else (ret (make-shared-substring str 0 n)
3433 (make-shared-substring str n))))))
3434(define (split-after-predicate pred str ret)
3435 (let loop ((n 0))
3436 (cond
3437 ((= n (length str)) (ret str ""))
3438 ((not (pred (string-ref str n))) (loop (1+ n)))
3439 (else (ret (make-shared-substring str 0 (1+ n))
3440 (make-shared-substring str (1+ n)))))))
3441
3442(define (split-discarding-predicate pred str ret)
3443 (let loop ((n 0))
3444 (cond
3445 ((= n (length str)) (ret str ""))
3446 ((not (pred (string-ref str n))) (loop (1+ n)))
3447 (else (ret (make-shared-substring str 0 n)
3448 (make-shared-substring str (1+ n)))))))
3449
3450(define-public (seperate-fields-discarding-char ch str ret)
3451 (let loop ((fields '())
3452 (str str))
3453 (cond
3454 ((string-rindex str ch)
3455 => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 w)) fields)
3456 (make-shared-substring str 0 w))))
3457 (else (ret (cons str fields))))))
3458
3459(define-public (seperate-fields-after-char ch str ret)
3460 (let loop ((fields '())
3461 (str str))
3462 (cond
3463 ((string-rindex str ch)
3464 => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 w)) fields)
3465 (make-shared-substring str 0 (+ 1 w)))))
3466 (else (ret (cons str fields))))))
3467
3468(define-public (seperate-fields-before-char ch str ret)
3469 (let loop ((fields '())
3470 (str str))
3471 (cond
3472 ((string-rindex str ch)
3473 => (lambda (pos) (loop (cons (make-shared-substring str w) fields)
3474 (make-shared-substring str 0 w))))
3475 (else (ret (cons str fields))))))
3476
3477\f
3478;;;;
3479;;; {String Prefix Predicates}
3480;;;
3481;;; Very simple:
3482;;;
3483:;; (define-public ((string-prefix-predicate pred?) prefix str)
3484;;; (and (<= (length prefix) (length str))
3485;;; (pred? prefix (make-shared-substring str 0 (length prefix)))))
3486;;;
3487;;; (define-public string-prefix=? (string-prefix-predicate string=?))
3488;;;
3489
3490(define-public ((string-prefix-predicate pred?) prefix str)
3491 (and (<= (length prefix) (length str))
3492 (pred? prefix (make-shared-substring str 0 (length prefix)))))
3493
3494(define-public string-prefix=? (string-prefix-predicate string=?))
3495
3496\f
3497;;;;
3498;;; {Strippers}
3499;;;
3500;;; <stripper> = sans-<removable-part>
3501;;;
3502;;; <removable-part> = surrounding-whitespace
3503;;; | trailing-whitespace
3504;;; | leading-whitespace
3505;;; | final-newline
3506;;;
3507
3508(define-public (sans-surrounding-whitespace s)
3509 (let ((st 0)
3510 (end (string-length s)))
3511 (while (and (< st (string-length s))
3512 (char-whitespace? (string-ref s st)))
3513 (set! st (1+ st)))
3514 (while (and (< 0 end)
3515 (char-whitespace? (string-ref s (1- end))))
3516 (set! end (1- end)))
3517 (if (< end st)
3518 ""
3519 (make-shared-substring s st end))))
3520
3521(define-public (sans-trailing-whitespace s)
3522 (let ((st 0)
3523 (end (string-length s)))
3524 (while (and (< 0 end)
3525 (char-whitespace? (string-ref s (1- end))))
3526 (set! end (1- end)))
3527 (if (< end st)
3528 ""
3529 (make-shared-substring s st end))))
3530
3531(define-public (sans-leading-whitespace s)
3532 (let ((st 0)
3533 (end (string-length s)))
3534 (while (and (< st (string-length s))
3535 (char-whitespace? (string-ref s st)))
3536 (set! st (1+ st)))
3537 (if (< end st)
3538 ""
3539 (make-shared-substring s st end))))
3540
3541(define-public (sans-final-newline str)
3542 (cond
3543 ((= 0 (string-length str))
3544 str)
3545
3546 ((char=? #\nl (string-ref str (1- (string-length str))))
3547 (make-shared-substring str 0 (1- (string-length str))))
3548
3549 (else str)))
3550\f
3551;;;;
3552;;; {has-trailing-newline?}
3553;;;
3554
3555(define-public (has-trailing-newline? str)
3556 (and (< 0 (string-length str))
3557 (char=? #\nl (string-ref str (1- (string-length str))))))
3558
3559
3560\f
3561
3562
3563(define-public (with-regexp-parts regexp fields str return fail)
3564 (let ((parts (regexec regexp str fields)))
3565 (if (number? parts)
3566 (fail parts)
3567 (apply return parts))))
3568
3569\f
3570
c56634ba
MD
3571;;; {Load debug extension code if debug extensions present.}
3572;;;
3573;;; *fixme* This is a temporary solution.
3574;;;
0f2d19dd 3575
c56634ba
MD
3576(if (memq 'debug-extensions *features*)
3577 (define-module (guile) :use-module (ice-9 debug))
3578 (define-module (guile)))