#!/usr/bin/env python3 import sys import os import toml typesIn = { "__unknown": "lua_touserdata", "char": "luaL_checkinteger", "int": "luaL_checkinteger", "long long": "luaL_checkinteger", "unsigned char": "luaL_checkinteger", "char *": "(char *)luaL_checkstring" } typesOut = { "__unknown": "lua_pushlightuserdata", "char": "lua_pushinteger", "int": "lua_pushinteger", "long long": "lua_pushinteger", "unsigned char": "lua_pushinteger", "char *": "lua_pushstring" } statics = toml.loads(open("src/values/statics.toml").read()) functions = toml.loads(open("src/values/functions.toml").read()) ofile = open("src/lua.c","w") supportedTarget = False if os.environ["target_os"] == "linux": ofile.write('''\ #include #include #include ''') supportedTarget = True if os.environ["target_os"] == "openbsd": ofile.write('''\ #include #include #include ''') supportedTarget = True if os.environ["target_os"] == "windows": ofile.write('''\ #include #include #include ''') supportedTarget = True if supportedTarget == False: print("Platform " +os.environ["target_os"]+ " unsupported by lua_translate.") sys.exit(1) ofile.write('''lua_State * engine_lua_state; #include "lua_manual.c" ''') for func in functions: if "lua" in functions[func]: if functions[func]["lua"] in ["no","manual"]: continue 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+ ' ' +functions[func]["argNames"][invarCount - 1]+ ' = ' +checkfunc+ '(L,' +str(invarCount)+ ');\n') invarCount += 1 argstring = "(" +",".join(functions[func]["argNames"])+ ")" 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[outtype] 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_lua_init() { engine_lua_state = luaL_newstate(); luaL_openlibs(engine_lua_state); ''') for static in statics: ofile.write('\tlua_pushinteger(engine_lua_state,' +static+ ');\n') ofile.write('\tlua_setglobal(engine_lua_state,"' +static+ '");\n') for func in functions: if "lua" in functions[func]: if functions[func]["lua"] in ["no","manual"]: continue 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('''\ engine_lua_init_manual(); luaL_loadfile(engine_lua_state,"mods/main/script/main.lua"); lua_call(engine_lua_state,0,0); }''')