This commit is contained in:
2026-05-14 14:39:34 -05:00
parent 3e47163ee5
commit a38b2736c6
8 changed files with 112 additions and 9 deletions

View File

@@ -1,9 +1,42 @@
#include "priv.h"
Code* code_load_bytecode(uint8_t* bytecode, size_t bytecode_sz)
#include <stdlib.h>
struct Code {
};
Code* code_new(uint8_t* bytecode, size_t bytecode_sz)
{
Code* code = xcalloc(1, sizeof(Code));
return code;
}
Code* code_load_bytecode_cb(void(*read_bytes)(size_t, void*), void* data)
void code_destroy(Code* code)
{
}
free(code);
}
size_t code_n_consts(Code const* code)
{
return 0; // TODO
}
TYC_CONST_TYPE code_const_type(Code const* code, size_t n)
{
return TC_REAL; // TODO
}
T_REAL code_const_real(Code const* code, size_t n)
{
return 0.f; // TODO
}
const char* code_const_string(Code const* code, size_t n)
{
return ""; // TODO
}
size_t code_n_functions(Code const* code)
{
return 0; // TODO
}

View File

@@ -5,12 +5,14 @@
#include "lua.h"
#include "lauxlib.h"
#include "lualib.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();
luaL_openlibs(L);
int r = luaL_loadbufferx(L, (const char *) lib_compiler_compiler_out, lib_compiler_compiler_out_len, "compiler", "b");
if (r == LUA_ERRSYNTAX)
@@ -18,6 +20,8 @@ TYC_RESULT code_assemble(const char* code, uint8_t** bytecode, size_t* bytecode_
else if (r == LUA_ERRMEM)
out_of_memory();
lua_call(L, 0, 1);
lua_pushstring(L, code);
r = lua_pcall(L, 1, 1, 0);
if (r == LUA_ERRMEM) {
@@ -29,7 +33,15 @@ TYC_RESULT code_assemble(const char* code, uint8_t** bytecode, size_t* bytecode_
return T_ERR_ASSEMBLER_SYNTAX_ERROR;
}
if (!lua_istable(L, -1))
abort();
*bytecode_sz = luaL_len(L, -1);
*bytecode = malloc(*bytecode_sz);
for (size_t i = 0; i < *bytecode_sz; ++i) {
lua_geti(L, -1, i + 1);
(*bytecode)[i] = (uint8_t) lua_tointeger(L, -1);
lua_pop(L, 1);
}
lua_close(L);
return T_OK;

View File

@@ -81,5 +81,6 @@ end
----------------------
return function(source)
print('test')
return { 0, 1, 2, 3, 4 }
end

View File

@@ -29,6 +29,10 @@ typedef struct Code Code;
typedef uint32_t HEAP_KEY;
typedef uint64_t TABLE_HASH;
typedef enum {
TC_STRING, TC_REAL,
} TYC_CONST_TYPE;
//
// UTILS
//
@@ -122,9 +126,15 @@ void heap_gc(Heap* h, VALUE const* roots, size_t n_roots);
TYC_RESULT code_assemble(const char* code, uint8_t** bytecode, size_t* bytecode_sz);
Code* code_load_bytecode(uint8_t* bytecode, size_t bytecode_sz);
Code* code_load_bytecode_cb(void(*read_bytes)(size_t, void*), void* data);
Code* code_new(uint8_t* bytecode, size_t bytecode_sz);
void code_destroy(Code* code);
size_t code_n_consts(Code const* code);
TYC_CONST_TYPE code_const_type(Code const* code, size_t n);
T_REAL code_const_real(Code const* code, size_t n);
const char* code_const_string(Code const* code, size_t n);
size_t code_n_functions(Code const* code);
#endif //TYCHE_PRIV_H

View File

@@ -13,4 +13,6 @@ typedef enum {
T_ERR_ASSEMBLER_SYNTAX_ERROR = -30,
} TYC_RESULT;
#define T_REAL float
#endif //TYCHE_TYCHE_H