[gambit-list] handling of macros

Marc Feeley feeley at iro.umontreal.ca
Sun Nov 13 20:02:37 EST 2005


On 8-Aug-05, at 12:00 PM, Christian wrote:

> Hello
>
> Some questions:
>
> (the first two of those may be rather general scheme philosophy
> questions)
>
> - why are DSSSL style argument declarations not allowed in
> define-macro?
>

In the upcoming beta 16 this will be allowed (see my next message).

> - why is there no (define-macro foo (lambda(...)...)) form? Is there
> a lowlevel variant for setting runtime and compiletime macro bindings
> to some lambda?
>

I'm not sure I understand.  This works fine:

 > (define-macro when (lambda (x y) `(if ,x ,y)))
 > (when #t 111)
111
 > (when #f 111)
 >

> - is there a way to get at the converter lambda from a (runtime-)macro
> definition?
>

Not pretty, but this works:

 > (define (macro-converter name)
     (cond ((##macro-lookup (##cte-top-cte ##interaction-cte) name)  
=> cdr)
           (else #f)))
 > (pp (macro-converter 'when))
(lambda (x y) `(,'if ,@`(,x ,@`(,y ,@'()))))
 > (macro-converter 'for)
#f

> - I need a way to unset a macro definition, so that the corresponding
> name can be used as normal function/variable binding from there on.
>

There is no way to do this currently.

> - I'd like to be able to scan a namespace for all bindings and macro
> bindings. For example for readline completion. (And to be able to
> remove all runtime macros.)
>

Look at the definition of ##cte-lookup in lib/_eval.scm .  Just cons- 
up a list of what is in the compile-time environment.

> - how can I macro-expand code with the original context? Until now
>   I've used
>
>     (caddr (##decompile (eval `(lambda() ,code))))

How about (pp (lambda () code)) ?

>
>   but code is eval'ed in a new lexical context of course.
>   (I haven't understood how to use ##macro-expand. I would be grateful
>   for any help for understanding the sources. How do the cte
>   (=compile-time environment?) and such work? What's the exact
>   concept?)
>
> I want macro expansion for writing macros which expand their arguments
> before working on them. For playing with optimizations (kind of like
> optimizing mini-compilers).
>

I understand what you want, but the Gambit interpreter and compiler  
aren't designed to expand the macros first and then compile the  
code.  The macro expansion and compilation are intertwined.  So there  
is no procedure in the interpreter to only macro expand some code.   
The closest to what you want really is "eval".

> - is there a way to preserve the line/column numbering of source code
> which is "wrapped" by macros? For something like for example (receive
> (a b) (code1) code2) it would be nice if when an error happens in
> code1 or code2, the error message didn't just mention the line/column
> of the opening paren of the receive form, but the line/column where
> the relevant part of code1 or code2 is in the original file.

It is really hard (impossible?) to preserve all source code location  
information when expanding macros that can perform arbitrary  
computation (i.e. which is what define-macro provides).  Source code  
location information is preserved by the syntax-case expander.   
Unfortunately the syntax-case expander that is provided with Gambit  
is not fully integrated with the system (some of the Gambit syntax  
extensions are not handled by the syntax-case system).

Marc




More information about the Gambit-list mailing list