me.fier.engine/lua_translate

82 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
import toml
typesIn = {
"__unknown": "lua_touserdata",
"char": "luaL_checkinteger",
"int": "luaL_checkinteger",
"long long": "luaL_checkinteger",
"char *": "(char *)luaL_checkstring"
}
typesOut = {
"__unknown": "lua_pushlightuserdata",
"char": "lua_pushinteger",
"int": "lua_pushinteger",
"long long": "lua_pushinteger",
"char *": "lua_pushstring"
}
functions = toml.loads(open("modules/engine/FUNCTIONS.toml").read())
ofile = open("modules/engine/addon/lua.c","w")
ofile.write('''\
#include <lua5.3/lua.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
lua_State * engine_lua_state;
''')
for func in functions:
invarCount = 1
funcnew = "engine_luaf_" +func.replace("engine_","",1)
ofile.write('int ' +funcnew+ '(lua_State *L) {\n')
for arg in functions[func]["arguments"]:
if not arg in typesIn:
checkfunc = typesIn["__unknown"]
else:
checkfunc = typesIn[arg]
ofile.write("\t" +arg+ ' invar' +str(invarCount)+ ' = ' +checkfunc+ '(L,' +str(invarCount)+ ');\n')
invarCount += 1
invarCount = 1
argstring = ""
for arg in functions[func]["arguments"]:
argstring += ",invar" +str(invarCount)
invarCount += 1
argstring = "(" +argstring.strip(",")+ ")"
outtype = functions[func]["type"]
if outtype == "void":
ofile.write('\t' + func + argstring + ";")
ofile.write('\n\treturn 0;')
else:
if not outtype in typesOut:
pushfunc = typesOut["__unknown"]
else:
pushfunc = typesOut[arg]
ofile.write('\t' +outtype+ ' outvar = ' +func + argstring + ";")
ofile.write("\n\t" +pushfunc+ '(L,outvar);')
ofile.write('\n\treturn 1;')
ofile.write('\n}\n\n')
ofile.write('''\
void engine_luaInit() {
engine_lua_state = luaL_newstate();
luaL_openlibs(engine_lua_state);
''')
for func in functions:
funcnew = "engine_luaf_" +func.replace("engine_","",1)
ofile.write('\tlua_pushcfunction(engine_lua_state,' +funcnew+ ');\n')
ofile.write('\tlua_setglobal (engine_lua_state,"' +func+ '");\n')
ofile.write('''\
luaL_loadfile(engine_lua_state,"assets/scripts/main.lua");
lua_call(engine_lua_state,0,0);
}''')