r6rs-libraries.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ;;; r6rs-libraries.scm --- Support for the R6RS `library' and `import' forms
  2. ;; Copyright (C) 2010 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. ((library (srfi colon-n rest ... (version ...)))
  40. (and (and-map sym? #'(srfi rest ...))
  41. (symbol? (syntax->datum #'colon-n))
  42. (eqv? (string-ref (symbol->string (syntax->datum #'colon-n)) 0) #\:))
  43. (let ((srfi-n (string->symbol
  44. (string-append
  45. "srfi-"
  46. (substring (symbol->string (syntax->datum #'colon-n))
  47. 1)))))
  48. (resolve-r6rs-interface
  49. (syntax-case #'(rest ...) ()
  50. (()
  51. #`(library (srfi #,srfi-n (version ...))))
  52. ((name rest ...)
  53. ;; SRFI 97 says that the first identifier after the colon-n
  54. ;; is used for the libraries name, so it must be ignored.
  55. #`(library (srfi #,srfi-n rest ... (version ...))))))))
  56. ((library (name name* ... (version ...)))
  57. (and-map sym? #'(name name* ...))
  58. (resolve-interface (syntax->datum #'(name name* ...))
  59. #:version (syntax->datum #'(version ...))))
  60. ((library (name name* ...))
  61. (and-map sym? #'(name name* ...))
  62. (resolve-r6rs-interface #'(library (name name* ... ()))))
  63. ((only import-set identifier ...)
  64. (and-map sym? #'(identifier ...))
  65. (let* ((mod (resolve-r6rs-interface #'import-set))
  66. (iface (make-custom-interface mod)))
  67. (for-each (lambda (sym)
  68. (module-add! iface sym
  69. (or (module-variable mod sym)
  70. (error "no binding `~A' in module ~A"
  71. sym mod))))
  72. (syntax->datum #'(identifier ...)))
  73. iface))
  74. ((except import-set identifier ...)
  75. (and-map sym? #'(identifier ...))
  76. (let* ((mod (resolve-r6rs-interface #'import-set))
  77. (iface (make-custom-interface mod)))
  78. (module-for-each/nonlocal (lambda (sym var)
  79. (module-add! iface sym var))
  80. mod)
  81. (for-each (lambda (sym)
  82. (if (module-local-variable iface sym)
  83. (module-remove! iface sym)
  84. (error "no binding `~A' in module ~A" sym mod)))
  85. (syntax->datum #'(identifier ...)))
  86. iface))
  87. ((prefix import-set identifier)
  88. (sym? #'identifier)
  89. (let* ((mod (resolve-r6rs-interface #'import-set))
  90. (iface (make-custom-interface mod))
  91. (pre (syntax->datum #'identifier)))
  92. (module-for-each/nonlocal
  93. (lambda (sym var)
  94. (module-add! iface (symbol-append pre sym) var))
  95. mod)
  96. iface))
  97. ((rename import-set (from to) ...)
  98. (and (and-map sym? #'(from ...)) (and-map sym? #'(to ...)))
  99. (let* ((mod (resolve-r6rs-interface #'import-set))
  100. (iface (make-custom-interface mod)))
  101. (module-for-each/nonlocal
  102. (lambda (sym var) (module-add! iface sym var))
  103. mod)
  104. (let lp ((in (syntax->datum #'((from . to) ...))) (out '()))
  105. (cond
  106. ((null? in)
  107. (for-each
  108. (lambda (pair)
  109. (if (module-local-variable iface (car pair))
  110. (error "duplicate binding for `~A' in module ~A"
  111. (car pair) mod)
  112. (module-add! iface (car pair) (cdr pair))))
  113. out)
  114. iface)
  115. (else
  116. (let ((var (or (module-variable mod (caar in))
  117. (error "no binding `~A' in module ~A"
  118. (caar in) mod))))
  119. (module-remove! iface (caar in))
  120. (lp (cdr in) (acons (cdar in) var out))))))))
  121. ((name name* ... (version ...))
  122. (and-map sym? #'(name name* ...))
  123. (resolve-r6rs-interface #'(library (name name* ... (version ...)))))
  124. ((name name* ...)
  125. (and-map sym? #'(name name* ...))
  126. (resolve-r6rs-interface #'(library (name name* ... ()))))))
  127. (define-syntax library
  128. (lambda (stx)
  129. (define (compute-exports ifaces specs)
  130. (define (re-export? sym)
  131. (or-map (lambda (iface) (module-variable iface sym)) ifaces))
  132. (define (replace? sym)
  133. (module-variable the-scm-module sym))
  134. (let lp ((specs specs) (e '()) (r '()) (x '()))
  135. (syntax-case specs (rename)
  136. (() (values e r x))
  137. (((rename (from to) ...) . rest)
  138. (and (and-map identifier? #'(from ...))
  139. (and-map identifier? #'(to ...)))
  140. (let lp2 ((in #'((from . to) ...)) (e e) (r r) (x x))
  141. (syntax-case in ()
  142. (() (lp #'rest e r x))
  143. (((from . to) . in)
  144. (cond
  145. ((re-export? (syntax->datum #'from))
  146. (lp2 #'in e (cons #'(from . to) r) x))
  147. ((replace? (syntax->datum #'from))
  148. (lp2 #'in e r (cons #'(from . to) x)))
  149. (else
  150. (lp2 #'in (cons #'(from . to) e) r x)))))))
  151. ((id . rest)
  152. (identifier? #'id)
  153. (let ((sym (syntax->datum #'id)))
  154. (cond
  155. ((re-export? sym)
  156. (lp #'rest e (cons #'id r) x))
  157. ((replace? sym)
  158. (lp #'rest e r (cons #'id x)))
  159. (else
  160. (lp #'rest (cons #'id e) r x))))))))
  161. (syntax-case stx (export import)
  162. ((_ (name name* ...)
  163. (export espec ...)
  164. (import ispec ...)
  165. body ...)
  166. (and-map identifier? #'(name name* ...))
  167. ;; Add () as the version.
  168. #'(library (name name* ... ())
  169. (export espec ...)
  170. (import ispec ...)
  171. body ...))
  172. ((_ (name name* ... (version ...))
  173. (export espec ...)
  174. (import ispec ...)
  175. body ...)
  176. (and-map identifier? #'(name name* ...))
  177. (call-with-values
  178. (lambda ()
  179. (compute-exports
  180. (map (lambda (im)
  181. (syntax-case im (for)
  182. ((for import-set import-level ...)
  183. (resolve-r6rs-interface #'import-set))
  184. (import-set (resolve-r6rs-interface #'import-set))))
  185. #'(ispec ...))
  186. #'(espec ...)))
  187. (lambda (exports re-exports replacements)
  188. (with-syntax (((e ...) exports)
  189. ((r ...) re-exports)
  190. ((x ...) replacements))
  191. ;; It would be nice to push the module that was current before the
  192. ;; definition, and pop it after the library definition, but I
  193. ;; actually can't see a way to do that. Helper procedures perhaps,
  194. ;; around a fluid that is rebound in save-module-excursion? Patches
  195. ;; welcome!
  196. #'(begin
  197. (define-module (name name* ...)
  198. #:pure
  199. #:version (version ...))
  200. (import ispec)
  201. ...
  202. (export e ...)
  203. (re-export r ...)
  204. (export! x ...)
  205. (@@ @@ (name name* ...) body)
  206. ...))))))))
  207. (define-syntax import
  208. (lambda (stx)
  209. (define (strip-for import-set)
  210. (syntax-case import-set (for)
  211. ((for import-set import-level ...)
  212. #'import-set)
  213. (import-set
  214. #'import-set)))
  215. (syntax-case stx ()
  216. ((_ import-set ...)
  217. (with-syntax (((library-reference ...) (map strip-for #'(import-set ...))))
  218. #'(eval-when (expand load eval)
  219. (let ((iface (resolve-r6rs-interface 'library-reference)))
  220. (call-with-deferred-observers
  221. (lambda ()
  222. (module-use-interfaces! (current-module) (list iface)))))
  223. ...
  224. (if #f #f)))))))