mirror of
https://github.com/minetest-mods/i3.git
synced 2025-07-05 01:50:22 +02:00
Add custom operators (Part 1)
This commit is contained in:
66
src/operators.lua
Normal file
66
src/operators.lua
Normal file
@ -0,0 +1,66 @@
|
||||
local fmt = string.format
|
||||
local _loadfile = loadfile
|
||||
local var = "[%w%.%[%]_]"
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
local function compile(data)
|
||||
for op, func in pairs(operators) do
|
||||
data = data:gsub("(" .. var .. "+)%s?" .. op .. "%s?(" .. var .. "*)", func)
|
||||
end
|
||||
|
||||
return data
|
||||
end
|
||||
|
||||
local function _load(path, line, data)
|
||||
if line then
|
||||
data = data:split"\n"
|
||||
data[line] = data[line]:gsub("(" .. var .. "+)%s?=%s?(" .. var .. "*)", function(_,b) return b end)
|
||||
data = table.concat(data, "\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)
|
||||
end
|
||||
end
|
||||
|
||||
return l, err
|
||||
end
|
||||
|
||||
function loadfile(path)
|
||||
return _load(path) or _loadfile(path)
|
||||
end
|
Reference in New Issue
Block a user