robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1---@diagnostic disable: no-unknown 2local mpack =vim . mpack 3 4local funcsfname =arg [ 1 ] 5local metadata_file =arg [ 2 ] 6local funcs_file =arg [ 3 ] 7local eval_file =arg [ 4 ] 8 9--Will generate funcs.generated.h with definition of functions static const array. 10 11local hashy =require 'gen.hashy' 12 13local hashpipe =assert ( io . open ( funcsfname , 'wb' )) 14 15hashpipe : write ( [[ 16#include "nvim/arglist.h" 17#include "nvim/channel.h" 18#include "nvim/cmdexpand.h" 19#include "nvim/cmdhist.h" 20#include "nvim/diff.h" 21#include "nvim/digraph.h" 22#include "nvim/eval.h" 23#include "nvim/eval/buffer.h" 24#include "nvim/eval/deprecated.h" 25#include "nvim/eval/fs.h" 26#include "nvim/eval/funcs.h" 27#include "nvim/eval/list.h" 28#include "nvim/eval/typval.h" 29#include "nvim/eval/vars.h" 30#include "nvim/eval/window.h" 31#include "nvim/ex_docmd.h" 32#include "nvim/ex_getln.h" 33#include "nvim/fold.h" 34#include "nvim/fuzzy.h" 35#include "nvim/indent.h" 36#include "nvim/indent_c.h" 37#include "nvim/input.h" 38#include "nvim/insexpand.h" 39#include "nvim/mapping.h" 40#include "nvim/match.h" 41#include "nvim/mbyte.h" 42#include "nvim/menu.h" 43#include "nvim/mouse.h" 44#include "nvim/move.h" 45#include "nvim/quickfix.h" 46#include "nvim/runtime.h" 47#include "nvim/search.h" 48#include "nvim/sign.h" 49#include "nvim/state.h" 50#include "nvim/strings.h" 51#include "nvim/testing.h" 52#include "nvim/undo.h" 53 54]] ) 55 56local funcs =loadfile ( eval_file )(). funcs 57for name , func in pairs ( funcs ) do 58local n =( func . func_float and 1 or 0 ) + ( func . func_lua and 1 or 0 ) + ( func . func and 1 or 0 ) 59assert ( n <= 1 , name .. ': only one of func, func_float, func_lua can be set' ) 60 61if func . func_float then 62func . func ='float_op_wrapper' 63func . data ='{ .func_float = &' .. func . func_float .. ' }' 64elseif func . func_lua then 65func . func ='lua_wrapper' 66func . data ='{ .func_lua = "' .. func . func_lua .. '" }' 67end 68end 69 70local metadata =mpack . decode ( io . open ( metadata_file , 'rb' ): read ( '*all' )) 71for _ , fun in ipairs ( metadata ) do 72if fun . eval then 73local args 74if type ( fun . opts_idx ) == 'number' then 75args ={ fun . opts_idx - 1 , # fun . parameters } 76else 77args =# fun . parameters 78end 79funcs [ fun . name ] ={ 80args =args , 81func ='api_wrapper' , 82data ='{ .func_api = &method_handlers[' .. fun . handler_id .. '] }' , 83} 84end 85end 86 87local func_names =vim . tbl_filter ( function ( name ) 88return name : match ( '__%d*$' ) == nil 89end , vim . tbl_keys ( funcs )) 90 91table . sort ( func_names ) 92 93local funcsdata =assert ( io . open ( funcs_file , 'w' )) 94funcsdata : write ( mpack . encode ( func_names )) 95funcsdata : close () 96 97local neworder , hashfun =hashy . hashy_hash ( 'find_internal_func' , func_names , function ( idx ) 98return 'functions[' .. idx .. '].name' 99end ) 100 101hashpipe : write ( 'static const EvalFuncDef functions[] = {\n' ) 102 103for _ , name in ipairs ( neworder ) do 104local def =funcs [ name ] 105local min_args =0 106local max_args =0 --- @type integer|string 107local def_args =def . args 108if type ( def_args ) == 'number' then 109min_args , max_args =def_args , def_args 110elseif type ( def_args ) == 'table' then 111min_args =def_args [ 1 ] or 0 112max_args =def_args [ 2 ] or 'MAX_FUNC_ARGS' 113end 114local base =def . base or 'BASE_NONE' 115local func =def . func or ( 'f_' .. name ) 116local data =def . data or '{ .null = NULL }' 117local fast =def . fast and 'true' or 'false' 118hashpipe : write ( 119( ' { "%s", %s, %s, %s, %s, &%s, %s },\n' ): format ( 120name , 121min_args , 122max_args , 123base , 124fast , 125func , 126data 127) 128) 129end 130hashpipe : write ( ' { NULL, 0, 0, BASE_NONE, false, NULL, { .null = NULL } },\n' ) 131hashpipe : write ( '};\n\n' ) 132hashpipe : write ( hashfun ) 133hashpipe : close ()