Event driven state machines: simple example (timers and signals)

Hi, I am trying to implement my edsm-engine (written for Linux in C long time ago) in Zig.
Here is what I have for the moment.

Some general words about event driven SMs as a programs design methodology as I see it:

  • combining event driven approach with state machines gives fine grained concurrency within single thread
  • and so can be used as an alternative to coroutines/fibers/greenlets (without compiler support)
  • as soon as you’ve drawn state diagrams for all types of machines needed by a program you automatically have a structure for the code (each transition is a couple of functions/methods, each reaction without state change is a function/method)

Since I’ve started to learn Zig only about a couple of months ago any critical comments regarding using (or misusing) the language are welcome.

One more example. I’ve added some i/o (reading from stdin)

I’ve adjusted the code for zig 0.11.0

Was stuck a bit at

engine/edsm.zig:93:30: error: extra capture in for loop
        for (stages) |stage, k| {
                             ^
engine/edsm.zig:93:30: note: run 'zig fmt' to upgrade your code automatically

but zig fmt did it right for me, great!

1 Like