1 ;;; r6rs-libraries.scm --- Support for the R6RS `library' and `import' forms
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ;; This file is included from boot-9.scm and assumes the existence of (and
21 ;; expands into) procedures and syntactic forms defined therein.
23 (define (resolve-r6rs-interface import-spec)
24 (define (make-custom-interface mod)
25 (let ((iface (make-module)))
26 (set-module-kind! iface 'custom-interface)
27 (set-module-name! iface (module-name mod))
29 (define (sym? x) (symbol? (syntax->datum x)))
31 (syntax-case import-spec (library only except prefix rename srfi)
32 ;; (srfi :n ...) -> (srfi srfi-n)
33 ((library (srfi colon-n rest ... (version ...)))
34 (and (and-map sym? #'(srfi rest ...))
35 (symbol? (syntax->datum #'colon-n))
36 (eqv? (string-ref (symbol->string (syntax->datum #'colon-n)) 0) #\:))
37 (let ((srfi-n (string->symbol
40 (substring (symbol->string (syntax->datum #'colon-n))
42 (resolve-r6rs-interface
43 #`(library (srfi #,srfi-n (version ...))))))
45 ((library (name name* ... (version ...)))
46 (and-map sym? #'(name name* ...))
47 (resolve-interface (syntax->datum #'(name name* ...))
48 #:version (syntax->datum #'(version ...))))
50 ((library (name name* ...))
51 (and-map sym? #'(name name* ...))
52 (resolve-r6rs-interface #'(library (name name* ... ()))))
54 ((only import-set identifier ...)
55 (and-map sym? #'(identifier ...))
56 (let* ((mod (resolve-r6rs-interface #'import-set))
57 (iface (make-custom-interface mod)))
58 (for-each (lambda (sym)
59 (module-add! iface sym
60 (or (module-local-variable mod sym)
61 (error "no binding `~A' in module ~A"
63 (syntax->datum #'(identifier ...)))
66 ((except import-set identifier ...)
67 (and-map sym? #'(identifier ...))
68 (let* ((mod (resolve-r6rs-interface #'import-set))
69 (iface (make-custom-interface mod)))
70 (module-for-each (lambda (sym var) (module-add! iface sym var)) mod)
71 (for-each (lambda (sym)
72 (if (module-local-variable iface sym)
73 (module-remove! iface sym)
74 (error "no binding `~A' in module ~A" sym mod)))
75 (syntax->datum #'(identifier ...)))
78 ((prefix import-set identifier)
80 (let* ((mod (resolve-r6rs-interface #'import-set))
81 (iface (make-custom-interface mod))
82 (pre (syntax->datum #'identifier)))
83 (module-for-each (lambda (sym var)
84 (module-add! iface (symbol-append pre sym) var))
88 ((rename import-set (from to) ...)
89 (and (and-map sym? #'(from ...)) (and-map sym? #'(to ...)))
90 (let* ((mod (resolve-r6rs-interface #'import-set))
91 (iface (make-custom-interface mod)))
92 (module-for-each (lambda (sym var) (module-add! iface sym var)) mod)
93 (let lp ((in (syntax->datum #'((from . to) ...))) (out '()))
98 (if (module-local-variable iface (car pair))
99 (error "duplicate binding for `~A' in module ~A"
101 (module-add! iface (car pair) (cdr pair))))
105 (let ((var (or (module-local-variable mod (caar in))
106 (error "no binding `~A' in module ~A"
108 (module-remove! iface (caar in))
109 (lp (cdr in) (acons (cdar in) var out))))))))
111 ((name name* ... (version ...))
112 (and-map sym? #'(name name* ...))
113 (resolve-r6rs-interface #'(library (name name* ... (version ...)))))
116 (and-map sym? #'(name name* ...))
117 (resolve-r6rs-interface #'(library (name name* ... ()))))))
119 (define-syntax library
121 (define (compute-exports ifaces specs)
122 (define (re-export? sym)
123 (or-map (lambda (iface) (module-local-variable iface sym)) ifaces))
124 (define (replace? sym)
125 (module-local-variable the-scm-module sym))
127 (let lp ((specs specs) (e '()) (r '()) (x '()))
128 (syntax-case specs (rename)
130 (((rename (from to) ...) . rest)
131 (and (and-map identifier? #'(from ...))
132 (and-map identifier? #'(to ...)))
133 (let lp2 ((in #'((from . to) ...)) (e e) (r r) (x x))
135 (() (lp #'rest e r x))
138 ((re-export? (syntax->datum #'from))
139 (lp2 #'in e (cons #'(from . to) r) x))
140 ((replace? (syntax->datum #'from))
141 (lp2 #'in e r (cons #'(from . to) x)))
143 (lp2 #'in (cons #'(from . to) e) r x)))))))
146 (let ((sym (syntax->datum #'id)))
149 (lp #'rest e (cons #'id r) x))
151 (lp #'rest e r (cons #'id x)))
153 (lp #'rest (cons #'id e) r x))))))))
155 (syntax-case stx (export import)
160 (and-map identifier? #'(name name* ...))
161 ;; Add () as the version.
162 #'(library (name name* ... ())
167 ((_ (name name* ... (version ...))
171 (and-map identifier? #'(name name* ...))
176 (syntax-case im (for)
177 ((for import-set import-level ...)
178 (resolve-r6rs-interface #'import-set))
179 (import-set (resolve-r6rs-interface #'import-set))))
182 (lambda (exports re-exports replacements)
183 (with-syntax (((e ...) exports)
185 ((x ...) replacements))
186 ;; It would be nice to push the module that was current before the
187 ;; definition, and pop it after the library definition, but I
188 ;; actually can't see a way to do that. Helper procedures perhaps,
189 ;; around a fluid that is rebound in save-module-excursion? Patches
192 (define-module (name name* ...)
194 #:version (version ...))
200 (@@ @@ (name name* ...) body)
203 (define-syntax import
205 (define (strip-for import-set)
206 (syntax-case import-set (for)
207 ((for import-set import-level ...)
213 (with-syntax (((library-reference ...) (map strip-for #'(import-set ...))))
214 #'(eval-when (eval load compile expand)
215 (let ((iface (resolve-r6rs-interface 'library-reference)))
216 (call-with-deferred-observers
218 (module-use-interfaces! (current-module) (list iface)))))