lua-users home
lua-l archive

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



On 16-Feb-07, at 11:05 AM, Asko Kauppi wrote:


In my tests with Lua Lanes, multithreaded programming using multiple Lua states, I've come to the wish to embed separate chunks of Lua code, within surrounding chunks. This can currently be done by using strings, but has a few limitations to it:

thread [[
   local a,b,c= ...
   -- ...
   return xx
]]

- Chunks embedded as strings are compiled (syntax checked) at runtime (the 'thread' call), not at compile time - Chunks embedded will have their own line numbering, not that of the actual (surrounding) source file - Chunk source takes up space (as a string) when it could actually be stored as a byte-compiled 'chunk'

One could have a special syntactic format of declaring 'sub-chunks', solving all the above issues at once. Maybe this could even be done as a tkn fltr ;) --not saying the word, i bet there's already people fltering out such posts!--

You could pass compiled chunks from state to state by serializing them with string.dump. This might not actually use up less space, though.

An alternative would be to do this inside a token filter. The token filter would fire up a new parser, with a separate token filter; the interior token filter would simply serialize the tokens, using some reasonably compact format. If the parse succeeded, the serialized string of tokens could be passed to the other state, and deserialized with a token filter which was not connected to the lexer (i.e. which just deserialized the token string).

This still involves parsing the chunk twice, but it saves one lex, and there is a good chance that the serialized token stream would be smaller than either the source code or a compiled chunk.