3 ;;;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
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.
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.
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.
21 ;;; This file is the first thing loaded into Guile. It adds many mundane
22 ;;; definitions and a few that are interesting.
24 ;;; The module system (hence the hierarchical namespace) are defined in this
29 ;;; During Guile development, we want to use debugging evaluator and record
30 ;;; positions of source expressions in boot-9.scm by default.
32 (debug-options-interface (cons 'debug (debug-options-interface)))
33 (read-options-interface (cons 'positions (read-options-interface)))
40 (if (not (memq sym *features*))
41 (set! *features* (cons sym *features*))))
46 (primitive-load-path "ice-9/r4rs.scm")
49 ;;; {Simple Debugging Tools}
53 ;; peek takes any number of arguments, writes them to the
54 ;; current ouput port, and returns the last argument.
55 ;; It is handy to wrap around an expression to look at
56 ;; a value each time is evaluated, e.g.:
58 ;; (+ 10 (troublesome-fn))
59 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
62 (define (peek . stuff)
67 (car (last-pair stuff)))
71 (define (warn . stuff)
72 (with-output-to-port (current-error-port)
75 (display ";;; WARNING ")
78 (car (last-pair stuff)))))
81 ;;; {apply and call-with-current-continuation}
83 ;;; These turn syntax, @apply and @call-with-current-continuation,
87 (set! apply (lambda (fun . args) (@apply fun (apply:nconc2last args))))
88 (define (call-with-current-continuation proc)
89 (@call-with-current-continuation proc))
93 ;;; {Trivial Functions}
97 (define (1+ n) (+ n 1))
98 (define (-1+ n) (+ n -1))
100 (define return-it noop)
101 (define (and=> value thunk) (and value (thunk value)))
102 (define (make-hash-table k) (make-vector k '()))
104 ;;; apply-to-args is functionally redunant with apply and, worse,
105 ;;; is less general than apply since it only takes two arguments.
107 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
108 ;;; perform binding in many circumstances when the "let" family of
109 ;;; of forms don't cut it. E.g.:
111 ;;; (apply-to-args (return-3d-mouse-coords)
116 (define (apply-to-args args fn) (apply fn args))
122 (define (ipow-by-squaring x k acc proc)
123 (cond ((zero? k) acc)
124 ((= 1 k) (proc acc x))
125 (else (logical:ipow-by-squaring (proc x x)
127 (if (even? k) acc (proc acc x))
130 (define string-character-length string-length)
134 ;; A convenience function for combining flag bits. Like logior, but
135 ;; handles the cases of 0 and 1 arguments.
137 (define (flags . args)
140 ((null? (cdr args)) (car args))
141 (else (apply logior args))))
144 ;;; {Symbol Properties}
147 (define (symbol-property sym prop)
148 (let ((pair (assoc prop (symbol-pref sym))))
149 (and pair (cdr pair))))
151 (define (set-symbol-property! sym prop val)
152 (let ((pair (assoc prop (symbol-pref sym))))
155 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
157 (define (symbol-property-remove! sym prop)
158 (let ((pair (assoc prop (symbol-pref sym))))
160 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
167 (define uniform-vector? array?)
168 (define make-uniform-vector dimensions->uniform-array)
169 ; (define uniform-vector-ref array-ref)
170 (define (uniform-vector-set! u i o)
171 (uniform-vector-set1! u o i))
172 (define uniform-vector-fill! array-fill!)
173 (define uniform-vector-read! uniform-array-read!)
174 (define uniform-vector-write uniform-array-write)
176 (define (make-array fill . args)
177 (dimensions->uniform-array args () fill))
178 (define (make-uniform-array prot . args)
179 (dimensions->uniform-array args prot))
180 (define (list->array ndim lst)
181 (list->uniform-array ndim '() lst))
182 (define (list->uniform-vector prot lst)
183 (list->uniform-array 1 prot lst))
184 (define (array-shape a)
185 (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
186 (array-dimensions a))))
192 (define (symbol->keyword symbol)
193 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
195 (define (keyword->symbol kw)
196 (let ((sym (keyword-dash-symbol kw)))
197 (string->symbol (substring sym 1 (length sym)))))
199 (define (kw-arg-ref args kw)
200 (let ((rem (member kw args)))
201 (and rem (pair? (cdr rem)) (cadr rem))))
205 ;;; MDJ 960919 <djurfeldt@nada.kth.se>: This code will probably be
206 ;;; removed before the first release of Guile. Later releases may
207 ;;; contain more fancy printing code.
209 (define (print obj . args)
210 (let ((default-args (list (current-output-port) 0 0 default-print-style #f)))
211 (apply-to-args (append args (list-cdr-ref default-args (length args)))
212 (lambda (port depth length style table)
214 ((and table (print-table-ref table obj))
215 ((print-style-tag-hook style 'eq-val)
216 obj port depth length style table))
218 (and table (print-table-add! table obj))
220 ((print-style-max-depth? style depth)
221 ((print-style-excess-depth-hook style)))
222 ((print-style-max-length? style length)
223 ((print-style-excess-length-hook style)))
225 ((print-style-hook style obj)
226 obj port depth length style table)))))))))
228 (define (make-print-style) (make-vector 59 '()))
230 (define (extend-print-style! style utag printer)
231 (hashq-set! style utag printer))
233 (define (print-style-hook style obj)
234 (let ((type-tag (tag obj)))
235 (or (hashq-ref style type-tag)
236 (hashq-ref style (logand type-tag 255))
239 (define (print-style-tag-hook style type-tag)
240 (or (hashq-ref style type-tag)
243 (define (print-style-max-depth? style d) #f)
244 (define (print-style-max-length? style l) #f)
245 (define (print-style-excess-length-hook style)
246 (hashq-ref style 'excess-length-hook))
247 (define (print-style-excess-depth-hook style)
248 (hashq-ref style 'excess-depth-hook))
250 (define (make-print-table) (make-vector 59 '()))
251 (define (print-table-ref table obj) (hashq-ref table obj))
252 (define (print-table-add! table obj) (hashq-set! table obj (gensym 'ref)))
254 (define (print-obj obj port depth length style table) (write obj port))
256 (define (print-pair pair port depth length style table)
260 (print (car pair) port (+ 1 depth) 0 style table)
263 ((and (pair? (cdr pair))
265 (not (print-table-ref table (cdr pair)))))
267 (display #\space port)
268 (print (cdr pair) port depth (+ 1 length) style table))
270 ((null? (cdr pair)) (display #\) port))
272 (else (display " . " port)
273 (print (cdr pair) port (+ 1 depth) 0
275 (display #\) port))))
277 (define (print-vector obj port depth length style table)
280 ((weak-key-hash-table? obj) (display "#wh(" port))
281 ((weak-value-hash-table? obj) (display "#whv(" port))
282 ((doubly-weak-hash-table? obj) (display "#whd(" port))
283 (else (display "#(" port))))
285 (if (< length (vector-length obj))
286 (print (vector-ref obj length) port (+ 1 depth) 0 style table))
289 ((>= (+ 1 length) (vector-length obj)) (display #\) port))
290 (else (display #\space port)
291 (print obj port depth
295 (define default-print-style (make-print-style))
297 (extend-print-style! default-print-style utag_vector print-vector)
298 (extend-print-style! default-print-style utag_wvect print-vector)
299 (extend-print-style! default-print-style utag_pair print-pair)
300 (extend-print-style! default-print-style 'eq-val
301 (lambda (obj port depth length style table)
306 (display (print-table-ref table obj))))))
312 (define record-type-vtable (make-vtable-vtable "prpr" 0))
314 (define (record-type? obj)
315 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
317 (define (make-record-type type-name fields . opt)
318 (let ((printer-fn (and (pair? opt) (car opt))))
319 (let ((struct (make-struct record-type-vtable 0
322 (map (lambda (f) "pw") fields)))
324 (copy-tree fields))))
325 ;; !!! leaks printer functions
326 ;; MDJ 960919 <djurfeldt@nada.kth.se>: *fixme* need to make it
327 ;; possible to print records nicely.
329 ; (extend-print-style! default-print-style
330 ; (logior utag_struct_base (ash (struct-vtable-tag struct) 8))
334 (define (record-type-name obj)
335 (if (record-type? obj)
336 (struct-ref obj struct-vtable-offset)
337 (error 'not-a-record-type obj)))
339 (define (record-type-fields obj)
340 (if (record-type? obj)
341 (struct-ref obj (+ 1 struct-vtable-offset))
342 (error 'not-a-record-type obj)))
344 (define (record-constructor rtd . opt)
345 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
346 (eval `(lambda ,field-names
347 (make-struct ',rtd 0 ,@(map (lambda (f)
348 (if (memq f field-names)
351 (record-type-fields rtd)))))))
353 (define (record-predicate rtd)
354 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
356 (define (record-accessor rtd field-name)
357 (let* ((pos (list-index (record-type-fields rtd) field-name)))
359 (error 'no-such-field field-name))
361 (and (eq? ',rtd (record-type-descriptor obj))
362 (struct-ref obj ,pos))))))
364 (define (record-modifier rtd field-name)
365 (let* ((pos (list-index (record-type-fields rtd) field-name)))
367 (error 'no-such-field field-name))
368 (eval `(lambda (obj val)
369 (and (eq? ',rtd (record-type-descriptor obj))
370 (struct-set! obj ,pos val))))))
373 (define (record? obj)
374 (and (struct? obj) (record-type? (struct-vtable obj))))
376 (define (record-type-descriptor obj)
379 (error 'not-a-record obj)))
387 (define (->bool x) (not (not x)))
393 (define (symbol-append . args)
394 (string->symbol (apply string-append args)))
396 (define (list->symbol . args)
397 (string->symbol (apply list->string args)))
399 (define (symbol . args)
400 (string->symbol (apply string args)))
402 (define (obarray-symbol-append ob . args)
403 (string->obarray-symbol (apply string-append ob args)))
405 (define obarray-gensym
407 (lambda (obarray . opt)
409 (set! opt '(%%gensym)))
410 (let loop ((proposed-name (apply string-append opt)))
411 (if (string->obarray-symbol obarray proposed-name #t)
412 (loop (apply string-append (append opt (begin (set! n (1+ n)) (list (number->string n))))))
413 (string->obarray-symbol obarray proposed-name))))))
415 (define (gensym . args) (apply obarray-gensym #f args))
421 (define (list-index l k)
427 (loop (+ n 1) (cdr l))))))
429 (define (make-list n init)
430 (let loop ((answer '())
434 (loop (cons init answer) (- n 1)))))
438 ;;; {and-map, or-map, and map-in-order}
440 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
441 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
442 ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
447 ;; Apply f to successive elements of l until exhaustion or f returns #f.
448 ;; If returning early, return #f. Otherwise, return the last value returned
449 ;; by f. If f has never been called because l is empty, return #t.
451 (define (and-map f lst)
452 (let loop ((result #t)
457 (loop (f (car l)) (cdr l))))))
461 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
462 ;; If returning early, return the return value of f.
464 (define (or-map f lst)
465 (let loop ((result #f)
469 (loop (f (car l)) (cdr l))))))
473 ;; Like map, but guaranteed to process the list in order.
475 (define (map-in-order fn l)
479 (map-in-order fn (cdr l)))))
483 ;;; !!!! these should be implemented using Tcl commands, not fports.
486 (define (feature? feature)
487 (and (memq feature *features*) #t))
489 ;; Using the vector returned by stat directly is probably not a good
490 ;; idea (it could just as well be a record). Hence some accessors.
491 (define (stat:dev f) (vector-ref f 0))
492 (define (stat:ino f) (vector-ref f 1))
493 (define (stat:mode f) (vector-ref f 2))
494 (define (stat:nlink f) (vector-ref f 3))
495 (define (stat:uid f) (vector-ref f 4))
496 (define (stat:gid f) (vector-ref f 5))
497 (define (stat:rdev f) (vector-ref f 6))
498 (define (stat:size f) (vector-ref f 7))
499 (define (stat:atime f) (vector-ref f 8))
500 (define (stat:mtime f) (vector-ref f 9))
501 (define (stat:ctime f) (vector-ref f 10))
502 (define (stat:blksize f) (vector-ref f 11))
503 (define (stat:blocks f) (vector-ref f 12))
505 ;; derived from stat mode.
506 (define (stat:type f) (vector-ref f 13))
507 (define (stat:perms f) (vector-ref f 14))
510 (if (feature? 'posix)
514 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
516 (if port (begin (close-port port) #t)
519 (define file-is-directory?
520 (if (feature? 'i/o-extensions)
522 (eq? (stat:type (stat str)) 'directory))
526 (let ((port (catch 'system-error
527 (lambda () (open-file (string-append str "/.")
530 (if port (begin (close-port port) #t)
533 (define (has-suffix? str suffix)
534 (let ((sufl (string-length suffix))
535 (sl (string-length str)))
537 (string=? (substring str (- sl sufl) sl) suffix))))
543 (define (error . args)
546 (scm-error 'misc-error #f "?" #f #f)
547 (let loop ((msg "%s")
549 (if (not (null? rest))
550 (loop (string-append msg " %S")
552 (scm-error 'misc-error #f msg args #f)))))
554 (define (scm-error key subr message args rest)
555 (throw key subr message args rest))
557 ;; bad-throw is the hook that is called upon a throw to a an unhandled
558 ;; key (unless the throw has four arguments, in which case
559 ;; it's usually interpreted as an error throw.)
560 ;; If the key has a default handler (a throw-handler-default property),
561 ;; it is applied to the throw.
563 (define (bad-throw key . args)
564 (let ((default (symbol-property key 'throw-handler-default)))
565 (or (and default (apply default key args))
566 (apply error "unhandled-exception:" key args))))
569 ;; A number of internally defined error types were represented
570 ;; as integers. Here is the mapping to symbolic names
571 ;; and error messages.
573 ;(define %%system-errors
574 ; '((-1 UNKNOWN "Unknown error")
575 ; (0 ARGn "Wrong type argument to ")
576 ; (1 ARG1 "Wrong type argument in position 1 to ")
577 ; (2 ARG2 "Wrong type argument in position 2 to ")
578 ; (3 ARG3 "Wrong type argument in position 3 to ")
579 ; (4 ARG4 "Wrong type argument in position 4 to ")
580 ; (5 ARG5 "Wrong type argument in position 5 to ")
581 ; (6 ARG5 "Wrong type argument in position 5 to ")
582 ; (7 ARG5 "Wrong type argument in position 5 to ")
583 ; (8 WNA "Wrong number of arguments to ")
584 ; (9 OVFLOW "Numerical overflow to ")
585 ; (10 OUTOFRANGE "Argument out of range to ")
586 ; (11 NALLOC "Could not allocate to ")
587 ; (12 STACK_OVFLOW "Stack overflow")
588 ; (13 EXIT "Exit (internal error?).")
589 ; (14 HUP_SIGNAL "hang-up")
590 ; (15 INT_SIGNAL "user interrupt")
591 ; (16 FPE_SIGNAL "arithmetic error")
592 ; (17 BUS_SIGNAL "bus error")
593 ; (18 SEGV_SIGNAL "segmentation violation")
594 ; (19 ALRM_SIGNAL "alarm")
595 ; (20 GC_SIGNAL "gc")
596 ; (21 TICK_SIGNAL "tick")))
599 (define (timer-thunk) #t)
600 (define (gc-thunk) #t)
601 (define (alarm-thunk) #t)
603 (define (signal-handler n)
605 ;; these numbers are set in libguile, not the same as those
606 ;; interned in posix.c for SIGSEGV etc.
608 (signal-messages `((14 . "hang-up")
609 (15 . "user interrupt")
610 (16 . "arithmetic error")
612 (18 . "segmentation violation"))))
614 ((= n 21) (unmask-signals) (timer-thunk))
615 ((= n 20) (unmask-signals) (gc-thunk))
616 ((= n 19) (unmask-signals) (alarm-thunk))
617 (else (set! the-last-stack
619 (list-ref (list %hup-thunk
626 (set! stack-saved? #t)
627 (if (not (and (memq 'debug (debug-options-interface))
628 (eq? (stack-id the-last-stack) 'repl-stack)))
629 (set! the-last-stack #f))
631 (let ((sig-pair (assoc n signal-messages)))
632 (scm-error 'error-signal #f
634 (cons n "Unknown signal: %s")))
641 ;;; {Non-polymorphic versions of POSIX functions}
643 (define (getgrnam name) (getgr name))
644 (define (getgrgid id) (getgr id))
645 (define (gethostbyaddr addr) (gethost addr))
646 (define (gethostbyname name) (gethost name))
647 (define (getnetbyaddr addr) (getnet addr))
648 (define (getnetbyname name) (getnet name))
649 (define (getprotobyname name) (getproto name))
650 (define (getprotobynumber addr) (getproto addr))
651 (define (getpwnam name) (getpw name))
652 (define (getpwuid uid) (getpw uid))
653 (define (getservbyname name proto) (%getserv name proto))
654 (define (getservbyport port proto) (%getserv port proto))
655 (define (endgrent) (setgr))
656 (define (endhostent) (sethost))
657 (define (endnetent) (setnet))
658 (define (endprotoent) (setproto))
659 (define (endpwent) (setpw))
660 (define (endservent) (setserv))
661 (define (file-position . args) (apply ftell args))
662 (define (file-set-position . args) (apply fseek args))
663 (define (getgrent) (getgr))
664 (define (gethostent) (gethost))
665 (define (getnetent) (getnet))
666 (define (getprotoent) (getproto))
667 (define (getpwent) (getpw))
668 (define (getservent) (getserv))
669 (define (reopen-file . args) (apply freopen args))
670 (define (setgrent arg) (setgr arg))
671 (define (sethostent arg) (sethost arg))
672 (define (setnetent arg) (setnet arg))
673 (define (setprotoent arg) (setproto arg))
674 (define (setpwent arg) (setpw arg))
675 (define (setservent arg) (setserv arg))
677 (define (move->fdes port fd)
678 (primitive-move->fdes port fd)
679 (set-port-revealed! port 1)
682 (define (release-port-handle port)
683 (let ((revealed (port-revealed port)))
685 (set-port-revealed! port (- revealed 1)))))
691 ;;; Here for backward compatability
693 (define scheme-file-suffix (lambda () ".scm"))
695 (define (in-vicinity vicinity file)
696 (let ((tail (let ((len (string-length vicinity)))
698 (string-ref vicinity (- len 1))))))
699 (string-append vicinity
700 (if (eq? tail #\/) "" "/")
704 ;;; {Loading by paths}
706 ;;; Load a Scheme source file named NAME, searching for it in the
707 ;;; directories listed in %load-path, and applying each of the file
708 ;;; name extensions listed in %load-extensions.
709 (define (load-from-path name)
710 (start-stack 'load-stack
711 (primitive-load-path name #t read-sharp)))
715 ;;; {Transcendental Functions}
717 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
718 ;;; Copyright (C) 1992, 1993 Jerry D. Hedden.
719 ;;; See the file `COPYING' for terms applying to this program.
723 (if (real? z) ($exp z)
724 (make-polar ($exp (real-part z)) (imag-part z))))
727 (if (and (real? z) (>= z 0))
729 (make-rectangular ($log (magnitude z)) (angle z))))
733 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
735 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
738 (let ((integer-expt integer-expt))
741 (integer-expt z1 z2))
742 ((and (real? z2) (real? z1) (>= z1 0))
745 (exp (* z2 (log z1))))))))
748 (if (real? z) ($sinh z)
749 (let ((x (real-part z)) (y (imag-part z)))
750 (make-rectangular (* ($sinh x) ($cos y))
751 (* ($cosh x) ($sin y))))))
753 (if (real? z) ($cosh z)
754 (let ((x (real-part z)) (y (imag-part z)))
755 (make-rectangular (* ($cosh x) ($cos y))
756 (* ($sinh x) ($sin y))))))
758 (if (real? z) ($tanh z)
759 (let* ((x (* 2 (real-part z)))
760 (y (* 2 (imag-part z)))
761 (w (+ ($cosh x) ($cos y))))
762 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
765 (if (real? z) ($asinh z)
766 (log (+ z (sqrt (+ (* z z) 1))))))
769 (if (and (real? z) (>= z 1))
771 (log (+ z (sqrt (- (* z z) 1))))))
774 (if (and (real? z) (> z -1) (< z 1))
776 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
779 (if (real? z) ($sin z)
780 (let ((x (real-part z)) (y (imag-part z)))
781 (make-rectangular (* ($sin x) ($cosh y))
782 (* ($cos x) ($sinh y))))))
784 (if (real? z) ($cos z)
785 (let ((x (real-part z)) (y (imag-part z)))
786 (make-rectangular (* ($cos x) ($cosh y))
787 (- (* ($sin x) ($sinh y)))))))
789 (if (real? z) ($tan z)
790 (let* ((x (* 2 (real-part z)))
791 (y (* 2 (imag-part z)))
792 (w (+ ($cos x) ($cosh y))))
793 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
796 (if (and (real? z) (>= z -1) (<= z 1))
798 (* -i (asinh (* +i z)))))
801 (if (and (real? z) (>= z -1) (<= z 1))
803 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
807 (if (real? z) ($atan z)
808 (/ (log (/ (- +i z) (+ +i z))) +2i))
814 ;;; {User Settable Hooks}
816 ;;; Parts of the C code check the bindings of these variables.
819 (define ticks-interrupt #f)
820 (define user-interrupt #f)
821 (define alarm-interrupt #f)
822 (define out-of-storage #f)
823 (define could-not-open #f)
824 (define end-of-program #f)
826 (define arithmetic-error #f)
827 (define read-sharp #f)
831 ;;; {Reader Extensions}
834 ;;; Reader code for various "#c" forms.
837 (define (parse-path-symbol s)
838 (define (separate-fields-discarding-char ch str ret)
839 (let loop ((fields '())
842 ((string-rindex str ch)
843 => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 pos)) fields)
844 (make-shared-substring str 0 pos))))
845 (else (ret (cons str fields))))))
846 (separate-fields-discarding-char #\/
849 (map string->symbol fields))))
852 (define (%read-sharp c port)
854 (error "unknown # object" c))
857 ((#\/) (let ((look (peek-char port)))
858 (if (or (eof-object? look)
860 (or (char-whitespace? look)
861 (string-index ")" look))))
863 (parse-path-symbol (read port #t read-sharp)))))
864 ((#\') (read port #t read-sharp))
865 ((#\.) (eval (read port #t read-sharp)))
866 ((#\b) (read:uniform-vector #t port))
867 ((#\a) (read:uniform-vector #\a port))
868 ((#\u) (read:uniform-vector 1 port))
869 ((#\e) (read:uniform-vector -1 port))
870 ((#\s) (read:uniform-vector 1.0 port))
871 ((#\i) (read:uniform-vector 1/3 port))
872 ((#\c) (read:uniform-vector 0+i port))
873 ((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
877 (define (read:array digit port)
878 (define chr0 (char->integer #\0))
879 (let ((rank (let readnum ((val (- (char->integer digit) chr0)))
880 (if (char-numeric? (peek-char port))
881 (readnum (+ (* 10 val)
882 (- (char->integer (read-char port)) chr0)))
884 (prot (if (eq? #\( (peek-char port))
886 (let ((c (read-char port)))
894 (else (error "read:array unknown option " c)))))))
895 (if (eq? (peek-char port) #\()
896 (list->uniform-array rank prot (read port #t read-sharp))
897 (error "read:array list not found"))))
899 (define (read:uniform-vector proto port)
900 (if (eq? #\( (peek-char port))
901 (list->uniform-array 1 proto (read port #t read-sharp))
902 (error "read:uniform-vector list not found")))
905 (define read-sharp (lambda a (apply %read-sharp a)))
912 ; mystery integers passed dynamic root error handlers
913 (define repl-quit -1)
914 (define repl-abort -2)
918 ;;; {Command Line Options}
921 (define (get-option argv kw-opts kw-args return)
926 ((or (not (eq? #\- (string-ref (car argv) 0)))
927 (eq? (string-length (car argv)) 1))
928 (return 'normal-arg (car argv) (cdr argv)))
930 ((eq? #\- (string-ref (car argv) 1))
931 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
932 (string-length (car argv))))
933 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
934 (kw-opt? (member kw kw-opts))
935 (kw-arg? (member kw kw-args))
936 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
937 (substring (car argv)
939 (string-length (car argv))))
941 (begin (set! argv (cdr argv)) (car argv))))))
942 (if (or kw-opt? kw-arg?)
943 (return kw arg (cdr argv))
944 (return 'usage-error kw (cdr argv)))))
947 (let* ((char (substring (car argv) 1 2))
948 (kw (symbol->keyword char)))
952 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
953 (new-argv (if (= 0 (string-length rest-car))
955 (cons (string-append "-" rest-car) (cdr argv)))))
956 (return kw #f new-argv)))
959 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
960 (arg (if (= 0 (string-length rest-car))
963 (new-argv (if (= 0 (string-length rest-car))
966 (return kw arg new-argv)))
968 (else (return 'usage-error kw argv)))))))
970 (define (for-next-option proc argv kw-opts kw-args)
971 (let loop ((argv argv))
972 (get-option argv kw-opts kw-args
973 (lambda (opt opt-arg argv)
974 (and opt (proc opt opt-arg argv loop))))))
976 (define (display-usage-report kw-desc)
979 (or (eq? (car kw) #t)
982 (help (cadr opt-desc))
983 (opts (car opt-desc))
984 (opts-proper (if (string? (car opts)) (cdr opts) opts))
985 (arg-name (if (string? (car opts))
986 (string-append "<" (car opts) ">")
988 (left-part (string-append
989 (with-output-to-string
991 (map (lambda (x) (display (keyword-symbol x)) (display " "))
994 (middle-part (if (and (< (length left-part) 30)
995 (< (length help) 40))
996 (make-string (- 30 (length left-part)) #\ )
999 (display middle-part)
1006 (define (delq-all! obj l)
1007 (let ((answer (cons '() l)))
1008 (let loop ((pos answer))
1010 ((null? (cdr pos)) (cdr answer))
1011 ((eq? (cadr pos) obj) (set-cdr! pos (cddr pos))
1013 (else (loop (cdr pos)))))))
1015 (define (transform-usage-lambda cases)
1016 (let* ((raw-usage (delq! 'else (map car cases)))
1017 (usage-sans-specials (map (lambda (x)
1018 (or (and (not (list? x)) x)
1019 (and (symbol? (car x)) #t)
1020 (and (boolean? (car x)) #t)
1023 (usage-desc (delq-all! #t usage-sans-specials))
1024 (kw-desc (map car usage-desc))
1025 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1026 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1027 (transmogrified-cases (map (lambda (case)
1028 (cons (let ((opts (car case)))
1029 (if (or (boolean? opts) (eq? 'else opts))
1032 ((symbol? (car opts)) opts)
1033 ((boolean? (car opts)) opts)
1034 ((string? (caar opts)) (cdar opts))
1035 (else (car opts)))))
1038 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1040 (let %next-arg ((%argv %argv))
1044 (lambda (%opt %arg %new-argv)
1046 ,@ transmogrified-cases))))))))
1051 ;;; {Low Level Modules}
1053 ;;; These are the low level data structures for modules.
1055 ;;; !!! warning: The interface to lazy binder procedures is going
1056 ;;; to be changed in an incompatible way to permit all the basic
1057 ;;; module ops to be virtualized.
1059 ;;; (make-module size use-list lazy-binding-proc) => module
1060 ;;; module-{obarray,uses,binder}[|-set!]
1061 ;;; (module? obj) => [#t|#f]
1062 ;;; (module-locally-bound? module symbol) => [#t|#f]
1063 ;;; (module-bound? module symbol) => [#t|#f]
1064 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1065 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1066 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1067 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1068 ;;; (module-symbol-binding module symbol opt-value)
1069 ;;; => [ <obj> | opt-value | an error occurs ]
1070 ;;; (module-make-local-var! module symbol) => #<variable...>
1071 ;;; (module-add! module symbol var) => unspecified
1072 ;;; (module-remove! module symbol) => unspecified
1073 ;;; (module-for-each proc module) => unspecified
1074 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1075 ;;; (set-current-module module) => unspecified
1076 ;;; (current-module) => #<module...>
1081 ;;; {Printing Modules}
1082 ;; This is how modules are printed. You can re-define it.
1084 (define (%print-module mod port depth length style table)
1086 (display (or (module-kind mod) "module") port)
1087 (let ((name (module-name mod)))
1091 (display name port))))
1093 (display (number->string (object-address mod) 16) port)
1098 ;; A module is characterized by an obarray in which local symbols
1099 ;; are interned, a list of modules, "uses", from which non-local
1100 ;; bindings can be inherited, and an optional lazy-binder which
1101 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1102 ;; bindings that would otherwise not be found locally in the module.
1105 (make-record-type 'module '(obarray uses binder eval-closure name kind)
1108 ;; make-module &opt size uses binder
1110 ;; Create a new module, perhaps with a particular size of obarray,
1111 ;; initial uses list, or binding procedure.
1116 (define (parse-arg index default)
1117 (if (> (length args) index)
1118 (list-ref args index)
1121 (if (> (length args) 3)
1122 (error "Too many args to make-module." args))
1124 (let ((size (parse-arg 0 1021))
1125 (uses (parse-arg 1 '()))
1126 (binder (parse-arg 2 #f)))
1128 (if (not (integer? size))
1129 (error "Illegal size to make-module." size))
1130 (if (not (and (list? uses)
1131 (and-map module? uses)))
1132 (error "Incorrect use list." uses))
1133 (if (and binder (not (procedure? binder)))
1135 "Lazy-binder expected to be a procedure or #f." binder))
1137 (let ((module (module-constructor (make-vector size '())
1138 uses binder #f #f #f)))
1140 ;; We can't pass this as an argument to module-constructor,
1141 ;; because we need it to close over a pointer to the module
1143 (set-module-eval-closure! module
1144 (lambda (symbol define?)
1146 (module-make-local-var! module symbol)
1147 (module-variable module symbol))))
1151 (define module-constructor (record-constructor module-type))
1152 (define module-obarray (record-accessor module-type 'obarray))
1153 (define set-module-obarray! (record-modifier module-type 'obarray))
1154 (define module-uses (record-accessor module-type 'uses))
1155 (define set-module-uses! (record-modifier module-type 'uses))
1156 (define module-binder (record-accessor module-type 'binder))
1157 (define set-module-binder! (record-modifier module-type 'binder))
1158 (define module-eval-closure (record-accessor module-type 'eval-closure))
1159 (define set-module-eval-closure! (record-modifier module-type 'eval-closure))
1160 (define module-name (record-accessor module-type 'name))
1161 (define set-module-name! (record-modifier module-type 'name))
1162 (define module-kind (record-accessor module-type 'kind))
1163 (define set-module-kind! (record-modifier module-type 'kind))
1164 (define module? (record-predicate module-type))
1167 (define (eval-in-module exp module)
1168 (eval2 exp (module-eval-closure module)))
1171 ;;; {Module Searching in General}
1173 ;;; We sometimes want to look for properties of a symbol
1174 ;;; just within the obarray of one module. If the property
1175 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1176 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1179 ;;; Other times, we want to test for a symbol property in the obarray
1180 ;;; of M and, if it is not found there, try each of the modules in the
1181 ;;; uses list of M. This is the normal way of testing for some
1182 ;;; property, so we state these properties without qualification as
1183 ;;; in: ``The symbol 'fnord is interned in module M because it is
1184 ;;; interned locally in module M2 which is a member of the uses list
1188 ;; module-search fn m
1190 ;; return the first non-#f result of FN applied to M and then to
1191 ;; the modules in the uses of m, and so on recursively. If all applications
1192 ;; return #f, then so does this function.
1194 (define (module-search fn m v)
1197 (or (module-search fn (car pos) v)
1200 (loop (module-uses m))))
1203 ;;; {Is a symbol bound in a module?}
1205 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1206 ;;; of S in M has been set to some well-defined value.
1209 ;; module-locally-bound? module symbol
1211 ;; Is a symbol bound (interned and defined) locally in a given module?
1213 (define (module-locally-bound? m v)
1214 (let ((var (module-local-variable m v)))
1216 (variable-bound? var))))
1218 ;; module-bound? module symbol
1220 ;; Is a symbol bound (interned and defined) anywhere in a given module
1223 (define (module-bound? m v)
1224 (module-search module-locally-bound? m v))
1226 ;;; {Is a symbol interned in a module?}
1228 ;;; Symbol S in Module M is interned if S occurs in
1229 ;;; of S in M has been set to some well-defined value.
1231 ;;; It is possible to intern a symbol in a module without providing
1232 ;;; an initial binding for the corresponding variable. This is done
1234 ;;; (module-add! module symbol (make-undefined-variable))
1236 ;;; In that case, the symbol is interned in the module, but not
1237 ;;; bound there. The unbound symbol shadows any binding for that
1238 ;;; symbol that might otherwise be inherited from a member of the uses list.
1241 (define (module-obarray-get-handle ob key)
1242 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1244 (define (module-obarray-ref ob key)
1245 ((if (symbol? key) hashq-ref hash-ref) ob key))
1247 (define (module-obarray-set! ob key val)
1248 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1250 (define (module-obarray-remove! ob key)
1251 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1253 ;; module-symbol-locally-interned? module symbol
1255 ;; is a symbol interned (not neccessarily defined) locally in a given module
1256 ;; or its uses? Interned symbols shadow inherited bindings even if
1257 ;; they are not themselves bound to a defined value.
1259 (define (module-symbol-locally-interned? m v)
1260 (not (not (module-obarray-get-handle (module-obarray m) v))))
1262 ;; module-symbol-interned? module symbol
1264 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1265 ;; or its uses? Interned symbols shadow inherited bindings even if
1266 ;; they are not themselves bound to a defined value.
1268 (define (module-symbol-interned? m v)
1269 (module-search module-symbol-locally-interned? m v))
1272 ;;; {Mapping modules x symbols --> variables}
1275 ;; module-local-variable module symbol
1276 ;; return the local variable associated with a MODULE and SYMBOL.
1278 ;;; This function is very important. It is the only function that can
1279 ;;; return a variable from a module other than the mutators that store
1280 ;;; new variables in modules. Therefore, this function is the location
1281 ;;; of the "lazy binder" hack.
1283 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1284 ;;; to a variable, return that variable object.
1286 ;;; If the symbols is not found at first, but the module has a lazy binder,
1287 ;;; then try the binder.
1289 ;;; If the symbol is not found at all, return #f.
1291 (define (module-local-variable m v)
1294 (let ((b (module-obarray-ref (module-obarray m) v)))
1295 (or (and (variable? b) b)
1296 (and (module-binder m)
1297 ((module-binder m) m v #f)))))
1300 ;; module-variable module symbol
1302 ;; like module-local-variable, except search the uses in the
1303 ;; case V is not found in M.
1305 (define (module-variable m v)
1306 (module-search module-local-variable m v))
1309 ;;; {Mapping modules x symbols --> bindings}
1311 ;;; These are similar to the mapping to variables, except that the
1312 ;;; variable is dereferenced.
1315 ;; module-symbol-binding module symbol opt-value
1317 ;; return the binding of a variable specified by name within
1318 ;; a given module, signalling an error if the variable is unbound.
1319 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1320 ;; return OPT-VALUE.
1322 (define (module-symbol-local-binding m v . opt-val)
1323 (let ((var (module-local-variable m v)))
1326 (if (not (null? opt-val))
1328 (error "Locally unbound variable." v)))))
1330 ;; module-symbol-binding module symbol opt-value
1332 ;; return the binding of a variable specified by name within
1333 ;; a given module, signalling an error if the variable is unbound.
1334 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1335 ;; return OPT-VALUE.
1337 (define (module-symbol-binding m v . opt-val)
1338 (let ((var (module-variable m v)))
1341 (if (not (null? opt-val))
1343 (error "Unbound variable." v)))))
1347 ;;; {Adding Variables to Modules}
1352 ;; module-make-local-var! module symbol
1354 ;; ensure a variable for V in the local namespace of M.
1355 ;; If no variable was already there, then create a new and uninitialzied
1358 (define (module-make-local-var! m v)
1359 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1360 (and (variable? b) b))
1361 (and (module-binder m)
1362 ((module-binder m) m v #t))
1364 (let ((answer (make-undefined-variable v)))
1365 (module-obarray-set! (module-obarray m) v answer)
1368 ;; module-add! module symbol var
1370 ;; ensure a particular variable for V in the local namespace of M.
1372 (define (module-add! m v var)
1373 (if (not (variable? var))
1374 (error "Bad variable to module-add!" var))
1375 (module-obarray-set! (module-obarray m) v var))
1379 ;; make sure that a symbol is undefined in the local namespace of M.
1381 (define (module-remove! m v)
1382 (module-obarray-remove! (module-obarray m) v))
1384 (define (module-clear! m)
1385 (vector-fill! (module-obarray m) '()))
1387 ;; MODULE-FOR-EACH -- exported
1389 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1391 (define (module-for-each proc module)
1392 (let ((obarray (module-obarray module)))
1393 (do ((index 0 (+ index 1))
1394 (end (vector-length obarray)))
1398 (proc (car bucket) (cdr bucket)))
1399 (vector-ref obarray index)))))
1402 (define (module-map proc module)
1403 (let* ((obarray (module-obarray module))
1404 (end (vector-length obarray)))
1412 (map (lambda (bucket)
1413 (proc (car bucket) (cdr bucket)))
1414 (vector-ref obarray i))
1418 ;;; {Low Level Bootstrapping}
1423 ;; A root module uses the symhash table (the system's privileged
1424 ;; obarray). Being inside a root module is like using SCM without
1425 ;; any module system.
1429 (define (root-module-closure m s define?)
1430 (let ((bi (and (symbol-interned? #f s)
1431 (builtin-variable s))))
1433 (or define? (variable-bound? bi))
1435 (module-add! m s bi)
1438 (define (make-root-module)
1439 (make-module 1019 '() root-module-closure))
1444 ;; An scm module is a module into which the lazy binder copies
1445 ;; variable bindings from the system symhash table. The mapping is
1446 ;; one way only; newly introduced bindings in an scm module are not
1447 ;; copied back into the system symhash table (and can be used to override
1448 ;; bindings from the symhash table).
1451 (define (make-scm-module)
1452 (make-module 1019 '()
1453 (lambda (m s define?)
1454 (let ((bi (and (symbol-interned? #f s)
1455 (builtin-variable s))))
1457 (variable-bound? bi)
1459 (module-add! m s bi)
1467 (define the-module #f)
1469 ;; set-current-module module
1471 ;; set the current module as viewed by the normalizer.
1473 (define (set-current-module m)
1476 (set! *top-level-lookup-closure* (module-eval-closure the-module))
1477 (set! *top-level-lookup-closure* #f)))
1482 ;; return the current module as viewed by the normalizer.
1484 (define (current-module) the-module)
1486 ;;; {Module-based Loading}
1489 (define (save-module-excursion thunk)
1490 (let ((inner-module (current-module))
1492 (dynamic-wind (lambda ()
1493 (set! outer-module (current-module))
1494 (set-current-module inner-module)
1495 (set! inner-module #f))
1498 (set! inner-module (current-module))
1499 (set-current-module outer-module)
1500 (set! outer-module #f)))))
1502 (define basic-load load)
1504 (define (load-module . args)
1505 (save-module-excursion (lambda () (apply basic-load args))))
1509 ;;; {MODULE-REF -- exported}
1511 ;; Returns the value of a variable called NAME in MODULE or any of its
1512 ;; used modules. If there is no such variable, then if the optional third
1513 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1515 (define (module-ref module name . rest)
1516 (let ((variable (module-variable module name)))
1517 (if (and variable (variable-bound? variable))
1518 (variable-ref variable)
1520 (error "No variable named" name 'in module)
1521 (car rest) ; default value
1524 ;; MODULE-SET! -- exported
1526 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1527 ;; to VALUE; if there is no such variable, an error is signaled.
1529 (define (module-set! module name value)
1530 (let ((variable (module-variable module name)))
1532 (variable-set! variable value)
1533 (error "No variable named" name 'in module))))
1535 ;; MODULE-DEFINE! -- exported
1537 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1538 ;; variable, it is added first.
1540 (define (module-define! module name value)
1541 (let ((variable (module-local-variable module name)))
1543 (variable-set! variable value)
1544 (module-add! module name (make-variable value name)))))
1546 ;; MODULE-USE! module interface
1548 ;; Add INTERFACE to the list of interfaces used by MODULE.
1550 (define (module-use! module interface)
1551 (set-module-uses! module
1552 (cons interface (delq! interface (module-uses module)))))
1555 ;;; {Recursive Namespaces}
1558 ;;; A hierarchical namespace emerges if we consider some module to be
1559 ;;; root, and variables bound to modules as nested namespaces.
1561 ;;; The routines in this file manage variable names in hierarchical namespace.
1562 ;;; Each variable name is a list of elements, looked up in successively nested
1565 ;;; (nested-ref some-root-module '(foo bar baz))
1566 ;;; => <value of a variable named baz in the module bound to bar in
1567 ;;; the module bound to foo in some-root-module>
1572 ;;; ;; a-root is a module
1573 ;;; ;; name is a list of symbols
1575 ;;; nested-ref a-root name
1576 ;;; nested-set! a-root name val
1577 ;;; nested-define! a-root name val
1578 ;;; nested-remove! a-root name
1581 ;;; (current-module) is a natural choice for a-root so for convenience there are
1584 ;;; local-ref name == nested-ref (current-module) name
1585 ;;; local-set! name val == nested-set! (current-module) name val
1586 ;;; local-define! name val == nested-define! (current-module) name val
1587 ;;; local-remove! name == nested-remove! (current-module) name
1591 (define (nested-ref root names)
1592 (let loop ((cur root)
1596 ((not (module? cur)) #f)
1597 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1599 (define (nested-set! root names val)
1600 (let loop ((cur root)
1602 (if (null? (cdr elts))
1603 (module-set! cur (car elts) val)
1604 (loop (module-ref cur (car elts)) (cdr elts)))))
1606 (define (nested-define! root names val)
1607 (let loop ((cur root)
1609 (if (null? (cdr elts))
1610 (module-define! cur (car elts) val)
1611 (loop (module-ref cur (car elts)) (cdr elts)))))
1613 (define (nested-remove! root names)
1614 (let loop ((cur root)
1616 (if (null? (cdr elts))
1617 (module-remove! cur (car elts))
1618 (loop (module-ref cur (car elts)) (cdr elts)))))
1620 (define (local-ref names) (nested-ref (current-module) names))
1621 (define (local-set! names val) (nested-set! (current-module) names val))
1622 (define (local-define names val) (nested-define! (current-module) names val))
1623 (define (local-remove names) (nested-remove! (current-module) names))
1629 ;;; The root of conventionally named objects not directly in the top level.
1632 ;;; #/app/modules/guile
1634 ;;; The directory of all modules and the standard root module.
1637 (define (module-public-interface m) (module-ref m '%module-public-interface #f))
1638 (define (set-module-public-interface! m i) (module-define! m '%module-public-interface i))
1639 (define the-root-module (make-root-module))
1640 (define the-scm-module (make-scm-module))
1641 (set-module-public-interface! the-root-module the-scm-module)
1642 (set-module-name! the-root-module 'the-root-module)
1643 (set-module-name! the-scm-module 'the-scm-module)
1645 (set-current-module the-root-module)
1647 (define app (make-module 31))
1648 (local-define '(app modules) (make-module 31))
1649 (local-define '(app modules guile) the-root-module)
1651 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1653 (define (resolve-module name . maybe-autoload)
1654 (let ((full-name (append '(app modules) name)))
1655 (let ((already (local-ref full-name)))
1658 (if (or (null? maybe-autoload) (car maybe-autoload))
1659 (try-module-autoload name))
1660 (make-modules-in (current-module) full-name))))))
1662 (define (beautify-user-module! module)
1663 (if (not (module-public-interface module))
1664 (let ((interface (make-module 31)))
1665 (set-module-name! interface (module-name module))
1666 (set-module-kind! interface 'interface)
1667 (set-module-public-interface! module interface)))
1668 (if (not (memq the-scm-module (module-uses module)))
1669 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1671 (define (make-modules-in module name)
1675 ((module-ref module (car name) #f) => (lambda (m) (make-modules-in m (cdr name))))
1676 (else (let ((m (make-module 31)))
1677 (set-module-kind! m 'directory)
1678 (set-module-name! m (car name))
1679 (module-define! module (car name) m)
1680 (make-modules-in m (cdr name)))))))
1682 (define (resolve-interface name)
1683 (let ((module (resolve-module name)))
1684 (and module (module-public-interface module))))
1687 (define %autoloader-developer-mode #t)
1689 (define (process-define-module args)
1690 (let* ((module-id (car args))
1691 (module (resolve-module module-id #f))
1693 (beautify-user-module! module)
1694 (let loop ((kws kws)
1695 (reversed-interfaces '()))
1697 (for-each (lambda (interface)
1698 (module-use! module interface))
1699 reversed-interfaces)
1702 (if (not (pair? (cdr kws)))
1703 (error "unrecognized defmodule argument" kws))
1704 (let* ((used-name (cadr kws))
1705 (used-module (resolve-module used-name)))
1706 (if (not (module-ref used-module '%module-public-interface #f))
1708 ((if %autoloader-developer-mode warn error)
1709 "no code for module" (module-name used-module))
1710 (beautify-user-module! used-module)))
1711 (let ((interface (module-public-interface used-module)))
1713 (error "missing interface for use-module" used-module))
1714 (loop (cddr kws) (cons interface reversed-interfaces)))))
1716 (error "unrecognized defmodule argument" kws)))))
1719 ;;; {Autoloading modules}
1721 (define autoloads-in-progress '())
1723 (define (try-module-autoload module-name)
1725 (define (sfx name) (string-append name (scheme-file-suffix)))
1726 (let* ((reverse-name (reverse module-name))
1727 (name (car reverse-name))
1728 (dir-hint-module-name (reverse (cdr reverse-name)))
1729 (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
1730 (resolve-module dir-hint-module-name #f)
1731 (and (not (autoload-done-or-in-progress? dir-hint name))
1734 (lambda () (autoload-in-progress! dir-hint name))
1736 (let loop ((dirs %load-path))
1737 (and (not (null? dirs))
1739 (let ((d (car dirs))
1743 (in-vicinity dir-hint name)
1744 (in-vicinity dir-hint (sfx name)))))
1745 (and (or-map (lambda (f)
1746 (let ((full (in-vicinity d f)))
1748 (and (file-exists? full)
1749 (not (file-is-directory? full))
1751 (save-module-excursion
1753 (load (string-append
1760 (loop (cdr dirs))))))
1761 (lambda () (set-autoloaded! dir-hint name didit)))
1764 (define autoloads-done '((guile . guile)))
1766 (define (autoload-done-or-in-progress? p m)
1767 (let ((n (cons p m)))
1768 (->bool (or (member n autoloads-done)
1769 (member n autoloads-in-progress)))))
1771 (define (autoload-done! p m)
1772 (let ((n (cons p m)))
1773 (set! autoloads-in-progress
1774 (delete! n autoloads-in-progress))
1775 (or (member n autoloads-done)
1776 (set! autoloads-done (cons n autoloads-done)))))
1778 (define (autoload-in-progress! p m)
1779 (let ((n (cons p m)))
1780 (set! autoloads-done
1781 (delete! n autoloads-done))
1782 (set! autoloads-in-progress (cons n autoloads-in-progress))))
1784 (define (set-autoloaded! p m done?)
1786 (autoload-done! p m)
1787 (let ((n (cons p m)))
1788 (set! autoloads-done (delete! n autoloads-done))
1789 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
1798 (define macro-table (make-weak-key-hash-table 523))
1799 (define xformer-table (make-weak-key-hash-table 523))
1801 (define (defmacro? m) (hashq-ref macro-table m))
1802 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
1803 (define (defmacro-transformer m) (hashq-ref xformer-table m))
1804 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
1806 (define defmacro:transformer
1808 (let* ((xform (lambda (exp env)
1809 (copy-tree (apply f (cdr exp)))))
1810 (a (procedure->memoizing-macro xform)))
1811 (assert-defmacro?! a)
1812 (set-defmacro-transformer! a f)
1817 (let ((defmacro-transformer
1818 (lambda (name parms . body)
1819 (let ((transformer `(lambda ,parms ,@body)))
1821 (,(lambda (transformer)
1822 (defmacro:transformer transformer))
1824 (defmacro:transformer defmacro-transformer)))
1826 (define defmacro:syntax-transformer
1830 (copy-tree (apply f (cdr exp)))))))
1832 (define (macroexpand-1 e)
1834 ((pair? e) (let* ((a (car e))
1835 (val (and (symbol? a) (defined? a) (eval a))))
1837 (apply (defmacro-transformer val) (cdr e))
1841 (define (macroexpand e)
1843 ((pair? e) (let* ((a (car e))
1844 (val (and (symbol? a) (defined? a) (eval a))))
1846 (macroexpand (apply (defmacro-transformer val) (cdr e)))
1851 (let ((*gensym-counter* -1))
1853 (set! *gensym-counter* (+ *gensym-counter* 1))
1855 (string-append "scm:G" (number->string *gensym-counter*))))))
1863 (define (repl read evaler print)
1864 (let loop ((source (read (current-input-port) #t read-sharp)))
1865 (print (evaler source))
1866 (loop (read (current-input-port) #t read-sharp))))
1868 ;; A provisional repl that acts like the SCM repl:
1870 (define scm-repl-silent #f)
1871 (define (assert-repl-silence v) (set! scm-repl-silent v))
1873 (define *unspecified* (if #f #f))
1874 (define (unspecified? v) (eq? v *unspecified*))
1876 (define scm-repl-print-unspecified #f)
1877 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
1879 (define scm-repl-verbose #f)
1880 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
1882 (define scm-repl-prompt #t)
1883 (define (assert-repl-prompt v) (set! scm-repl-prompt v))
1885 (define the-prompt-string "guile> ")
1887 (define (error-catching-loop thunk)
1888 (define (loop first)
1895 (lambda () (unmask-signals))
1899 ;; This line is needed because mark doesn't do closures quite right.
1900 ;; Unreferenced locals should be collected.
1903 (let loop ((v (thunk)))
1906 (lambda () (mask-signals))))
1910 (apply throw args))))
1912 (lambda (key . args)
1919 (apply throw 'switch-repl args))
1922 ;; This is one of the closures that require
1923 ;; (set! first #f) above
1927 (display "ABORT: " (current-error-port))
1928 (write args (current-error-port))
1929 (newline (current-error-port))
1930 (if (and (not has-shown-debugger-hint?)
1931 (not (memq 'backtrace (debug-options-interface)))
1932 (stack? the-last-stack))
1934 (newline (current-error-port))
1935 (display "Type \"(backtrace)\" to get more information,
1936 or type \"$\" to enter the debugger.\n" (current-error-port))
1937 (set! has-shown-debugger-hint? #t)))
1938 (set! stack-saved? #f)))
1941 ;; This is the other cons-leak closure...
1943 (cond ((= (length args) 4)
1944 (apply handle-system-error key args))
1946 (apply bad-throw key args))))))))))
1947 (and next (loop next))))
1948 (loop (lambda () #t)))
1950 (define the-last-stack #f)
1951 (define stack-saved? #f)
1953 (define (save-stack . narrowing)
1954 (cond (stack-saved?)
1955 ((not (memq 'debug (debug-options-interface)))
1956 (set! the-last-stack #f)
1957 (set! stack-saved? #t))
1959 (set! the-last-stack
1962 (apply make-stack #t save-stack eval narrowing))
1964 (apply make-stack #t save-stack gsubr-apply narrowing))
1966 (apply make-stack #t save-stack tk-stack-mark narrowing))
1968 (apply make-stack #t save-stack narrowing))
1969 (else (let ((id (stack-id #t)))
1970 (and (procedure? id)
1971 (apply make-stack #t save-stack id narrowing))))))
1972 (set! stack-saved? #t))))
1974 (define before-error-hook #f)
1975 (define after-error-hook #f)
1976 (define before-backtrace-hook #f)
1977 (define after-backtrace-hook #f)
1979 (define has-shown-debugger-hint? #f)
1981 (define (handle-system-error key . args)
1982 (let ((cep (current-error-port)))
1983 (cond ((not (stack? the-last-stack)))
1984 ((memq 'backtrace (debug-options-interface))
1985 (and before-backtrace-hook (before-backtrace-hook))
1987 (display-backtrace the-last-stack cep)
1989 (and after-backtrace-hook (after-backtrace-hook))))
1990 (and before-error-hook (before-error-hook))
1991 (apply display-error the-last-stack cep args)
1992 (and after-error-hook (after-error-hook))
1994 (throw 'abort key)))
1996 (define (quit . args)
1997 (apply throw 'quit args))
1999 (define has-shown-backtrace-hint? #f)
2005 (display-backtrace the-last-stack (current-output-port))
2007 (if (and (not has-shown-backtrace-hint?)
2008 (not (memq 'backtrace (debug-options-interface))))
2011 "Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2012 automatically if an error occurs in the future.\n")
2013 (set! has-shown-backtrace-hint? #t))))
2014 (display "No backtrace available.\n")))
2016 (define (error-catching-repl r e p)
2017 (error-catching-loop (lambda () (p (e (r))))))
2019 (define (gc-run-time)
2020 (cdr (assq 'gc-time-taken (gc-stats))))
2022 (define before-read-hook #f)
2023 (define after-read-hook #f)
2025 (define (scm-style-repl)
2029 (repl-report-reset (lambda () #f))
2030 (repl-report-start-timing (lambda ()
2031 (set! start-gc-rt (gc-run-time))
2032 (set! start-rt (get-internal-run-time))))
2033 (repl-report (lambda ()
2035 (display (inexact->exact
2036 (* 1000 (/ (- (get-internal-run-time) start-rt)
2037 internal-time-units-per-second))))
2039 (display (inexact->exact
2040 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2041 internal-time-units-per-second))))
2042 (display " msec in gc)\n")))
2046 (display the-prompt-string)
2048 (repl-report-reset)))
2049 (and before-read-hook (before-read-hook))
2050 (let ((val (read (current-input-port) #t read-sharp)))
2051 (and after-read-hook (after-read-hook))
2052 (if (eof-object? val)
2054 (if scm-repl-verbose
2057 (display ";;; EOF -- quitting")
2062 (-eval (lambda (sourc)
2063 (repl-report-start-timing)
2064 (start-stack 'repl-stack (eval sourc))))
2066 (-print (lambda (result)
2067 (if (not scm-repl-silent)
2069 (if (or scm-repl-print-unspecified
2070 (not (unspecified? result)))
2074 (if scm-repl-verbose
2079 (if scm-repl-verbose
2081 (display ";;; QUIT executed, repl exitting")
2087 (if scm-repl-verbose
2089 (display ";;; ABORT executed.")
2092 (repl -read -eval -print))))
2094 (error-catching-repl -read
2098 (define (stand-alone-repl)
2099 (let ((oport (current-input-port)))
2100 (set-current-input-port *stdin*)
2102 (set-current-input-port oport)))
2106 ;;; {IOTA functions: generating lists of numbers}
2108 (define (reverse-iota n) (if (> n 0) (cons (1- n) (reverse-iota (1- n))) '()))
2109 (define (iota n) (list-reverse! (reverse-iota n)))
2114 ;;; with `continue' and `break'.
2117 (defmacro while (cond . body)
2118 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2119 (break (lambda val (apply throw 'break val))))
2121 (lambda () (continue))
2122 (lambda v (cadr v)))))
2130 ;; actually....hobbit might be able to hack these with a little
2134 (defmacro define-macro (first . rest)
2135 (let ((name (if (symbol? first) first (car first)))
2139 `(lambda ,(cdr first) ,@rest))))
2140 `(define ,name (defmacro:transformer ,transformer))))
2143 (defmacro define-syntax-macro (first . rest)
2144 (let ((name (if (symbol? first) first (car first)))
2148 `(lambda ,(cdr first) ,@rest))))
2149 `(define ,name (defmacro:syntax-transformer ,transformer))))
2151 ;;; {Module System Macros}
2154 (defmacro define-module args
2155 `(let* ((process-define-module process-define-module)
2156 (set-current-module set-current-module)
2157 (module (process-define-module ',args)))
2158 (set-current-module module)
2161 (define define-private define)
2163 (defmacro define-public args
2165 (error "bad syntax" (list 'define-public args)))
2166 (define (defined-name n)
2169 ((pair? n) (defined-name (car n)))
2172 ((null? args) (syntax))
2174 (#t (let ((name (defined-name (car args))))
2176 (let ((public-i (module-public-interface (current-module))))
2177 ;; Make sure there is a local variable:
2179 (module-define! (current-module)
2181 (module-ref (current-module) ',name #f))
2183 ;; Make sure that local is exported:
2185 (module-add! public-i ',name (module-variable (current-module) ',name)))
2187 ;; Now (re)define the var normally.
2189 (define-private ,@ args))))))
2193 (defmacro defmacro-public args
2195 (error "bad syntax" (list 'defmacro-public args)))
2196 (define (defined-name n)
2201 ((null? args) (syntax))
2203 (#t (let ((name (defined-name (car args))))
2205 (let ((public-i (module-public-interface (current-module))))
2206 ;; Make sure there is a local variable:
2208 (module-define! (current-module)
2210 (module-ref (current-module) ',name #f))
2212 ;; Make sure that local is exported:
2214 (module-add! public-i ',name (module-variable (current-module) ',name)))
2216 ;; Now (re)define the var normally.
2218 (defmacro ,@ args))))))
2223 (define load load-module)
2224 ;(define (load . args)
2225 ; (start-stack 'load-stack (apply load-module args)))
2229 ;;; {I/O functions for Tcl channels (disabled)}
2231 ;; (define in-ch (get-standard-channel TCL_STDIN))
2232 ;; (define out-ch (get-standard-channel TCL_STDOUT))
2233 ;; (define err-ch (get-standard-channel TCL_STDERR))
2235 ;; (define inp (%make-channel-port in-ch "r"))
2236 ;; (define outp (%make-channel-port out-ch "w"))
2237 ;; (define errp (%make-channel-port err-ch "w"))
2239 ;; (define %system-char-ready? char-ready?)
2241 ;; (define (char-ready? p)
2242 ;; (if (not (channel-port? p))
2243 ;; (%system-char-ready? p)
2244 ;; (let* ((channel (%channel-port-channel p))
2245 ;; (old-blocking (channel-option-ref channel :blocking)))
2247 ;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking "0"))
2248 ;; (lambda () (not (eof-object? (peek-char p))))
2249 ;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking old-blocking))))))
2251 ;; (define (top-repl)
2252 ;; (with-input-from-port inp
2254 ;; (with-output-to-port outp
2256 ;; (with-error-to-port errp
2258 ;; (scm-style-repl))))))))
2260 ;; (set-current-input-port inp)
2261 ;; (set-current-output-port outp)
2262 ;; (set-current-error-port errp)
2264 (define (top-repl) (scm-style-repl))
2266 (defmacro false-if-exception (expr)
2267 `(catch #t (lambda () ,expr)
2271 ;;; {Calling Conventions}
2272 (define-module (ice-9 calling))
2276 ;;; This file contains a number of macros that support
2277 ;;; common calling conventions.
2280 ;;; with-excursion-function <vars> proc
2281 ;;; <vars> is an unevaluated list of names that are bound in the caller.
2282 ;;; proc is a procedure, called:
2283 ;;; (proc excursion)
2285 ;;; excursion is a procedure isolates all changes to <vars>
2286 ;;; in the dynamic scope of the call to proc. In other words,
2287 ;;; the values of <vars> are saved when proc is entered, and when
2288 ;;; proc returns, those values are restored. Values are also restored
2289 ;;; entering and leaving the call to proc non-locally, such as using
2290 ;;; call-with-current-continuation, error, or throw.
2292 (defmacro-public with-excursion-function (vars proc)
2293 `(,proc ,(excursion-function-syntax vars)))
2297 ;;; with-getter-and-setter <vars> proc
2298 ;;; <vars> is an unevaluated list of names that are bound in the caller.
2299 ;;; proc is a procedure, called:
2300 ;;; (proc getter setter)
2302 ;;; getter and setter are procedures used to access
2303 ;;; or modify <vars>.
2305 ;;; setter, called with keywords arguments, modifies the named
2306 ;;; values. If "foo" and "bar" are among <vars>, then:
2308 ;;; (setter :foo 1 :bar 2)
2309 ;;; == (set! foo 1 bar 2)
2311 ;;; getter, called with just keywords, returns
2312 ;;; a list of the corresponding values. For example,
2313 ;;; if "foo" and "bar" are among the <vars>, then
2315 ;;; (getter :foo :bar)
2316 ;;; => (<value-of-foo> <value-of-bar>)
2318 ;;; getter, called with no arguments, returns a list of all accepted
2319 ;;; keywords and the corresponding values. If "foo" and "bar" are
2320 ;;; the *only* <vars>, then:
2323 ;;; => (:foo <value-of-bar> :bar <value-of-foo>)
2325 ;;; The unusual calling sequence of a getter supports too handy
2328 ;;; (apply setter (getter)) ;; save and restore
2330 ;;; (apply-to-args (getter :foo :bar) ;; fetch and bind
2331 ;;; (lambda (foo bar) ....))
2333 ;;; ;; [ "apply-to-args" is just like two-argument "apply" except that it
2334 ;;; ;; takes its arguments in a different order.
2337 (defmacro-public with-getter-and-setter (vars proc)
2338 `(,proc ,@ (getter-and-setter-syntax vars)))
2340 ;;; with-getter vars proc
2341 ;;; A short-hand for a call to with-getter-and-setter.
2342 ;;; The procedure is called:
2345 (defmacro-public with-getter (vars proc)
2346 `(,proc ,(car (getter-and-setter-syntax vars))))
2349 ;;; with-delegating-getter-and-setter <vars> get-delegate set-delegate proc
2350 ;;; Compose getters and setters.
2352 ;;; <vars> is an unevaluated list of names that are bound in the caller.
2354 ;;; get-delegate is called by the new getter to extend the set of
2355 ;;; gettable variables beyond just <vars>
2356 ;;; set-delegate is called by the new setter to extend the set of
2357 ;;; gettable variables beyond just <vars>
2359 ;;; proc is a procedure that is called
2360 ;;; (proc getter setter)
2362 (defmacro-public with-delegating-getter-and-setter (vars get-delegate set-delegate proc)
2363 `(,proc ,@ (delegating-getter-and-setter-syntax vars get-delegate set-delegate)))
2366 ;;; with-delegating-getter-and-setter <vars> get-delegate set-delegate proc
2367 ;;; <vars> is an unevaluated list of names that are bound in the caller.
2370 ;;; (proc excursion getter setter)
2373 ;;; with-getter-and-setter
2374 ;;; with-excursion-function
2376 (defmacro-public with-excursion-getter-and-setter (vars proc)
2377 `(,proc ,(excursion-function-syntax vars)
2378 ,@ (getter-and-setter-syntax vars)))
2381 (define (excursion-function-syntax vars)
2382 (let ((saved-value-names (map gensym vars))
2383 (tmp-var-name (gensym 'temp))
2384 (swap-fn-name (gensym 'swap))
2385 (thunk-name (gensym 'thunk)))
2386 `(lambda (,thunk-name)
2387 (letrec ((,tmp-var-name #f)
2389 (lambda () ,@ (map (lambda (n sn) `(set! ,tmp-var-name ,n ,n ,sn ,sn ,tmp-var-name))
2390 vars saved-value-names)))
2391 ,@ (map (lambda (sn n) `(,sn ,n)) saved-value-names vars))
2398 (define (getter-and-setter-syntax vars)
2399 (let ((args-name (gensym 'args))
2400 (an-arg-name (gensym 'an-arg))
2401 (new-val-name (gensym 'new-value))
2402 (loop-name (gensym 'loop))
2403 (kws (map symbol->keyword vars)))
2404 (list `(lambda ,args-name
2405 (let ,loop-name ((,args-name ,args-name))
2406 (if (null? ,args-name)
2409 `(let ((all-vals (,loop-name ',kws)))
2410 (let ,loop-name ((vals all-vals)
2414 `(,(car kws) ,(car vals) ,@(,loop-name (cdr vals) (cdr kws)))))))
2415 (map (lambda (,an-arg-name)
2418 (map (lambda (kw v) `((,kw) ,v)) kws vars)
2419 `((else (throw 'bad-get-option ,an-arg-name))))))
2423 (let ,loop-name ((,args-name ,args-name))
2424 (or (null? ,args-name)
2425 (null? (cdr ,args-name))
2426 (let ((,an-arg-name (car ,args-name))
2427 (,new-val-name (cadr ,args-name)))
2430 (map (lambda (kw v) `((,kw) (set! ,v ,new-val-name))) kws vars)
2431 `((else (throw 'bad-set-option ,an-arg-name)))))
2432 (,loop-name (cddr ,args-name)))))))))
2434 (define (delegating-getter-and-setter-syntax vars get-delegate set-delegate)
2435 (let ((args-name (gensym 'args))
2436 (an-arg-name (gensym 'an-arg))
2437 (new-val-name (gensym 'new-value))
2438 (loop-name (gensym 'loop))
2439 (kws (map symbol->keyword vars)))
2440 (list `(lambda ,args-name
2441 (let ,loop-name ((,args-name ,args-name))
2442 (if (null? ,args-name)
2446 `(let ((all-vals (,loop-name ',kws)))
2447 (let ,loop-name ((vals all-vals)
2451 `(,(car kws) ,(car vals) ,@(,loop-name (cdr vals) (cdr kws)))))))
2453 (map (lambda (,an-arg-name)
2456 (map (lambda (kw v) `((,kw) ,v)) kws vars)
2457 `((else (car (,get-delegate ,an-arg-name)))))))
2461 (let ,loop-name ((,args-name ,args-name))
2462 (or (null? ,args-name)
2463 (null? (cdr ,args-name))
2464 (let ((,an-arg-name (car ,args-name))
2465 (,new-val-name (cadr ,args-name)))
2468 (map (lambda (kw v) `((,kw) (set! ,v ,new-val-name))) kws vars)
2469 `((else (,set-delegate ,an-arg-name ,new-val-name)))))
2470 (,loop-name (cddr ,args-name)))))))))
2475 ;;; with-configuration-getter-and-setter <vars-etc> proc
2477 ;;; Create a getter and setter that can trigger arbitrary computation.
2479 ;;; <vars-etc> is a list of variable specifiers, explained below.
2482 ;;; (proc getter setter)
2484 ;;; Each element of the <vars-etc> list is of the form:
2486 ;;; (<var> getter-hook setter-hook)
2488 ;;; Both hook elements are evaluated; the variable name is not.
2489 ;;; Either hook may be #f or procedure.
2491 ;;; A getter hook is a thunk that returns a value for the corresponding
2492 ;;; variable. If omitted (#f is passed), the binding of <var> is
2495 ;;; A setter hook is a procedure of one argument that accepts a new value
2496 ;;; for the corresponding variable. If omitted, the binding of <var>
2497 ;;; is simply set using set!.
2499 (defmacro-public with-configuration-getter-and-setter (vars-etc proc)
2500 `((lambda (simpler-get simpler-set body-proc)
2501 (with-delegating-getter-and-setter ()
2502 simpler-get simpler-set body-proc))
2506 ,@(map (lambda (v) `((,(symbol->keyword (car v)))
2509 (else `(list ,(car v))))))
2512 (lambda (kw new-val)
2514 ,@(map (lambda (v) `((,(symbol->keyword (car v)))
2516 ((caddr v) => (lambda (proc) `(,proc new-val)))
2517 (else `(set! ,(car v) new-val)))))
2522 (defmacro-public with-delegating-configuration-getter-and-setter (vars-etc delegate-get delegate-set proc)
2523 `((lambda (simpler-get simpler-set body-proc)
2524 (with-delegating-getter-and-setter ()
2525 simpler-get simpler-set body-proc))
2529 ,@(append! (map (lambda (v) `((,(symbol->keyword (car v)))
2532 (else `(list ,(car v))))))
2534 `((else (,delegate-get kw))))))
2536 (lambda (kw new-val)
2538 ,@(append! (map (lambda (v) `((,(symbol->keyword (car v)))
2540 ((caddr v) => (lambda (proc) `(,proc new-val)))
2541 (else `(set! ,(car v) new-val)))))
2543 `((else (,delegate-set kw new-val))))))
2548 ;;; let-configuration-getter-and-setter <vars-etc> proc
2550 ;;; This procedure is like with-configuration-getter-and-setter (q.v.)
2551 ;;; except that each element of <vars-etc> is:
2553 ;;; (<var> initial-value getter-hook setter-hook)
2555 ;;; Unlike with-configuration-getter-and-setter, let-configuration-getter-and-setter
2556 ;;; introduces bindings for the variables named in <vars-etc>.
2557 ;;; It is short-hand for:
2559 ;;; (let ((<var1> initial-value-1)
2560 ;;; (<var2> initial-value-2)
2562 ;;; (with-configuration-getter-and-setter ((<var1> v1-get v1-set) ...) proc))
2564 (defmacro-public let-with-configuration-getter-and-setter (vars-etc proc)
2565 `(let ,(map (lambda (v) `(,(car v) ,(cadr v))) vars-etc)
2566 (with-configuration-getter-and-setter ,(map (lambda (v) `(,(car v) ,(caddr v) ,(cadddr v))) vars-etc)
2572 ;;; {Implementation of COMMON LISP list functions for Scheme}
2574 (define-module (ice-9 common-list))
2576 ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme
2577 ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer.
2579 ;Permission to copy this software, to redistribute it, and to use it
2580 ;for any purpose is granted, subject to the following restrictions and
2583 ;1. Any copy made of this software must include this copyright notice
2586 ;2. I have made no warrantee or representation that the operation of
2587 ;this software will be error-free, and I am under no obligation to
2588 ;provide any services, by way of maintenance, update, or otherwise.
2590 ;3. In conjunction with products arising from the use of this
2591 ;material, there shall be no use of my name in any advertising,
2592 ;promotional, or sales literature without prior written consent in
2595 ;;;From: hugh@ear.mit.edu (Hugh Secker-Walker)
2596 (define-public (make-list k . init)
2597 (set! init (if (pair? init) (car init)))
2599 (result '() (cons init result)))
2602 (define-public (adjoin e l) (if (memq e l) l (cons e l)))
2604 (define-public (union l1 l2)
2605 (cond ((null? l1) l2)
2607 (else (union (cdr l1) (adjoin (car l1) l2)))))
2609 (define-public (intersection l1 l2)
2610 (cond ((null? l1) l1)
2612 ((memv (car l1) l2) (cons (car l1) (intersection (cdr l1) l2)))
2613 (else (intersection (cdr l1) l2))))
2615 (define-public (set-difference l1 l2)
2616 (cond ((null? l1) l1)
2617 ((memv (car l1) l2) (set-difference (cdr l1) l2))
2618 (else (cons (car l1) (set-difference (cdr l1) l2)))))
2620 (define-public (reduce-init p init l)
2623 (reduce-init p (p init (car l)) (cdr l))))
2625 (define-public (reduce p l)
2627 ((null? (cdr l)) (car l))
2628 (else (reduce-init p (car l) (cdr l)))))
2630 (define-public (some pred l . rest)
2633 (and (not (null? l))
2634 (or (pred (car l)) (mapf (cdr l))))))
2635 (else (let mapf ((l l) (rest rest))
2636 (and (not (null? l))
2637 (or (apply pred (car l) (map car rest))
2638 (mapf (cdr l) (map cdr rest))))))))
2640 (define-public (every pred l . rest)
2644 (and (pred (car l)) (mapf (cdr l))))))
2645 (else (let mapf ((l l) (rest rest))
2647 (and (apply pred (car l) (map car rest))
2648 (mapf (cdr l) (map cdr rest))))))))
2650 (define-public (notany pred . ls) (not (apply some pred ls)))
2652 (define-public (notevery pred . ls) (not (apply every pred ls)))
2654 (define-public (find-if t l)
2655 (cond ((null? l) #f)
2656 ((t (car l)) (car l))
2657 (else (find-if t (cdr l)))))
2659 (define-public (member-if t l)
2660 (cond ((null? l) #f)
2662 (else (member-if t (cdr l)))))
2664 (define-public (remove-if p l)
2665 (cond ((null? l) '())
2666 ((p (car l)) (remove-if p (cdr l)))
2667 (else (cons (car l) (remove-if p (cdr l))))))
2669 (define-public (delete-if! pred list)
2670 (let delete-if ((list list))
2671 (cond ((null? list) '())
2672 ((pred (car list)) (delete-if (cdr list)))
2674 (set-cdr! list (delete-if (cdr list)))
2677 (define-public (delete-if-not! pred list)
2678 (let delete-if ((list list))
2679 (cond ((null? list) '())
2680 ((not (pred (car list))) (delete-if (cdr list)))
2682 (set-cdr! list (delete-if (cdr list)))
2685 (define-public (butlast lst n)
2686 (letrec ((l (- (length lst) n))
2688 (cond ((null? lst) lst)
2690 (cons (car lst) (bl (cdr lst) (+ -1 n))))
2692 (bl lst (if (negative? n)
2693 (slib:error "negative argument to butlast" n)
2696 (define-public (and? . args)
2697 (cond ((null? args) #t)
2698 ((car args) (apply and? (cdr args)))
2701 (define-public (or? . args)
2702 (cond ((null? args) #f)
2704 (else (apply or? (cdr args)))))
2706 (define-public (has-duplicates? lst)
2707 (cond ((null? lst) #f)
2708 ((member (car lst) (cdr lst)) #t)
2709 (else (has-duplicates? (cdr lst)))))
2711 (define-public (list* x . y)
2715 (cons (car x) (list*1 (cdr x)))))
2718 (cons x (list*1 y))))
2721 ;; Apply P to each element of L, returning a list of elts
2722 ;; for which P returns a non-#f value.
2724 (define-public (pick p l)
2729 ((p (car l)) (loop (cons (car l) s) (cdr l)))
2730 (else (loop s (cdr l))))))
2733 ;; Apply P to each element of L, returning a list of the
2734 ;; non-#f return values of P.
2736 (define-public (pick-mappings p l)
2741 ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l))))
2742 (else (loop s (cdr l))))))
2744 (define-public (uniq l)
2747 (let ((u (uniq (cdr l))))
2748 (if (memq (car l) u)
2750 (cons (car l) u)))))
2753 ;;; {Functions for browsing modules}
2755 (define-module (ice-9 ls)
2756 :use-module (ice-9 common-list))
2759 ;;; local-definitions-in root name
2760 ;;; Returns a list of names defined locally in the named
2761 ;;; subdirectory of root.
2762 ;;; definitions-in root name
2763 ;;; Returns a list of all names defined in the named
2764 ;;; subdirectory of root. The list includes alll locally
2765 ;;; defined names as well as all names inherited from a
2766 ;;; member of a use-list.
2768 ;;; A convenient interface for examining the nature of things:
2770 ;;; ls . various-names
2772 ;;; With just one argument, interpret that argument as the
2773 ;;; name of a subdirectory of the current module and
2774 ;;; return a list of names defined there.
2776 ;;; With more than one argument, still compute
2777 ;;; subdirectory lists, but return a list:
2778 ;;; ((<subdir-name> . <names-defined-there>)
2779 ;;; (<subdir-name> . <names-defined-there>)
2783 (define-public (local-definitions-in root names)
2784 (let ((m (nested-ref root names))
2786 (if (not (module? m))
2788 (module-for-each (lambda (k v) (set! answer (cons k answer))) m))
2791 (define-public (definitions-in root names)
2792 (let ((m (nested-ref root names)))
2793 (if (not (module? m))
2796 (cons (local-definitions-in m '())
2797 (map (lambda (m2) (definitions-in m2 '()))
2798 (module-uses m)))))))
2800 (define-public (ls . various-refs)
2802 (if (cdr various-refs)
2804 (cons ref (definitions-in (current-module) ref)))
2806 (definitions-in (current-module) (car various-refs)))))
2808 (define-public (lls . various-refs)
2810 (if (cdr various-refs)
2812 (cons ref (local-definitions-in (current-module) ref)))
2814 (local-definitions-in (current-module) (car various-refs)))))
2816 (define-public (recursive-local-define name value)
2817 (let ((parent (reverse! (cdr (reverse name)))))
2818 (and parent (make-modules-in (current-module) parent))
2819 (local-define name value)))
2823 (define-module (ice-9 q))
2825 ;;;; Copyright (C) 1995 Free Software Foundation, Inc.
2827 ;;;; This program is free software; you can redistribute it and/or modify
2828 ;;;; it under the terms of the GNU General Public License as published by
2829 ;;;; the Free Software Foundation; either version 2, or (at your option)
2830 ;;;; any later version.
2832 ;;;; This program is distributed in the hope that it will be useful,
2833 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2834 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2835 ;;;; GNU General Public License for more details.
2837 ;;;; You should have received a copy of the GNU General Public License
2838 ;;;; along with this software; see the file COPYING. If not, write to
2839 ;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
2843 ;;; Q: Based on the interface to
2845 ;;; "queue.scm" Queues/Stacks for Scheme
2846 ;;; Written by Andrew Wilcox (awilcox@astro.psu.edu) on April 1, 1992.
2852 ;;; A list is just a bunch of cons pairs that follows some constrains, right?
2853 ;;; Association lists are the same. Hash tables are just vectors and association
2854 ;;; lists. You can print them, read them, write them as constants, pun them off as other data
2855 ;;; structures etc. This is good. This is lisp. These structures are fast and compact
2856 ;;; and easy to manipulate arbitrarily because of their simple, regular structure and
2857 ;;; non-disjointedness (associations being lists and so forth).
2859 ;;; So I figured, queues should be the same -- just a "subtype" of cons-pair
2860 ;;; structures in general.
2862 ;;; A queue is a cons pair:
2863 ;;; ( <the-q> . <last-pair> )
2865 ;;; <the-q> is a list of things in the q. New elements go at the end of that list.
2867 ;;; <last-pair> is #f if the q is empty, and otherwise is the last pair of <the-q>.
2869 ;;; q's print nicely, but alas, they do not read well because the eq?-ness of
2870 ;;; <last-pair> and (last-pair <the-q>) is lost by read. The procedure
2874 ;;; recomputes and resets the <last-pair> component of a queue.
2877 (define-public (sync-q! obj) (set-cdr! obj (and (car obj) (last-pair (car obj)))))
2882 (define-public (make-q) (cons '() '()))
2885 ;;; Return true if obj is a Q.
2886 ;;; An object is a queue if it is equal? to '(#f . #f) or
2887 ;;; if it is a pair P with (list? (car P)) and (eq? (cdr P) (last-pair P)).
2889 (define-public (q? obj) (and (pair? obj)
2890 (or (and (null? (car obj))
2894 (eq? (cdr obj) (last-pair (car obj)))))))
2898 (define-public (q-empty? obj) (null? (car obj)))
2901 ;;; Throw a q-empty exception if Q is empty.
2902 (define-public (q-empty-check q) (if (q-empty? q) (throw 'q-empty q)))
2906 ;;; Return the first element of Q.
2907 (define-public (q-front q) (q-empty-check q) (caar q))
2910 ;;; Return the last element of Q.
2911 (define-public (q-rear q) (q-empty-check q) (cadr q))
2914 ;;; Remove all occurences of obj from Q.
2915 (define-public (q-remove! q obj)
2916 (while (memq obj (car q))
2917 (set-car! q (delq! obj (car q))))
2918 (set-cdr! q (last-pair (car q))))
2921 ;;; Add obj to the front of Q
2922 (define-public (q-push! q d)
2923 (let ((h (cons d (car q))))
2929 ;;; Add obj to the rear of Q
2930 (define-public (enq! q d)
2931 (let ((h (cons d '())))
2932 (if (not (null? (cdr q)))
2933 (set-cdr! (cdr q) h)
2938 ;;; Take the front of Q and return it.
2939 (define-public (q-pop! q)
2949 ;;; Take the front of Q and return it.
2950 (define-public deq! q-pop!)
2953 ;;; Return the number of enqueued elements.
2955 (define-public (q-length q) (length (car q)))
2960 ;;; {The runq data structure}
2962 (define-module (ice-9 runq)
2963 :use-module (ice-9 q))
2965 ;;;; Copyright (C) 1996 Free Software Foundation, Inc.
2967 ;;;; This program is free software; you can redistribute it and/or modify
2968 ;;;; it under the terms of the GNU General Public License as published by
2969 ;;;; the Free Software Foundation; either version 2, or (at your option)
2970 ;;;; any later version.
2972 ;;;; This program is distributed in the hope that it will be useful,
2973 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
2974 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2975 ;;;; GNU General Public License for more details.
2977 ;;;; You should have received a copy of the GNU General Public License
2978 ;;;; along with this software; see the file COPYING. If not, write to
2979 ;;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
2984 ;;; One way to schedule parallel computations in a serial environment is
2985 ;;; to explicitly divide each task up into small, finite execution time,
2986 ;;; strips. Then you interleave the execution of strips from various
2987 ;;; tasks to achieve a kind of parallelism. Runqs are a handy data
2988 ;;; structure for this style of programming.
2990 ;;; We use thunks (nullary procedures) and lists of thunks to represent
2991 ;;; strips. By convention, the return value of a strip-thunk must either
2992 ;;; be another strip or the value #f.
2994 ;;; A runq is a procedure that manages a queue of strips. Called with no
2995 ;;; arguments, it processes one strip from the queue. Called with
2996 ;;; arguments, the arguments form a control message for the queue. The
2997 ;;; first argument is a symbol which is the message selector.
2999 ;;; A strip is processed this way: If the strip is a thunk, the thunk is
3000 ;;; called -- if it returns a strip, that strip is added back to the
3001 ;;; queue. To process a strip which is a list of thunks, the CAR of that
3002 ;;; list is called. After a call to that CAR, there are 0, 1, or 2 strips
3003 ;;; -- perhaps one returned by the thunk, and perhaps the CDR of the
3004 ;;; original strip if that CDR is not nil. The runq puts whichever of
3005 ;;; these strips exist back on the queue. (The exact order in which
3006 ;;; strips are put back on the queue determines the scheduling behavior of
3007 ;;; a particular queue -- it's a parameter.)
3014 ;;; (runq-control q msg . args)
3016 ;;; processes in the default way the control messages that
3017 ;;; can be sent to a runq. Q should be an ordinary
3018 ;;; Q (see utils/q.scm).
3020 ;;; The standard runq messages are:
3022 ;;; 'add! strip0 strip1... ;; to enqueue one or more strips
3023 ;;; 'enqueue! strip0 strip1... ;; to enqueue one or more strips
3024 ;;; 'push! strip0 ... ;; add strips to the front of the queue
3025 ;;; 'empty? ;; true if it is
3026 ;;; 'length ;; how many strips in the queue?
3027 ;;; 'kill! ;; empty the queue
3028 ;;; else ;; throw 'not-understood
3030 (define-public (runq-control q msg . args)
3032 ((add!) (for-each (lambda (t) (enq! q t)) args) '*unspecified*)
3033 ((enque!) (for-each (lambda (t) (enq! q t)) args) '*unspecified*)
3034 ((push!) (for-each (lambda (t) (q-push! q t)) args) '*unspecified*)
3035 ((empty?) (q-empty? q))
3036 ((length) (q-length q))
3037 ((kill!) (set! q (make-q)))
3038 (else (throw 'not-understood msg args))))
3040 (define (run-strip thunk) (catch #t thunk (lambda ign (warn 'runq-strip thunk ign) #f)))
3045 ;;; Make a runq that discards all messages except "length", for which
3048 (define-public (make-void-runq)
3052 (lambda (msg . args)
3058 ;;; (make-fair-runq)
3060 ;;; Returns a runq procedure.
3061 ;;; Called with no arguments, the procedure processes one strip from the queue.
3062 ;;; Called with arguments, it uses runq-control.
3064 ;;; In a fair runq, if a strip returns a new strip X, X is added
3065 ;;; to the end of the queue, meaning it will be the last to execute
3066 ;;; of all the remaining procedures.
3068 (define-public (make-fair-runq)
3069 (letrec ((q (make-q))
3073 (apply runq-control q ctl)
3074 (and (not (q-empty? q))
3075 (let ((next-strip (deq! q)))
3077 ((procedure? next-strip) (let ((k (run-strip next-strip)))
3078 (and k (enq! q k))))
3079 ((pair? next-strip) (let ((k (run-strip (car next-strip))))
3081 (if (not (null? (cdr next-strip)))
3082 (enq! q (cdr next-strip)))))
3088 ;;; (make-exclusive-runq)
3090 ;;; Returns a runq procedure.
3091 ;;; Called with no arguments, the procedure processes one strip from the queue.
3092 ;;; Called with arguments, it uses runq-control.
3094 ;;; In an exclusive runq, if a strip W returns a new strip X, X is added
3095 ;;; to the front of the queue, meaning it will be the next to execute
3096 ;;; of all the remaining procedures.
3098 ;;; An exception to this occurs if W was the CAR of a list of strips.
3099 ;;; In that case, after the return value of W is pushed onto the front
3100 ;;; of the queue, the CDR of the list of strips is pushed in front
3101 ;;; of that (if the CDR is not nil). This way, the rest of the thunks
3102 ;;; in the list that contained W have priority over the return value of W.
3104 (define-public (make-exclusive-runq)
3105 (letrec ((q (make-q))
3109 (apply runq-control q ctl)
3110 (and (not (q-empty? q))
3111 (let ((next-strip (deq! q)))
3113 ((procedure? next-strip) (let ((k (run-strip next-strip)))
3114 (and k (q-push! q k))))
3115 ((pair? next-strip) (let ((k (run-strip (car next-strip))))
3116 (and k (q-push! q k)))
3117 (if (not (null? (cdr next-strip)))
3118 (q-push! q (cdr next-strip)))))
3124 ;;; (make-subordinate-runq-to superior basic-inferior)
3126 ;;; Returns a runq proxy for the runq basic-inferior.
3128 ;;; The proxy watches for operations on the basic-inferior that cause
3129 ;;; a transition from a queue length of 0 to a non-zero length and
3130 ;;; vice versa. While the basic-inferior queue is not empty,
3131 ;;; the proxy installs a task on the superior runq. Each strip
3132 ;;; of that task processes N strips from the basic-inferior where
3133 ;;; N is the length of the basic-inferior queue when the proxy
3134 ;;; strip is entered. [Countless scheduling variations are possible.]
3136 (define-public (make-subordinate-runq-to superior-runq basic-runq)
3137 (let ((runq-task (cons #f #f)))
3140 (if (basic-runq 'empty?)
3141 (set-cdr! runq-task #f)
3142 (do ((n (basic-runq 'length) (1- n)))
3148 (let ((answer (basic-runq)))
3153 ((suspend) (set-cdr! runq-task #f))
3154 (else (let ((answer (apply basic-runq ctl)))
3155 (if (and (not (cdr runq-task)) (not (basic-runq 'empty?)))
3157 (set-cdr! runq-task runq-task)
3158 (superior-runq 'add! runq-task)))
3163 ;;; (define fork-strips (lambda args args))
3164 ;;; Return a strip that starts several strips in
3165 ;;; parallel. If this strip is enqueued on a fair
3166 ;;; runq, strips of the parallel subtasks will run
3167 ;;; round-robin style.
3169 (define fork-strips (lambda args args))
3173 ;;; (strip-sequence . strips)
3175 ;;; Returns a new strip which is the concatenation of the argument strips.
3177 (define-public ((strip-sequence . strips))
3178 (let loop ((st (let ((a strips)) (set! strips #f) a)))
3179 (and (not (null? st))
3180 (let ((then ((car st))))
3182 (lambda () (loop (cons then (cdr st))))
3183 (lambda () (loop (cdr st))))))))
3187 ;;; (fair-strip-subtask . initial-strips)
3189 ;;; Returns a new strip which is the synchronos, fair,
3190 ;;; parallel execution of the argument strips.
3194 (define-public (fair-strip-subtask . initial-strips)
3195 (let ((st (make-fair-runq)))
3196 (apply st 'add! initial-strips)
3202 (define-module (ice-9 string-fun))
3206 ;;; Various string funcitons, particularly those that take
3207 ;;; advantage of the "shared substring" capability.
3210 ;;; {String Fun: Dividing Strings Into Fields}
3212 ;;; The names of these functions are very regular.
3213 ;;; Here is a grammar of a call to one of these:
3215 ;;; <string-function-invocation>
3216 ;;; := (<action>-<seperator-disposition>-<seperator-determination> <seperator-param> <str> <ret>)
3218 ;;; <str> = the string
3220 ;;; <ret> = The continuation. String functions generally return
3221 ;;; multiple values by passing them to this procedure.
3223 ;;; <action> = split
3224 ;;; | separate-fields
3226 ;;; "split" means to divide a string into two parts.
3227 ;;; <ret> will be called with two arguments.
3229 ;;; "separate-fields" means to divide a string into as many
3230 ;;; parts as possible. <ret> will be called with
3231 ;;; however many fields are found.
3233 ;;; <seperator-disposition> = before
3237 ;;; "before" means to leave the seperator attached to
3238 ;;; the beginning of the field to its right.
3239 ;;; "after" means to leave the seperator attached to
3240 ;;; the end of the field to its left.
3241 ;;; "discarding" means to discard seperators.
3243 ;;; Other dispositions might be handy. For example, "isolate"
3244 ;;; could mean to treat the separator as a field unto itself.
3246 ;;; <seperator-determination> = char
3249 ;;; "char" means to use a particular character as field seperator.
3250 ;;; "predicate" means to check each character using a particular predicate.
3252 ;;; Other determinations might be handy. For example, "character-set-member".
3254 ;;; <seperator-param> = A parameter that completes the meaning of the determinations.
3255 ;;; For example, if the determination is "char", then this parameter
3256 ;;; says which character. If it is "predicate", the parameter is the
3262 ;;; (separate-fields-discarding-char #\, "foo, bar, baz, , bat" list)
3263 ;;; => ("foo" " bar" " baz" " " " bat")
3265 ;;; (split-after-char #\- 'an-example-of-split list)
3266 ;;; => ("an-" "example-of-split")
3268 ;;; As an alternative to using a determination "predicate", or to trying to do anything
3269 ;;; complicated with these functions, consider using regular expressions.
3272 (define-public (split-after-char char str ret)
3274 ((string-index str char) => 1+)
3275 (else (string-length str)))))
3276 (ret (make-shared-substring str 0 end)
3277 (make-shared-substring str end))))
3279 (define-public (split-before-char char str ret)
3280 (let ((end (or (string-index str char)
3281 (string-length str))))
3282 (ret (make-shared-substring str 0 end)
3283 (make-shared-substring str end))))
3285 (define-public (split-discarding-char char str ret)
3286 (let ((end (string-index str char)))
3289 (ret (make-shared-substring str 0 end)
3290 (make-shared-substring str (1+ end))))))
3292 (define-public (split-after-char-last char str ret)
3294 ((string-rindex str char) => 1+)
3296 (ret (make-shared-substring str 0 end)
3297 (make-shared-substring str end))))
3299 (define-public (split-before-char-last char str ret)
3300 (let ((end (or (string-rindex str char) 0)))
3301 (ret (make-shared-substring str 0 end)
3302 (make-shared-substring str end))))
3304 (define-public (split-discarding-char-last char str ret)
3305 (let ((end (string-rindex str char)))
3308 (ret (make-shared-substring str 0 end)
3309 (make-shared-substring str (1+ end))))))
3311 (define (split-before-predicate pred str ret)
3314 ((= n (length str)) (ret str ""))
3315 ((not (pred (string-ref str n))) (loop (1+ n)))
3316 (else (ret (make-shared-substring str 0 n)
3317 (make-shared-substring str n))))))
3318 (define (split-after-predicate pred str ret)
3321 ((= n (length str)) (ret str ""))
3322 ((not (pred (string-ref str n))) (loop (1+ n)))
3323 (else (ret (make-shared-substring str 0 (1+ n))
3324 (make-shared-substring str (1+ n)))))))
3326 (define (split-discarding-predicate pred str ret)
3329 ((= n (length str)) (ret str ""))
3330 ((not (pred (string-ref str n))) (loop (1+ n)))
3331 (else (ret (make-shared-substring str 0 n)
3332 (make-shared-substring str (1+ n)))))))
3334 (define-public (separate-fields-discarding-char ch str ret)
3335 (let loop ((fields '())
3338 ((string-rindex str ch)
3339 => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 w)) fields)
3340 (make-shared-substring str 0 w))))
3341 (else (ret (cons str fields))))))
3343 (define-public (separate-fields-after-char ch str ret)
3344 (let loop ((fields '())
3347 ((string-rindex str ch)
3348 => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 w)) fields)
3349 (make-shared-substring str 0 (+ 1 w)))))
3350 (else (ret (cons str fields))))))
3352 (define-public (separate-fields-before-char ch str ret)
3353 (let loop ((fields '())
3356 ((string-rindex str ch)
3357 => (lambda (pos) (loop (cons (make-shared-substring str w) fields)
3358 (make-shared-substring str 0 w))))
3359 (else (ret (cons str fields))))))
3362 ;;; {String Fun: String Prefix Predicates}
3366 ;;; (define-public ((string-prefix-predicate pred?) prefix str)
3367 ;;; (and (<= (length prefix) (length str))
3368 ;;; (pred? prefix (make-shared-substring str 0 (length prefix)))))
3370 ;;; (define-public string-prefix=? (string-prefix-predicate string=?))
3373 (define-public ((string-prefix-predicate pred?) prefix str)
3374 (and (<= (length prefix) (length str))
3375 (pred? prefix (make-shared-substring str 0 (length prefix)))))
3377 (define-public string-prefix=? (string-prefix-predicate string=?))
3380 ;;; {String Fun: Strippers}
3382 ;;; <stripper> = sans-<removable-part>
3384 ;;; <removable-part> = surrounding-whitespace
3385 ;;; | trailing-whitespace
3386 ;;; | leading-whitespace
3390 (define-public (sans-surrounding-whitespace s)
3392 (end (string-length s)))
3393 (while (and (< st (string-length s))
3394 (char-whitespace? (string-ref s st)))
3396 (while (and (< 0 end)
3397 (char-whitespace? (string-ref s (1- end))))
3398 (set! end (1- end)))
3401 (make-shared-substring s st end))))
3403 (define-public (sans-trailing-whitespace s)
3405 (end (string-length s)))
3406 (while (and (< 0 end)
3407 (char-whitespace? (string-ref s (1- end))))
3408 (set! end (1- end)))
3411 (make-shared-substring s st end))))
3413 (define-public (sans-leading-whitespace s)
3415 (end (string-length s)))
3416 (while (and (< st (string-length s))
3417 (char-whitespace? (string-ref s st)))
3421 (make-shared-substring s st end))))
3423 (define-public (sans-final-newline str)
3425 ((= 0 (string-length str))
3428 ((char=? #\nl (string-ref str (1- (string-length str))))
3429 (make-shared-substring str 0 (1- (string-length str))))
3433 ;;; {String Fun: has-trailing-newline?}
3436 (define-public (has-trailing-newline? str)
3437 (and (< 0 (string-length str))
3438 (char=? #\nl (string-ref str (1- (string-length str))))))
3442 ;;; {String Fun: with-regexp-parts}
3444 (define-public (with-regexp-parts regexp fields str return fail)
3445 (let ((parts (regexec regexp str fields)))
3448 (apply return parts))))
3451 ;;; {Load debug extension code if debug extensions present.}
3453 ;;; *fixme* This is a temporary solution.
3456 (if (memq 'debug-extensions *features*)
3457 (define-module (guile) :use-module (ice-9 debug)))
3460 ;;; {Load thread code if threads are present.}
3462 ;;; *fixme* This is a temporary solution.
3465 (if (memq 'threads *features*)
3466 (define-module (guile) :use-module (ice-9 threads)))
3469 ;;; {Load emacs interface support if emacs option is given.}
3471 ;;; *fixme* This is a temporary solution.
3474 (if (or (member "-e" (cdr (program-arguments)))
3475 (member "--emacs" (cdr (program-arguments))))
3476 (define-module (guile) :use-module (ice-9 emacs)))
3480 (define-module (guile))
3482 (append! %load-path (cons "." ()))