srfi-7.scm 9.1 KB

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