lua-users home
lua-l archive

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


----- Original Message -----
From: Jerome Vuarand
Date: 10/18/2011 7:25 AM
Some languages, like C or C++, use a preprocessor to implement such features. The compiler itself takes its translations units whole and consistent, just like Lua. I believe you can use existing preprocessors (m4, gcc -E, etc.) and apply them to Lua source files.
I wrapped the mcpp preprocessor [1] as a Lua module. You can find it here [2].

An example:

function preprocess(filename, defines, includes)
    require 'mcpp'

    local mcppInput = {}
    mcppInput[#mcppInput + 1] = "-P"
    mcppInput[#mcppInput + 1] = filename

    for _, define in ipairs(defines) do
        mcppInput[#mcppInput + 1] = "-D"
        mcppInput[#mcppInput + 1] = define
    end

    for _, includeDirectory in ipairs(includes) do
        mcppInput[#mcppInput + 1] = "-I"
        mcppInput[#mcppInput + 1] = includeDirectory
    end

    return mcpp.preprocess(mcppInput)
end

-Josh

[1] - http://mcpp.sourceforge.net/
[2] - https://github.com/jjensen/luaplus51-all/tree/master/Src/Modules/mcpp