summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-02-19 19:03:51 -0600
committersanine <sanine.not@pm.me>2023-02-19 19:03:51 -0600
commitfb0eb6abac31ab1945a5b32e68824c9f9420d71f (patch)
treeb414bb9f93af85755df553d003ed88afc24cef50 /util
parentbc4af3b47260ee0f79343303b135d1ea32cde4d4 (diff)
refactor: bind (nearly) all nvg functions
Diffstat (limited to 'util')
-rw-r--r--util/generate-binding.lua62
-rw-r--r--util/test.lua40
2 files changed, 102 insertions, 0 deletions
diff --git a/util/generate-binding.lua b/util/generate-binding.lua
new file mode 100644
index 0000000..1c5359f
--- /dev/null
+++ b/util/generate-binding.lua
@@ -0,0 +1,62 @@
+local b = {}
+setmetatable(b, {__index=G})
+setfenv(1, b)
+
+
+--===== enums =====--
+local Enum = {}
+-- create a single enum element
+function Enum.new(name, id)
+ local self = {
+ name = string.upper(name),
+ id = id,
+ }
+ setmetatable(self, Enum)
+ return self
+end
+-- create multiple enums
+function Enum.new_multi(tbl)
+ local enums = {}
+ for id, name in ipairs(tbl) do
+ table.insert(enums, Enum.new(name, id))
+ end
+ return enums
+end
+-- make enums read-only
+function Enum.__newindex(self)
+ error("Attempted to set index on Enum")
+end
+-- make enums print nicely
+function Enum.__tostring(self)
+ return self.name
+end
+-- allow comparinge enums
+function Enum.__eq(self, other)
+ return self.id == other.id
+end
+
+
+local function check_match(string, pattern, rule)
+ local match = string.match(string, '^' .. pattern)
+ if match then
+ return match, rule(match)
+ end
+end
+
+local function append_match(string, pattern, rule,
+
+
+local function lex(string)
+ for _, typename in ipairs(c_int_types) do
+ if (string.sub(string, 1, #typename) == typename) then
+ return
+ end
+end
+
+
+function bind(signature)
+
+end
+
+
+return b
diff --git a/util/test.lua b/util/test.lua
new file mode 100644
index 0000000..9b9fc49
--- /dev/null
+++ b/util/test.lua
@@ -0,0 +1,40 @@
+function test(description, func)
+ io.write(description .. ': ')
+ local result, msg = pcall(func)
+ if result == true then
+ print("OK")
+ else
+ print("FAIL")
+ print(debug.traceback(msg))
+ print()
+ end
+end
+
+
+local b = require 'generate-binding'
+
+
+test("simplest possible binding", function()
+ local binding = b.bind("void some_function();")
+ assert(binding == [[
+int some_function_bind(lua_State *L)
+{
+ some_function();
+ return 0;
+}]])
+end)
+
+
+test("complicated binding", function()
+ local binding = b.bind("const char * qqq(int a, float q, unsigned char m);")
+ assert(binding == [[
+int qqq_bind(lua_State *L)
+{
+ lua_Integer a = luaL_checkinteger(L, 1);
+ lua_Number q = luaL_checknumber(L, 2);
+ lua_Integer m = luaL_checkinteger(L, 3);
+ const char *result = qqq(a, q, m);
+ lua_pushstring(L, result);
+ return 1;
+}]])
+end)