2016-11-13 14:42:57 +01:00
|
|
|
local WNOA = "wrong number of arguments"
|
|
|
|
local UAT = "unsupported argument type"
|
|
|
|
local SE = "error with cmd-cmd executing: "
|
2016-12-29 14:14:20 +01:00
|
|
|
local STRL = "attempt on exceeding maximum string length"
|
2016-11-13 14:42:57 +01:00
|
|
|
|
|
|
|
local s
|
|
|
|
s = {
|
2016-12-28 13:44:53 +01:00
|
|
|
mov = function(params)
|
2016-11-13 14:42:57 +01:00
|
|
|
return true, params[2]
|
|
|
|
end,
|
|
|
|
|
2017-02-03 23:20:22 +01:00
|
|
|
xchg = function(params)
|
|
|
|
return true, params[2], params[1]
|
|
|
|
end,
|
|
|
|
|
2016-12-31 17:16:16 +01:00
|
|
|
getvar = function(params, faden)
|
|
|
|
local p = params[1]
|
|
|
|
if type(p) ~= "string" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
p = faden.vars[p]
|
2017-01-12 18:35:11 +01:00
|
|
|
return true, {p, p ~= nil}
|
2016-12-31 17:16:16 +01:00
|
|
|
end,
|
|
|
|
|
2017-04-13 20:12:02 +02:00
|
|
|
copytable = function(params, thread)
|
|
|
|
if type(params[1]) ~= "string"
|
|
|
|
or type(params[2]) ~= "string" then
|
|
|
|
return false, "two strings expected"
|
|
|
|
end
|
|
|
|
local fromprefix = params[2] .. "."
|
|
|
|
local toprefix = params[1] .. "."
|
|
|
|
if fromprefix == toprefix then
|
|
|
|
return false, "origin and target table names must be different"
|
|
|
|
end
|
|
|
|
local tocopy = {}
|
|
|
|
local frmln = #fromprefix
|
|
|
|
for name,v in pairs(thread.vars) do
|
|
|
|
if name:sub(1, frmln) == fromprefix then
|
|
|
|
tocopy[#tocopy+1] = name:sub(frmln+1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i = 1,#tocopy do
|
|
|
|
local field = tocopy[i]
|
|
|
|
thread.vars[toprefix .. field] = thread.vars[fromprefix .. field]
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
2016-12-29 14:14:20 +01:00
|
|
|
add = function(params, faden)
|
2016-11-13 14:42:57 +01:00
|
|
|
local p1,p2 = unpack(params)
|
|
|
|
local t1 = type(p1)
|
2017-01-12 18:35:11 +01:00
|
|
|
if t1 ~= type(p2) then
|
2016-11-13 14:42:57 +01:00
|
|
|
return false, "different argument types"
|
|
|
|
end
|
|
|
|
|
|
|
|
if t1 == "number" then
|
|
|
|
return true, p1 + p2
|
|
|
|
end
|
2016-12-28 18:27:41 +01:00
|
|
|
if t1 == "boolean" then
|
2017-02-03 23:20:22 +01:00
|
|
|
for _,k in pairs(params) do
|
|
|
|
if k then
|
|
|
|
return true, true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true, false
|
2016-12-28 18:27:41 +01:00
|
|
|
end
|
2017-01-12 18:35:11 +01:00
|
|
|
if t1 == "string" then
|
|
|
|
if #p1 + #p2 > faden.strlen_max then
|
|
|
|
return false, STRL
|
|
|
|
end
|
|
|
|
return true, p1 .. p2
|
2016-12-29 14:14:20 +01:00
|
|
|
end
|
2017-01-12 18:35:11 +01:00
|
|
|
return false, UAT
|
2016-12-28 18:27:41 +01:00
|
|
|
end,
|
|
|
|
|
2017-02-03 23:20:22 +01:00
|
|
|
sub = function(params)
|
|
|
|
local b = params[2]
|
|
|
|
b = b and tonumber(b)
|
|
|
|
local t2 = type(b)
|
|
|
|
if t2 ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
local t1 = type(params[1])
|
|
|
|
if t1 == "number" then
|
|
|
|
return true, params[1] - b
|
|
|
|
end
|
|
|
|
if t1 == "string" then
|
|
|
|
return true, params[1]:sub(b, params[3] and tonumber(params[3]))
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
2016-12-28 18:27:41 +01:00
|
|
|
mul = function(params)
|
|
|
|
local p1,p2 = unpack(params)
|
|
|
|
local t1 = type(p1)
|
|
|
|
local t2 = type(p2)
|
|
|
|
if t1 == "string"
|
|
|
|
and t2 == "number" then
|
2016-12-29 14:14:20 +01:00
|
|
|
if #p1 * p2 > faden.strlen_max then
|
|
|
|
return false, STRL
|
|
|
|
end
|
2016-12-28 18:27:41 +01:00
|
|
|
return true, p1:rep(p2)
|
|
|
|
end
|
|
|
|
if t1 ~= t2 then
|
|
|
|
return false, "different argument types"
|
|
|
|
end
|
|
|
|
|
|
|
|
if t1 == "number" then
|
|
|
|
return true, p1 * p2
|
|
|
|
end
|
|
|
|
if t1 == "boolean" then
|
2017-02-03 23:20:22 +01:00
|
|
|
for _,k in pairs(params) do
|
|
|
|
if not k then
|
|
|
|
return true, false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true, p1 and p2 -- nil is tested in the first two params
|
2016-11-13 14:42:57 +01:00
|
|
|
end
|
|
|
|
return false, UAT
|
|
|
|
end,
|
|
|
|
|
2017-02-03 23:20:22 +01:00
|
|
|
div = function(params)
|
|
|
|
local p1,p2 = unpack(params)
|
|
|
|
if type(p1) ~= "number"
|
|
|
|
or type(p2) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
return true, p1 / p2
|
|
|
|
end,
|
|
|
|
|
|
|
|
inc = function(params)
|
|
|
|
if type(params[1]) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
return true, params[1] + 1
|
|
|
|
end,
|
|
|
|
|
|
|
|
dec = function(params)
|
|
|
|
if type(params[1]) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
return true, params[1] - 1
|
|
|
|
end,
|
|
|
|
|
2016-12-28 18:27:41 +01:00
|
|
|
neg = function(params)
|
|
|
|
local v = params[1]
|
|
|
|
local t = type(v)
|
|
|
|
if t == "number" then
|
|
|
|
return true, -v
|
|
|
|
end
|
|
|
|
if t == "boolean" then
|
|
|
|
return true, not v
|
|
|
|
end
|
2017-01-12 18:35:11 +01:00
|
|
|
if t == "string" then
|
2017-02-03 23:20:22 +01:00
|
|
|
return true, v:rev() -- does string.rev exist?
|
2017-01-12 18:35:11 +01:00
|
|
|
end
|
|
|
|
return false, UAT
|
2016-12-28 18:27:41 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
inv = function(params)
|
|
|
|
local p = params[1]
|
|
|
|
if type(p) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
return true, 1 / p
|
|
|
|
end,
|
|
|
|
|
|
|
|
mod = function(params)
|
|
|
|
local p1,p2 = unpack(params)
|
2017-01-12 18:35:11 +01:00
|
|
|
if type(p1) ~= "number"
|
|
|
|
or type(p2) ~= "number" then
|
2016-12-28 18:27:41 +01:00
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
return true, p1 % p2
|
|
|
|
end,
|
|
|
|
|
2016-11-13 14:42:57 +01:00
|
|
|
jmp = function(params, faden)
|
2017-01-12 18:35:11 +01:00
|
|
|
if #params >= 2
|
|
|
|
and not params[2] then
|
|
|
|
return true
|
2016-11-13 14:42:57 +01:00
|
|
|
end
|
|
|
|
local p = params[1]
|
|
|
|
if type(p) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
|
|
|
if p < 1
|
|
|
|
or p%1 ~= 0
|
|
|
|
or p > #faden.liste + 1 then
|
|
|
|
return false, "jump target out of range"
|
|
|
|
end
|
|
|
|
faden.ip = p-1
|
2016-12-28 22:05:48 +01:00
|
|
|
if not s.usleep({0}, faden) then
|
2016-11-13 14:42:57 +01:00
|
|
|
error(SE)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
2016-12-29 14:14:20 +01:00
|
|
|
call = function(params, faden)
|
2017-04-13 20:13:01 +02:00
|
|
|
local subsucc,msg = s.push({faden.ip+1}, faden)
|
2016-12-29 14:14:20 +01:00
|
|
|
if not subsucc then
|
|
|
|
return false, SE .. msg
|
|
|
|
end
|
|
|
|
subsucc,msg = s.jmp(params, faden)
|
|
|
|
if not subsucc then
|
|
|
|
return false, SE .. msg
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
|
|
|
ret = function(_, faden)
|
|
|
|
local subsucc,msg = s.pop({true}, faden)
|
|
|
|
if not subsucc then
|
|
|
|
return false, SE .. msg
|
|
|
|
end
|
2016-12-31 17:16:16 +01:00
|
|
|
subsucc,msg = s.jmp(msg, faden)
|
2016-12-29 14:14:20 +01:00
|
|
|
if not subsucc then
|
|
|
|
return false, SE .. msg
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
2016-11-13 14:42:57 +01:00
|
|
|
push = function(params, faden)
|
|
|
|
local pc = #params
|
|
|
|
if pc == 0 then
|
|
|
|
return false, WNOA
|
|
|
|
end
|
|
|
|
for i = 1,pc do
|
|
|
|
faden.stack[faden.sp] = params[i]
|
|
|
|
faden.sp = faden.sp-1
|
|
|
|
end
|
|
|
|
if faden.sp < 0 then
|
|
|
|
return false, "stack overflow"
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
|
|
|
pop = function(params, faden)
|
|
|
|
local pc = #params
|
|
|
|
if pc == 0 then
|
|
|
|
return false, WNOA
|
|
|
|
end
|
|
|
|
local rt = {}
|
|
|
|
for i = 1,pc do
|
|
|
|
faden.sp = faden.sp+1
|
|
|
|
rt[i] = faden.stack[faden.sp]
|
|
|
|
end
|
|
|
|
if faden.sp > faden.sb then
|
|
|
|
return false, "stack underflow"
|
|
|
|
end
|
|
|
|
return true, rt
|
|
|
|
end,
|
|
|
|
|
2016-12-28 13:44:53 +01:00
|
|
|
equal = function(params)
|
2016-11-13 14:42:57 +01:00
|
|
|
return true, params[1] == params[2]
|
|
|
|
end,
|
|
|
|
|
2016-12-28 13:44:53 +01:00
|
|
|
less = function(params)
|
2016-12-29 22:37:03 +01:00
|
|
|
local p1,p2 = unpack(params)
|
2016-12-31 17:16:16 +01:00
|
|
|
local t1 = type(p1)
|
2017-01-12 18:35:11 +01:00
|
|
|
if t1 ~= type(p2) then
|
2016-12-31 17:16:16 +01:00
|
|
|
return false, "different argument types"
|
|
|
|
end
|
|
|
|
|
|
|
|
if t1 ~= "number"
|
|
|
|
and t1 ~= "string" then
|
2016-11-13 14:42:57 +01:00
|
|
|
return false, UAT
|
|
|
|
end
|
2016-12-29 22:37:03 +01:00
|
|
|
return true, p1 < p2
|
2016-11-13 14:42:57 +01:00
|
|
|
end,
|
|
|
|
|
2017-02-03 23:20:22 +01:00
|
|
|
greater = function(params)
|
|
|
|
return s.less{params[2], params[1]}
|
|
|
|
end,
|
|
|
|
|
2016-12-28 22:05:48 +01:00
|
|
|
usleep = function(params, faden)
|
2016-11-13 14:42:57 +01:00
|
|
|
local p = params[1]
|
|
|
|
if type(p) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
2016-12-28 22:05:48 +01:00
|
|
|
faden.rebirth = minetest.get_us_time() + p
|
2016-11-13 14:42:57 +01:00
|
|
|
faden:stop()
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
2016-12-29 14:14:20 +01:00
|
|
|
sleep = function(params, faden)
|
|
|
|
local p = params[1]
|
|
|
|
if type(p) ~= "number" then
|
|
|
|
return false, UAT
|
|
|
|
end
|
2017-01-03 14:44:06 +01:00
|
|
|
local subsucc,msg = s.usleep({p * 1000000}, faden)
|
2016-12-29 14:14:20 +01:00
|
|
|
if not subsucc then
|
2017-01-12 18:35:11 +01:00
|
|
|
error(SE .. msg)
|
2016-12-29 14:14:20 +01:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
2016-12-28 18:47:28 +01:00
|
|
|
get_us_time = function()
|
|
|
|
return true, minetest.get_us_time()
|
|
|
|
end,
|
|
|
|
|
2016-12-28 18:27:41 +01:00
|
|
|
tostring = function(params)
|
2017-02-03 23:20:22 +01:00
|
|
|
return true, params[1] and tostring(params[1]) or nil
|
|
|
|
end,
|
|
|
|
|
|
|
|
tonumber = function(params)
|
|
|
|
return true, params[1] and tonumber(params[1])
|
|
|
|
end,
|
|
|
|
|
|
|
|
toboolean = function(params)
|
|
|
|
return true, params[1] and true or false
|
2016-12-28 18:27:41 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
print = function(params, faden)
|
|
|
|
for i = 1,#params do
|
|
|
|
params[i] = tostring(params[i])
|
|
|
|
end
|
|
|
|
faden.log = faden.log .. table.concat(params, "\t") .. "\n"
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
|
2016-11-13 14:42:57 +01:00
|
|
|
flush = function(params, faden)
|
|
|
|
return faden:flush(params)
|
|
|
|
end,
|
|
|
|
}
|
2016-12-28 18:27:41 +01:00
|
|
|
|
2016-12-29 14:14:20 +01:00
|
|
|
local so_math_fcts = {"sin", "asin", "cos", "acos", "tan", "atan", "exp", "log",
|
2016-12-28 18:27:41 +01:00
|
|
|
"abs", "sign", "floor", "ceil"}
|
|
|
|
for i = 1,#so_math_fcts do
|
|
|
|
i = so_math_fcts[i]
|
|
|
|
s[i] = function(params)
|
|
|
|
local p = params[1]
|
|
|
|
if type(p) ~= "number" then
|
2017-01-12 18:35:11 +01:00
|
|
|
return false, "this arithmetic function needs a number as argument"
|
2016-12-28 18:27:41 +01:00
|
|
|
end
|
|
|
|
return true, math[i](p)
|
|
|
|
end
|
|
|
|
end
|
2016-12-29 14:14:20 +01:00
|
|
|
|
|
|
|
local to_math_fcts = {"pow"}
|
|
|
|
for i = 1,#to_math_fcts do
|
|
|
|
i = to_math_fcts[i]
|
|
|
|
s[i] = function(params)
|
|
|
|
local p1,p2 = unpack(params)
|
|
|
|
if type(p1) ~= "number"
|
|
|
|
or type(p2) ~= "number" then
|
2017-01-12 18:35:11 +01:00
|
|
|
return false, "this arithmetic function needs 2 numbers as args"
|
2016-12-29 14:14:20 +01:00
|
|
|
end
|
|
|
|
return true, math[i](p1, p2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-03 23:20:22 +01:00
|
|
|
local abbreviations = {
|
|
|
|
mov = ":=",
|
|
|
|
add = "+",
|
|
|
|
sub = "-",
|
|
|
|
mul = "*",
|
|
|
|
div = "/",
|
|
|
|
inc = "++",
|
|
|
|
dec = "--",
|
|
|
|
neg = "!",
|
|
|
|
less = "<",
|
|
|
|
equal = "==",
|
|
|
|
jmp = "@",
|
|
|
|
usleep = "...",
|
|
|
|
}
|
|
|
|
-- TODO set metatable
|
|
|
|
|
2016-11-13 14:42:57 +01:00
|
|
|
return s
|