From bd05c036e46f6fbbaa8c79414a48dc02d00e7d33 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Mon, 31 Mar 2014 22:45:21 +1000 Subject: Clean up after my thingasm. --- ClientHamr/GuiLua/skang.lua | 47 +++++++++++++++++--------------------------- ClientHamr/GuiLua/test.lua | 13 ++++++------ ClientHamr/GuiLua/test_c.c | 48 ++++++++++++++++++++++----------------------- 3 files changed, 48 insertions(+), 60 deletions(-) (limited to 'ClientHamr/GuiLua') diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index 9e3dfd8..25886c2 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua @@ -206,6 +206,13 @@ csv2table = function (csv) end +shiftLeft = function (tab) + local result = tab[1] + table.remove(tab, 1) + return result +end + + -- My clever boolean check, this is the third language I've written this in. B-) -- true 1 yes ack ok one positive absolutely affirmative 'ah ha' 'shit yeah' 'why not' local isTrue = 't1aopswy' @@ -624,35 +631,22 @@ thingasm = function (names, ...) -- Check how we were called, and re arrange stuff to match. if 0 == #params then - if ('table' == type(names)) then - -- thingasm{...} + if ('table' == type(names)) then -- thingasm{...} params = names - names = params[1] - table.remove(params, 1) - if 'table' == type(names) then - -- thingasm{module, 'foo', ...} + names = shiftLeft(params) + if 'table' == type(names) then -- thingasm{module, 'foo', ...} module = names - names = params[1] - table.remove(params, 1) + names = shiftLeft(params) end - else - -- thingasm("foo") - end + end -- thingasm("foo") otherwise else if 'table' == type(names) then module = names - if 'string' == type(...) then - -- C or __call(table, string, ..) - params = {...} - elseif 'table' == type(...) then - -- __call(table, table) - params = ... + if 'string' == type(...) then params = {...} -- C or __call(table, string, ..) + elseif 'table' == type(...) then params = ... -- __call(table, table) end - names = params[1] - table.remove(params, 1) - else - -- thingasm('foo', ...) - end + names = shiftLeft(params) + end -- thingasm('foo', ...) otherwise end -- Break out the names. @@ -683,23 +677,18 @@ thingasm = function (names, ...) end local thingy = modThing.__stuff[name] - if not thingy then -- This is a new Thing. + if not thingy then -- This is a new Thing. new = true thingy = {} thingy.module = module thingy.names = names - -- To pick up isValid, pattern, and the other stuff. - setmetatable(thingy, {__index = Thing}) + setmetatable(thingy, {__index = Thing}) -- To pick up isValid, pattern, and the other stuff. end -- Pull out positional arguments. thingy.help = params[1] or thingy.help thingy.default = params[2] or thingy.default local types = params[3] or table.concat(thingy.types or {}, ',') - thingy.widget = params[4] or thingy.widget - thingy.required = params[5] or thingy.required - thingy.acl = params[6] or thingy.acl - thingy.boss = params[7] or thingy.boss -- Pull out named arguments. for k, v in pairs(params) do diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index aebe4ae..34f2bf9 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua @@ -31,7 +31,7 @@ local fool -- TODO - Could have a table of tables, and ipair through the top level, passing the inner ones to skang.thing{}. -skang.thingasm('fooble,f', 'Help text goes here', 1, nil, '"edit", "The fooble:", 1, 1, 10, 50', true) +skang.thingasm{'fooble,f', 'Help text goes here', 1, widget='"edit", "The fooble:", 1, 1, 10, 50', required=true} skang.thingasm('bar', 'Help text', "Default") skang.thingasm('foo') @@ -54,6 +54,7 @@ local copy = skang.copy(test, 'copy') print('End ' .. test.bar .. ' ' .. test.VERSION .. ' ' .. skang.get(test, 'ffunc', 'help') .. ' ->> ' .. skang.get(test, 'f', 'action')) print('foo = ' .. test.foo .. ' ->> ' .. skang.get(test, 'foo', 'help')) +print('cfooble = ' .. test_c.cfooble .. ' ->> ' .. skang.get(test_c, 'cfooble', 'help') .. '[' .. skang.get(test_c, 'cfooble', 'widget') .. ']') print('cfunc ->> ' .. skang.get(test_c, 'cfunc', 'help')) test.ffunc('one', 2) test_c.cfunc(0, 'zero') @@ -120,10 +121,10 @@ print('') local stuff = {} stuff.t = {} -skang.thingasm{stuff, 'a', help = 'A test stufflet'} -skang.thingasm{stuff.t, 'b', help = 'A sub stufflet'} -skang.thingasm{stuff.t, 'c', help = 'Another sub stufflet'} -skang.thingasm{stuff, 's', help = 'A Stuff', types='table'} +skang.thingasm{stuff, 'a', 'A test stufflet'} +skang.thingasm{stuff.t, 'b', 'A sub stufflet'} +skang.thingasm{stuff.t, 'c', 'Another sub stufflet'} +skang.thingasm{stuff, 's', 'A Stuff', types='table'} stuff.s{'sa,a', help = 'A stufflet in a Stuff'} stuff.s{'sb,b', help = 'Another stufflet in a Stuff'} @@ -141,7 +142,7 @@ print(skang.get(stuff.t, 'c', 'help')) print(skang.get(stuff, 's', 'help')) print(skang.get(stuff.s, 'sa', 'help')) print(skang.get(stuff.s, 'sb', 'help')) -skang.thingasm{test, 'baz,b', help = 'A test stufflet for test'} +skang.thingasm{test, 'baz,b', 'A test stufflet for test'} print(skang.get(test, 'b', 'help')) print(skang.get(test, 'f', 'help')) --skang.printTableStart(getmetatable(stuff.s), '', 'stuff.s metatable') diff --git a/ClientHamr/GuiLua/test_c.c b/ClientHamr/GuiLua/test_c.c index 10af91c..8f6ee7b 100644 --- a/ClientHamr/GuiLua/test_c.c +++ b/ClientHamr/GuiLua/test_c.c @@ -65,6 +65,7 @@ returned, but we want to do something different with skang. */ int luaopen_test_c(lua_State *L) { + lua_Number i; // In theory, the only thing on the stack now is 'test_c' from the require() call. @@ -121,16 +122,29 @@ int luaopen_test_c(lua_State *L) // skang.thing('cfooble,c', 'Help text goes here', 1, 'number', \"'edit', 'The fooble:', 1, 1, 10, 50\", true) lua_getfield(L, skang, "thingasm"); + i = 1; + lua_newtable(L); + lua_pushnumber(L, i++); lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. + lua_settable(L, -3); + + lua_pushnumber(L, i++); lua_pushstring(L, "cfooble,c"); - lua_pushstring(L, "Help text"); + lua_settable(L, -3); + + lua_pushnumber(L, i++); + lua_pushstring(L, "cfooble help text"); + lua_settable(L, -3); + + lua_pushnumber(L, i++); lua_pushnumber(L, 1); - lua_pushnil(L); + lua_settable(L, -3); + lua_pushstring(L, "'edit', 'The cfooble:', 1, 1, 10, 50"); + lua_setfield(L, -2, "widget"); lua_pushboolean(L, 1); // Is required. - lua_pushnil(L); // Default ACL. - lua_pushnil(L); // No boss. - lua_call(L, 9, 0); + lua_setfield(L, -2, "required"); + lua_call(L, 1, 0); // skang.thing('cbar', 'Help text', 'Default') lua_getfield(L, skang, "thingasm"); @@ -138,25 +152,13 @@ int luaopen_test_c(lua_State *L) lua_pushstring(L, "cbar"); lua_pushstring(L, "Help text"); lua_pushstring(L, "Default"); - lua_pushnil(L); // No type. - lua_pushnil(L); // No widget. - lua_pushnil(L); // Not required. - lua_pushnil(L); // Default ACL. - lua_pushnil(L); // No boss. - lua_call(L, 9, 0); + lua_call(L, 4, 0); // skang.thing('cfoo') lua_getfield(L, skang, "thingasm"); - lua_pushstring(L, "cfoo"); - lua_pushnil(L); // No help. - lua_pushnil(L); // No default. - lua_pushnil(L); // No type. - lua_pushnil(L); // No widget. - lua_pushnil(L); // Not required. - lua_pushnil(L); // Default ACL. - lua_pushnil(L); // No boss. lua_getfield(L, LUA_REGISTRYINDEX, ourName); // Coz getfenv() can't find C environment. - lua_call(L, 9, 0); + lua_pushstring(L, "cfoo"); + lua_call(L, 2, 0); // skang.thing('cfunc', 'Help Text', ffunc, 'number,string') lua_getfield(L, skang, "thingasm"); @@ -165,11 +167,7 @@ int luaopen_test_c(lua_State *L) lua_pushstring(L, "cfunc does nothing really"); lua_pushcfunction(L, cfunc); lua_pushstring(L, "number,string"); - lua_pushnil(L); // No widget. - lua_pushnil(L); // Not required. - lua_pushnil(L); // Default ACL. - lua_pushnil(L); // No boss. - lua_call(L, 9, 0); + lua_call(L, 5, 0); // skang.moduleEnd(_M) lua_getfield(L, skang, "moduleEnd"); -- cgit v1.1