From 2e19a90d2def8d8d1ec34d2cd5cb64a64759fba7 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Wed, 12 Oct 2016 12:58:46 +0200 Subject: [PATCH] library refactoring --- libs/inspect.lua | 28 +++++++++++++++------------- libs/module.lua | 32 ++++++++++++++++---------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/libs/inspect.lua b/libs/inspect.lua index 84990f4..d41f441 100644 --- a/libs/inspect.lua +++ b/libs/inspect.lua @@ -5,11 +5,11 @@ function print(msg) end function format_key(k) - return type(k) == 'string' and k or '[' .. tostring(k) .. ']' + return type(k) == 'string' and k or '['..tostring(k)..']' end function format_value(v) - return type(v) == 'string' and '"' .. v .. '"' or tostring(v) + return type(v) == 'string' and '"'..v..'"' or tostring(v) end function print_table(t, depth) @@ -27,15 +27,17 @@ local max_depth function print_pair(k, v, depth) local padding = strrep(' ', depth * 4) - if depth == max_depth then - print(padding .. '...') - return - end - print(padding .. format_key(k) .. ' = ' .. format_value(v)) + print(padding..format_key(k)..' = '..format_value(v)) if type(v) == 'table' then - print(padding .. '{') - print_table(v, depth + 1) - print(padding .. '}') + if next(v) then + print(padding..'{') + if depth == max_depth then + print(padding..' ...') + else + print_table(v, depth + 1) + end + print(padding .. '}') + end end end @@ -53,13 +55,13 @@ local function setting(v) if type(v) == 'number' then max_depth = v elseif type(v) == 'function' then - print('#' .. v()) + print('#'..v()) else - print('#' .. v) + print('#'..v) end end -local mt = { __metatable=false, __call=inspect, __div=inspect } +local mt = {__metatable=false, __call=inspect, __div=inspect} function mt:__index(key) setting(key) diff --git a/libs/module.lua b/libs/module.lua index 31cc59b..b133afe 100644 --- a/libs/module.lua +++ b/libs/module.lua @@ -2,25 +2,25 @@ if module then return end local strfind, type, setmetatable, setfenv, _G = strfind, type, setmetatable, setfenv, getfenv(0) local PRIVATE, PUBLIC, FIELD, ACCESSOR, MUTATOR = 0, 1, 2, 4, 6 -local MODES = { FIELD, ACCESSOR, MUTATOR } +local MODES = {FIELD, ACCESSOR, MUTATOR} local READ, WRITE = '', '=' -local OPERATION = { [FIELD]=READ, [ACCESSOR]=READ, [MUTATOR]=WRITE } -local META = { get=ACCESSOR, set=MUTATOR } +local OPERATION = {[FIELD]=READ, [ACCESSOR]=READ, [MUTATOR]=WRITE} +local META = {get=ACCESSOR, set=MUTATOR} -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 nop, id = function() end, function(v) return v end local function proxy_mt(fields, mutators) - return { __metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end } + return {__metatable=false, __index=fields, __newindex=function(_, k, v) return mutators[k](v) end} end local nop_default_mt, definition_helper_mt, require, include, create_module local loaded, _interface, _environment, _access, _name = {}, {}, {}, {}, {} -nop_default_mt = { __index=function() return nop end } +nop_default_mt = {__index=function() return nop end} -definition_helper_mt = { __metatable=false } +definition_helper_mt = {__metatable=false} function definition_helper_mt:__index(k) _name[self] = _name[self] and error('Invalid modifier "%s".', k) or k return self @@ -30,7 +30,7 @@ function definition_helper_mt:__newindex(k, v) module, name = loaded[self], _name[self] if name then mode = META[k] or error('Invalid modifier "%s"', k) else name, mode = k, FIELD end if type(name) ~= 'string' or not strfind(name, '^[_%a][_%w]*') then error('Invalid identifier "%s".', name) end - module.defined[name .. OPERATION[mode]] = module.defined[name .. OPERATION[mode]] and error('Duplicate identifier "%s".', name) or true + module.defined[name..OPERATION[mode]] = module.defined[name..OPERATION[mode]] and error('Duplicate identifier "%s".', name) or true module[mode][name], module[_access[self] + mode][name] = v, v _access[self], _name[self] = nil, nil end @@ -41,8 +41,8 @@ function include(self, name) local module = name and loaded[name] or error('No module "%s".', name) for _, mode in MODES do for k, v in module[PUBLIC + mode] do - if not self.defined[k .. OPERATION[mode]] or error('Import conflict for "%s".', k) then - self.defined[k .. OPERATION[mode]], self[mode][k] = true, v + if not self.defined[k..OPERATION[mode]] or error('Import conflict for "%s".', k) then + self.defined[k..OPERATION[mode]], self[mode][k] = true, v end end end @@ -52,19 +52,19 @@ function create_module(name) if type(name) ~= 'string' then error('Invalid module name "%s".', name) end local P, environment, interface, definition_helper, modifiers, accessors, mutators, fields, public_accessors, public_mutators, public_fields environment, interface, definition_helper = {}, {}, setmetatable({}, definition_helper_mt) - accessors = { private=function() _access[definition_helper] = PRIVATE; return definition_helper end, public=function() _access[definition_helper] = PUBLIC; return definition_helper end } - mutators = setmetatable({ _=nop }, { __index=function(_, k) return function(v) _G[k] = v end end }) + accessors = {private=function() _access[definition_helper] = PRIVATE; return definition_helper end, public=function() _access[definition_helper] = PUBLIC; return definition_helper end} + mutators = setmetatable({_=nop}, {__index=function(_, k) return function(v) _G[k] = v end end}) fields = setmetatable( - { _M=environment, _G=_G, require=require, include=function(interface) include(P, interface) end, error=error, nop=nop, id=id }, - { __index=function(_, k) local accessor = accessors[k]; if accessor then return accessor() else return _G[k] end end } + {_M=environment, _G=_G, require=require, include=function(interface) include(P, interface) end, error=error, nop=nop, id=id}, + {__index=function(_, k) local accessor = accessors[k]; if accessor then return accessor() else return _G[k] end end} ) public_accessors = setmetatable({}, nop_default_mt) public_mutators = setmetatable({}, nop_default_mt) - public_fields = setmetatable({}, { __index=function(_, k) return public_accessors[k]() end }) + public_fields = setmetatable({}, {__index=function(_, k) return public_accessors[k]() end}) setmetatable(environment, proxy_mt(fields, mutators)) setmetatable(interface, proxy_mt(public_fields, public_mutators)) P = { - defined = { _M=true, _G=true, require=true, include=true, error=true, nop=true, id=true, public=true, private=true, ['_=']=true }, + defined = {_M=true, _G=true, require=true, include=true, error=true, nop=true, id=true, public=true, private=true, ['_=']=true}, [ACCESSOR] = accessors, [MUTATOR] = mutators, [FIELD] = fields,