Re:Metaprogramming custom control structures in C (2012)
by wudangmonk on Saturday July 10, 2021 @01:29PM
(#27795010)
If you want to use metaprogramming in C you are better off doing the parsing/tokenizing yourself and creating your own macros than trying to use the C preprocessor, its a lot less work.
[ Parent
| on HN ]
Re:Metaprogramming custom control structures in C (2012)
by ludocode on Saturday July 10, 2021 @03:45PM
(#27795921)
But then your code can't be used by anyone without your preprocessor. There is a lot of value in plain C metaprogramming because it can be compiled with an ordinary C compiler.
You can, however, use a script to generate some of the preprocessor boilerplate while still having the templates configurable and instantiable with an ordinary C compiler. This is how my metaprogramming library Pottery works:
https://github.com/ludocode/pottery
It uses #include for templates rather than code block macros, something the article doesn't really go into. It's more powerful this way and the templates are far more readable; aside from the generated metaprogramming boilerplate, the templated code looks like (and preprocesses to) ordinary C.
Re:Metaprogramming custom control structures in C (2012)
by colejohnson66 on Saturday July 10, 2021 @09:48PM
(#27797881)
Let me preface this with: I’m not a Rust fanatic (I still like C#). But Rust’s “procedural macros” are pretty interesting. They’re not just regular typed macros (which Rust has), but a function that takes a token stream and outputs a new token stream. This allows pretty cool macros like hex! for example: https://docs.rs/hex/0.4.2/hex/
Re:Metaprogramming custom control structures in C (2012)
by bruce343434 on Saturday July 10, 2021 @03:02PM
(#27795638)
So basically create your own language + compiler that compiles to C?
Re:Metaprogramming custom control structures in C (2012)
by auxym on Saturday July 10, 2021 @03:23PM
(#27795780)
Or use Nim, which compiles to C :)
Re:Metaprogramming custom control structures in C (2012)
by bruce343434 on Saturday July 10, 2021 @03:28PM
(#27795803)
To each their own :)
Re:Metaprogramming custom control structures in C (2012)
by wudangmonk on Saturday July 10, 2021 @03:10PM
(#27795694)
Yes, its less work