refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-09-04 21:21:47 +02:00
parent e888efcf2a
commit 867505c1a8
23 changed files with 172 additions and 173 deletions
+17 -18
View File
@@ -62,43 +62,42 @@ do local f, mt
public.perm {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = nil end}
end
do local mt, key = {}, nil
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
end
do local SET, ARRAY, ARRAY0, TABLE = 1, 2, 3, 4
local function constructor(type)
do local function constructor(type)
local chunk = 'local setn, error = table.setn, error; return function('
for i = 1, 99 do chunk = chunk..'a'..i..',' end
chunk = chunk..'overflow) local t = t'
if type == SET then
for i = 1, 99 do chunk = chunk .. 'a' .. i..',' end
chunk = chunk .. 'overflow) local t = t'
if type == 'set' then
for i = 1, 99 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = true', chunk, i, i) end
elseif type == ARRAY then
chunk = chunk..'; setn(t, 99); if a1 == nil then return t end; t[1] = a1'
elseif type == 'array' then
chunk = chunk .. '; setn(t, 99); if a1 == nil then return t end; t[1] = a1'
for i = 2, 99 do chunk = format('%s; if a%d == nil then setn(t, %d); return t end; t[%d] = a%d', chunk, i, i - 1, i, i) end
elseif type == ARRAY0 then
chunk = chunk..'; setn(t, 99)'
elseif type == 'array0' then
chunk = chunk .. '; setn(t, 99)'
for i = 1, 99 do chunk = format('%s; t[%d] = a%d', chunk, i, i) end
elseif type == TABLE then
elseif type == 'table' then
for i = 1, 97, 2 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = a%d', chunk, i, i, i + 1) end
end
chunk = chunk..'; return (overflow == nil or error "Overflow.") and t end'
chunk = chunk .. '; return (overflow == nil or error "Overflow.") and t end'
local f = loadstring(chunk); setfenv(f, M); return f()
end
public(); S, A, A0, T = constructor(SET), constructor(ARRAY), constructor(ARRAY0), constructor(TABLE)
public(); S, A, A0, T = constructor'set', constructor'array', constructor 'array0', constructor 'table'
end
do local chunk = 'return function(i'
for i = 1, 99 do chunk = chunk..',a'..i end
chunk = chunk..')'
for i = 1, 99 do chunk = chunk .. ',a' .. i end
chunk = chunk .. ')'
for i = 1, 99 do
chunk = format('%s if i == %d then return a%i', chunk, i, i)
for j = i + 1, 99 do chunk = chunk..',a'..j end
chunk = chunk..' end'
for j = i + 1, 99 do chunk = chunk .. ',a' .. j end
chunk = chunk .. ' end'
end
chunk = chunk..' end'
chunk = chunk .. ' end'
public.select = loadstring(chunk)()
end
+4 -4
View File
@@ -5,14 +5,14 @@ local PUBLIC, PRIVATE = 1, 2
local CALL, INDEX, NEWINDEX = 1, 2, 3
local inspect
do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..msg) end
do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE .. msg) end
local mt = {__metatable=false, __pow=function(self, v) self(v); return v end}
function mt:__call(...)
for i = 1, arg.n do
print('arg'..i..' = '..(type(arg[i]) == 'string' and '"'..arg[i]..'"' or tostring(arg[i])))
print('arg' .. i..' = ' .. (type(arg[i]) == 'string' and '"' .. arg[i] .. '"' or tostring(arg[i])))
if type(arg[i]) == 'table' and next(arg[i]) then
print('{')
for k, v in arg[i] do print(' '..(type(k) == 'string' and k or '['..tostring(k)..']')..' = '..(type(v) == 'string' and '"'..v..'"' or tostring(v))) end
for k, v in arg[i] do print(' ' .. (type(k) == 'string' and k or '[' .. tostring(k) .. ']') .. ' = ' .. (type(v) == 'string' and '"' .. v..'"' or tostring(v))) end
print('}')
end
end
@@ -21,7 +21,7 @@ do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE.
inspect = setmetatable({}, mt)
end
local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end
local function error(msg, ...) return _G.error(format(msg or '', unpack(arg)) .. '\n' .. debugstack(), 0) end
local function import_error() error('Import error.') end
local function declaration_error() error('Declaration error.') end
local function collision_error(key) error('"%s" already exists.', key) end