This commit is contained in:
2026-05-14 13:02:43 -05:00
parent 2265b8cf08
commit 3e47163ee5
6 changed files with 144 additions and 3 deletions

36
lib/compiler.c Normal file
View File

@@ -0,0 +1,36 @@
#include "priv.h"
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "compiler/compiler.lua.h"
TYC_RESULT code_assemble(const char* code, uint8_t** bytecode, size_t* bytecode_sz)
{
lua_State* L = luaL_newstate();
int r = luaL_loadbufferx(L, (const char *) lib_compiler_compiler_out, lib_compiler_compiler_out_len, "compiler", "b");
if (r == LUA_ERRSYNTAX)
abort();
else if (r == LUA_ERRMEM)
out_of_memory();
lua_pushstring(L, code);
r = lua_pcall(L, 1, 1, 0);
if (r == LUA_ERRMEM) {
out_of_memory();
} else if (r == LUA_ERRERR) {
abort();
} else if (r == LUA_ERRRUN) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
return T_ERR_ASSEMBLER_SYNTAX_ERROR;
}
lua_close(L);
return T_OK;
}