refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-26 04:53:19 +02:00
parent 20a8a01be0
commit 8618798480
+17 -26
View File
@@ -1,10 +1,10 @@
local type, setmetatable, setfenv, unpack, next, mask, combine, pcall, _G = type, setmetatable, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0)
local type, setmetatable, getfenv, setfenv, unpack, next, mask, combine, pcall, _G = type, setmetatable, getfenv, setfenv, unpack, next, bit.band, bit.bor, pcall, getfenv(0)
local error, import_error, declaration_error, collision_error, mutability_error
local pass, start_declaration, declarator, metadata_mt, property_mt, env_mt, interface_mt
local pass, start_declaration, advance_declaration, env_mt, interface_mt, metadata_mt, property_mt
local NULL, PRIVATE, PUBLIC, MUTABLE, DYNAMIC, PROPERTY = 0, 1, 2, 4, 8, 16
local NULL, PRIVATE, PUBLIC, MUTABLE, PROPERTY = 0, 1, 2, 4, 8
local INDEX, NEWINDEX, CALL = 1, 2, 4
local _state, _env_state = {}, {}
local _state = {}
function error(message, ...)
return _G.error(format(message or '', unpack(arg))..'\n'..debugstack(), 0)
@@ -17,34 +17,28 @@ mutability_error = function(key) error('Field "%s" is immutable.', key) end
pass = function() end
do
local function dynamize(f)
return function(...) setfenv(f, getfenv(2)); return f(unpack(arg)) end
end
local function unpack_property_value(t)
local get, set; get, set, t.get, t.set = t.get or pass, t.set or pass, nil, nil
if next(t) or type(get) ~= 'function' or type(set) ~= 'function' then error() end
return get, set
end
local function declare(state, modifiers, key, value)
local metadata, dynamic, property = state.metadata, mask(DYNAMIC, modifiers) ~= 0, mask(PROPERTY, modifiers) ~= 0
local metadata = state.metadata
metadata[key] = metadata[key] and collision_error(key) or modifiers
if property then
if mask(PROPERTY, modifiers) == 0 then
state.data[key] = value
else
local success, getter, setter = pcall(unpack_property_value, value)
if success or declaration_error() then
if dynamic then getter, setter = dynamize(getter), dynamize(setter) end
state.getters[key], state.setters[key] = getter, setter
end
else
if dynamic and (type(value) == 'function' or declaration_error()) then value = dynamize(value) end
state.data[key] = value
end
end
do
local mt = {__metatable=false, __call=pass}
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, dynamic=DYNAMIC, property=PROPERTY}
local COMPATIBLE = {private=MUTABLE+PROPERTY, public=DYNAMIC+PROPERTY, mutable=PRIVATE, dynamic=PUBLIC+PROPERTY, property=PUBLIC}
local advance_declaration, module, modifiers, property
local MODIFIER = {private=PRIVATE, public=PUBLIC, mutable=MUTABLE, property=PROPERTY}
local COMPATIBLE = {private=MUTABLE+PROPERTY, public=PROPERTY, mutable=PRIVATE, property=PRIVATE+PUBLIC}
local module, modifiers, property
local function intercept(_, key, value)
if type == INDEX and (not property or declaration_error()) then
local modifier, compatible = MODIFIER[key], COMPATIBLE[key]
@@ -55,18 +49,17 @@ do
elseif type == NEWINDEX then
if property then key, value = property, {[key]=value} end
declare(module, modifiers, key, value)
mt.__call = pass
advance_declaration = pass
return true
elseif type == CALL then
local property, modifiers; property, modifiers, modifiers = property, modifiers, PRIVATE
if property then property = nil; declare(module, modifiers, property, value) else module.modifiers = modifiers end
mt.__call = pass
advance_declaration = pass
return true
end
end
declarator = setmetatable({}, mt)
function start_declaration(state, modifier)
if intercept ~= pass then declaration_error() end
if advance_declaration ~= pass then declaration_error() end
advance_declaration, module, modifiers, property = intercept, state, modifier, nil
end
end
@@ -120,19 +113,18 @@ function interface_mt:__newindex(key, value, state) state=_state[self]
end
metadata_mt = {__index=function() return 0 end}
property_mt = {__index=function() return pass end}
property_mt = {__index=function() return pass end }
local function create_module(...)
local state, env, interface
env, interface = setmetatable({}, env_mt), setmetatable({}, interface_mt)
state = {
metadata = {_=PROPERTY, _G=PRIVATE, M=PRIVATE, error=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, dynamic=PROPERTY, property=PROPERTY},
fields = {_G=_G, M=env, error=error},
metadata = {_=PROPERTY, _G=PRIVATE, M=PRIVATE, error=PRIVATE, private=PROPERTY, public=PROPERTY, mutable=PROPERTY, property=PROPERTY},
fields = {_G=_G, M=env, I=interface, error=error},
getters = setmetatable({
private = function() start_declaration(PRIVATE, state); return getfenv(2) end,
public = function() start_declaration(PUBLIC, state); return getfenv(2) end,
mutable = function() start_declaration(MUTABLE, state); return getfenv(2) end,
dynamic = function() start_declaration(DYNAMIC, state); return getfenv(2) end,
property = function() start_declaration(PROPERTY, state); return getfenv(2) end,
}, property_mt),
setters = setmetatable({}, property_mt),
@@ -147,5 +139,4 @@ local function create_module(...)
end
state[env], state[interface] = state, state
return env, interface
-- -- TODO create_env (per module call)
end