Finite state machine in D

Philippe Sigaud philippe.sigaud at gmail.com
Thu Sep 27 04:15:47 PDT 2012


On Thu, Sep 27, 2012 at 10:15 AM, Druzhinin Alexandr
<news at digitalmars.com> wrote:
> 27.09.2012 14:48, Mirko Pilger пишет:
>
>>> What is the D-way to implement fsm with code generation?

I'm not sure you need code generation. Using D functions literals or
closures already gives you a good part of a FSM:

States are functions, that accept a current 'payload' and return a
tuple consisting of another function and the new payload.
The FSM driver just call the current state on the current payload,
stores the new stat/payload pair and loop until it reaches a terminal
state.

I can search at home to see what I have (if I kept anything), if
you're interested. It's a FSM in 10-20 lines of code, IIRC.

If you really want code generation, could you give us some use case?
std.regex contains a compile-time regex engine that is, in a way, a
FSM on steroids. I have a parser generator project on github that also
does code generation, but both modules are for a particular type of
automata: parsers/matchers, not a generic FSM.


More information about the Digitalmars-d-learn mailing list