r6rs-libraries.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ;;; r6rs-libraries.scm --- Support for the R6RS `library' and `import' forms
  2. ;; Copyright (C) 2010, 2019 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;; This file is included from boot-9.scm and assumes the existence of (and
  18. ;; expands into) procedures and syntactic forms defined therein.
  19. (define (resolve-r6rs-interface import-spec)
  20. (define (make-custom-interface mod)
  21. (let ((iface (make-module)))
  22. (set-module-kind! iface 'custom-interface)
  23. (set-module-name! iface (module-name mod))
  24. iface))
  25. (define (module-for-each/nonlocal f mod)
  26. (define (module-and-uses mod)
  27. (let lp ((in (list mod)) (out '()))
  28. (cond
  29. ((null? in) (reverse out))
  30. ((memq (car in) out) (lp (cdr in) out))
  31. (else (lp (append (module-uses (car in)) (cdr in))
  32. (cons (car in) out))))))
  33. (for-each (lambda (mod)
  34. (module-for-each f mod))
  35. (module-and-uses mod)))
  36. (define (sym? x) (symbol? (syntax->datum x)))
  37. (syntax-case import-spec (library only except prefix rename srfi)
  38. ;; (srfi :n ...) -> (srfi srfi-n ...)
  39. ;; (srfi n ...) -> (srfi srfi-n ...)
  40. ((library (srfi n rest ... (version ...)))
  41. (and (and-map sym? #'(srfi rest ...))
  42. (or (and
  43. (symbol? (syntax->datum #'n))
  44. (let ((str (symbol->string (syntax->datum #'n))))
  45. (and (string-prefix? ":" str)
  46. (and=> (string->number (substring str 1))
  47. exact-integer?))))
  48. (exact-integer? (syntax->datum #'n))))
  49. (let ((srfi-n (string->symbol
  50. (string-append
  51. "srfi-"
  52. (let ((n (syntax->datum #'n)))
  53. (if (symbol? n)
  54. (substring (symbol->string n) 1)
  55. (number->string n)))))))
  56. (resolve-r6rs-interface
  57. (syntax-case #'(rest ...) ()
  58. (()
  59. #`(library (srfi #,srfi-n (version ...))))
  60. ((name rest ...)
  61. ;; SRFI 97 says that the first identifier after the `n'
  62. ;; is used for the libraries name, so it must be ignored.
  63. #`(library (srfi #,srfi-n rest ... (version ...))))))))
  64. ((library (name name* ... (version ...)))
  65. (and-map sym? #'(name name* ...))
  66. (resolve-interface (syntax->datum #'(name name* ...))
  67. #:version (syntax->datum #'(version ...))))
  68. ((library (name name* ...))
  69. (and-map sym? #'(name name* ...))
  70. (resolve-r6rs-interface #'(library (name name* ... ()))))
  71. ((only import-set identifier ...)
  72. (and-map sym? #'(identifier ...))
  73. (let* ((mod (resolve-r6rs-interface #'import-set))
  74. (iface (make-custom-interface mod)))
  75. (for-each (lambda (sym)
  76. (module-add! iface sym
  77. (or (module-variable mod sym)
  78. (error "no binding `~A' in module ~A"
  79. sym mod)))
  80. (when (hashq-ref (module-replacements mod) sym)
  81. (hashq-set! (module-replacements iface) sym #t)))
  82. (syntax->datum #'(identifier ...)))
  83. iface))
  84. ((except import-set identifier ...)
  85. (and-map sym? #'(identifier ...))
  86. (let* ((mod (resolve-r6rs-interface #'import-set))
  87. (iface (make-custom-interface mod)))
  88. (module-for-each/nonlocal (lambda (sym var)
  89. (module-add! iface sym var))
  90. mod)
  91. (for-each (lambda (sym)
  92. (unless (module-local-variable iface sym)
  93. (error "no binding `~A' in module ~A" sym mod))
  94. (module-remove! iface sym))
  95. (syntax->datum #'(identifier ...)))
  96. iface))
  97. ((prefix import-set identifier)
  98. (sym? #'identifier)
  99. (let* ((mod (resolve-r6rs-interface #'import-set))
  100. (iface (make-custom-interface mod))
  101. (pre (syntax->datum #'identifier)))
  102. (module-for-each/nonlocal
  103. (lambda (sym var)
  104. (let ((sym* (symbol-append pre sym)))
  105. (module-add! iface sym* var)
  106. (when (hashq-ref (module-replacements mod) sym)
  107. (hashq-set! (module-replacements iface) sym* #t))))
  108. mod)
  109. iface))
  110. ((rename import-set (from to) ...)
  111. (and (and-map sym? #'(from ...)) (and-map sym? #'(to ...)))
  112. (let* ((mod (resolve-r6rs-interface #'import-set))
  113. (replacements (module-replacements mod))
  114. (iface (make-custom-interface mod)))
  115. (module-for-each/nonlocal
  116. (lambda (sym var) (module-add! iface sym var))
  117. mod)
  118. (let lp ((in (syntax->datum #'((from . to) ...))) (out '()))
  119. (cond
  120. ((null? in)
  121. (for-each
  122. (lambda (v)
  123. (let ((to (vector-ref v 0))
  124. (replace? (vector-ref v 1))
  125. (var (vector-ref v 2)))
  126. (when (module-local-variable iface to)
  127. (error "duplicate binding for `~A' in module ~A" to mod))
  128. (module-add! iface to var)
  129. (when replace?
  130. (hashq-set! replacements to #t))))
  131. out)
  132. iface)
  133. (else
  134. (let* ((from (caar in))
  135. (to (cdar in))
  136. (var (module-variable mod from))
  137. (replace? (hashq-ref replacements from)))
  138. (unless var (error "no binding `~A' in module ~A" from mod))
  139. (module-remove! iface from)
  140. (hashq-remove! replacements from)
  141. (lp (cdr in) (cons (vector to replace? var) out))))))))
  142. ((name name* ... (version ...))
  143. (and-map sym? #'(name name* ...))
  144. (resolve-r6rs-interface #'(library (name name* ... (version ...)))))
  145. ((name name* ...)
  146. (and-map sym? #'(name name* ...))
  147. (resolve-r6rs-interface #'(library (name name* ... ()))))))
  148. (define-syntax library
  149. (lambda (stx)
  150. (define (compute-exports ifaces specs)
  151. (define (re-export? sym)
  152. (or-map (lambda (iface) (module-variable iface sym)) ifaces))
  153. (define (replace? sym)
  154. (module-variable the-scm-module sym))
  155. (let lp ((specs specs) (e '()) (r '()) (x '()))
  156. (syntax-case specs (rename)
  157. (() (values e r x))
  158. (((rename (from to) ...) . rest)
  159. (and (and-map identifier? #'(from ...))
  160. (and-map identifier? #'(to ...)))
  161. (let lp2 ((in #'((from . to) ...)) (e e) (r r) (x x))
  162. (syntax-case in ()
  163. (() (lp #'rest e r x))
  164. (((from . to) . in)
  165. (cond
  166. ((re-export? (syntax->datum #'from))
  167. (lp2 #'in e (cons #'(from . to) r) x))
  168. ((replace? (syntax->datum #'from))
  169. (lp2 #'in e r (cons #'(from . to) x)))
  170. (else
  171. (lp2 #'in (cons #'(from . to) e) r x)))))))
  172. ((id . rest)
  173. (identifier? #'id)
  174. (let ((sym (syntax->datum #'id)))
  175. (cond
  176. ((re-export? sym)
  177. (lp #'rest e (cons #'id r) x))
  178. ((replace? sym)
  179. (lp #'rest e r (cons #'id x)))
  180. (else
  181. (lp #'rest (cons #'id e) r x))))))))
  182. (syntax-case stx (export import)
  183. ((_ (name name* ...)
  184. (export espec ...)
  185. (import ispec ...)
  186. body ...)
  187. (and-map identifier? #'(name name* ...))
  188. ;; Add () as the version.
  189. #'(library (name name* ... ())
  190. (export espec ...)
  191. (import ispec ...)
  192. body ...))
  193. ((_ (name name* ... (version ...))
  194. (export espec ...)
  195. (import ispec ...)
  196. body ...)
  197. (and-map identifier? #'(name name* ...))
  198. (call-with-values
  199. (lambda ()
  200. (compute-exports
  201. (map (lambda (im)
  202. (syntax-case im (for)
  203. ((for import-set import-level ...)
  204. (resolve-r6rs-interface #'import-set))
  205. (import-set (resolve-r6rs-interface #'import-set))))
  206. #'(ispec ...))
  207. #'(espec ...)))
  208. (lambda (exports re-exports replacements)
  209. (with-syntax (((e ...) exports)
  210. ((r ...) re-exports)
  211. ((x ...) replacements))
  212. ;; It would be nice to push the module that was current before the
  213. ;; definition, and pop it after the library definition, but I
  214. ;; actually can't see a way to do that. Helper procedures perhaps,
  215. ;; around a fluid that is rebound in save-module-excursion? Patches
  216. ;; welcome!
  217. #'(begin
  218. (define-module (name name* ...)
  219. #:pure
  220. #:version (version ...))
  221. (import ispec)
  222. ...
  223. (export e ...)
  224. (re-export r ...)
  225. (export! x ...)
  226. (@@ @@ (name name* ...) body)
  227. ...))))))))
  228. (define-syntax import
  229. (lambda (stx)
  230. (define (strip-for import-set)
  231. (syntax-case import-set (for)
  232. ((for import-set import-level ...)
  233. #'import-set)
  234. (import-set
  235. #'import-set)))
  236. (syntax-case stx ()
  237. ((_ import-set ...)
  238. (with-syntax (((library-reference ...) (map strip-for #'(import-set ...))))
  239. #'(eval-when (expand load eval)
  240. (let ((iface (resolve-r6rs-interface 'library-reference)))
  241. (call-with-deferred-observers
  242. (lambda ()
  243. (module-use-interfaces! (current-module) (list iface)))))
  244. ...
  245. (if #f #f)))))))