lua-users home
lua-l archive

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


On Wed, Nov 9, 2011 at 10:22 AM, Xavier Wang <weasley.wx@gmail.com> wrote:
> So maybe we can merge the 2, 3 ways. that is, we create a pure lua
> module to generate binding code, but we can use automatic binding
> generator to generate the Lua code. the interface may like this:

That's a good use of Lua as a DSL.  But I am a little confused - this
module is a script that generates C extension code? So then where does
the automatic binding generate?

My approach to the C extension issue is to preprocess C until it looks nicer ;)

#include <string.h>

module "str" {

  def at (Str s, Int i = 0) {
    lua_pushlstring(L,&s[i-1],1);
    return 1;
  }

  def upto (Str s, Str delim = " ") {
    lua_pushinteger(L, strcspn(s,delim) + 1);
    return 1;
  }

}

This is processed by LuaMacro into the usual kind of C, but handles
some of the irritating bookkeeping for you. It is clever enough to
generate #line directives and so generally it's plain sailing to track
errors etc.

This was used to generate the winapi bindings. Here's the main file:

https://github.com/stevedonovan/winapi/blob/master/winapi.l.c

which is converted into

https://github.com/stevedonovan/winapi/blob/master/winapi.c

Note how 'classes' are defined!

C is absolutely _built_ for preprocessing; the rules of this
particular game are (a) the translation rules should be few and easily
understood and (b) the output should not look machine-generated.
(There's a flag to preprocess without #line if you wish to fool your
colleagues)

steve d.