lua-users home
lua-l archive

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

arguably, it's not too hard to find a limited subset of Lua that coincides with _javascript_ after a few text replacements (begin/end => {  }  and such)

Supposing that you do it for some practical reason rather than the simple pleasure of hacking, if a simple solution works, there's no reason to pick a more complex one.

> Actually, if you plan to write a translator that converges toward full Lua
> support, some architectural choices made by Vlad would be seriously
> unoptimal, although they made sense in his case.

that would be a really amazing project, i guess it would amount to a
full compiler.

If you're looking for full compatibility with no efficiency nor readability constraint at all, then the simplest way would probably be to implement a bytecode interpreter in _javascript_. However, while it looks like a serious / tedious development effort, I doubt it would qualify as "amazing" :)

A more challenging approach would be to try to keep the generated code as structurally similar as possible to the original Lua input. Of course, the more "tricky primitives" you rule out (setfenv, setmetatable, coroutine.*, ...), the easier it is to achieve structural integrity; eventually you'll reach the point where you only handle the common subset of Lua and _javascript_, and then you could probably just rely on a set of string.gsub(), as you proposed above.

A more interesting approach would be a gracefully degrading one: as your program start to introduce usage of trickier primitives, you "degrade" to more faithful but less readable translation schemes. Ideally, you should be able to contain such degradations within limited parts of a program, so that, for instance, using a metatable in one bit of a program doesn't force to use special function encodings metamethods in the whole rest of that program. As an extreme, coroutines might have to be encoded through CPS transformation!

That would involve non-modular program analysis, the design of several levels of translation which are reasonnably interoperable, some clever propagation algorithms... That would be amazing indeed :)
-- Fabien.