[gambit-list] debugging macros

Marc Feeley feeley at iro.umontreal.ca
Tue Jan 27 15:51:27 EST 2009

On 27-Jan-09, at 2:34 PM, eryiling wrote:

> suppose I have:
> (define-macro (foo ...))
> the standard way to 'debug' this is to do:
> (pp (lambda () (foo ...)))
> however, this requires that (foo ...) expands to proper scheme code  
> (i.e. not things like "(a b . c)"), and also code that has well  
> defined semantic meaning (i.e. no undefined vars)
> as i write macros, i find that most of the time (in fact, except for  
> the starting point and the ending point), my macro  expansion offten  
> results in non-scheme code; is there a way i can see this output?  
> i.e. I wnat to see:
> (macroexpand '(foo ...))
> rather than the particular error for why (foo ... ) does not expand  
> to legit scheme code
> thanks!

Here's the trick I often use.  Say you have the macro definition:

(define-macro (foo x y)

you simply add a (list 'quote ...) around the body of your macro:

(define-macro (foo x y)
   (list 'quote ...something...))

Then when you can do

(pp (foo 1 2))

and this will print the s-expression that ...something... generated.

For example:

% gsi
Gambit v4.4.0

 > (define-macro (bind var val expr)
     (list 'quote `(let ((,var ,val)) ,expr)))
 > (pp (bind x (+ 1 2) (* x x)))
(let ((x (+ 1 2))) (* x x))

Once you are done debugging, you strip off the (list 'quote ...)  


More information about the Gambit-list mailing list