Skip to content

Commit

Permalink
mod obu: Cleanup transpilation-specific control flow (#598)
Browse files Browse the repository at this point in the history
Some `continue`s in loops were not translated, and instead turned into a
long `if` block with the opposite condition. This turns them back into
`continue`.

And `switch`es with early `break`s and fall-throughs were turned into
`current_block_[0-9]+`-style state machines that are very confusing to
read. This keeps them as state machines, but uses this pattern:

```rust
let mut state = scrutinee;
loop {
    match state {
        A => {
            ...
            state = B;
            continue; // fall-through
        }
        B => {
            if early_break_condition {
                break;
            }
        }
    }
    break;
}
```

`break`s at the end of `switch` cases turn into nothing like a normal
`match` arm. Early `break`s turn into `break`s out of the surrounding
`loop`. And fall-throughs turn into switching to the following state and
`continue`ing. This is a lot more readable than the
`current_block_[0-9]+`-style state machines and closely matches how C
writes it.

There is one early `break` that I left as a long `else` block as I think
it's easier to read that way without adding the surrounding `loop`.
  • Loading branch information
kkysen authored Dec 8, 2023
2 parents 0a9567d + bc0f83a commit 40b3eca
Showing 1 changed file with 348 additions and 372 deletions.
Loading

0 comments on commit 40b3eca

Please sign in to comment.