Note that there are some explanatory texts on larger screens.

plurals
  1. POC nested switches: outer switch's case inside inner switch
    text
    copied!<p>I'm adding coroutine support to an interpreter I'm writing and I'd like to do something like the following:</p> <pre><code>typedef enum { bar_stuff, bar_other } Bar; typedef enum { foo_error=-1, foo_none=0, foo_again } Foo_state; Foo_state do_foo(Bar bar,Foo_state foo) { switch(foo) { case foo_none: //start switch(bar) { case bar_stuff: //do stuff return foo_none; case bar_other: //do other stuff return foo_again; case foo_again: //!! this doesn't work /* edit: this is supposed to be a case of * switch(foo), not switch(bar) */ //do more other stuff return foo_none; default: //stuff return foo_none; } default: //fail return foo_error; } } </code></pre> <p>Obviously this dosn't work (I get duplicate case value, alternative is probably undefined behavior/segfault). I could write switch(bar) as an if/else if/else chain, but I was hoping there was a better way.</p> <p>I'm using gcc if that makes a difference.</p> <h3>Edit:</h3> <p>The following would work, but would be a PITA to maintain:</p> <pre><code>Foo_state do_foo2(Bar bar,Foo_state foo) { switch(foo) { case foo_none: goto case_foo_none; case foo_again: goto case_foo_again; default: //fail return foo_error; } case_foo_none: //start switch(bar) { case bar_stuff: //do stuff return foo_none; case bar_other: //do other stuff return foo_again; case_foo_again: //do more other stuff return foo_none; default: //stuff return foo_none; } } </code></pre> <h3>Edit 2:</h3> <p>Well, this doesn't seem to be yielding the aforementioned 'better way', so I'd like know if anyone foresees a problem with writing it like this:</p> <pre><code>Foo_state do_foo3(Bar bar,Foo_state foo) { switch(foo) { case foo_none: //start if(bar == bar_stuff) { printf("do stuff\n"); return foo_none; } else if(bar == bar_other) { printf("do other stuff\n"); return foo_again; case foo_again: //continue printf("do more other stuff\n"); return foo_none; } else { printf("stuff\n"); return foo_none; } default: //fail return foo_error; } } </code></pre> <p>The problem I see with this is missing a bar_* value (since there are several functions like this, and some of the enums have dozens of values), but I suppose a test script for that should work...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload