lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Jorge <xxopxe@gmail.com> writes:

> On Wed, 2010-01-13 at 15:44 +0100, David Kastrup wrote:
>> Wesley Smith <wesley.hoke@gmail.com> writes:
>> >> Tail calls are good for state machines.
>> >> ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-443.pdf
>> >
>> > I find coroutines more elegant.
>> 
>> Yup.  Nothing beats using the instruction pointer and dynamic state as
>> state variable.
>
> Ok, hit me with a spoon, can anyone post an example of using
> coroutines to implement a state machine?

Uh, the posting you replied to contained an example right after the part
you quoted.  Let me fetch that spoon.

> I guess each state is a coroutine, and transitions are yields and
> resumes, but the details escape me.

No, the whole "state machine" is one coroutine.  You just program the
control flow that is to be modelled by the state machine.  Yield/resume
pairs are used to get new input, and the input is processed with normal
conditionals and other branches.  Every code path containing a yield
call corresponds to a state.

-- 
David Kastrup