.
This commit is contained in:
36
lib/compiler.c
Normal file
36
lib/compiler.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user