lua-users home
lua-l archive

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


On Tue, Jun 19, 2012 at 10:22 PM, Thijs Schreijer
<thijs@thijsschreijer.nl> wrote:
> Feedback welcome!

Yep, it's good to have a template to start from, I always got confused
at first and then eventually I had enough C code to copy 'n paste
from. Obviously not very scientific!

Currently, your template is Lua 5.1, but it would be straightforward
to throw in a few #ifdefs to handle most of the issues.

But there's more than one way to skin this avocado [1]

With winapi, I used LuaMacro to preprocess the C code. Here's a simple example:

// preprocess using luam -C -llc str.l.c > str.c
#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;
  }
}

And that would handle the double-entry bookkeeping, emit #line
directives, and generally I've had no trouble working with the
original. For full-blown effect, compare winapi.l.c with its output
winapi.c.  The 'class' macro is not perfect (no obvious way) to
indicate inheritance) but man it saved a lot of typing.

The curious thing is that is mostly how I use LuaMacro these days ;)

steve d.

[1] a more vegetarian-friendly alternative to the usual. No
metaphorical kittens were hurt in its production.