From: Panicz Maciej Godek <godek.maciek@gmail.com>
To: "guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: Way to control the order of macro expansion
Date: Fri, 3 Jan 2014 01:21:00 +0100 [thread overview]
Message-ID: <CAMFYt2bc-YPVjq23QsGYXuZV8Bb1WNm+5njq4oUY_R1oheDioQ@mail.gmail.com> (raw)
In-Reply-To: <CAMFYt2ZBO+4U5=BK=4x8UVKEpkaYyOQUeq6EQ+nn4fWRO_HA3Q@mail.gmail.com>
It turned out, that the above solution doesn't work
exactly as expected -- since the scopes of "private"
and "interface" get separated (by the with-syntax
form that I have been so proud of), it is impossible
for the public forms to refer to private bindings.
To solve that issue, I had to pass the private
bindings to the function passed with-syntax
and then retrieve them. For some reason (which
is unclear to me) I had to convert them to datum
and then back to syntax, because if they were
given verbatim, they failed to get renamed
properly. I'd appreciate if anyone could explain
it to me, or reviewed the code below to tell
whether the names and comments aren't
misleading (e.g. I used the name "lexical-context"
for a variable, but I'm not sure whether the entity
that it refers to can properly be called "lexical
context")
(define-syntax private+public
(lambda (stx)
(define (interface+name+body specs lexical-context)
;; the second argument is the lexical context that we
;; want to preserve while extracting the specs
(define (interface-name interface)
(match interface
((head . tail)
(interface-name head))
((? symbol? name)
name)))
`(,(datum->syntax stx (syntax->datum lexical-context))
;; for some reason we need to deconstruct and reconstruct
;; lexical-context here
,(map (lambda(spec)
(syntax-case spec ()
((interface . body)
(datum->syntax stx `(,(syntax->datum #'interface)
,(interface-name
(syntax->datum #'interface))
,(syntax->datum #'body))))))
specs)))
(syntax-case stx ()
((_ (private ...) ((pub-define . pub-spec) ...))
;; we pass the private definitions to the interface+name+body,
;; because we want to be able to refer to private definitions
;; from within the public ones (otherwise the macro processor
;; would treat them as if they were within separate contexts)
(with-syntax ((((private ...) ((interface name body) ...))
(interface+name+body #'(pub-spec ...) #'(private ...))))
#'(begin
(define name (and (defined? 'name) name))
...
(let ()
private ...
(set! name
(let ()
(pub-define interface . body)
name))
...)))))))
prev parent reply other threads:[~2014-01-03 0:21 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-01-01 22:46 Way to control the order of macro expansion Panicz Maciej Godek
2014-01-02 14:30 ` Panicz Maciej Godek
2014-01-02 16:17 ` Chris Vine
2014-01-03 0:21 ` Panicz Maciej Godek [this message]
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=CAMFYt2bc-YPVjq23QsGYXuZV8Bb1WNm+5njq4oUY_R1oheDioQ@mail.gmail.com \
--to=godek.maciek@gmail.com \
--cc=guile-user@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).