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