[Top][All Lists]

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

[Chicken-users] Redefining a macro in terms of its earlier definition

From: Tony Sidaway
Subject: [Chicken-users] Redefining a macro in terms of its earlier definition
Date: Thu, 19 Nov 2009 12:14:54 +0000

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.

So I want the new macro definition to expand to code that tests for a
condition and evaluates to a new value if a condition holds, but
evaluates exactly the same as the old expansion if the condition is

Let's assume I want to write the second version of mac as an explicit
renaming macro.  What it boils down to is that I need to capture the
expansion of the old version of mac within the definition of the new

Suppose the first version of mac is exported by module mactest1

I can do something like this:

(import-for-syntax mactest1)

This gives me access to the macro expansion of the old mac at macro
expansion time.


  (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.  The error message displays it as

(if39 (#%string? "Hello") (printf40 "mactest2 ~s~%" "Hello") #<unspecified>)

(if39, printf40 and #%string? are results of the naming and are bound
to the same syntax as if, printf and string?)

The value #<unspecified> is the result of executing the expression
(mac (cadr x)) within the macro expander, which is obviously not what
I wanted.

Without import-for-syntax, no expansion occurs because the macro
expander has no knowledge of the original macro and it treats mac as
an unbound variable.ᩡ

Restoring define-for-syntax and quoting the internal invocation of mac, thus:

',(mac (cadr x))

is more successful, but the quoted expression is evaluated by the
resulting code irrespective of the result of the if.

Placing the quote inside the unquote is not good:

,'(mac (cadr x))

This seems to send the macro expander into an eternal loop, presumably
trying to expand its own body indefinitely.

What I need is for the expansion of the original macro to be inserted
into the expansion of the new macro in the place where
"#<unspecified>" appears above.

Is this possible?

reply via email to

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