refactor(eval): use Hashy McHashFace instead of gperf

this removes gperf as a build dependency
This commit is contained in:
bfredl
2022-05-12 18:56:40 +02:00
parent de5ccf2348
commit 36613b888b
8 changed files with 35 additions and 161 deletions

View File

@@ -1,9 +1,12 @@
local mpack = require('mpack')
local nvimsrcdir = arg[1]
local autodir = arg[2]
local metadata_file = arg[3]
local funcs_file = arg[4]
local shared_file = arg[2]
local autodir = arg[3]
local metadata_file = arg[4]
local funcs_file = arg[5]
_G.vim = loadfile(shared_file)()
if nvimsrcdir == '--help' then
print([[
@@ -20,7 +23,9 @@ package.path = nvimsrcdir .. '/?.lua;' .. package.path
local funcsfname = autodir .. '/funcs.generated.h'
local gperfpipe = io.open(funcsfname .. '.gperf', 'wb')
local hashy = require'generators.hashy'
local hashpipe = io.open(funcsfname, 'wb')
local funcs = require('eval').funcs
local metadata = mpack.unpack(io.open(metadata_file, 'rb'):read("*all"))
@@ -38,21 +43,15 @@ local funcsdata = io.open(funcs_file, 'w')
funcsdata:write(mpack.pack(funcs))
funcsdata:close()
gperfpipe:write([[
%language=ANSI-C
%global-table
%readonly-tables
%define initializer-suffix ,0,0,BASE_NONE,NULL,NULL
%define word-array-name functions
%define hash-function-name hash_internal_func_gperf
%define lookup-function-name find_internal_func_gperf
%omit-struct-type
%struct-type
VimLFuncDef;
%%
]])
for name, def in pairs(funcs) do
local names = vim.tbl_keys(funcs)
local neworder, hashfun = hashy.hashy_hash("find_internal_func", names, function (idx)
return "functions["..idx.."].name"
end)
hashpipe:write("static const EvalFuncDef functions[] = {\n")
for _, name in ipairs(neworder) do
local def = funcs[name]
local args = def.args or 0
if type(args) == 'number' then
args = {args, args}
@@ -62,7 +61,10 @@ for name, def in pairs(funcs) do
local base = def.base or "BASE_NONE"
local func = def.func or ('f_' .. name)
local data = def.data or "NULL"
gperfpipe:write(('%s, %s, %s, %s, &%s, (FunPtr)%s\n')
hashpipe:write((' { "%s", %s, %s, %s, &%s, (FunPtr)%s },\n')
:format(name, args[1], args[2], base, func, data))
end
gperfpipe:close()
hashpipe:write(' { NULL, 0, 0, BASE_NONE, NULL, NULL },\n')
hashpipe:write("};\n\n")
hashpipe:write(hashfun)
hashpipe:close()