mirror of
https://github.com/Reuh/candran.git
synced 2025-10-27 09:59:29 +00:00
125 lines
3.1 KiB
Text
125 lines
3.1 KiB
Text
local candran = require("candran")
|
|
local util = {}
|
|
|
|
function util.search(modpath, exts={})
|
|
for _, ext in ipairs(exts) do
|
|
for path in package.path:gmatch("[^;]+") do
|
|
local fpath = path:gsub("%.lua", "."..ext):gsub("%?", (modpath:gsub("%.", "/")))
|
|
local f = io.open(fpath)
|
|
if f then
|
|
f:close()
|
|
return fpath
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function util.load(str, name, env)
|
|
if _VERSION == "Lua 5.1" then
|
|
local fn, err = loadstring(str, name)
|
|
if not fn then return fn, err end
|
|
return env ~= nil and setfenv(fn, env) or fn
|
|
else
|
|
if env then
|
|
return load(str, name, nil, env)
|
|
else
|
|
return load(str, name)
|
|
end
|
|
end
|
|
end
|
|
|
|
function util.recmerge(...)
|
|
local r = {}
|
|
for _, t in ipairs({...}) do
|
|
for k, v in pairs(t) do
|
|
if type(v) == "table" then
|
|
r[k] = util.merge(v, r[k])
|
|
else
|
|
r[k] = v
|
|
end
|
|
end
|
|
end
|
|
return r
|
|
end
|
|
|
|
function util.merge(...)
|
|
local r = {}
|
|
for _, t in ipairs({...}) do
|
|
for k, v in pairs(t) do
|
|
r[k] = v
|
|
end
|
|
end
|
|
return r
|
|
end
|
|
|
|
util.cli = {
|
|
-- add option to set Candran options to an argparse parser
|
|
addCandranOptions = function(parser)
|
|
parser:group("Compiler options",
|
|
parser:option("-t --target")
|
|
:description "Target Lua version: lua54, lua53, lua52, luajit or lua51"
|
|
:default(candran.default.target),
|
|
|
|
parser:option("--indentation")
|
|
:description "Character(s) used for indentation in the compiled file"
|
|
:default(candran.default.indentation),
|
|
|
|
parser:option("--newline")
|
|
:description "Character(s) used for newlines in the compiled file"
|
|
:default(candran.default.newline),
|
|
|
|
parser:option("--variable-prefix")
|
|
:description "Prefix used when Candran needs to set a local variable to provide some functionality"
|
|
:default(candran.default.variablePrefix),
|
|
|
|
parser:flag("--no-map-lines")
|
|
:description "Do not add comments at the end of each line indicating the associated source line and file (error rewriting will not work)"
|
|
)
|
|
|
|
parser:group("Preprocessor options",
|
|
parser:flag("--no-builtin-macros")
|
|
:description "Disable built-in macros",
|
|
|
|
parser:option("-D --define")
|
|
:description "Define a preprocessor constant"
|
|
:args("1-2")
|
|
:argname{"name", "value"}
|
|
:count("*"),
|
|
|
|
parser:option("-I --import")
|
|
:description "Statically import a module into the compiled file"
|
|
:argname("module")
|
|
:count("*")
|
|
)
|
|
|
|
parser:option("--chunkname")
|
|
:description "Chunkname used when running the code"
|
|
|
|
parser:flag("--no-rewrite-errors")
|
|
:description "Disable error rewriting when running the code"
|
|
end,
|
|
|
|
-- convert parsed arguments to a Candran options table
|
|
makeCandranOptions = function(args)
|
|
local preprocessorEnv = {}
|
|
for _, o in ipairs(args.define) do
|
|
preprocessorEnv[o[1]] = tonumber(o[2]) or o[2] or true
|
|
end
|
|
|
|
local options = {
|
|
target = args.target,
|
|
indentation = args.indentation,
|
|
newline = args.newline,
|
|
variablePrefix = args.variable_prefix,
|
|
mapLines = not args.no_map_lines,
|
|
chunkname = args.chunkname,
|
|
rewriteErrors = not args.no_rewrite_errors,
|
|
builtInMacros = not args.no_builtin_macros,
|
|
preprocessorEnv = preprocessorEnv,
|
|
import = args.import
|
|
}
|
|
return options
|
|
end
|
|
}
|
|
|
|
return util
|