r6rs-libraries.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ;;; r6rs-libraries.scm --- Support for the R6RS `library' and `import' forms
  2. ;; Copyright (C) 2010, 2019, 2023 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. ;; Note that we can't use top-level define for helpers here as it will
  20. ;; pollute the (guile) module.
  21. (define (resolve-r6rs-interface import-spec)
  22. (define (sym? stx)
  23. (symbol? (syntax->datum stx)))
  24. (define (n? stx)
  25. (let ((n (syntax->datum stx)))
  26. (and (exact-integer? n)
  27. (not (negative? n)))))
  28. (define (colon-n? x)
  29. (let ((sym (syntax->datum x)))
  30. (and (symbol? sym)
  31. (let ((str (symbol->string sym)))
  32. (and (string-prefix? ":" str)
  33. (let ((num (string->number (substring str 1))))
  34. (and (exact-integer? num)
  35. (not (negative? num)))))))))
  36. (define (srfi-name? stx)
  37. (syntax-case stx (srfi)
  38. ((srfi n rest ...)
  39. (cond ((n? #'n) 'r7rs)
  40. ((colon-n? #'n) 'r6rs)
  41. (else #f)))
  42. (_ #f)))
  43. (define (module-name? stx)
  44. (or (srfi-name? stx)
  45. (syntax-case stx ()
  46. ((name name* ...)
  47. (and-map sym? #'(name name* ...)))
  48. (_ #f))))
  49. (define (make-srfi-n context n)
  50. (datum->syntax
  51. context
  52. (string->symbol
  53. (string-append
  54. "srfi-"
  55. (let ((n (syntax->datum n)))
  56. (if (symbol? n)
  57. (substring (symbol->string n) 1)
  58. (number->string n)))))))
  59. (define (make-custom-interface mod)
  60. (let ((iface (make-module)))
  61. (set-module-kind! iface 'custom-interface)
  62. (set-module-name! iface (module-name mod))
  63. iface))
  64. (define (module-for-each/nonlocal f mod)
  65. (define (module-and-uses mod)
  66. (let lp ((in (list mod)) (out '()))
  67. (cond
  68. ((null? in) (reverse out))
  69. ((memq (car in) out) (lp (cdr in) out))
  70. (else (lp (append (module-uses (car in)) (cdr in))
  71. (cons (car in) out))))))
  72. (for-each (lambda (mod)
  73. (module-for-each f mod))
  74. (module-and-uses mod)))
  75. (syntax-case import-spec (library only except prefix rename srfi)
  76. ;; XXX: This is R7RS-specific, but it's here since we want the
  77. ;; `import' procedure below to accept (srfi 64) as well as
  78. ;; (srfi :64).
  79. ;;
  80. ;; (srfi n ...) -> (srfi srfi-n ...)
  81. ((library (srfi n rest ... (version ...)))
  82. (eq? 'r7rs (srfi-name? #'(srfi n rest ...)))
  83. (let ((srfi-n (make-srfi-n #'srfi #'n)))
  84. (resolve-r6rs-interface
  85. #`(library (srfi #,srfi-n rest ... (version ...))))))
  86. ;; (srfi :n ...) -> (srfi srfi-n ...)
  87. ((library (srfi n rest ... (version ...)))
  88. (eq? 'r6rs (srfi-name? #'(srfi n rest ...)))
  89. (let ((srfi-n (make-srfi-n #'srfi #'n)))
  90. (resolve-r6rs-interface
  91. (syntax-case #'(rest ...) ()
  92. (()
  93. #`(library (srfi #,srfi-n (version ...))))
  94. ((name rest ...)
  95. ;; SRFI 97 says that the first identifier after the `n'
  96. ;; is used for the libraries name, so it must be ignored.
  97. #`(library (srfi #,srfi-n rest ... (version ...))))))))
  98. ((library (name name* ... (version ...)))
  99. (and-map sym? #'(name name* ...))
  100. (resolve-interface (syntax->datum #'(name name* ...))
  101. #:version (syntax->datum #'(version ...))))
  102. ((library (name name* ...))
  103. (and-map sym? #'(name name* ...))
  104. (resolve-r6rs-interface #'(library (name name* ... ()))))
  105. ((only import-set identifier ...)
  106. (and-map sym? #'(identifier ...))
  107. (let* ((mod (resolve-r6rs-interface #'import-set))
  108. (iface (make-custom-interface mod)))
  109. (for-each (lambda (sym)
  110. (module-add! iface sym
  111. (or (module-variable mod sym)
  112. (error "no binding `~A' in module ~A"
  113. sym mod)))
  114. (when (hashq-ref (module-replacements mod) sym)
  115. (hashq-set! (module-replacements iface) sym #t)))
  116. (syntax->datum #'(identifier ...)))
  117. iface))
  118. ((except import-set identifier ...)
  119. (and-map sym? #'(identifier ...))
  120. (let* ((mod (resolve-r6rs-interface #'import-set))
  121. (iface (make-custom-interface mod)))
  122. (module-for-each/nonlocal (lambda (sym var)
  123. (module-add! iface sym var))
  124. mod)
  125. (for-each (lambda (sym)
  126. (unless (module-local-variable iface sym)
  127. (error "no binding `~A' in module ~A" sym mod))
  128. (module-remove! iface sym))
  129. (syntax->datum #'(identifier ...)))
  130. iface))
  131. ((prefix import-set identifier)
  132. (sym? #'identifier)
  133. (let* ((mod (resolve-r6rs-interface #'import-set))
  134. (iface (make-custom-interface mod))
  135. (pre (syntax->datum #'identifier)))
  136. (module-for-each/nonlocal
  137. (lambda (sym var)
  138. (let ((sym* (symbol-append pre sym)))
  139. (module-add! iface sym* var)
  140. (when (hashq-ref (module-replacements mod) sym)
  141. (hashq-set! (module-replacements iface) sym* #t))))
  142. mod)
  143. iface))
  144. ((rename import-set (from to) ...)
  145. (and (and-map sym? #'(from ...)) (and-map sym? #'(to ...)))
  146. (let* ((mod (resolve-r6rs-interface #'import-set))
  147. (replacements (module-replacements mod))
  148. (iface (make-custom-interface mod)))
  149. (module-for-each/nonlocal
  150. (lambda (sym var) (module-add! iface sym var))
  151. mod)
  152. (let lp ((in (syntax->datum #'((from . to) ...))) (out '()))
  153. (cond
  154. ((null? in)
  155. (for-each
  156. (lambda (v)
  157. (let ((to (vector-ref v 0))
  158. (replace? (vector-ref v 1))
  159. (var (vector-ref v 2)))
  160. (when (module-local-variable iface to)
  161. (error "duplicate binding for `~A' in module ~A" to mod))
  162. (module-add! iface to var)
  163. (when replace?
  164. (hashq-set! replacements to #t))))
  165. out)
  166. iface)
  167. (else
  168. (let* ((from (caar in))
  169. (to (cdar in))
  170. (var (module-variable mod from))
  171. (replace? (hashq-ref replacements from)))
  172. (unless var (error "no binding `~A' in module ~A" from mod))
  173. (module-remove! iface from)
  174. (hashq-remove! replacements from)
  175. (lp (cdr in) (cons (vector to replace? var) out))))))))
  176. ((name name* ... (version ...))
  177. (module-name? #'(name name* ...))
  178. (resolve-r6rs-interface #'(library (name name* ... (version ...)))))
  179. ((name name* ...)
  180. (module-name? #'(name name* ...))
  181. (resolve-r6rs-interface #'(library (name name* ... ()))))))
  182. (define-syntax library
  183. (lambda (stx)
  184. (define (sym? stx)
  185. (symbol? (syntax->datum stx)))
  186. (define (module-name? stx)
  187. (syntax-case stx ()
  188. ((name name* ...)
  189. (and-map sym? #'(name name* ...)))
  190. (_ #f)))
  191. (define (compute-exports ifaces specs)
  192. (define (re-export? sym)
  193. (or-map (lambda (iface) (module-variable iface sym)) ifaces))
  194. (define (replace? sym)
  195. (module-variable the-scm-module sym))
  196. (let lp ((specs specs) (e '()) (r '()) (x '()))
  197. (syntax-case specs (rename)
  198. (() (values e r x))
  199. (((rename (from to) ...) . rest)
  200. (and (and-map identifier? #'(from ...))
  201. (and-map identifier? #'(to ...)))
  202. (let lp2 ((in #'((from . to) ...)) (e e) (r r) (x x))
  203. (syntax-case in ()
  204. (() (lp #'rest e r x))
  205. (((from . to) . in)
  206. (cond
  207. ((re-export? (syntax->datum #'from))
  208. (lp2 #'in e (cons #'(from . to) r) x))
  209. ((replace? (syntax->datum #'from))
  210. (lp2 #'in e r (cons #'(from . to) x)))
  211. (else
  212. (lp2 #'in (cons #'(from . to) e) r x)))))))
  213. ((id . rest)
  214. (identifier? #'id)
  215. (let ((sym (syntax->datum #'id)))
  216. (cond
  217. ((re-export? sym)
  218. (lp #'rest e (cons #'id r) x))
  219. ((replace? sym)
  220. (lp #'rest e r (cons #'id x)))
  221. (else
  222. (lp #'rest (cons #'id e) r x))))))))
  223. (syntax-case stx (export import srfi)
  224. ((_ (name name* ...)
  225. (export espec ...)
  226. (import ispec ...)
  227. body ...)
  228. (module-name? #'(name name* ...))
  229. ;; Add () as the version.
  230. #'(library (name name* ... ())
  231. (export espec ...)
  232. (import ispec ...)
  233. body ...))
  234. ((_ (name name* ... (version ...))
  235. (export espec ...)
  236. (import ispec ...)
  237. body ...)
  238. (module-name? #'(name name* ...))
  239. (call-with-values
  240. (lambda ()
  241. (compute-exports
  242. (map (lambda (im)
  243. (syntax-case im (for)
  244. ((for import-set import-level ...)
  245. (resolve-r6rs-interface #'import-set))
  246. (import-set (resolve-r6rs-interface #'import-set))))
  247. #'(ispec ...))
  248. #'(espec ...)))
  249. (lambda (exports re-exports replacements)
  250. (with-syntax (((e ...) exports)
  251. ((r ...) re-exports)
  252. ((x ...) replacements))
  253. ;; It would be nice to push the module that was current before the
  254. ;; definition, and pop it after the library definition, but I
  255. ;; actually can't see a way to do that. Helper procedures perhaps,
  256. ;; around a fluid that is rebound in save-module-excursion? Patches
  257. ;; welcome!
  258. #'(begin
  259. (define-module (name name* ...)
  260. #:pure
  261. #:version (version ...))
  262. (import ispec)
  263. ...
  264. (export e ...)
  265. (re-export r ...)
  266. (export! x ...)
  267. (@@ @@ (name name* ...) body)
  268. ...))))))))
  269. (define-syntax import
  270. (lambda (stx)
  271. (define (strip-for import-set)
  272. (syntax-case import-set (for)
  273. ((for import-set import-level ...)
  274. #'import-set)
  275. (import-set
  276. #'import-set)))
  277. (syntax-case stx ()
  278. ((_ import-set ...)
  279. (with-syntax (((library-reference ...) (map strip-for #'(import-set ...))))
  280. #'(eval-when (expand load eval)
  281. (let ((iface (resolve-r6rs-interface 'library-reference)))
  282. (call-with-deferred-observers
  283. (lambda ()
  284. (module-use-interfaces! (current-module) (list iface)))))
  285. ...
  286. (if #f #f)))))))