unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: guile-devel <guile-devel@gnu.org>
Subject: Re: syntax-local-binding
Date: Sun, 15 Jan 2012 18:22:06 +0100	[thread overview]
Message-ID: <87zkdo7uf5.fsf@pobox.com> (raw)
In-Reply-To: <874nvw99za.fsf@pobox.com> (Andy Wingo's message of "Sun, 15 Jan 2012 18:00:41 +0100")

[-- Attachment #1: Type: text/plain, Size: 155 bytes --]

On Sun 15 Jan 2012 18:00, Andy Wingo <wingo@pobox.com> writes:

> Attached is a patch that implements a new accessor,
> syntax-local-binding.

Here 'tis!


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-add-syntax-local-binding.patch --]
[-- Type: text/x-diff, Size: 4309 bytes --]

From 09ba44abeb47cdf4ec61df6f7386217f0cbe30c7 Mon Sep 17 00:00:00 2001
From: Andy Wingo <wingo@pobox.com>
Date: Sun, 15 Jan 2012 17:51:02 +0100
Subject: [PATCH] add syntax-local-binding

* module/ice-9/boot-9.scm (syntax-local-binding): New binding.

* module/ice-9/psyntax.scm: Locally define a fluid that holds the
  "transformer environment".  with-transformer-environment calls a
  procedure with the transformer environment, or raises an error if
  called outside the extent of a transformer.  Bind
  transformer-environment in expand-macro.
  (syntax-local-binding): New procedure to return binding information of
  a lexially bound identifier (a lexical, local macro, or pattern
  variable).
---
 module/ice-9/boot-9.scm  |    1 +
 module/ice-9/psyntax.scm |   39 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index f661d08..9cdd8d1 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -389,6 +389,7 @@ If there is no handler at all, Guile prints an error and then exits."
 (define generate-temporaries #f)
 (define bound-identifier=? #f)
 (define free-identifier=? #f)
+(define syntax-local-binding #f)
 
 ;; $sc-dispatch is an implementation detail of psyntax. It is used by
 ;; expanded macros, to dispatch an input against a set of patterns.
diff --git a/module/ice-9/psyntax.scm b/module/ice-9/psyntax.scm
index 1bf3c32..dcabafe 100644
--- a/module/ice-9/psyntax.scm
+++ b/module/ice-9/psyntax.scm
@@ -786,6 +786,14 @@
                       id))))))
          (else (syntax-violation 'id-var-name "invalid id" id)))))
 
+    (define transformer-environment
+      (make-fluid
+       (lambda (k)
+         (error "called outside the dynamic extent of a syntax transformer"))))
+
+    (define (with-transformer-environment k)
+      ((fluid-ref transformer-environment) k))
+
     ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
     ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
 
@@ -1321,8 +1329,10 @@
                    (syntax-violation #f "encountered raw symbol in macro output"
                                      (source-wrap e w (wrap-subst w) mod) x))
                   (else (decorate-source x s)))))
-        (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
-                              (new-mark))))
+        (with-fluids ((transformer-environment
+                       (lambda (k) (k e r w s rib mod))))
+          (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
+                                (new-mark)))))
 
     (define expand-body
       ;; In processing the forms of the body, we create a new, empty wrap.
@@ -2435,6 +2445,31 @@
     (set! syntax-source
           (lambda (x) (source-annotation x)))
 
+    (set! syntax-local-binding
+          (lambda (id)
+            (arg-check nonsymbol-id? id 'syntax-local-value)
+            (with-transformer-environment
+             (lambda (e r w s rib mod)
+               (define (strip-anti-mark w)
+                 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
+                   (if (and (pair? ms) (eq? (car ms) the-anti-mark))
+                       ;; output is from original text
+                       (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
+                       ;; output introduced by macro
+                       (error "what!!!"))))
+               (let ((label (id-var-name (syntax-object-expression id)
+                                         (strip-anti-mark (syntax-object-wrap id)))))
+                 (if (not (string? label))
+                     (error "identifier not lexically bound" id))
+                 (let ((b (assq-ref r label)))
+                   (if (not b)
+                       (error "displaced lexical" id))
+                   (case (binding-type b)
+                     ((lexical) (values 'lexical (binding-value b)))
+                     ((macro) (values 'local-macro (binding-value b)))
+                     ((syntax) (values 'pattern-variable (binding-value b)))
+                     (else (error "unpossible!" b)))))))))
+
     (set! generate-temporaries
           (lambda (ls)
             (arg-check list? ls 'generate-temporaries)
-- 
1.7.8.3


[-- Attachment #3: Type: text/plain, Size: 26 bytes --]


-- 
http://wingolog.org/

  reply	other threads:[~2012-01-15 17:22 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-15 17:00 syntax-local-binding Andy Wingo
2012-01-15 17:22 ` Andy Wingo [this message]
2012-01-19 11:41   ` syntax-local-binding Andy Wingo
2012-01-20 20:26     ` syntax-local-binding Mark H Weaver
2012-01-20 21:23       ` syntax-local-binding Andy Wingo
2012-01-20 22:03         ` syntax-local-binding Mark H Weaver
2012-01-22  0:03           ` syntax-local-binding Ludovic Courtès
2012-01-23 16:05           ` syntax-local-binding Andy Wingo
2012-01-23 21:03             ` syntax-local-binding Mark H Weaver
2012-01-23 22:19               ` syntax-local-binding Andy Wingo
2012-01-24  2:11                 ` syntax-local-binding Mark H Weaver
2012-01-24 11:42                   ` syntax-local-binding Andy Wingo
2012-01-24 17:29                     ` syntax-local-binding Noah Lavine
2012-01-24 10:30                 ` syntax-local-binding Peter TB Brett
2012-01-24 10:38                   ` syntax-local-binding David Kastrup
2012-01-24 11:26                   ` syntax-local-binding Andy Wingo
2012-01-24 13:25                     ` syntax-local-binding Mark H Weaver
2012-01-24 20:28                       ` mark uniqueness (Was: Re: syntax-local-binding) Andy Wingo
2012-01-25  0:26                         ` mark uniqueness Mark H Weaver
2012-01-25  9:02                           ` Andy Wingo
2012-01-24 21:22                       ` syntax-local-binding Andy Wingo
2012-01-25  2:30                         ` syntax-local-binding Mark H Weaver
2012-01-25  7:49                           ` syntax-local-binding Stefan Israelsson Tampe
2012-01-25 11:18                           ` syntax-local-binding Andy Wingo
2012-01-25 13:18                           ` syntax-local-binding Ludovic Courtès
2012-01-25 18:08                             ` syntax-local-binding Mark H Weaver
2012-01-26 11:21                             ` syntax-local-binding Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zkdo7uf5.fsf@pobox.com \
    --to=wingo@pobox.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).