minetest-i4/i4/util/luacheck.lua

142 lines
2.9 KiB
Lua
Raw Permalink Normal View History

2021-11-30 11:39:35 -06:00
local exec = os.execute
local fmt, find, sub = string.format, string.find, string.sub
local var = "[%w%.%[%]\"\'_]"
2022-06-22 18:14:45 -05:00
local _,_, fs_elements = dofile("../src/styles.lua")
2021-11-30 11:39:35 -06:00
2022-01-14 20:12:58 -06:00
exec "clear"
2021-11-30 11:39:35 -06:00
local function split(str, delim, include_empty, max_splits, sep_is_pattern)
delim = delim or ","
max_splits = max_splits or -2
local items = {}
local pos, len = 1, #str
local plain = not sep_is_pattern
max_splits = max_splits + 1
repeat
local np, npe = find(str, delim, pos, plain)
np, npe = (np or (len+1)), (npe or (len+1))
if (not np) or (max_splits == 1) then
np = len + 1
npe = np
end
local s = sub(str, pos, np - 1)
if include_empty or (s ~= "") then
max_splits = max_splits - 1
items[#items + 1] = s
end
pos = npe + 1
until (max_splits == 0) or (pos > (len + 1))
return items
end
local files = {
"api",
"bags",
"caches",
"callbacks",
"common",
2022-06-23 07:32:59 -05:00
"compression",
2021-11-30 11:39:35 -06:00
"detached_inv",
2022-01-08 10:05:29 -06:00
"fields",
2021-11-30 11:39:35 -06:00
"groups",
"gui",
"hud",
"model_aliases",
"progressive",
"styles",
}
local operators = {
["([%+%-%*%^/&|])="] = function(a, b, c)
return fmt("%s = %s %s %s", a, a, b, c)
end,
["+%+"] = function(a, b)
return fmt("%s = %s + 1\n%s", a, a, b)
end,
["&"] = function(a, b)
return fmt("bit.band(%s, %s)", a, b)
end,
["|"] = function(a, b)
return fmt("bit.bor(%s, %s)", a, b)
end,
["<<"] = function(a, b)
return fmt("bit.lshift(%s, %s)", a, b)
end,
[">>"] = function(a, b)
return fmt("bit.rshift(%s, %s)", a, b)
end,
["<<="] = function(a, b)
return fmt("%s = bit.lshift(%s, %s)", a, a, b)
end,
[">>="] = function(a, b)
return fmt("%s = bit.rshift(%s, %s)", a, a, b)
end,
}
local function compile(data)
data = data:gsub("IMPORT%((.-)%)", function(a)
return "local " .. a:gsub("\"", "") .. " = i3.get(" .. a .. ")"
end)
2022-06-22 18:14:45 -05:00
data = data:gsub("([%w_]+)%(", function(a)
if fs_elements[a] then
return fmt("fs('%s',", a)
end
end)
2022-06-23 07:32:59 -05:00
data = data:gsub("([%w_]+)-%-\n", function(a)
return fmt("%s = %s - 1", a, a)
end)
2021-11-30 11:39:35 -06:00
for op, func in pairs(operators) do
data = data:gsub("(" .. var .. "+)%s?" .. op .. "%s?(" .. var .. "*)", func)
end
return data
end
for _, p in ipairs(files) do
local function _load(path, line, data, t)
if line then
if not t then
t = split(data, "\n")
end
t[line] = t[line]:gsub("(" .. var .. "+)%s?=%s?(" .. var .. "*)", "%2")
data = table.concat(t, "\n")
else
local file = assert(io.open(path, "r"))
data = file:read"*a"
file:close()
data = compile(data)
end
local l, err = loadstring(data)
if not l then
local err_line = tonumber(err:match(":(%d+):"))
if line ~= err_line then
return _load(path, err_line, data, t)
end
end
2022-06-23 07:32:59 -05:00
local _file = io.open(path:match("(.*)%.") .. ".lc", "w")
2021-11-30 11:39:35 -06:00
_file:write(data)
_file:close()
end
2022-01-14 20:12:58 -06:00
_load("../src/" .. p .. ".lua")
2021-11-30 11:39:35 -06:00
end
2022-01-14 20:12:58 -06:00
exec "luacheck ../init.lua"
2022-06-22 18:14:45 -05:00
exec "luacheck ../src/preprocessor.lua"
2022-06-23 07:32:59 -05:00
exec "luacheck ../src/*.lc"
exec "rm ../src/*.lc"