lua-users home
lua-l archive

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


On 12/02/2011 08:42 PM, Christian Bielert wrote:
I'm currently in the process of integrating luajit into my game engine, using its most superb FFI. I have chosen lua for various reasons, among others that I want my own class-system and lua offers the most flexibility in this.

Of course, lua only offers this flexibility by not providing this mechanism in the first place. And sadly, while it does provide the tools to build your own such mechanism, there's no way to adjust its syntax for this. As such, my current plan is to use a preprocessor to create the kind of syntax that I desire, such as "class" constructs or typed function signatures. Unrelated, but the a+=1 syntax wouldn't hurt either :)


In either case, my current concern is mostly the semantics, not the syntax. A thing that I will need pretty early on will be clean, fast type-checks on function entry. http://lua-users.org/wiki/LuaTypeChecking provides some useful information on that, though I personally would prefer something along the lines of:

function param_guard(var, dtype, default)
   if not var then return default end
   if type(var) ~= dtype then error("Type error") end
end

function foo(a, b, c)
   a = param_guard(a, "number")
   a = param_guard(b, "string", "nA")
   a = param_guard(c, "int")
end


I've been experimenting with exactly this sort of thing here:

https://github.com/richardhundt/lupa/tree/method-accessors

It's essentially a transpiler which compiles an OO curly brackety language to Lua source code. It supports guard expressions which are evaluated at run-time. I had this extended at some point to include cdata types.

The tests show examples of the language:

https://github.com/richardhundt/lupa/blob/method-accessors/test/class.ku

run with:

./bin/kula <file>

I decided to do a complete rewrite though in this branch:

https://github.com/richardhundt/lupa/tree/setfenv-for-namespaces

and I haven't got guards in yet since I've decided to build a bytecode compiler for LuaJIT2 first:

https://github.com/richardhundt/luajit2-codegen

It's all highly experimental and a bit messy, the branches diverge hugely, but right now I prefer to keep it that way because I haven't settled on the syntax and semantics yet.

Hopefully, though, you can get an idea of what can be done and how it performs. You need a fresh version of LPeg to make it work.

Cheers,
Richard