lua-users home
lua-l archive

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


> Point taken. Its a bit of a pain "if nil"-ing some code out and then
having to
> comment it out as well to avoid it being in the release build. I mentioned
> assertions as the C preprocessor solves both of these problems (ie. C
macros can
> be used). The Lua preprocessor was removed in v4, I cant remember the
reason.
> Perhaps a simple preprocessor would be the answer. It could be written in
Lua
> and distributed with the source, perhaps as an optional wrapper to dofile.
>
> Nick
>

I actually wrote a simple pre-processor for my MUD server. It originally
started out as a way to store meta-data about a script file, but I
eventually expanded it into a semi-full preprocessor. It does defines, ifs
and ifnots. Basically, an example script might look like:

@define MY_CONSTANT "Some Constant"
@define MY_NUMBER 128
@define _DEBUG

@if _DEBUG
function SomeFunction()
    print("Debug SomeFunction()")
end
@else
function SomeFunction()
    print("Release SomeFunction()")
end
@endif

print(MY_CONSTANT .. " : " .. MY_NUMBER)
SomeFunction()

In addition to @if there is @ifnot, which is of course the inverse. @if and
@ifnot use the definition of a macro as their conditional. This if I put a
Lua comment in front of @define _DEBUG, the @if _DEBUG would evaluate false,
and use the @else code instead. Note that the choice of @ as my statement
token was arbitrary, and it could be changed with a single line of code to
any symbol you saw fit (heck, if this were meant to be distributed as a Lua
extension library, you could make it a C #define in the header file).

Obviously its not perfect. It does not do full macro expansion with
parameterized macros and it doesn't do nested @ifs.

I have the code for the pre-processor wrapped up in a C++ class,
CScriptFile. It uses some utility functions I also wrote, and it all relies
heavily on C++ and STL. If your interested in the code, let me know, I will
send it to you (though it may not compile right out of the box, as it relies
on some software services that only my entire software package offers it).

Matt "Kerion" Holmes