how to write macroexpand

Per Bothner per@bothner.com
Thu Aug 12 21:37:00 GMT 2010


On 08/11/2010 03:50 AM, Jakub Jankiewicz wrote:
> I want to write macroexpand macro I come up with this:
>
> (define-macro (macroexpand expr)
>      `(apply ((field ,(car expr) 'expander)) ',(cdr expr)))

> When I use it with my own macro
>
> (define-macro (for params . body)
>    (let ((iter (gentemp)) (step (if (= (length params) 4)
> ...

define-macro is deprecated and barely supported, because it
does not respect "macro hygiene".  A modern Scheme macro
system works on "syntax objects", not lists.  You should
be using syntax-case or optionally the Kawa convenience
syntax define-syntax-case.

> |kawa:178|# (macroexpand (for (i 1 10) (display i)))
>
> I get it right
>
> (let GS.8 ((i 1)) (if (<= i 10) (begin (display i) (GS.8 (+ i 1)))))
>
> but when I try to use it with macro 'if' or 'let'
>
> #|kawa:183|# (macroexpand (if #t "true" "false"))
> gnu.mapping.WrongArguments: call to 'gnu.expr.ModuleMethod' has too many
> arguments (3; must be 1)
> 	at gnu.mapping.MethodProc.matchFailAsException(MethodProc.java:96)

Presumably because your macro uses define-macro, while if uses 
define-syntax.

> How to fix macroexpand?

It may be difficult - it might need to be done as a primitive, written 
in Java.

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.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/



More information about the Kawa mailing list