me and C have our disagreements but sometimes i just gotta say "god I love C" when the jank hits just right

I should probably explain what prompted this

I'm my VM there's try/catch. So when transpiling to C, if there's an error, jump to the handler. So far so good

And I'm generating C code programmatically so I can just if (error) goto myhandler; everywhere but that sucks for readability

Fortunately I use macros to keep the code way more readable and all of those macros use a THROW macro when there's an error. So the throw macro can jump to the handler, problem solved! Right?

Well, not so much

See you can't store a label in C in a variable and then goto on the variable to jump to whatever label you stored in there

I mean you can, sometimes, but it's an extension that isn’t universally supported. So how do I tell throw where to jump to? Macro abuse! :D

For each block of code you know statically what the error handler label is. So if I define an EXCEPTION_HANDLER macro that has that name in it, the throw macro can use that and it'll change depending on where in the code throw is used

So now I have a bunch of this in my code

EXCEPTION_HANDLER
EXCEPTION_HANDLER myhandler

Then a block of code followed by

EXCEPTION_HANDLER
EXCEPTION_HANDLER default_exception_handler

Is it jank? Yes, extremely. Does it do what I need it to do? Also yes. Do I feel embarrassed about this in any way? Not at all

My implementation of defer is also like this but it's even worse because defer needs to be able to jump back due to early exits

So I define the entire deferred block of code as a single macro. And then based on whether the compiler supports computed gotos (labels stuck in a variable) or not it either jumps to the block and back out and puts the block in once or it just inserts the whole damn block every single time because C hates you

Actually the defer thing is so grotesque I'm gonna get out of bed so I can show you all how awful it is

Eniko Fox

@eniko@mastodon.gamedev.place

So the first bit is the macros that would make the magic happen and then the 2nd bit is what it would look like in actual code

It's awful I love it

July 17, 2025 at 7:36:26 PM

Cinema sins voice: The fact that this works! ding

This is reminding me of the kind of stuff Gambit would get up to in order to translate Scheme's first-class continuations into C

Elk Logo

Welcome to Elk!

Elk is a nimble Mastodon web client. You can login to your Mastodon account and use it to interact with the fediverse.

Expect some bugs and missing features here and there. Elk is Open Source and we're actively improving it as a community project. Join us and let's build it together!

If you'd like to report a bug, help us testing, give feedback, or contribute, reach out to us on GitHub and get involved.

To boost development, you can sponsor the Team through GitHub Sponsors. We hope you enjoy Elk!

Joaquín Sánchez三咲智子 Kevin DengAnthony FuPatakTAKAHASHI ShuujiDaniel Roe

The Elk Team