lua-users home
lua-l archive

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



Our backup alternative is precompile the rules off-line
and generate the corresponding state machine (expressed
as a lua script)

Bear in mind (sorry, this doesn't actually answer your question, but is just an observation on a neat feature of Lua which you might not be aware of when creating state machine code) that Lua supports proper tail calls, meaning that if the value a function returns is computed _directly by calling another function_, that call (the tail call) is implemented as a jump, not as a call.

So tail calls don't consume any call stack, and can therefore be recursively nested to any depth without ever running out of memory.

This makes them an ideal and readable way to implement state machines.

Each state is a function, and each state transition is a tail call from one state function to another. Even for machine-generated state machines this is rather handy -- transitions are expressed directly and naturally in the language itself, and the code to perform the transitions is generated directly, and super-efficiently, by the compiler.

There's a clear example in the Programming in Lua book, V1 of which is on-line and V2 of which is in any good bookshop (and at Amazon).