refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-26 15:04:54 +02:00
parent afe61c4999
commit c7b208aa72
+52 -41
View File
@@ -1,23 +1,21 @@
if module then return end if module then return end
local type, setmetatable, setfenv, unpack, next, intersection, union, pcall, _G = type, setmetatable, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0) local type, setmetatable, setfenv, unpack, next, pcall, _G = type, setmetatable, setfenv, unpack, next, pcall, getfenv(0)
local start_declaration, declaration, env_mt, interface_mt, noop_mt local start_declaration, declaration, env_mt, interface_mt, noop_mt
local PRIVATE, PUBLIC, PROPERTY = 0, 1, 2
local INDEX, NEWINDEX, CALL = 1, 2, 3 local INDEX, NEWINDEX, CALL = 1, 2, 3
local _state = {}
local function error(message, ...) local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end
return _G.error(format(message or '', unpack(arg))..'\n'..debugstack(), 0)
end
local function import_error() error 'Invalid imports.' end local function import_error() error 'Invalid imports.' end
local function declaration_error() error 'Invalid declaration.' end local function declaration_error() error 'Invalid declaration.' end
local function collision_error(key) error('"%s" already exists.', key) end local function collision_error(key) error('"%s" already exists.', key) end
local function noop() end local function nop() end
local function id(_) return _ end local function id(_) return _ end
--function vararg_id(...) return unpack(arg) end
local function const(_) return function() return _ end end local function const(_) return function() return _ end end
--function vararg_const(...) return function() return unpack(arg) end end --local function vararg_id(...) return unpack(arg) end
--local function vararg_const(...) return function() return unpack(arg) end end
local _state, _public, _type = {}, {}, {}
do do
local function extract(v) local function extract(v)
@@ -30,46 +28,55 @@ do
local function declare(self, modifiers, key, value) local function declare(self, modifiers, key, value)
local metadata = self.metadata local metadata = self.metadata
metadata[key] = metadata[key] and collision_error(key) or modifiers metadata[key] = metadata[key] and collision_error(key) or modifiers
if intersection(PROPERTY, modifiers) == 0 then if intersection(ACCESSOR, modifiers) == 0 then
self.methods[key] = value self.methods[key] = value
else else
local success, f, mutator = pcall(extract, value) local success, f, mutator = pcall(extract, value)
if success or declaration_error() then if success or declaration_error() then
self.methods[key], self.mutators[key] = type(f) == 'function' and f or const(f), mutator or noop self.methods[key], self.mutators[key] = type(f) == 'function' and f or const(f), mutator or nop
end end
end end
end end
declaration = noop declaration = nop
do do
local MODIFIER = {private=PRIVATE, public=PUBLIC, property=PROPERTY} local PUBLIC = {public=true, private=false}
local state, modifiers, property local PREFIX_TYPE = {method=CALL, accessor=INDEX, mutator=NEWINDEX}
local function intercept(self, type, key, value) local SUFFIX_TYPE = {call=CALL, get=INDEX, set=NEWINDEX}
local state, access, type, name
local function intercept(self, event, key, value)
if self ~= state then declaration_error() end if self ~= state then declaration_error() end
if type == INDEX and (not property or declaration_error()) then if event == INDEX and (not name or declaration_error()) then
local modifier = MODIFIER[key] if ACCESS[key] then
if not modifier then modifier, property = PROPERTY, key end access = (access and declaration_error()) or ACCESS[key]
modifiers = union(modifiers, modifier) elseif PREFIX_TYPE[key] then
type = (type and declaration_error()) or PREFIX_TYPE[key]
elseif not type or declaration_error() then
name = key
end
return true return true
elseif type == NEWINDEX then elseif event == NEWINDEX then
if property then key, value = property, {[key]=value} end if name then
declare(self, union(modifiers, self.modifiers), key, value) type = SUFFIX_TYPE[key] or declaration_error()
declaration = noop key = name
end
declare(self, access, type, key, value)
declaration = nop
return true return true
elseif type == CALL then elseif event == CALL then
if property then declare(self, union(modifiers, self.modifiers), property, value) else self.modifiers = modifiers end if name then declare(self, access, type, name, value) else self.access, self.type = access, type end
declaration = noop declaration = nop
return true return true
end end
end end
function start_declaration(self, modifier) function start_declaration(self, modifier)
if declaration ~= noop then declaration_error() end if declaration ~= nop then declaration_error() end
declaration, state, modifiers, property = intercept, self, modifier, nil declaration, state, modifiers, property = intercept, self, modifier, nil
end end
end end
env_mt = {__metatable=false} env_mt = {__metatable=false}
function env_mt:__index(key, state) state=_state[self] function env_mt:__index(key) local state=_state[self]
if declaration(state, INDEX, key) then return self end if declaration(state, INDEX, key) then return self end
if intersection(PROPERTY, state.metadata[key] or 0) ~= 0 then if intersection(PROPERTY, state.metadata[key] or 0) ~= 0 then
return state.getters[key]() return state.getters[key]()
@@ -78,12 +85,12 @@ do
if value ~= nil then return value else return _G[key] end if value ~= nil then return value else return _G[key] end
end end
end end
function env_mt:__newindex(key, value, state) state=_state[self] function env_mt:__newindex(key, value) local state=_state[self]
if declaration(state, NEWINDEX, key, value) then return end if declaration(state, NEWINDEX, key, value) then return end
local modifiers = state.metadata[key] local modifiers = state.metadata[key]
if modifiers then if modifiers then
if intersection(PROPERTY, modifiers) ~= 0 then if intersection(PROPERTY, modifiers) ~= 0 then
state.setters[key](value) state.mutators[key](value)
end end
else else
declare(state, state.modifiers, key, value) declare(state, state.modifiers, key, value)
@@ -95,7 +102,7 @@ do
end end
interface_mt = {__metatable=false} interface_mt = {__metatable=false}
function interface_mt:__index(key, state) state=_state[self] function interface_mt:__index(key) local state=_state[self]
local masked = intersection(PUBLIC+PROPERTY, state.metadata[key] or 0) local masked = intersection(PUBLIC+PROPERTY, state.metadata[key] or 0)
if masked == PUBLIC+PROPERTY then if masked == PUBLIC+PROPERTY then
return state.getters[key]() return state.getters[key]()
@@ -103,41 +110,45 @@ function interface_mt:__index(key, state) state=_state[self]
return state.methods[key] return state.methods[key]
end end
end end
function interface_mt:__newindex(key, value, state) state=_state[self] function interface_mt:__newindex(key, value) local state=_state[self]
if intersection(PUBLIC+PROPERTY, state.metadata[key] or 0) == PUBLIC+PROPERTY then if intersection(PUBLIC+PROPERTY, state.metadata[key] or 0) == PUBLIC+PROPERTY then
return state.setters[key](value) return state.mutators[key](value)
-- elseif masked == PUBLIC and type(state.methods[key]) == 'function' then elseif masked == PUBLIC and type(state.methods[key]) == 'function' then
-- return state.methods[key](value) return state.methods[key](value)
end end
end end
function interface_mt:__call(key, ...) local state=_state[self]
-- TODO new instance
end
noop_mt = {__index=function() return noop end} noop_mt = {__index=function() return nop end}
function module(...) function module(...)
local state, env, interface local state, env, interface
env, interface = setmetatable({}, env_mt), setmetatable({}, interface_mt) env, interface = setmetatable({}, env_mt), setmetatable({}, interface_mt)
state = { state = {
metadata = {_=PROPERTY, error=PRIVATE, noop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, private=PROPERTY, public=PROPERTY, property=PROPERTY}, metadata = {_=ACCESSOR, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, private=ACCESSOR, public=ACCESSOR, accessor=ACCESSOR},
methods = setmetatable({ methods = setmetatable({
_G = function() return _G end, _G = function() return _G end,
M = function() return env end, M = function() return env end,
I = function() return interface end, I = function() return interface end,
private = function() start_declaration(state, PRIVATE); return env end, private = function() start_declaration(state, PRIVATE); return env end,
public = function() start_declaration(state, PUBLIC); return env end, public = function() start_declaration(state, PUBLIC); return env end,
property = function() start_declaration(state, PROPERTY); return env end, accessor = function() start_declaration(state, ACCESSOR); return env end,
mutator = function() start_declaration(state, MUTATOR); return env end,
error = error, error = error,
noop = noop, nop = nop,
id = id, id = id,
const = const, const = const,
}, noop_mt), }, noop_mt),
setters = setmetatable({}, noop_mt), mutators = setmetatable({}, noop_mt),
modifiers = PRIVATE, modifiers = PRIVATE,
} }
for i=1,arg.n do for i=1,arg.n do
local module = state[arg[i] or import_error()] or import_error() local module = state[arg[i] or import_error()] or import_error()
for k, v in module.metadata do for k, v in module.metadata do
if intersection(PUBLIC, v) ~= 0 and (not state.metadata[k] or import_error()) then if intersection(PUBLIC, v) ~= 0 and (not state.metadata[k] or import_error()) then
state.metadata[k], state.methods[k], state.getters[k], state.setters[k] = v, module.methods[k], module.getters[k], module.setters[k] state.metadata[k], state.methods[k], state.mutators[k] = v, module.methods[k], module.mutators[k]
end end
end end
end end