logological / gpp

GPP, a generic preprocessor
https://logological.org/gpp
GNU Lesser General Public License v3.0
192 stars 33 forks source link

Cannot define a macro that defines macros #36

Open sharpjs opened 5 years ago

sharpjs commented 5 years ago

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
logological commented 4 years ago

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.

fadado commented 4 years ago

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>
>&
sharpjs commented 4 years ago

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

blueforesticarus commented 6 months ago

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.