[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Table of functions as state machine
- From: "Mike Crowe" <drmikecrowe@...>
- Date: Fri, 18 Jul 2008 01:26:11 -0400
Hi folks,
I've worked out a process that works for me, but I'm convinced it could be easier. I'm organizing a state machine in a table, choosing an entry point, and letting the machine run it until the last function is called.
This is an asynchronous process which is polled, so it may be called multiple times before it changes states.
Some questions:
1) I'd love a way to add debugging code to what the function name is (i.e. if it is "process1", for example)
2) I ran across something about tail calls, or tail recursion, does that apply?
-- It might start as:
state = system["menu"].prepare
....
--- In the polling loop:
if type(state) == "function" then
ret,state = state(data)
return state
end
.....
-- The function table
system = {
menu = {
prepare = function(data)
...
return newdata,process1
end,
process1 = function(data)
...
return newdata,process2
end,
process2 = function(data)
....
return newdata,nil
end,
},
update = {
prepare = function(id,data)
...
return newdata,process1
end,
process1 = function(data)
...
return newdata,process2
end,
process2 = function(data)
....
return newdata,nil
end,
},
}