block nesting: I take half of it back

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

block nesting: I take half of it back

Norman Ramsey-3
I spoke a bit too soon.  It turns out if your problem is too many
begins, you can diagnose it without help from lua, by doing some macro
hacking and using atexit(3).  For too many ends, Lua would still have
to tell you the line on which the bad end occurs.

It would be even nicer if each begin returned a cookie that had to
passed to the matching end at run time, and you could diagnose these
things instantly...


Norman

P.S.  Here's the trick:

In your header file, this:

extern char **blockfiles;
extern int *blocklines;
#define lua_beginblock() (*blockfiles++ = __FILE__, *blocklines++ = __LINE__, \
			  lua_beginblock())
#define lua_endblock() (--blockfiles, --*blocklines, lua_endblock())
extern void showall(void);


And in your main()

  atexit(showall);

and the implementation is:

static char *bfiles[1024];
static int blines[1024];
char **blockfiles = bfiles;
int *blocklines = blines;
void showall(void) {
  while (blockfiles > bfiles)
    fprintf(stderr, "Open block at %s:%d\n", *--blockfiles, *--blocklines);
}


And a sample output is:


: nr@labrador 2379 ; r ./p
./pdbconv pack locus.asc small.pdb
Database.records is table
listlength is function
3 should be 3
35 records in database
lua: `lua_beginblock': too many nested blocks
lua: exit(1). Unable to recover
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240
Open block at pdbconv.nw:240


Reply | Threaded
Open this post in threaded view
|

Re: block nesting: I take half of it back

Norman Ramsey-3
Following up my own post, this code will cause a core dump:

 > #define lua_endblock() (--blockfiles, --*blocklines, lua_endblock())

Remove the *.

I hate C.