mratsim / Synthesis

Synthesis is a compiletime, procedure-based, low-overhead, no-allocation, state-machine generator optimized for communicating processes and threads
Other
89 stars 5 forks source link

Query state #7

Open WinstonHartnett opened 4 years ago

WinstonHartnett commented 4 years ago

Is there any way to query the state of a machine directly (e.g. without manually setting an extra flag)?

mratsim commented 4 years ago

No there is no way, in the generated code the "state" doesn't exist even in memory it's simply the location of the code you are in. For example in pseudo code

type
  GrabLunch = enum
    OpenTheDoor
    WalkToRestaurant
    OrderDish
    WalkBack
    HaveLunch
    Exit

Would become

proc grabLunch(restaurant: Restaurant) =
  block OpenTheDoor:
    discard
  block WalkToRestaurant:
    if restaurant.isClosed():
      goto WalkBack
    else
      goto OrderDish
  block OrderDish:
    discard
  block WalkBack:
    if restaurant.isClosed():
      goto Exit
    else:
      goto HaveLunch
  block HaveLunch:
    discard
  block Exit:
    discard

The various states are just labels of code sections and are not materialized.

That said you always know when you are in a certain state.

Do you need something like a currentState() function?

WinstonHartnett commented 4 years ago

Ah, I see. Yes, a currentState function would be helpful.

I am hoping to use Synthesis for FSMs in a quick game I am writing with the Godot game engine Nim bindings. Sometimes, different code needs to run continuously at a different rate than the FSM's synthesize proc, which would be easy with a currentState function.