1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;;; Copyright (C) 2012 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
19 (define-module (ice-9 local-eval)
20 #:use-module (ice-9 format)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:use-module (system base compile)
24 #:use-module (system syntax)
25 #:export (the-environment local-eval local-compile))
27 (define-record-type lexical-environment-type
28 (make-lexical-environment scope wrapper boxes patterns)
31 (wrapper lexenv-wrapper)
33 (patterns lexenv-patterns))
35 (set-record-type-printer!
36 lexical-environment-type
38 (format port "#<lexical-environment ~S (~S bindings)>"
39 (syntax-module (lexenv-scope e))
40 (+ (length (lexenv-boxes e)) (length (lexenv-patterns e))))))
42 (define-syntax syntax-object-of
45 ((_ x) #`(quote #,(datum->syntax #'x #'x))))))
47 (define-syntax-rule (make-box v)
52 (define (make-transformer-from-box id trans)
53 (set-procedure-property! trans 'identifier-syntax-box id)
56 (define-syntax-rule (identifier-syntax-from-box box)
57 (make-transformer-from-box
58 (syntax-object-of box)
59 (identifier-syntax (id (box))
60 ((set! id x) (box x)))))
62 (define (unsupported-binding name)
63 (make-variable-transformer
67 "unsupported binding captured by (the-environment)"
70 (define (within-nested-ellipses id lvl)
71 (let loop ((s id) (n lvl))
74 (loop #`(#,s (... ...)) (- n 1)))))
76 ;; Analyze the set of bound identifiers IDS. Return four values:
78 ;; capture: A list of forms that will be emitted in the expansion of
79 ;; `the-environment' to capture lexical variables.
81 ;; formals: Corresponding formal parameters for use in the lambda that
82 ;; re-introduces those variables. These are temporary identifiers, and
83 ;; as such if we have a nested `the-environment', there is no need to
84 ;; capture them. (See the notes on nested `the-environment' and
87 ;; wrappers: A list of procedures of type SYNTAX -> SYNTAX, used to wrap
88 ;; the expression to be evaluated in forms that re-introduce the
89 ;; variable. The forms will be nested so that the variable shadowing
90 ;; semantics of the original form are maintained.
92 ;; patterns: A terrible hack. The issue is that for pattern variables,
93 ;; we can't emit lexically nested with-syntax forms, like:
95 ;; (with-syntax ((foo 1)) (the-environment))
96 ;; => (with-syntax ((foo 1))
97 ;; ... #'(with-syntax ((foo ...)) ... exp) ...)
99 ;; The reason is that the outer "foo" substitutes into the inner "foo",
100 ;; yielding something like:
102 ;; (with-syntax ((foo 1))
103 ;; ... (with-syntax ((1 ...)) ...)
105 ;; Which ain't what we want. So we hide the information needed to
106 ;; re-make the inner pattern binding form in the lexical environment
107 ;; object, and then introduce those identifiers via another with-syntax.
110 ;; There are four different kinds of lexical bindings: normal lexicals,
111 ;; macros, displaced lexicals, and pattern variables. See the
112 ;; documentation of syntax-local-binding for more info on these.
114 ;; We capture normal lexicals via `make-box', which creates a
115 ;; case-lambda that can reference or set a variable. These get
116 ;; re-introduced with an identifier-syntax.
118 ;; We can't capture macros currently. However we do recognize our own
119 ;; macros that are actually proxying lexicals, so that nested
120 ;; `the-environment' forms are possible. In that case we drill down to
121 ;; the identifier for the already-existing box, and just capture that
124 ;; And that's it: we skip displaced lexicals, and the pattern variables
125 ;; are discussed above.
127 (define (analyze-identifiers ids)
129 (datum->syntax #'here (gensym "t ")))
130 (let lp ((ids ids) (capture '()) (formals '()) (wrappers '()) (patterns '()))
133 (values capture formals wrappers patterns))
135 (let ((id (car ids)) (ids (cdr ids)))
136 (call-with-values (lambda () (syntax-local-binding id))
140 (if (or-map (lambda (x) (bound-identifier=? x id)) formals)
141 (lp ids capture formals wrappers patterns)
144 (cons #`(make-box #,id) capture)
147 #`(let-syntax ((#,id (identifier-syntax-from-box #,t)))
152 (lp ids capture formals wrappers patterns))
154 (let ((b (procedure-property val 'identifier-syntax-box)))
156 (lp ids (cons b capture) (cons b formals)
158 #`(let-syntax ((#,id (identifier-syntax-from-box #,b)))
162 (lp ids capture formals
164 #`(let-syntax ((#,id (unsupported-binding '#,id)))
169 (let ((t (datum->syntax id (gensym "p ")))
170 (nested (within-nested-ellipses id (cdr val))))
171 (lp ids capture formals
173 #`(with-syntax ((#,t '#,nested))
176 ;; This dance is to hide these pattern variables
177 ;; from the expander.
178 (cons (list (datum->syntax #'here (syntax->datum id))
183 (error "what" type val))))))))))
185 (define-syntax the-environment
189 #'(the-environment the-environment))
190 ((the-environment scope)
191 (call-with-values (lambda ()
193 (syntax-locally-bound-identifiers #'scope)))
194 (lambda (capture formals wrappers patterns)
195 (define (wrap-expression x)
196 (let lp ((x x) (wrappers wrappers))
199 (lp ((car wrappers) x) (cdr wrappers)))))
200 (with-syntax (((f ...) formals)
202 (((pname plvl pformal) ...) patterns)
203 (wrapped (wrap-expression #'(begin #f exp))))
204 #'(make-lexical-environment
206 (lambda (exp pformal ...)
207 (with-syntax ((exp exp)
213 (list (list 'pname plvl #'pformal) ...)))))))))
215 (define (env-module e)
217 ((lexical-environment? e) (resolve-module (syntax-module (lexenv-scope e))))
219 (else (error "invalid lexical environment" e))))
221 (define (env-boxes e)
223 ((lexical-environment? e) (lexenv-boxes e))
225 (else (error "invalid lexical environment" e))))
227 (define (local-wrap x e)
229 ((lexical-environment? e)
230 (apply (lexenv-wrapper e)
231 (datum->syntax (lexenv-scope e) x)
236 (within-nested-ellipses (datum->syntax scope name) lvl)))
237 (lexenv-patterns e))))
238 ((module? e) #`(lambda () #f #,x))
239 (else (error "invalid lexical environment" e))))
241 (define (local-eval x e)
242 "Evaluate the expression @var{x} within the lexical environment @var{e}."
243 (apply (eval (local-wrap x e) (env-module e))
246 (define* (local-compile x e #:key (opts '()))
247 "Compile and evaluate the expression @var{x} within the lexical
248 environment @var{e}."
249 (apply (compile (local-wrap x e) #:env (env-module e)
250 #:from 'scheme #:opts opts)