[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Redefining a macro in terms of its earlier definitio

From: Peter Bex
Subject: Re: [Chicken-users] Redefining a macro in terms of its earlier definition
Date: Thu, 19 Nov 2009 13:20:55 +0100
User-agent: Mutt/

On Thu, Nov 19, 2009 at 12:14:54PM +0000, Tony Sidaway wrote:
> Suppose I have a macro, mac.
> I want to modify the expansion to expand to something along these lines:
> (if condition new-code old-mac)
> where old-mac would be an expansion of the old macro mac with exactly
> the same arguments.  This would enable macros to be extended in a way
> that is compatible with their earlier definitions, without duplicating
> the original expansion explicitly.
> Now
>   (define-syntax (mac x r c)
>     (let (      (%if (r 'if))
>                 (%printf (r 'printf)
>         (%string? (r 'string?)))
>       `(,%if (,%string? ,(cadr x))
>          (,%printf "mactest2 ~s~%" ,(cadr x))
>          ,(mac (cadr x)))))
> However this doesn't work because as well as expanding the old version
> of the macro it also executes it.

What you want is to test the string at macro expansion time and either
expand to the new definition or an application of the old macro:

   (define-syntax (mac x r c)
     (let (      (%if (r 'if))
                 (%printf (r 'printf))
                 (%old-mac (r 'old-mac)))
          (%string? (r 'string?)))
       (if (string? (cadr x))
           `(,%printf "mactest2 ~s~%" ,(cadr x))
           `(,%old-mac ,(cadr x)))))

I think you need to import the old macro under a different name,
otherwise you would indeed be calling yourself.  So the file in which
the above macro is located would contain something like:

   (import (rename macro-supplying-module (mac old-mac)))

I haven't tested any of the above.  Unfortunately, I haven't proved
it correct either ;)

"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth

reply via email to

[Prev in Thread] Current Thread [Next in Thread]