-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_example.lua
More file actions
62 lines (56 loc) · 1.58 KB
/
advanced_example.lua
File metadata and controls
62 lines (56 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
luafsm = require('luafsm')
-- example use of a factory to generate states
function new_char_printer(char, transition)
return function()
io.write(char)
return true, transition
end
end
-- example use of a function to generate superstates
function printer(str, transition)
state = {}
state.substates = {}
for index = 1, #str do
if index < #str then
state.substates[index] =
new_char_printer(str:sub(index, index), index + 1)
else
state.substates[index] =
new_char_printer(str:sub(index, index))
end
end
state.entry = 1
state.exit = {true, transition}
return state
end
-- define a hello world printing state machine
print_hello_world = {
entry = "print_hello",
substates = {
print_hello = printer("hello", "print_space"),
print_space = function()
io.write(" ")
return true, "print_world"
end,
print_world = {
entry = "print_w",
substates = {
print_w = new_char_printer("w", "print_o"),
print_o = new_char_printer("o", "print_r"),
print_r = new_char_printer("r", "print_l"),
print_l = new_char_printer("l", "print_d"),
print_d = new_char_printer("d"),
},
exit = {true, "print_exclamation_mark"}
},
print_exclamation_mark = function()
print("!")
return true
end,
},
exit = true,
}
-- create an instance of the state machine
fsm = luafsm.create(print_hello_world)
-- run the state machine until it finishes
while fsm() == false do end