refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-09-06 04:48:17 +02:00
parent 1a40dd1d47
commit 00a242e0e2
2 changed files with 60 additions and 30 deletions
+57 -27
View File
@@ -2,7 +2,7 @@ green_t = module
--setglobal('green_t', green_t and error(nil) or module)
local next, setn, type, setmetatable = next, table.setn, type, setmetatable
local wipe, release, acquire, acquire_temp, empty
local wipe, release, acquire, acquire_auto, empty
-- TODO mandatory operation table mandate + comply/fulfill functions
local pool, pool_size, overflow_pool, auto_release = {}, 0, setmetatable({}, { __mode='k' }), {}
@@ -33,14 +33,6 @@ function release(t)
end
public.release = release
function public.ret(t)
if getn(t) > 0 then
return tremove(t, 1), ret(t)
else
release(t)
end
end
function acquire()
if pool_size > 0 then
pool_size = pool_size - 1
@@ -55,36 +47,47 @@ function acquire()
end
public.t.get = acquire
function acquire_temp()
function acquire_auto()
local t = acquire()
auto_release[t] = true
return t
end
public.tt.get = acquire_temp
public.tt.get = acquire_auto
function public.ret(t)
if getn(t) > 0 then
return tremove(t, 1), ret(t)
else
release(t)
end
end
do
local mt = { __newindex=nop }
function empty() return setmetatable(acquire_temp(), mt) end
function empty() return setmetatable(acquire_auto(), mt) end
public.empty.get = empty
end
do
local f = function(_, v) if type(v) == 'table' then auto_release[v] = true end return v end
local mt = { __call=f, __sub=f }
public.temp {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = true end}
end
do
local f = function(_, v) if type(v) == 'table' then auto_release[v] = nil end return v end
local mt = { __call=f, __sub=f }
public.perm { get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = nil end }
local function apply(v, enable)
if type(v) ~= 'table' then return end
auto_release[v] = enable and true or nil
end
local function define_modifier(name, enable)
local f = function(_, v) apply(v, enable); return v end
local mt = { __call=f, __sub=f }
public[name] { get=function() return setmetatable(acquire_auto(), mt) end, set=function(v) apply(v, enable) end }
end
define_modifier('temp', true)
define_modifier('perm', false)
end
do
local mt, key = {}, nil
function mt:__unm() local temp = mt.__index; mt.__index = nil; return temp end
function mt:__index(k) key = k; return self end
function mt:__call(v) self[key] = v; key = nil return self end
function public.__(t) mt.__newindex = wipe(t); return setmetatable(acquire_temp(), mt) end
function mt:__call(v) self[key] = v; key = nil; return self end
function public.__(t) mt.__newindex = wipe(t); return setmetatable(acquire_auto(), mt) end
end
local function arg_chunk(k, n)
@@ -105,6 +108,33 @@ function public.pseudo_vararg_function(body, upvals)
return f()
end
do
local body = [[
if arg100 ~= nil then error("Vararg overflow.") end
local n
repeat
]]
for i = 99, 1, -1 do
body = body .. format('if a%1$d ~= nil then n = %1$d; break end;', i)
end
body = body .. [[
until true
local t = acquire_auto()
setn(t, n)
repeat
]]
for i = 1, 99 do
body = body .. format('if %1$d > n then break end; t[%1$d] = a%1$d;', i)
end
body = body .. [[
until true
return f(t)
]]
function public.vararg(f)
return pseudo_vararg_function(body, {f=f, error=error, setn=setn, acquire=acquire_auto, release=release})
end
end
local function insert_chunk(mode)
local body = 'repeat '
if mode == 'k' then
@@ -126,7 +156,7 @@ local function insert_chunk(mode)
body = body .. format('if a%1$d == nil then break end; a1[a%1$d] = a%d;', i, i+1)
end
end
return body .. 'if a100 ~= nil then error"Overflow." end until true;'
return body .. 'if a100 ~= nil then error("Vararg overflow.") end until true;'
end
do
@@ -135,10 +165,10 @@ do
local mt = {__call = pseudo_vararg_function(insert_chunk(mode) .. 'setmetatable(a1, nil); return a1', upvals)}
return function() return setmetatable(acquire(), mt) end
end
public.S.get = pseudo_literal'k'
public.A.get = pseudo_literal'v'
public.A0.get = pseudo_literal'v0'
public.T.get = pseudo_literal'kv'
public.S.get = pseudo_literal('k')
public.A.get = pseudo_literal('v')
public.A0.get = pseudo_literal('v0')
public.T.get = pseudo_literal('kv')
end
do