This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
Re: how to write macroexpand
- From: Per Bothner <per at bothner dot com>
- To: kawa at sourceware dot org
- Date: Sun, 17 Oct 2010 16:53:07 -0700
- Subject: Re: how to write macroexpand
- References: <loom.20100811T123159-717@post.gmane.org> <4C6469A8.4000005@bothner.com> <m239s44hd9.fsf@gmail.com>
On 10/17/2010 02:52 PM, Helmut Eller wrote:
* Per Bothner [2010-08-12 21:37] writes:
So my question is: What do you need macro-expand for? The main use I
can think
of is when debugging macros. Note that the --debug-print-expr command-line
option prints out "expanded" syntax, though the syntax isn't actually valid
Scheme - just a pretty-printed form of the Expression tree.
Below is the code for a simple macroexpand that uses the first stage of
the compiler and turns the AST back to a s-expressions which are
hopefully more readable than the debug dumps.
Nice - I'd like to include this in gnu/kawa/slib. A couple of issues:
(1) I need some kind of copyright statement (as part of the source file):
preferably either that you're releasing the code to the public domain,
or you're assigning copyright to me on behalf of the Kawa project.
(2) I was trying to figure out if there is a standard name for this kind
of function. SRFI-96 http://srfi.schemers.org/srfi-96/srfi-96.html does
have macroexpand, but only in the context of supporting defmacro. I see that
Guile has a macroexpand, but I haven't found the documentation, and it
doesn't seem to do as much. Racket has expand-syntax and related forms,
but these yield syntax objects, not sexprs. Common Lisp has macroexpand,
but it doesn't seem like it does a full expansion - just at the toplevel.
The name macroexpand is fine - I just wonder if someone knows if other
Schemes
use a different name.
(3) We should have at least a handful of test-cases to make sure I
don't inadvertently break macroexpand, given that it reaches into
implementation internals.
Unfortunately, some
fields in the IfExps class are private so we can't do that for ifs.
I just checked in accessors for that.
Another issue are resolved functions in the AST that can't be easily
mapped back to the source names.
I guess there are cases where we prematurely create a QuoteExp rather
than a ReferenceExp. But the few tests I tried seemed to work OK.
...
The pretty printer doesn't turn quote back to ' which would improve
readability and it's even worse for backquote.
Fixing that would be tricky, since the right thing is content-dependent.
A macro-expand->string function (that returns a string rather than an
sexpr) might be able to do a better job.
Note also that nothing is printed for '#!void.
I changed DisplayFormat.java so that readable output of Values.empty
returns "#!void". That does not handle the general case of multiple values,
which are still printed non-readably.
Thanks for this contribution!
--
--Per Bothner
per@bothner.com http://per.bothner.com/