lua-users home
lua-l archive

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



I've thought of adding CPP (c preprocessor) support to loading lua scripts. It would simply feed them through the cpp first, then to lua. This would allow:
	#if 0 	(for commenting out blocks)
	#if HAVE_SOMETHING
	...and all the rest

There's (presumably) a ready-made hook for this in Lua 5.0 somewhere, so no patching would be necessary.

Anyone already done this?

-ak


Ando Sonenblick kirjoittaa maanantaina, 4. elokuuta 2003, kello 20:19:

Gang,

I know lua has no preprocessor, but is there a way to implement constants in
lua?

I'm setting up a table, currently with hard coded numbers:

t = {
    a = function() Dispatch(100) end,
    b = function() Dispatch(101) end,
    c = function() Dispatch(102) end
    }

(Dispatch is a registered C routine to do the internal dispatching in my
app.)

I'd like the flexibility of defining a base number (eg 100) and the rest be offsets so that if I need to shift the entire range of numbers, I need only
change one.

For example

base = 100

t = {
    a = function() Dispatch(base) end,
    b = function() Dispatch(base + 1) end,
    c = function() Dispatch(base + 2) end
    }

I know I can do the above, but I don't want the extra bit of variable look up and math on each call... I want "base + x" to be resolved at compile time
into a number constant.

Is this possible?

If not, I may implement my own preprocessor to do what I need. One question
about that:

If I replace instances of base with a constant number:

t = {
    a = function() Dispatch(100) end,
    b = function() Dispatch(100 + 1) end,
    c = function() Dispatch(100 + 2) end
    }

And then have lua compile that, is the compile process smart enough to
collapse the contant math into a single contant: such as

t = {
    a = function() Dispatch(100) end,
    b = function() Dispatch(101) end,
    c = function() Dispatch(102) end
    }

Or if I call b, will it perform 100 + 1 and than pass that to Dispatch?

Thx,
Ando


-----------------
SpriTec Software
www.spritec.com