Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot define a macro that defines macros #36

Open
sharpjs opened this issue Jun 18, 2019 · 4 comments
Open

Cannot define a macro that defines macros #36

sharpjs opened this issue Jun 18, 2019 · 4 comments

Comments

@sharpjs
Copy link

sharpjs commented Jun 18, 2019

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
Copy link
Owner

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
Copy link

fadado commented Aug 8, 2019

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
Copy link
Author

sharpjs commented Aug 10, 2019

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

@blueforesticarus
Copy link

blueforesticarus commented Dec 29, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants