/usr/share/scheme48-1.9/srfi/srfi-7.scm is in scheme48 1.9-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | ; Part of Scheme 48 1.9. See file COPYING for notices and license.
; Authors: Richard Kelsey, Jonathan Rees, Martin Gasbichler, Mike Sperber
; A command for loading SRFI-7 programs. This is a user command because
; it gets loaded after the command processor is built.
(define-user-command-syntax 'load-srfi-7-program "<name> <filename>"
"load an SRFI-7 program"
'(name filename))
(define-user-command-syntax 'load-srfi-7-script "<name> <filename>"
"load an SRFI-7 script"
'(name filename))
; We create a structure, EVAL it in the configuration package, give
; it a name, and then load it.
(define (load-srfi-7-program name filename)
(load-srfi-7 name filename read-srfi-7-program))
(define (load-srfi-7-script name filename)
(load-srfi-7 name filename read-srfi-7-script))
(define (load-srfi-7 name filename read)
(eval-from-file `((define ,name ,(read filename)))
(config-package)
filename)
(let ((structure (eval name (config-package))))
(note-structure-name! structure name)
(ensure-loaded structure)))
; Add the LOAD-SRFI-7-PROGRAM to the user's command environment.
(environment-define! (user-command-environment)
'load-srfi-7-program
load-srfi-7-program)
(environment-define! (user-command-environment)
'load-srfi-7-script
load-srfi-7-script)
; Read a program from FILENAME and return the code for a structure that
; contains it.
(define (read-srfi-7 filename script?)
(call-with-input-file filename
(lambda (in)
(if script?
(skip-line in))
(let ((program (read in)))
(if (and (pair? program)
(eq? (car program) 'program))
(receive (needed source)
(parse-program program available-srfis)
(if needed
(program->structure-exp needed source)
(assertion-violation 'read-srfi-7
"cannot satisfy program's requirements")))
(assertion-violation 'read-srfi-7 "program not found in file" filename))))))
(define (skip-line port)
(let loop ()
(let ((char (read-char port)))
(if (and (not (eof-object? char))
(not (char=? #\newline char)))
(loop)))))
(define (read-srfi-7-program filename)
(read-srfi-7 filename #f))
(define (read-srfi-7-script filename)
(read-srfi-7 filename #t))
; Returns a STRUCTURE expression for a program that uses the SRFIs listed
; in NEEDED and whose source is SOURCE.
(define (program->structure-exp needed source)
(let ((shadowed (find-shadowed needed)))
`(structure (export)
(open ,(if (null? shadowed)
'scheme
`(modify scheme (hide . ,shadowed)))
. ,needed)
. ,(transform-source source))))
(define (transform-source source)
(map (lambda (source)
(if (eq? (car source)
'code)
(cons 'begin (cdr source))
source))
source))
; Returns a list of the names that SRFIS redefine from Scheme.
(define (find-shadowed srfis)
(apply append (map (lambda (srfi)
(cond ((assq srfi shadowed)
=> cdr)
(else
'())))
srfis)))
;----------------
; Parsing a program to find the source that we will use for it.
;
; The arguments are a PROGRAM form and a list of the names of available SRFIs.
; Two values are returned: a list of needed SRFIs and the program source as
; a list of (files ...) and (code ...) clauses.
;
; This searches through the possible sets of SRFIs to find one that works for
; the program. The EITHER macro is used to try choices. There are only two
; places where choices occur: FEATURE-COND clauses and OR predicates (which
; includes (NOT (AND ...)) predicates).
(define (parse-program program available)
(receive (needed available source)
(with-nondeterminism
(lambda ()
(either (process-clauses (cdr program) '() available)
(values #f #f #f))))
(if needed
(values needed (reverse source))
(values #f #f))))
(define-syntax program
(syntax-rules ()
((program clauses ...)
(receive (needed source)
(parse-program '(program clauses ...) available-srfis)
(if needed
(let ((shadowed (find-shadowed needed)))
(if (not (null? shadowed))
(open-scheme-shadowed! shadowed))
(for-each open-structure (if (null? shadowed)
(cons 'scheme needed)
needed))
(for-each
(lambda (exp) (eval exp (interaction-environment)))
(transform-source source)))
(assertion-violation 'program "cannot satisfy program's requirements"))))))
(define (open-structure name)
(let* ((c (config-package))
(struct (environment-ref c name)))
(ensure-loaded struct)
(package-open! (interaction-environment) (lambda () struct))))
(define (open-scheme-shadowed! shadowed)
(let* ((c (config-package)))
(package-open! (interaction-environment)
(lambda ()
(make-modified-structure
(environment-ref c 'scheme)
`((hide ,@shadowed)))))))
; NEEDED is a list of SRFIs that we already know we need. AVAILABLE is a list
; of other SRFIs that can be used. This returns new needed and available lists
; as well as a list of (files ...) and (code ...) clauses.
;
; This is a simple dispatch on the types of clauses. For REQUIRES and
; FEATURE-COND we call another procedure to check the requirements list
; or cond clauses.
(define (process-clauses clauses needed available)
(let loop ((clauses clauses)
(needed needed)
(available available)
(source '()))
(if (null? clauses)
(values needed available source)
(let ((clause (car clauses)))
(case (car clause)
((requires)
(receive (needed available)
(check-predicate `(and . ,(cdr clause)) #f needed available)
(loop (cdr clauses)
needed
available
source)))
((feature-cond)
(receive (needed available more-source)
(process-cond-clauses (cdr clause) needed available)
(loop (cdr clauses)
needed
available
(append more-source source))))
((code files)
(loop (cdr clauses)
needed
available
(cons clause source)))
(else
(assertion-violation 'process-clauses "bad program clause" clause)))))))
; Loop down CLAUSES looking for one whose predicate can be satisfied.
; If we find one we process its clauses. EITHER is used to allow us
; to backtrack in case of failure.
(define (process-cond-clauses clauses needed available)
(let loop ((clauses clauses))
(if (null? clauses)
(fail)
(either (receive (needed available)
(check-predicate (caar clauses) #f needed available)
(process-clauses (cdar clauses) needed available))
(loop (cdr clauses))))))
; REQUIREMENT is one of:
; (and <requirement> ...)
; (or <requirement> ...)
; (not <requirement>)
; <srfi-name>
; NEGATE? is true if we really want the negation of REQUIREMENT.
;
; AND and OR have their own procedures, which get flipped by negation.
; NOT just flips NEGATE?. There are two separate procedures for positive
; and negative occurances of <srfi-name>.
(define (check-predicate requirement negate? needed available)
(cond ((pair? requirement)
(case (car requirement)
((and)
((if negate? any-satisfied all-satisfied)
(cdr requirement) negate? needed available))
((or)
((if negate? all-satisfied any-satisfied)
(cdr requirement) negate? needed available))
((not)
(check-predicate (cadr requirement)
(not negate?)
needed
available))))
(negate?
(do-not-want requirement needed available))
(else
(want requirement needed available))))
; We want SRFI. If it is NEEDED we are fine. If it is in AVAILABLE we
; move it to needed. Otherwise we lose.
(define (want srfi needed available)
(cond ((memq srfi needed)
(values needed available))
((memq srfi available)
(values (cons srfi needed)
(delq srfi available))) ; not really necessary
(else
(fail))))
; We do not want SRFI. If it is NEEDED we lose. If it is in AVAILABLE we
; get rid of it. Otherwise we win as-is.
(define (do-not-want srfi needed available)
(cond ((memq srfi needed)
(fail))
((memq srfi available)
(values needed
(delq srfi available)))
(else
(values needed available))))
; Two loops for `and' and `or'. The `and' loop needs to update NEEDED
; and AVAILABLE as it goes, the `or' keeps reusing the originals.
(define (all-satisfied list negate? needed available)
(let loop ((list list) (needed needed) (available available))
(if (null? list)
(values needed available)
(receive (needed available)
(check-predicate (car list) negate? needed available)
(if needed
(loop (cdr list) needed available)
(fail))))))
; Again, we use EITHER to allow for backtracking.
(define (any-satisfied list negate? needed available)
(let loop ((list list))
(if (null? list)
(fail)
(either (check-predicate (car list) negate? needed available)
(loop (cdr list))))))
;----------------
; Our own copy to avoid having to load BIG-UTIL to get the original.
(define (delq thing list)
(cond ((null? list)
'())
((eq? thing (car list))
(cdr list))
(else
(cons (car list)
(delq thing (cdr list))))))
|