From 13a946a86869ad8188a24e90d5543c157f517343 Mon Sep 17 00:00:00 2001 From: Novatux Date: Sun, 25 Jan 2015 10:31:23 +0100 Subject: [PATCH] Localize a few variables --- bit32.lua | 72 +++++++++++++++++++++++++++--------------------------- cptr.lua | 2 +- screen.lua | 10 ++++---- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/bit32.lua b/bit32.lua index 5eb9866..7f86d86 100644 --- a/bit32.lua +++ b/bit32.lua @@ -3,96 +3,96 @@ bit32 = {} local N = 32 local P = 2^N -bit32.bnot = function(x) - x = x%P - return P-1-x +function bit32.bnot(x) + x = x % P + return P - 1 - x end -bit32.band = function(x, y) +function bit32.band(x, y) -- Common usecases, they deserve to be optimized - if y == 0xff then return x%0x100 end - if y == 0xffff then return x%0x10000 end - if y == 0xffffffff then return x%0x100000000 end + if y == 0xff then return x % 0x100 end + if y == 0xffff then return x % 0x10000 end + if y == 0xffffffff then return x % 0x100000000 end - x, y = x%P, y%P + x, y = x % P, y % P local r = 0 local p = 1 for i = 1, N do - a, b = x%2, y%2 - x, y = math.floor(x/2), math.floor(y/2) - if a+b == 2 then + local a, b = x % 2, y % 2 + x, y = math.floor(x / 2), math.floor(y / 2) + if a + b == 2 then r = r + p end - p = 2*p + p = 2 * p end return r end -bit32.bor = function(x, y) +function bit32.bor(x, y) -- Common usecases, they deserve to be optimized if y == 0xff then return x - (x%0x100) + 0xff end if y == 0xffff then return x - (x%0x10000) + 0xffff end if y == 0xffffffff then return 0xffffffff end - x, y = x%P, y%P + x, y = x % P, y % P local r = 0 local p = 1 for i = 1, N do - a, b = x%2, y%2 - x, y = math.floor(x/2), math.floor(y/2) - if a+b >= 1 then + local a, b = x % 2, y % 2 + x, y = math.floor(x / 2), math.floor(y / 2) + if a + b >= 1 then r = r + p end - p = 2*p + p = 2 * p end return r end -bit32.bxor = function(x, y) - x, y = x%P, y%P +function bit32.bxor(x, y) + x, y = x % P, y % P local r = 0 local p = 1 for i = 1, N do - a, b = x%2, y%2 + local a, b = x%2, y%2 x, y = math.floor(x/2), math.floor(y/2) - if a+b == 1 then + if a + b == 1 then r = r + p end - p = 2*p + p = 2 * p end return r end -bit32.lshift = function(x, s_amount) +function bit32.lshift(x, s_amount) if math.abs(s_amount) >= N then return 0 end - x = x%P + x = x % P if s_amount < 0 then - return math.floor(x*(2^s_amount)) + return math.floor(x * (2 ^ s_amount)) else - return (x*(2^s_amount))%P + return (x * (2 ^ s_amount)) % P end end -bit32.rshift = function(x, s_amount) +function bit32.rshift(x, s_amount) if math.abs(s_amount) >= N then return 0 end - x = x%P + x = x % P if s_amount > 0 then - return math.floor(x*(2^-s_amount)) + return math.floor(x * (2 ^ - s_amount)) else - return (x*(2^-s_amount))%P + return (x * (2 ^ -s_amount)) % P end end -bit32.arshift = function(x, s_amount) +function bit32.arshift(x, s_amount) if math.abs(s_amount) >= N then return 0 end - x = x%P + x = x % P if s_amount > 0 then local add = 0 if x >= P/2 then - add = P - 2^(N-s_amount) + add = P - 2 ^ (N - s_amount) end - return math.floor(x*(2^-s_amount))+add + return math.floor(x * (2 ^ -s_amount)) + add else - return (x*(2^-s_amount))%P + return (x * (2 ^ -s_amount)) % P end end diff --git a/cptr.lua b/cptr.lua index c8e8438..1f0c67a 100644 --- a/cptr.lua +++ b/cptr.lua @@ -159,7 +159,7 @@ function run_computer(turtle, cptr) if cptr.stopped then return end cptr.cycles = math.max(MAX_CYCLES, cptr.cycles + CYCLES_PER_STEP) while true do - instr = cptr[cptr.PC] + local instr = cptr[cptr.PC] local f = ITABLE[instr] if f == nil then return end cptr.PC = u16(cptr.PC + 1) diff --git a/screen.lua b/screen.lua index ed861f6..043c29f 100644 --- a/screen.lua +++ b/screen.lua @@ -28,8 +28,8 @@ end local function escape(text) -- Remove all \0's in the string, that cannot be done using string.gsub as there can't be \0's in a pattern - text2 = "" - for i=1, string.len(text) do + local text2 = "" + for i = 1, string.len(text) do if string.byte(text, i) ~= 0 then text2 = text2 .. string.sub(text, i, i) end end return minetest.formspec_escape(text2) @@ -48,9 +48,9 @@ end function screen.create_text_formspec(text, basex, basey) local f = lines(text) - s = "" - i = basey - 0.25 - for _,x in ipairs(f) do + local s = "" + local i = basey - 0.25 + for _, x in ipairs(f) do s = s .. "]label[" .. basex .. "," .. tostring(i) .. ";" .. escape(x) i = i + 0.3 end