gpp
gpp copied to clipboard
Cannot define a macro that defines macros
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
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.
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>
>&
Thanks, @fadado . I will give that a try!
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.