Generate lua function table

This commit is contained in:
Krzosa Karol
2024-08-10 08:17:37 +02:00
parent 97b1cf9e73
commit a12a10ee0b
4 changed files with 62 additions and 32 deletions

View File

@@ -457,10 +457,39 @@ void GenerateConfig() {
}
}
void GenerateLuaApi() {
MA_Scratch scratch;
S8_String file = OS_ReadFile(scratch, "../src/text_editor/lua_api.cpp");
S8_List list = S8_Split(scratch, file, "\n");
S8_List funcs = {};
for (S8_Node *it = list.first; it; it = it->next) {
S8_String s = S8_Trim(it->string);
if (S8_StartsWith(s, "int Lua_")) {
s = S8_Skip(s, 4);
S8_Seek(s, "(", 0, &s.len);
S8_Add(scratch, &funcs, s);
}
}
S8_List sb = {};
S8_AddF(scratch, &sb, "luaL_Reg LuaFunctions[] = {\n");
for (S8_Node *it = funcs.first; it; it = it->next) {
S8_String lua_name = S8_Skip(it->string, 4);
S8_AddF(scratch, &sb, " {\"%.*s\", %.*s},\n", S8_Expand(lua_name), S8_Expand(it->string));
}
S8_AddF(scratch, &sb, " {NULL, NULL},\n");
S8_AddF(scratch, &sb, "};\n");
S8_String output = S8_Merge(scratch, sb);
OS_WriteFile("../src/text_editor/lua_api_generated.cpp", output);
}
int main() {
MA_InitScratch();
SRC_InitCache(Perm, "te.cache");
GenerateLuaApi();
GenerateConfig();
int result = CompileTextEditor();
// int result = CompileNewPlatform();

View File

@@ -92,7 +92,7 @@ void Open(String16 path) {
Open(string);
}
int LuaAppendCmd(lua_State *L) {
int Lua_AppendCmd(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
String working_dir = GetCurrentBufferDir();
@@ -100,7 +100,7 @@ int LuaAppendCmd(lua_State *L) {
return 0;
}
int LuaNewCmd(lua_State *L) {
int Lua_NewCmd(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
@@ -109,13 +109,13 @@ int LuaNewCmd(lua_State *L) {
return 0;
}
int LuaKill(lua_State *L) {
int Lua_Kill(lua_State *L) {
Window *window = GetCurrentWindow();
KillProcess(window->active_view);
return 0;
}
int LuaOpen(lua_State *L) {
int Lua_Open(lua_State *L) {
Scratch scratch;
String path = luaL_checkstring(L, 1);
lua_pop(L, 1);
@@ -123,7 +123,7 @@ int LuaOpen(lua_State *L) {
return 0;
}
int LuaPrint(lua_State *L) {
int Lua_Print(lua_State *L) {
Scratch scratch;
String string = lua_tostring(L, 1);
lua_pop(L, 1);
@@ -131,7 +131,7 @@ int LuaPrint(lua_State *L) {
return 0;
}
int LuaListBuffers(lua_State *L) {
int Lua_ListBuffers(lua_State *L) {
Window *window = GetWindow(ActiveWindow);
View *view = GetView(window->active_view);
@@ -154,7 +154,7 @@ int LuaListBuffers(lua_State *L) {
return 0;
}
int LuaGetBufferList(lua_State *L) {
int Lua_GetBufferList(lua_State *L) {
lua_createtable(L, 0, (int)Buffers.len);
int i = 1;
@@ -168,7 +168,7 @@ int LuaGetBufferList(lua_State *L) {
return 1;
}
int LuaFileExists(lua_State *L) {
int Lua_FileExists(lua_State *L) {
String path = luaL_checkstring(L, 1);
lua_pop(L, 1);
bool exists = FileExists(path);
@@ -176,24 +176,24 @@ int LuaFileExists(lua_State *L) {
return 1;
}
int LuaGetWorkingDir(lua_State *L) {
int Lua_GetWorkingDir(lua_State *L) {
lua_pushlstring(LuaState, WorkingDir.data, WorkingDir.len);
return 1;
}
int LuaGetCurrentBufferName(lua_State *L) {
int Lua_GetCurrentBufferName(lua_State *L) {
String name = GetCurrentBufferName();
lua_pushlstring(LuaState, name.data, name.len);
return 1;
}
int LuaGetCurrentBufferDir(lua_State *L) {
int Lua_GetCurrentBufferDir(lua_State *L) {
String name = GetCurrentBufferDir();
lua_pushlstring(LuaState, name.data, name.len);
return 1;
}
int LuaOpenFilesHere(lua_State *L) {
int Lua_OpenFilesHere(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow());
Scratch scratch;
@@ -203,7 +203,7 @@ int LuaOpenFilesHere(lua_State *L) {
return 0;
}
int LuaSearch(lua_State *L) {
int Lua_Search(lua_State *L) {
Window *seek_window = GetCurrentWindow();
seek_window->search_string = lua_tostring(L, 1);
lua_pop(L, 1);
@@ -213,7 +213,7 @@ int LuaSearch(lua_State *L) {
return 0;
}
int LuaSearchB(lua_State *L) {
int Lua_SearchB(lua_State *L) {
Window *seek_window = GetCurrentWindow();
seek_window->search_string = lua_tostring(L, 1);
lua_pop(L, 1);
@@ -231,24 +231,7 @@ static void HookLuaForceExit(lua_State *L, lua_Debug *debug) {
luaL_error(L, "lua execution got interrupted");
}
luaL_Reg LuaFunctions[] = {
{ "Open", LuaOpen},
{ "ListBuffers", LuaListBuffers},
{ "GetBufferList", LuaGetBufferList},
{ "GetWorkingDir", LuaGetWorkingDir},
{ "FileExists", LuaFileExists},
{"GetCurrentBufferName", LuaGetCurrentBufferName},
{ "GetCurrentBufferDir", LuaGetCurrentBufferDir},
{ "print", LuaPrint},
{ "Print", LuaPrint},
{ "Kill", LuaKill},
{ "NewC", LuaNewCmd},
{ "AppendC", LuaAppendCmd},
{ "OpenFilesHere", LuaOpenFilesHere},
{ "Search", LuaSearch},
{ "SearchB", LuaSearchB},
{ NULL, NULL},
};
extern luaL_Reg LuaFunctions[];
void InitLua() {
LuaState = luaL_newstate();

View File

@@ -0,0 +1,17 @@
luaL_Reg LuaFunctions[] = {
{"AppendCmd", Lua_AppendCmd},
{"NewCmd", Lua_NewCmd},
{"Kill", Lua_Kill},
{"Open", Lua_Open},
{"Print", Lua_Print},
{"ListBuffers", Lua_ListBuffers},
{"GetBufferList", Lua_GetBufferList},
{"FileExists", Lua_FileExists},
{"GetWorkingDir", Lua_GetWorkingDir},
{"GetCurrentBufferName", Lua_GetCurrentBufferName},
{"GetCurrentBufferDir", Lua_GetCurrentBufferDir},
{"OpenFilesHere", Lua_OpenFilesHere},
{"Search", Lua_Search},
{"SearchB", Lua_SearchB},
{NULL, NULL},
};

View File

@@ -42,6 +42,7 @@ int FullScreenPositionX, FullScreenPositionY;
#include "lua.hpp"
#include "lua_api.cpp"
#include "lua_api_generated.cpp"
#include "generated.cpp"
#include "window_draw.cpp"