Title: "resettable" loop DSL with C macros?
Tags: c;macros;dsl
Question: I want a loop to run once unless set to repeat, but I don't like the ugliness of having to explicitly set variables as part of normal program flow.
I'm using this but the project maintainers didn't like it:
int ok = 0;
while (ok^=1) {
// ...
if (something_failed) ok = 0;
(compare to while (!ok) { ok = 1; // ...)
The nice thing is that you can wrap these in macros:
#define RETRY(x) while (x^=1)
#define FAIL(x) x = 0
and use them as
int ok = 0;
RETRY(ok) {
// ...
if (something_failed) FAIL(ok);
How can I make these macros work without the weird xor-assign?
Comment: STOP. Macros are not used for that. Write simple while loops.

Comment: Lua uses vmcase and vmswitch or w/e macros for the VM. glib uses no-op macros (`N_` I think) for gettext markers. I'd consider both of them DSLs?

Comment: While macros are occasionally used to emit inline boiler-plate code, this particular example does seem ugly.

Comment: C is a general-purpose language. When in Rome, do as the Romans. And I'm fairly certain that the Romans never used function-like macros to hide away control statements.


Here is another answer: Using XOR 1 to toggle something between 0 and 1 repeatedly is perfectly fine, particularly in hardware-related code. Is this what you are trying to do? But this isn't how you are using it, so it doesn't make sense. Also, using it together with a signed int is questionable.
Please don't invent some ugly macro language, that's 10 times worse! This is the worst thing you can do.
There exists no reason why you can't simply do something along the lines of this:
bool retry = true;
while(retry)
retry = false;
if(something_failed) retry = true;
Comment for this answer: I'm not using `ok` after the loop, so the final value doesn't matter.
Comment for this answer: Exactly. Setting it to 0 means I want to go for a new attempt. It's called `RETRY`, that's because you *run it again* on failure.
Comment for this answer: @SoniEx2 Yeah and...? The more I read this, the less sense it makes. Do you realize that `ok^=1` changes ok from 0 to 1 and from 1 to 0? So that if you have `if (something_failed) ok = 0;` you will stay in the loop.
Comment for this answer: @SoniEx2 Ok so forget about everything I said about XOR making sense, you code is nothing but obfuscation. What you actually want is `bool retry = true;
while(retry)
retry = false;
if(something_failed) retry = true;
}`. Write self-documenting code that can be understood by other programmers.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:06/04/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><macros><dsl>', 'Title': '"resettable" loop DSL with C macros?', 'ViewCount': 70, 'id': 50688488, 'user_AboutMe': '<p>Pronouns: they/them</p>\n', 'user_AccountId': 4544581.0, 'user_DisplayName': 'SoniEx2', 'user_Reputation': 1734.0, 'user_Views': 229.0, 'user_id': 3691554.0}}