diff --git a/README.md b/README.md index 07099ae..677130e 100644 --- a/README.md +++ b/README.md @@ -145,4 +145,4 @@ end TODO: * add string. instructions -* add inc,dec,sub,div +* metatable diff --git a/faden.lua b/faden.lua index 49d4a0e..3885844 100644 --- a/faden.lua +++ b/faden.lua @@ -85,7 +85,9 @@ return function(faden_manip, parsed) try_rebirth = function(self) if minetest.get_us_time() >= self.rebirth then self:continue() + return true end + return false end, exit = function(self) self:flush() diff --git a/standardbefehlssatz.lua b/standardbefehlssatz.lua index dc5ce9b..3c609eb 100644 --- a/standardbefehlssatz.lua +++ b/standardbefehlssatz.lua @@ -9,6 +9,10 @@ s = { return true, params[2] end, + xchg = function(params) + return true, params[2], params[1] + end, + getvar = function(params, faden) local p = params[1] if type(p) ~= "string" then @@ -29,7 +33,12 @@ s = { return true, p1 + p2 end if t1 == "boolean" then - return true, p1 and p2 + for _,k in pairs(params) do + if k then + return true, true + end + end + return true, false end if t1 == "string" then if #p1 + #p2 > faden.strlen_max then @@ -40,6 +49,22 @@ s = { return false, UAT end, + 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, + mul = function(params) local p1,p2 = unpack(params) local t1 = type(p1) @@ -59,11 +84,39 @@ s = { return true, p1 * p2 end if t1 == "boolean" then - return true, p1 or p2 + 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 end return false, UAT end, + 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, + neg = function(params) local v = params[1] local t = type(v) @@ -74,7 +127,7 @@ s = { return true, not v end if t == "string" then - return true, v:rev() + return true, v:rev() -- does string.rev exist? end return false, UAT end, @@ -190,6 +243,10 @@ s = { return true, p1 < p2 end, + greater = function(params) + return s.less{params[2], params[1]} + end, + usleep = function(params, faden) local p = params[1] if type(p) ~= "number" then @@ -217,7 +274,15 @@ s = { end, tostring = function(params) - return true, tostring(params[1]) + 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 end, print = function(params, faden) @@ -259,4 +324,20 @@ for i = 1,#to_math_fcts do end end +local abbreviations = { + mov = ":=", + add = "+", + sub = "-", + mul = "*", + div = "/", + inc = "++", + dec = "--", + neg = "!", + less = "<", + equal = "==", + jmp = "@", + usleep = "...", +} +-- TODO set metatable + return s diff --git a/util/standartbefehlssatz_doc.lua b/util/standartbefehlssatz_doc.lua index cfadaeb..5de9b8c 100644 --- a/util/standartbefehlssatz_doc.lua +++ b/util/standartbefehlssatz_doc.lua @@ -13,13 +13,19 @@ local types = { local instr = { {"mov", ", ", "Copies from into to."}, + {"xchg", ", ", "Switches a with b."}, {"getvar", "[, ]", "Sets varname to the current variable called if it's set. If exists is passed, it's set to a bool indicating whether the variable was set."}, {"add", ", ", "to += from"}, - {"add", ", ", "to = to and from"}, + {"add", "[, [, […]]]", "to indicates whether at least one of the arguments is true"}, {"add", ", ", "to = to .. from"}, + {"sub", ", ", "num -= tosub"}, + {"sub", ", [, ]", "str = str:sub(start, end)"}, {"mul", ", ", "to *= from"}, - {"mul", ", ", "to = to or from"}, + {"mul", "[, [, […]]]", "to indicates whether all of the arguments are true"}, {"mul", ", ", "str = str:rep(from)"}, + {"div", ", ", "num /= todiv"}, + {"inc", "", "++num"}, + {"dec", "", "--num"}, {"neg", "", "num = -num"}, {"neg", "", "var = not var"}, {"neg", "", "str = str:rev()"}, @@ -33,10 +39,13 @@ local instr = { {"equal", ", ", "a = a == b"}, {"less", ", ", "a = a < b; after executing, a is a bool"}, {"less", ", ", "a = a < b; less also works for strings"}, + {"greater", ", ", "executes less b,a"}, {"usleep", "", "aborts the program for at least floor(max(0, t)) ms"}, {"sleep", "", "executes usleep with t * 1000000 as argument"}, {"get_us_time", "", "stores minetest.get_us_time() into to; can be used to measure time differences"}, {"tostring", "", "var = tostring(var)"}, + {"tonumber", "", "var = tonumber(var)"}, + {"toboolean", "", "var = var and true or false"}, {"print", "[, [, […]]]", "adds variables to the log, seperated by \\t, \\n is added at the end"}, {"flush", "", "Output the log, this should vary for every mod."}, }