gpp icon indicating copy to clipboard operation
gpp copied to clipboard

Cannot define a macro that defines macros

Open sharpjs opened this issue 5 years ago • 4 comments

How does one define a macro that defines other macros?

What I think should work...

#define DEF(k,v) #eval \#define k v
DEF(foo,bar)
foo <-- should be lowercase BAR
k   <-- should be lowercase K

...does not.

$ gpp test.txt
#define foo bar
foo <-- should be lowercase BAR
k   <-- should be lowercase K

Taking out the backslash...

#define DEF(k,v) #eval #define k v
DEF(foo,bar)
foo <-- should be lowercase BAR
k   <-- should be lowercase K

...does not help.

$ gpp test.txt

foo <-- should be lowercase BAR
v   <-- should be lowercase K

sharpjs avatar Jun 18 '19 03:06 sharpjs

In principle it is possible to define user versions of meta-macros, though this can be tricky and I'm not sure that there is any way of getting GPP to evaluate something as a macro name. I've tried several variations on your example and can't come up with anything that works.

logological avatar Aug 07 '19 09:08 logological

This is very tricky, and my example uses a highly customized syntax, but I hope the idea should be clear. I use twice defeval, the second time escaped, and this allow to define and redefine a local macro (_partial in the example). Finally the new macro is undefined.

<# Quote character, only for this file #>&
<%mode quote "\\">&
<#
 # Usage: <%partial name arg...>
 #
 # Like include but passing (up to 8) parameters to the included file.
 # Assume 'm' filename extension.
 #>&
<%define partial
    <%defeval _partial
        <\%defeval _partial
            <\%include "$1.m">
        >
    ><%_partial><%_partial "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"><%undef _partial>
>&

fadado avatar Aug 08 '19 06:08 fadado

Thanks, @fadado . I will give that a try!

sharpjs avatar Aug 10 '19 13:08 sharpjs

Really weird to me that there isn't a dead simple way to do this. I ran into this limitation within 2 minutes of using gpp.

blueforesticarus avatar Dec 29 '23 08:12 blueforesticarus