lua-users home
lua-l archive

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


On Thu, Oct 4, 2012 at 9:33 AM, Rapin Patrick <rapin.patrick@gmail.com> wrote:
>
>
> I would better use standard Lua syntax with a normal "if", but instead use a
> specially marked condition. Something like :
> ----
> function COMPILE_TIME(cond) return cond end
>
> if COMPILE_TIME(condition) then
>   print("something")
> else
>   print("something else")
> end
> -----
>
> The advantage is that it the script will run normally unprocessed, no matter
> what "condition" is.
> Now, since Lua language is not very difficult to parse, it should then not
> be a big problem to write a filter that looks for "if (not)?
> COMPILE_TIME(.*) then .* end" pattern, evaluate the constant condition, and
> outputs a scripts without those runtime tests.
>

Seconded! Such a thing would be very nice for debugging, too, since it
means you don't have to "recompile" Lua code when testing changes.

Though if you want to get a little more into it, here's a somewhat
more involved (but more useful) proposal:

(1) Use a PREPROCESSOR() function that can be searched for and
replaced with a constant at compile time.
(2) Do static analysis on the processed AST to find constant
expressions that can be pre-evaluated.
(3) Do another pass on the AST to find control structures with
constant conditionals and eliminate the unused cases.

/s/ Adam