Discussion: Porting 58000 lines of D and C++ to jai, Part 0: Why and How
FeepingCreature
feepingcreature at gmail.com
Thu Nov 24 12:09:37 UTC 2022
On Thursday, 24 November 2022 at 12:04:27 UTC, FeepingCreature
wrote:
> For instance, this is valid code:
>
> import std.stdio;
> void switchtest(int i, bool b)
> {
> switch (i)
> {
> case 1:
> if (b)
> {
> case 2:
> writefln!"Case 1b or 2: %s, %s"(i, b);
> return;
> }
> break;
> default: break;
> }
> assert(false);
> }
>
> void main()
> {
> switchtest(1, true);
> switchtest(2, false);
> }
Ie. to explain what's happening here:
The syntax tree structure of `if/else` is `if (EXPRESSION)
STATEMENT [else STATEMENT]`. `static foreach` does not match
`else`, so it fails to parse.
The syntax tree structure of `switch/case` is not, as you would
expect, `switch (EXPRESSION) { [case EXPRESSION: STATEMENT*]* }`.
Instead, it's `switch (EXPRESSION) STATEMENT` and `STATEMENT =
... | CASE_STATEMENT`, `CASE_STATEMENT = case EXPRESSION:`, where
the CASE_STATEMENT is *semantically* (not syntactically) verified
to occur inside `switch`, with no other restrictions on their
placement. That's the only reason it works with `static foreach`.
(All parse rules made up from memory, not taken from the spec.)
More information about the Digitalmars-d
mailing list