Anonymous user
Finite state machine: Difference between revisions
→{{header|Nim}}
m (→{{header|Phix}}: added syntax colouring the hard way) |
|||
Line 1,050:
OK, quitting
</pre>
=={{header|Nim}}==
{{trans Kotlin}}
<lang Nim>import strutils
type State {.pure.} = enum Ready, Waiting, Exit, Dispense, Refunding
proc getAnswer(message: string; answers: set[char]): char =
while true:
stdout.write message, ' '
stdout.flushFile
result = (stdin.readLine().toLowerAscii & ' ')[0]
if result in answers: return
proc fsm =
echo "Please enter your option when prompted"
echo "(any characters after the first will be ignored)"
var state = State.Ready
while true:
case state
of State.Ready:
let trans = getAnswer("\n(D)ispense or (Q)uit :", {'d', 'q'})
state = if trans == 'd': State.Waiting else: State.Exit
of State.Waiting:
echo "OK, put your money in the slot"
let trans = getAnswer("(S)elect product or choose a (R)efund :", {'s', 'r'})
state = if trans == 's': State.Dispense else: State.Refunding
of State.Dispense:
discard getAnswer("(R)emove product :", {'r'})
state = State.Ready
of State.Refunding:
# No transitions defined.
echo "OK, refunding your money"
state = State.Ready
of State.Exit:
echo "OK, quitting"
break
fsm()</lang>
{{out}}
<pre>Please enter your option when prompted
(any characters after the first will be ignored)
(D)ispense or (Q)uit : d
OK, put your money in the slot
(S)elect product or choose a (R)efund : s
(R)emove product : r
(D)ispense or (Q)uit : d
OK, put your money in the slot
(S)elect product or choose a (R)efund : r
OK, refunding your money
(D)ispense or (Q)uit : q
OK, quitting</pre>
=={{header|Ol}}==
|