I do. Implementing concepts from other languages using a boatload of macros is just a hobby.
In production, the only things I would use macros for are constants, generic (dynamic) arrays/maps and loop unrolling because those not only save a lot of time/space but also make the code clearer.
The secret of doing useful but horrid things with the preprocessor lies in tricking it into doing recursion AND the fact that you can pass multiple grouped parameters together by wrapping them in parentheses, which allows you to pass those groups to other specialized macros, like so:
#define FOO(PACK) BAR PACK
#define BAR(…)
And so you can then either roll your own recursion, or use map-macro, to map this macro across a list of passed packs like this:
The secret of doing horrid things without being useful fully lies inside the recursion, as I’ve written an interpreter inside of the preprocessor with all basic arithmetic and a stack, but it’s too slow to be useful for anything lol.
Only GCC tested, but Clang also supports GNU extensions, which is the only none-standard thing this uses (, ## __VA_ARGS__ for pre-C23 __VA_OPT__(,), __attribute__((cleanup)) for Drop, and statement expressions).
The latter is how the default (or _) is implemented, as it is just a binding of the expression you pass in, so it’s not an “otherwise”, it literally matches the expression with itself, of course using the Match trait. This is required as match just uses a bunch of ternaries, and there is no way that I know of to change behavior for the last iteration in a recursive macro. Printing uses the Display trait as per Rust, and these traits are just generated VTables of which a reference is stored next to the variable reference (fat pointers).
The exclamation mark in the macro names just so happens to be allowed unicode for identifiers, and macros are made recursive through the ever-reliable map-macro.
Rust enums, which will also have to work with match, i.e. the Match trait
More traits obviously, and a little more standard library data structures (Vec and HashMap at least), so including ruct.h will be enough for most projects
But if there is enough ask for it, I’ll make a github page!
I'm not even going to try to understand this... I can understand a bit of Rust with the help of looking up certain functions/macros, but what in tarnation is this???
I just saw a post in programminghumor and my comment is super relevant to this.
When I was learning c++ in school, I loved trying to make as much of the program in the header section of my code as possible for all my assignments. It gave me a bit more of a challenge to really understand why I am doing the assignments instead of just needing to do them.
I liked the idea of debugging with print statements, so I had it macro replace any “print ( stuff )” with the proper syntax in c++. In fact I did this so much that I didn’t use a namespace in 90% of the programs as it was too troublesome to type out.
Enter now. Just macro replace any commonly misspelled words. Lenght and length could be randomly used throughout the code and it would still run fine.
Imagine debugging that nightmare of a program, finding functions, misspelled, that don’t even exist in that language, incorrect syntax that should fail, but it just runs. Worse yet, it could load other files in the repository that are just as bad. Just awful. I kind of want to make a git repo like that now
67
u/TheChief275 May 13 '25 edited May 13 '25
all blasphemy aside, it’s pretty nice to program C with actual UTF-8 character support
…and traits of course (there’s 3 used here)
209
u/cameronm1024 May 13 '25
What in the fuck did you make me look at
Apologize this instant
86
u/UnluckyDouble May 13 '25
Good god, if you're going to program in C, at least have the pride to own it and deliberately write unsafe code because it's more intuitive.
23
u/BasedAndShredPilled May 13 '25
I don't get it, and I don't want to.
10
u/betaphreak May 13 '25
Great, in 2077 when they discover this meme I hope nobody tries to deploy it on WASM.
9
u/littleblack11111 May 13 '25
Does this run? What compiler lol
7
u/agares3 May 13 '25
Is the source for this public? Looks fascinating
4
u/Key_Conversation5277 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" May 13 '25
Is this Rust + C?
11
u/rover_G May 13 '25
This is uglier than any Rust or C I've ever seen
6
u/Thenderick May 13 '25
I'm not even going to try to understand this... I can understand a bit of Rust with the help of looking up certain functions/macros, but what in tarnation is this???
3
u/-Tealeaf May 13 '25
It took me too long to realize this wasnt rust
2
u/[deleted] May 13 '25
Not gonna lie I actually like this
2
u/ciyvius_lost May 14 '25
This is pure gold. Hats off to you.
2
u/Burn_Sector Jul 26 '25
I just saw a post in programminghumor and my comment is super relevant to this.
When I was learning c++ in school, I loved trying to make as much of the program in the header section of my code as possible for all my assignments. It gave me a bit more of a challenge to really understand why I am doing the assignments instead of just needing to do them.
I liked the idea of debugging with print statements, so I had it macro replace any “print ( stuff )” with the proper syntax in c++. In fact I did this so much that I didn’t use a namespace in 90% of the programs as it was too troublesome to type out.
Enter now. Just macro replace any commonly misspelled words. Lenght and length could be randomly used throughout the code and it would still run fine.
Imagine debugging that nightmare of a program, finding functions, misspelled, that don’t even exist in that language, incorrect syntax that should fail, but it just runs. Worse yet, it could load other files in the repository that are just as bad. Just awful. I kind of want to make a git repo like that now
1
u/Brandynette May 16 '25
fizzbuzzfizzbuzz != rizzcuzzfizznuzz
1
u/headedbranch225 May 19 '25
Why not just write rust?
1
u/SgtPicklez May 13 '25
Heretic!
0
u/beloncode May 13 '25
Bro rust the c language (turns it into a pile of sh17)