From eaf7c486b81c96e71f31618757cb0b72db31fa52 Mon Sep 17 00:00:00 2001 From: Jean-Patrick Guerrero Date: Mon, 29 Nov 2021 02:47:36 +0100 Subject: [PATCH] Add custom operators (Part 2) --- init.lua | 4 ++-- src/api.lua | 4 ++-- src/caches.lua | 4 ++-- src/operators.lua | 15 +++++++++++---- tests/test_operators.lua | 10 ++++------ 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/init.lua b/init.lua index 5d031ca..a14cb29 100644 --- a/init.lua +++ b/init.lua @@ -1,8 +1,8 @@ local modpath = core.get_modpath"i3" -dofile(modpath .. "/src/operators.lua") +local _loadfile = dofile(modpath .. "/src/operators.lua") local function lf(path) - return loadfile(modpath .. path) + return _loadfile(modpath .. path) end i3 = { diff --git a/src/api.lua b/src/api.lua index 59c42ed..0fda2fd 100644 --- a/src/api.lua +++ b/src/api.lua @@ -88,7 +88,7 @@ function i3.register_craft(def) end for symbol in gmatch(concat(def.grid), ".") do - c = c + 1 + c++ def.items[c] = def.key[symbol] end else @@ -110,7 +110,7 @@ function i3.register_craft(def) end for name in gmatch(concat(items, ","), "[%s%w_:]+") do - c = c + 1 + c++ def.items[c] = clean_name(name) end end diff --git a/src/caches.lua b/src/caches.lua index e1e703f..8f1b802 100644 --- a/src/caches.lua +++ b/src/caches.lua @@ -104,7 +104,7 @@ local function drop_table(name, drop) end if not di.rarity then - count_sure = count_sure + 1 + count_sure++ end end end @@ -146,7 +146,7 @@ local function cache_recipes(item) end for k, v in pairs(replacements[item]) do - k = k + shift + k += shift if _recipes[k] then _recipes[k].replacements = v diff --git a/src/operators.lua b/src/operators.lua index 8ebfcc0..06e9448 100644 --- a/src/operators.lua +++ b/src/operators.lua @@ -1,6 +1,5 @@ local fmt = string.format -local _loadfile = loadfile -local var = "[%w%.%[%]_]" +local var = "[%w%.%[%]\"\'_]" local operators = { ["([%+%-%*%^/&|])="] = function(a, b, c) @@ -26,6 +25,14 @@ local operators = { [">>"] = 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) @@ -61,6 +68,6 @@ local function _load(path, line, data) return l, err end -function loadfile(path) - return _load(path) or _loadfile(path) +return function(path) + return _load(path) or loadfile(path) end diff --git a/tests/test_operators.lua b/tests/test_operators.lua index 0c1e3ab..dc7981c 100644 --- a/tests/test_operators.lua +++ b/tests/test_operators.lua @@ -1,24 +1,22 @@ local a, b, c = 0, 0, 0 - b+=1 c++; local foo = "bar"; print(c-=1) print(c++) local t = { a = a++, - b = b++, - c = c++, + b = 2, + c = c+=2, d = a&3, + e = 1, } +t["b"] <<= 4 print(dump(t)) - --c += 1 c*=2 - local i = 16 i += i<<4 print(i) -- 272 - print(a+=2) -- 2 print(c++) -- 3 print(a-=1) -- -1