r6rs-libraries.scm 8.7 KB

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