by RodgerTheGreat · 14y ago · 0 comments
I am reminded of a particularly interesting DSL[1] for Forth that makes state machines look like their transition tables:
4 WIDE FSM: <Fixed.Pt#>
\ input: | other? | num? | minus? | dp? |
\ state: ---------------------------------------------
( 0 ) DROP >0 EMIT >1 EMIT >1 EMIT >2
( 1 ) DROP >1 EMIT >1 DROP >1 EMIT >2
( 2 ) DROP >2 EMIT >2 DROP >2 DROP >2 ;
[1]http://galileo.phys.virginia.edu/classes/551.jvn.fall01/fsm....The state machine would look something like this:
(define a
(automaton init
(init : (c -> loop))
(loop : (a -> loop)
(d -> loop)
(r -> end))
(end : )))it's fairly simple syntax, using goto's.
I used nested C# Enumerators to implement a parser which didn't turn into too much of a mess, but it was a small project.
Structured programming limits you in the state machines you can construct, but probably in a good way.
Compact and efficient state machines are immensely useful when working with small microcontrollers that don't even run an OS.
Yes, though tail call optimization will allow also handle this problem well.