Finite state machine: Difference between revisions
Content deleted Content added
Added XPL0 example. |
Added Algol 68 |
||
Line 91: | Line 91: | ||
Refunding money |
Refunding money |
||
Machine ready: (d)eposit, or (q)uit?q |
Machine ready: (d)eposit, or (q)uit?q |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
<syntaxhighlight lang="algol68"> |
|||
BEGIN # finite state machine # |
|||
# mode representing a state in the FSM # |
|||
MODE FSMSTATE = STRUCT( INT state # code for the state # |
|||
, PROC INT next state # routine to change state # |
|||
); |
|||
# executes the FSM defined by states, starting from the initial state # |
|||
# and terminating when the exit state is reached # |
|||
PROC run fsm = ( []FSMSTATE states, INT initial state, exit state )VOID: |
|||
BEGIN |
|||
INT state := initial state; |
|||
WHILE state /= exit state DO |
|||
print( ( "Ready: " ) ); |
|||
BOOL found := FALSE; |
|||
FOR s pos FROM LWB states TO UPB states WHILE NOT found DO |
|||
IF found := state OF states[ s pos ] = state THEN |
|||
state := next state OF states[ s pos ] |
|||
FI |
|||
OD; |
|||
IF NOT found THEN |
|||
# in an invalid state - restart # |
|||
print( ( "(resetting)", newline ) ); |
|||
state := initial state |
|||
FI |
|||
OD |
|||
END # run fsm # ; |
|||
BEGIN # test FSM # |
|||
# possible states # |
|||
INT exit = 0, ready = 1, waiting = 2, dispense = 3, refunding = 4; |
|||
# prompts the user for a single character code and returns it # |
|||
# the user is re-prompted until they enter one of the characters in # |
|||
# answers # |
|||
PROC get code = ( STRING prompt, answers )CHAR: |
|||
BEGIN |
|||
CHAR response; |
|||
WHILE print( ( prompt, ": " ) ); |
|||
STRING answer; |
|||
read( ( answer, newline ) ); |
|||
response := IF answer = "" THEN REPR 0 ELSE answer[ LWB answer ] FI; |
|||
IF response >= "a" AND response <= "z" THEN |
|||
# convert lowercase response to upper # |
|||
response := REPR ( ABS response + ( ABS "A" - ABS "a" ) ) |
|||
FI; |
|||
NOT char in string( response, NIL, answers ) |
|||
DO SKIP OD; |
|||
response |
|||
END # get code # ; |
|||
run fsm( ( ( ready |
|||
, INT: IF "Q" = get code( "Enter D to deposit, Q to Quit", "DQ" ) |
|||
THEN exit |
|||
ELSE waiting |
|||
FI |
|||
) |
|||
, ( waiting |
|||
, INT: IF "S" = get code( "Enter S to Select R to Refund", "SR" ) |
|||
THEN dispense |
|||
ELSE refunding |
|||
FI |
|||
) |
|||
, ( dispense |
|||
, INT: BEGIN get code( "Remove your product and Enter R", "R" ); ready END |
|||
) |
|||
, ( refunding |
|||
, INT: BEGIN print( ( "Refunding", newline ) ); ready END |
|||
) |
|||
) |
|||
, ready |
|||
, exit |
|||
) |
|||
END |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Ready: Enter D to deposit, Q to Quit: d |
|||
Ready: Enter S to Select R to Refund: s |
|||
Ready: Remove your product and Enter R: r |
|||
Ready: Enter D to deposit, Q to Quit: d |
|||
Ready: Enter S to Select R to Refund: r |
|||
Ready: Refunding |
|||
Ready: Enter D to deposit, Q to Quit: q |
|||
</pre> |
</pre> |
||