Synthesis icon indicating copy to clipboard operation
Synthesis copied to clipboard

Query state

Open WinstonHartnett opened this issue 5 years ago • 2 comments

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

WinstonHartnett avatar Jun 24 '20 21:06 WinstonHartnett

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?

mratsim avatar Jun 27 '20 12:06 mratsim

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.

WinstonHartnett avatar Jun 30 '20 05:06 WinstonHartnett