Polystate: Composable Finite State Machines

I encountered a compiler bug and am waiting for an official fix.

1 Like

New impl:

examples:

Atm state graph

The blue arrow indicates Next, and the black arrow indicates Current.

The way to work around the zig compiler bug above is to have at least one black arrow.

1 Like

That bug is unfortunate, I’ve ran into a few labeled switch bugs myself (they seem to be all too common).

I came up with a hack to fix it, though. Just update run_handler like so:

pub fn run_handler(curr_id: StateId, ctx: *Context) RetType {
    @setEvalBranchQuota(10_000_000);
    sw: switch (curr_id) {
        inline 0...state_map.avl.len - 1 => |idx| {
            // Remove when https://github.com/ziglang/zig/issues/24323 is fixed:
            {
                var runtime_false = false;
                _ = &runtime_false;
                if (runtime_false) continue :sw 0;
            }

            << rest of switch prong >>
        },
        else => unreachable,
    }
}
1 Like