get owned

This commit is contained in:
2026-06-18 22:38:01 +02:00
parent 3e1d0ba5d9
commit 2d043ca39f
7 changed files with 156 additions and 168 deletions

View File

@@ -5,7 +5,7 @@
typedef struct noomC_Local { typedef struct noomC_Local {
unsigned int startpc; unsigned int startpc;
unsigned int endpc; unsigned int endpc;
const char *name; const char* name;
unsigned int namelen; unsigned int namelen;
unsigned int stackslot; unsigned int stackslot;
noom_bool_t dropped; noom_bool_t dropped;
@@ -14,7 +14,7 @@ typedef struct noomC_Local {
} noomC_Local; } noomC_Local;
typedef struct noomC_Upval { typedef struct noomC_Upval {
const char *name; const char* name;
unsigned int namelen; unsigned int namelen;
unsigned short slot; unsigned short slot;
// if stolen, this means that slot is actually an upvalue index // if stolen, this means that slot is actually an upvalue index
@@ -27,8 +27,8 @@ typedef struct noomC_Upval {
typedef struct noomC_Compiler { typedef struct noomC_Compiler {
// steal constants from this // steal constants from this
struct noomC_Compiler *parent; struct noomC_Compiler* parent;
noomV_Function *target; noomV_Function* target;
unsigned localc; unsigned localc;
unsigned upvalc; unsigned upvalc;
unsigned curstack; unsigned curstack;
@@ -39,4 +39,4 @@ typedef struct noomC_Compiler {
void noomC_compiler_init(noomC_Compiler* compiler); void noomC_compiler_init(noomC_Compiler* compiler);
// pushes the compiled function on the stack, or just crashes lol // pushes the compiled function on the stack, or just crashes lol
noom_Exit noomC_compile(noom_LuaVM *vm, const noomP_Parser *parser, const noomP_Node *node, noomV_String *chunkname, noomV_Table *env); noom_Exit noomC_compile(noom_LuaVM* vm, const noomP_Parser* parser, const noomP_Node* node, noomV_String* chunkname, noomV_Table* env);

View File

@@ -4,10 +4,10 @@
int noom_startswith(const char* str, const char* compare); int noom_startswith(const char* str, const char* compare);
int noom_memeq(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb); int noom_memeq(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb);
noom_uint_t noom_strlen(const char *s); noom_uint_t noom_strlen(const char* s);
int noom_strcmp(const char *a, const char* b); int noom_strcmp(const char* a, const char* b);
void noom_safe_strcpy(char* buffer, noom_uint_t* pos, noom_uint_t buffer_size, const char* src); void noom_safe_strcpy(char* buffer, noom_uint_t* pos, noom_uint_t buffer_size, const char* src);
char *noom_strndup(const char *s, noom_uint_t len); char* noom_strndup(const char* s, noom_uint_t len);
void noom_memcpy(void* dst, const void* src, noom_uint_t n); void noom_memcpy(void* dst, const void* src, noom_uint_t n);

View File

@@ -403,15 +403,9 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
noom_uint_t order = 0; noom_uint_t order = 0;
int succ = 0; int succ = 0;
while (s[len] == '=') { while (s[len] == '=') order++, len++;
order++;
len++;
}
if (s[len] == '[') { if (s[len] == '[') len++, succ = 1;
len++;
succ = 1;
}
if (succ) { // it is a multi-line string. if (succ) { // it is a multi-line string.
while (1) { while (1) {
@@ -420,10 +414,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
noom_uint_t order2 = 0; noom_uint_t order2 = 0;
noom_uint_t startp = len; // intentionally after the `]` noom_uint_t startp = len; // intentionally after the `]`
while (s[len] == '=') { while (s[len] == '=') order2++, len++;
order2++;
len++;
}
if (s[len] == ']' && order == order2) { // holy shit it's real if (s[len] == ']' && order == order2) { // holy shit it's real
len++; len++;

View File

@@ -17,7 +17,7 @@ typedef enum noomL_ErrorType {
NOOML_ERROR_NONE = 0, NOOML_ERROR_NONE = 0,
NOOML_ERROR_UNKNOWN, NOOML_ERROR_UNKNOWN,
NOOML_ERROR_MALFORMED_NUM, NOOML_ERROR_MALFORMED_NUM,
NOOML_ERROR_UNFINISHED_COMMENT, NOOML_ERROR_UNFINISHED_COMMENT,
NOOML_ERROR_UNFINISHED_STRING, NOOML_ERROR_UNFINISHED_STRING,
NOOML_ERROR_UNFINISHED_LONG_STRING, NOOML_ERROR_UNFINISHED_LONG_STRING,
@@ -35,7 +35,6 @@ typedef struct noomL_Token {
noom_uint_t length; noom_uint_t length;
} noomL_Token; } noomL_Token;
int noomL_isalpha(char c); int noomL_isalpha(char c);
int noomL_isnumber(char c); int noomL_isnumber(char c);
int noomL_isalphanum(char c); int noomL_isalphanum(char c);
@@ -46,9 +45,8 @@ int noomL_ishex(char c);
noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version); noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version);
noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersion version); noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersion version);
const char *noomL_formatTokenType(noomL_TokenType token_type); const char* noomL_formatTokenType(noomL_TokenType token_type);
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token, noom_LuaVersion version); // TODO: add more error data noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token, noom_LuaVersion version); // TODO: add more error data
// UB if syntax error // UB if syntax error
noom_uint_t noomL_tokenlen(const char *s, noom_uint_t start, noom_LuaVersion version); noom_uint_t noomL_tokenlen(const char* s, noom_uint_t start, noom_LuaVersion version);

View File

@@ -7,7 +7,7 @@
#define NOOM_VERSION_MAJOR 0 #define NOOM_VERSION_MAJOR 0
#define NOOM_VERSION_MINOR 0 #define NOOM_VERSION_MINOR 0
#define NOOM_VERSION_PATCH 0 #define NOOM_VERSION_PATCH 0
#if NOOM_VERSION_PATCH==0 #if NOOM_VERSION_PATCH == 0
#define NOOM_VERSION_FULL NN_XSTR(NOOM_VERSION_MAJOR) "." NN_XSTR(NOOM_VERSION_MINOR) #define NOOM_VERSION_FULL NN_XSTR(NOOM_VERSION_MAJOR) "." NN_XSTR(NOOM_VERSION_MINOR)
#else #else
#define NOOM_VERSION_FULL NN_XSTR(NOOM_VERSION_MAJOR) "." NN_XSTR(NOOM_VERSION_MINOR) "." NN_XSTR(NOOM_VERSION_PATCH) #define NOOM_VERSION_FULL NN_XSTR(NOOM_VERSION_MAJOR) "." NN_XSTR(NOOM_VERSION_MINOR) "." NN_XSTR(NOOM_VERSION_PATCH)
@@ -140,11 +140,11 @@ typedef enum noom_CoroutineStatus {
typedef struct noom_LuaVM noom_LuaVM; typedef struct noom_LuaVM noom_LuaVM;
typedef noom_Exit noom_CFunction(noom_LuaVM *vm); typedef noom_Exit noom_CFunction(noom_LuaVM* vm);
typedef noom_Exit noom_KFunction(noom_LuaVM *vm, noom_Exit status, void *ctx); typedef noom_Exit noom_KFunction(noom_LuaVM* vm, noom_Exit status, void* ctx);
noom_LuaVM *noom_createVM(noom_LuaVersion version); noom_LuaVM* noom_createVM(noom_LuaVersion version);
void noom_destroyVM(noom_LuaVM *vm); void noom_destroyVM(noom_LuaVM* vm);
// type of a stack slot. // type of a stack slot.
// If negative, its relative to the top where -1 is the top. // If negative, its relative to the top where -1 is the top.
@@ -154,57 +154,57 @@ typedef noom_int_t noom_slot_t;
// Push data // Push data
noom_Exit noom_pushint(noom_LuaVM *vm, noom_int_t integer); noom_Exit noom_pushint(noom_LuaVM* vm, noom_int_t integer);
noom_Exit noom_pushnumber(noom_LuaVM *vm, noom_float_t number); noom_Exit noom_pushnumber(noom_LuaVM* vm, noom_float_t number);
noom_Exit noom_pushbool(noom_LuaVM *vm, noom_bool_t boolean); noom_Exit noom_pushbool(noom_LuaVM* vm, noom_bool_t boolean);
noom_Exit noom_pushstring(noom_LuaVM *vm, const char *s); noom_Exit noom_pushstring(noom_LuaVM* vm, const char* s);
noom_Exit noom_pushlstring(noom_LuaVM *vm, const char *s, noom_uint_t len); noom_Exit noom_pushlstring(noom_LuaVM* vm, const char* s, noom_uint_t len);
noom_Exit noom_createtable(noom_LuaVM *vm, noom_uint_t prealloc); noom_Exit noom_createtable(noom_LuaVM* vm, noom_uint_t prealloc);
// Push a C closure using the top [upvalues] stackslots as upvalues // Push a C closure using the top [upvalues] stackslots as upvalues
noom_Exit noom_pushcclosure(noom_LuaVM *vm, noom_CFunction *f, noom_uint_t upvalues); noom_Exit noom_pushcclosure(noom_LuaVM* vm, noom_CFunction* f, noom_uint_t upvalues);
#define noom_pushcfunction(vm, f) noom_pushcclosure((vm), (f), 0) #define noom_pushcfunction(vm, f) noom_pushcclosure((vm), (f), 0)
noom_Exit noom_pushuserdata(noom_LuaVM *vm, void *userdata); noom_Exit noom_pushuserdata(noom_LuaVM* vm, void* userdata);
noom_Exit noom_pushlightuserdata(noom_LuaVM *vm, void *userdata); noom_Exit noom_pushlightuserdata(noom_LuaVM* vm, void* userdata);
// Pops the top function and pushes a new coroutine with that function being called // Pops the top function and pushes a new coroutine with that function being called
noom_Exit noom_pushcoroutine(noom_LuaVM *vm); noom_Exit noom_pushcoroutine(noom_LuaVM* vm);
// Pushes the error object, if any. If no error, nil is pushed // Pushes the error object, if any. If no error, nil is pushed
noom_Exit noom_pusherror(noom_LuaVM *vm); noom_Exit noom_pusherror(noom_LuaVM* vm);
// Pushes the globals table itself // Pushes the globals table itself
noom_Exit noom_pushglobals(noom_LuaVM *vm); noom_Exit noom_pushglobals(noom_LuaVM* vm);
// Pushes the registry itself // Pushes the registry itself
noom_Exit noom_pushregistry(noom_LuaVM *vm); noom_Exit noom_pushregistry(noom_LuaVM* vm);
// Pushes the running coroutine // Pushes the running coroutine
noom_Exit noom_pushrunning(noom_LuaVM *vm); noom_Exit noom_pushrunning(noom_LuaVM* vm);
// Stack stuff // Stack stuff
// Gets the amount of values on the stack // Gets the amount of values on the stack
// Signed because of C math rules, but it cannot be negative and is perfectly safe to cast to unsigned // Signed because of C math rules, but it cannot be negative and is perfectly safe to cast to unsigned
noom_int_t noom_getstacksize(noom_LuaVM *vm); noom_int_t noom_getstacksize(noom_LuaVM* vm);
// Sets the stack size, popping or pushing nil as need be. // Sets the stack size, popping or pushing nil as need be.
// Its signed so it gives a stack *underflow* on negative values, instead of a confusing stack overflow // Its signed so it gives a stack *underflow* on negative values, instead of a confusing stack overflow
noom_Exit noom_setstacksize(noom_LuaVM *vm, noom_int_t amount); noom_Exit noom_setstacksize(noom_LuaVM* vm, noom_int_t amount);
#define noom_pushnils(vm, x) noom_setstacksize((vm), noom_getstacksize((vm))+(x)) #define noom_pushnils(vm, x) noom_setstacksize((vm), noom_getstacksize((vm)) + (x))
#define noom_pushnil(vm) noom_pushnils((vm), 1) #define noom_pushnil(vm) noom_pushnils((vm), 1)
#define noom_popn(vm, n) noom_setstacksize((vm), noom_getstacksize((vm)) - (n)) #define noom_popn(vm, n) noom_setstacksize((vm), noom_getstacksize((vm)) - (n))
#define noom_pop(vm) noom_popn((vm), 1) #define noom_pop(vm) noom_popn((vm), 1)
// Marks a stackslot as to-be-closed // Marks a stackslot as to-be-closed
noom_Exit noom_markTBC(noom_LuaVM *vm, noom_slot_t slot); noom_Exit noom_markTBC(noom_LuaVM* vm, noom_slot_t slot);
// Preallocate stack space to OOM/stack overflow *early* // Preallocate stack space to OOM/stack overflow *early*
noom_Exit noom_preallocStack(noom_LuaVM *vm, noom_uint_t amount); noom_Exit noom_preallocStack(noom_LuaVM* vm, noom_uint_t amount);
// Set the value at [dst] to [src] // Set the value at [dst] to [src]
noom_Exit noom_copyto(noom_LuaVM *vm, noom_slot_t dst, noom_slot_t src); noom_Exit noom_copyto(noom_LuaVM* vm, noom_slot_t dst, noom_slot_t src);
// Set the value at [dst] to be a pointer to [src]. // Set the value at [dst] to be a pointer to [src].
// This is used to link upvalues. // This is used to link upvalues.
// This means that writes to either are reflected in both. // This means that writes to either are reflected in both.
noom_Exit noom_linkto(noom_LuaVM *vm, noom_slot_t dst, noom_slot_t src); noom_Exit noom_linkto(noom_LuaVM* vm, noom_slot_t dst, noom_slot_t src);
// Stack rotation // Stack rotation
noom_Exit noom_rotate(noom_LuaVM *vm, noom_int_t amount); noom_Exit noom_rotate(noom_LuaVM* vm, noom_int_t amount);
// Operations // Operations
// Dupes the top [n] values [m] times // Dupes the top [n] values [m] times
noom_Exit noom_dupearr(noom_LuaVM *vm, noom_uint_t n, noom_uint_t m); noom_Exit noom_dupearr(noom_LuaVM* vm, noom_uint_t n, noom_uint_t m);
#define noom_dupen(vm, n) noom_dupearr((vm), 1, (n)) #define noom_dupen(vm, n) noom_dupearr((vm), 1, (n))
#define noom_dupe(vm) noom_dupen((vm), 1) #define noom_dupe(vm) noom_dupen((vm), 1)
@@ -215,82 +215,82 @@ noom_Exit noom_dupearr(noom_LuaVM *vm, noom_uint_t n, noom_uint_t m);
// This will pop argc+1 values, where the first one, at [-argc-1], is the value being called, and everything after is arguments. // This will pop argc+1 values, where the first one, at [-argc-1], is the value being called, and everything after is arguments.
// [retc] is the desired return count. It is signed, as negative values mean to return ALL values, regardless of how many. // [retc] is the desired return count. It is signed, as negative values mean to return ALL values, regardless of how many.
// If the function returns too many values, the excess is popped. If it returns too few, nils are pushed. // If the function returns too many values, the excess is popped. If it returns too few, nils are pushed.
noom_Exit noom_callk(noom_LuaVM *vm, noom_uint_t argc, noom_int_t retc, noom_KFunction *f, void *ctx); noom_Exit noom_callk(noom_LuaVM* vm, noom_uint_t argc, noom_int_t retc, noom_KFunction* f, void* ctx);
#define noom_call(vm, argc, retc) noom_callk((vm), (argc), (retc), 0, 0) #define noom_call(vm, argc, retc) noom_callk((vm), (argc), (retc), 0, 0)
// Sets the return count of the function. Should be the last statement to prevent confusing behavior. // Sets the return count of the function. Should be the last statement to prevent confusing behavior.
// The actual *values* returned are the top [retc] values on the stack after the function exits. // The actual *values* returned are the top [retc] values on the stack after the function exits.
noom_Exit noom_return(noom_LuaVM *vm, noom_uint_t retc); noom_Exit noom_return(noom_LuaVM* vm, noom_uint_t retc);
// Attempts to yield the current coroutine with the top [n] values and a supplied continuation function. // Attempts to yield the current coroutine with the top [n] values and a supplied continuation function.
// If the continuation function's memory address is 0, it is replaced with a default function which // If the continuation function's memory address is 0, it is replaced with a default function which
// does nothing and returns no values. // does nothing and returns no values.
// When the coroutine is resumed, the continuation function is invoked. // When the coroutine is resumed, the continuation function is invoked.
noom_Exit noom_yieldk(noom_LuaVM *vm, noom_uint_t n, noom_KFunction *f, void *ctx); noom_Exit noom_yieldk(noom_LuaVM* vm, noom_uint_t n, noom_KFunction* f, void* ctx);
#define noom_yield(vm, n) noom_yieldk((vm), (n), 0, 0) #define noom_yield(vm, n) noom_yieldk((vm), (n), 0, 0)
// Like a noom_call, except instead of calling, it resumes coroutines. // Like a noom_call, except instead of calling, it resumes coroutines.
// This cannot yield but allows yields. // This cannot yield but allows yields.
noom_Exit noom_resume(noom_LuaVM *vm, noom_uint_t argc, noom_uint_t retc); noom_Exit noom_resume(noom_LuaVM* vm, noom_uint_t argc, noom_uint_t retc);
// Pops a coroutine, gives its status through the out-pointer // Pops a coroutine, gives its status through the out-pointer
noom_Exit noom_corostatus(noom_LuaVM *vm, noom_CoroutineStatus *status); noom_Exit noom_corostatus(noom_LuaVM* vm, noom_CoroutineStatus* status);
// Pops a coroutine, closes it. // Pops a coroutine, closes it.
// This means it'll unwind the callstack of the coroutine and call to-be-closed values as needed. // This means it'll unwind the callstack of the coroutine and call to-be-closed values as needed.
// If those error, which can happen, this errors as well. // If those error, which can happen, this errors as well.
noom_Exit noom_coroclose(noom_LuaVM *vm); noom_Exit noom_coroclose(noom_LuaVM* vm);
// Get the type of a slot. Invalid slots are just [nil] // Get the type of a slot. Invalid slots are just [nil]
noom_LuaType noom_typeof(noom_LuaVM *vm, noom_slot_t x); noom_LuaType noom_typeof(noom_LuaVM* vm, noom_slot_t x);
// TODO: evaluate if k variants of these should be used, as // TODO: evaluate if k variants of these should be used, as
// these *could* yield due to metatables // these *could* yield due to metatables
// Pops the top 2 values and pushes the result of the operation // Pops the top 2 values and pushes the result of the operation
noom_Exit noom_binop(noom_LuaVM *vm, noom_BinOp op); noom_Exit noom_binop(noom_LuaVM* vm, noom_BinOp op);
// Pops the top 2 values and pushes the result of the operation // Pops the top 2 values and pushes the result of the operation
noom_Exit noom_unaryop(noom_LuaVM *vm, noom_UnaryOp op); noom_Exit noom_unaryop(noom_LuaVM* vm, noom_UnaryOp op);
// Pops (table, key) and pushes `table[key]` // Pops (table, key) and pushes `table[key]`
noom_Exit noom_gettable(noom_LuaVM *vm); noom_Exit noom_gettable(noom_LuaVM* vm);
// Pops (table, key, value) and sets `table[key] = value` // Pops (table, key, value) and sets `table[key] = value`
noom_Exit noom_settable(noom_LuaVM *vm); noom_Exit noom_settable(noom_LuaVM* vm);
// Gets the metatable. This gets the actual metatable, __metatable is ignored // Gets the metatable. This gets the actual metatable, __metatable is ignored
noom_Exit noom_getmetatable(noom_LuaVM *vm); noom_Exit noom_getmetatable(noom_LuaVM* vm);
// Pops (table, metatable) and sets the metatable of [table] to [metatable] // Pops (table, metatable) and sets the metatable of [table] to [metatable]
noom_Exit noom_setmetatable(noom_LuaVM *vm); noom_Exit noom_setmetatable(noom_LuaVM* vm);
// Given stack values (table, key), it pops key and pushes the next key as well as `table[nkey]`. // Given stack values (table, key), it pops key and pushes the next key as well as `table[nkey]`.
// If key is nil, the next key is actually the first key of the table, as nil cannot be a key. // If key is nil, the next key is actually the first key of the table, as nil cannot be a key.
// This lets you iterate stuff with like `pairs`. // This lets you iterate stuff with like `pairs`.
// This DOES NOT account for __pairs. // This DOES NOT account for __pairs.
noom_Exit noom_next(noom_LuaVM *vm); noom_Exit noom_next(noom_LuaVM* vm);
// Taking data out // Taking data out
noom_Exit noom_tobool(noom_LuaVM *vm, noom_slot_t x, noom_bool_t *b); noom_Exit noom_tobool(noom_LuaVM* vm, noom_slot_t x, noom_bool_t* b);
noom_Exit noom_toint(noom_LuaVM *vm, noom_slot_t x, noom_int_t *n); noom_Exit noom_toint(noom_LuaVM* vm, noom_slot_t x, noom_int_t* n);
noom_Exit noom_tonumber(noom_LuaVM *vm, noom_slot_t x, noom_float_t *n); noom_Exit noom_tonumber(noom_LuaVM* vm, noom_slot_t x, noom_float_t* n);
// NOTE: the string is not automatically retained until function exit. // NOTE: the string is not automatically retained until function exit.
// KEEP THE VALUE SOMEWHERE OR GC MAY FREE IT. // KEEP THE VALUE SOMEWHERE OR GC MAY FREE IT.
noom_Exit noom_tolstring(noom_LuaVM *vm, noom_slot_t x, const char **s, noom_uint_t *len); noom_Exit noom_tolstring(noom_LuaVM* vm, noom_slot_t x, const char** s, noom_uint_t* len);
#define noom_tostring(vm, x, s) noom_tolstring((vm), (x), (s), 0) #define noom_tostring(vm, x, s) noom_tolstring((vm), (x), (s), 0)
// Casts to a pointer if applicable, memory address 0x0 if not. // Casts to a pointer if applicable, memory address 0x0 if not.
// The pointer is effectively meaningless, and only really matters // The pointer is effectively meaningless, and only really matters
// for the %p format specifier in `string.format()` // for the %p format specifier in `string.format()`
noom_Exit noom_topointer(noom_LuaVM *vm, noom_slot_t x, const void **p); noom_Exit noom_topointer(noom_LuaVM* vm, noom_slot_t x, const void** p);
// Type coercion // Type coercion
// Converts the value at [x] to a boolean // Converts the value at [x] to a boolean
noom_Exit noom_cast2truthy(noom_LuaVM *vm, noom_slot_t x); noom_Exit noom_cast2truthy(noom_LuaVM* vm, noom_slot_t x);
// Converts the value at [x] to a number // Converts the value at [x] to a number
noom_Exit noom_cast2num(noom_LuaVM *vm, noom_slot_t x); noom_Exit noom_cast2num(noom_LuaVM* vm, noom_slot_t x);
// Converts the value at [x] to a string, ignoring __tostring and __name. // Converts the value at [x] to a string, ignoring __tostring and __name.
noom_Exit noom_cast2str(noom_LuaVM *vm, noom_slot_t x); noom_Exit noom_cast2str(noom_LuaVM* vm, noom_slot_t x);
// Misc // Misc
// Run the garbage collector to clean up memory. // Run the garbage collector to clean up memory.
// DO NOT RUN IN THE ALLOCATOR IF ANY IS SUPPLIED, OR YOU WILL GET THE WORST POSSIBLE ERRORS. // DO NOT RUN IN THE ALLOCATOR IF ANY IS SUPPLIED, OR YOU WILL GET THE WORST POSSIBLE ERRORS.
void noom_gc(noom_LuaVM *vm); void noom_gc(noom_LuaVM* vm);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -3,9 +3,9 @@
typedef enum noomP_NodeType { typedef enum noomP_NodeType {
NOOMP_NODE_PROGRAM, NOOMP_NODE_PROGRAM,
NOOMP_NODE_VARNAME, NOOMP_NODE_VARNAME,
NOOMP_NODE_LOCALDECLARATION, NOOMP_NODE_LOCALDECLARATION,
NOOMP_NODE_IFSTATEMENT, NOOMP_NODE_IFSTATEMENT,
NOOMP_NODE_WHILELOOP, NOOMP_NODE_WHILELOOP,
@@ -18,7 +18,7 @@ typedef enum noomP_NodeType {
NOOMP_NODE_ATTRIBUTE, NOOMP_NODE_ATTRIBUTE,
NOOMP_NODE_BREAK, NOOMP_NODE_BREAK,
NOOMP_NODE_VARIABLE, NOOMP_NODE_VARIABLE,
NOOMP_NODE_NUMBERLITERAL, NOOMP_NODE_NUMBERLITERAL,
NOOMP_NODE_BOOLEANLITERAL, NOOMP_NODE_BOOLEANLITERAL,
@@ -129,7 +129,7 @@ typedef struct noomP_Node {
noomP_NodeType type; noomP_NodeType type;
noom_uint_t source_offset; noom_uint_t source_offset;
noom_uint_t subnodec; noom_uint_t subnodec;
noom_uint_t subnode_cap; noom_uint_t subnode_cap;
struct noomP_Node** subnodes; struct noomP_Node** subnodes;
@@ -139,7 +139,7 @@ typedef struct noomP_Node {
typedef struct noomP_Parser { // todo: track location in code with line/column? typedef struct noomP_Parser { // todo: track location in code with line/column?
noom_LuaVersion version; noom_LuaVersion version;
const char* code; const char* code;
const char* filename; const char* filename;
noom_uint_t last_token_offset; noom_uint_t last_token_offset;
@@ -152,7 +152,7 @@ typedef struct noomP_Parser { // todo: track location in code with line/column?
noomP_Node* last_node; noomP_Node* last_node;
} noomP_Parser; } noomP_Parser;
const char *noomP_formatNodeType(noomP_NodeType node_type); const char* noomP_formatNodeType(noomP_NodeType node_type);
int noomP_peek(noomP_Parser* parser, noomL_Token* token); int noomP_peek(noomP_Parser* parser, noomL_Token* token);
void noomP_skip(noomP_Parser* parser, noomL_Token* token); void noomP_skip(noomP_Parser* parser, noomL_Token* token);
@@ -168,4 +168,3 @@ noomP_Node* noomP_parseStatement(noomP_Parser* parser);
int noomP_parse(const char* code, const char* filename, noom_LuaVersion version, noomP_Node** outpointer, noomP_Parser* parser); int noomP_parse(const char* code, const char* filename, noom_LuaVersion version, noomP_Node** outpointer, noomP_Parser* parser);
int noomP_initParser(noomP_Parser* parser, const char* code, const char* filename, noom_LuaVersion version); int noomP_initParser(noomP_Parser* parser, const char* code, const char* filename, noom_LuaVersion version);

170
src/vm.h
View File

@@ -8,7 +8,7 @@
#endif #endif
#ifndef NOOM_MAXCALL #ifndef NOOM_MAXCALL
#define NOOM_MAXCALL (NOOM_MAXSTACK/32) #define NOOM_MAXCALL (NOOM_MAXSTACK / 32)
#endif #endif
// Defines values, threads, bullshit, idc // Defines values, threads, bullshit, idc
@@ -25,8 +25,8 @@ typedef enum noomV_ObjTag {
typedef struct noomV_Object { typedef struct noomV_Object {
noomV_ObjTag tag; noomV_ObjTag tag;
noom_bool_t marked; noom_bool_t marked;
struct noomV_Object *next; struct noomV_Object* next;
struct noomV_Object *nextGray; struct noomV_Object* nextGray;
} noomV_Object; } noomV_Object;
typedef enum noomV_ValueTag : unsigned char { typedef enum noomV_ValueTag : unsigned char {
@@ -49,9 +49,9 @@ typedef struct noomV_Value {
noom_bool_t boolean; noom_bool_t boolean;
noom_int_t integer; noom_int_t integer;
noom_float_t number; noom_float_t number;
void *lightuserdata; void* lightuserdata;
noomV_Object *obj; noomV_Object* obj;
struct noomV_Pointer *ptr; struct noomV_Pointer* ptr;
}; };
} noomV_Value; } noomV_Value;
@@ -64,7 +64,7 @@ typedef struct noomV_String {
typedef struct noomV_Table { typedef struct noomV_Table {
noomV_Object obj; noomV_Object obj;
struct noomV_Table *meta; struct noomV_Table* meta;
// amount of entries allocated // amount of entries allocated
noom_uint_t entries; noom_uint_t entries;
// how many entries are defined. // how many entries are defined.
@@ -74,7 +74,7 @@ typedef struct noomV_Table {
noom_uint_t len; noom_uint_t len;
// actual values. Given index i, V[i] is the key and V[i+entries] is the value. // actual values. Given index i, V[i] is the key and V[i+entries] is the value.
// This is because we mostly read the key, so we should put more keys in cache. // This is because we mostly read the key, so we should put more keys in cache.
noomV_Value *entrydata; noomV_Value* entrydata;
} noomV_Table; } noomV_Table;
typedef struct noomV_Pointer { typedef struct noomV_Pointer {
@@ -109,7 +109,7 @@ typedef enum noomV_Opcode : unsigned char {
NOOMV_INSTR_PUSHCLOSURE, NOOMV_INSTR_PUSHCLOSURE,
// High-level ops // High-level ops
// Call the function stored at stack[op.a]. All values after that are treated at arguments. op.uD is the expected return count plus one, and if op.uD is 0, // Call the function stored at stack[op.a]. All values after that are treated at arguments. op.uD is the expected return count plus one, and if op.uD is 0,
// all returns are pushed. // all returns are pushed.
NOOMV_INSTR_CALL, NOOMV_INSTR_CALL,
@@ -142,7 +142,7 @@ typedef enum noomV_Opcode : unsigned char {
NOOMV_INSTR_SETGLOBAL, NOOMV_INSTR_SETGLOBAL,
// Control flow // Control flow
// returns from a function. The amount returned is from op.uD // returns from a function. The amount returned is from op.uD
NOOMV_INSTR_RET, NOOMV_INSTR_RET,
@@ -188,14 +188,14 @@ typedef struct noomV_Inst {
} noomV_Inst; } noomV_Inst;
typedef struct noomV_UpvalDesc { typedef struct noomV_UpvalDesc {
char *name; char* name;
unsigned char idx; unsigned char idx;
// whether the index is a stack index // whether the index is a stack index
noom_bool_t isStack; noom_bool_t isStack;
} noomV_UpvalDesc; } noomV_UpvalDesc;
typedef struct noomV_LocalDesc { typedef struct noomV_LocalDesc {
char *name; char* name;
unsigned char stackIdx; unsigned char stackIdx;
// to forbid changing it with debug.setlocal // to forbid changing it with debug.setlocal
noom_bool_t isConst; noom_bool_t isConst;
@@ -208,78 +208,78 @@ typedef struct noomV_LocalDesc {
typedef struct noomV_Function { typedef struct noomV_Function {
noomV_Object obj; noomV_Object obj;
// source location string // source location string
noomV_String *chunkname; noomV_String* chunkname;
// globals. Lua 5.1 only, use the _ENV upvalue for Lua 5.2+ // globals. Lua 5.1 only, use the _ENV upvalue for Lua 5.2+
noomV_Table *env; noomV_Table* env;
noomV_Inst *code; noomV_Inst* code;
noomV_Value *consts; noomV_Value* consts;
struct noomV_Function **protos; struct noomV_Function** protos;
noomV_UpvalDesc *upvals; noomV_UpvalDesc* upvals;
noomV_LocalDesc *locals; noomV_LocalDesc* locals;
unsigned int codesize; unsigned int codesize;
// line of function // line of function
unsigned int linedefined; unsigned int linedefined;
// line of end // line of end
unsigned int lastlinedefined; unsigned int lastlinedefined;
// very size limited // very size limited
// ░░░░░░░░░ ░ ▒░░▒ ▒░ ░░ ░ // ░░░░░░░░░ ░ ▒░░▒ ▒░ ░░ ░
// ░░░░░░░░░ █░█░▓▓ ░█▒████▒░ █░░████▓ // ░░░░░░░░░ █░█░▓▓ ░█▒████▒░ █░░████▓
// ░░░░░░░░░ ▓████████░█░▒████▓░████ ░█░ // ░░░░░░░░░ ▓████████░█░▒████▓░████ ░█░
// ░░░░░░░░░ ░░░ ░█▓░█▒█░█░▒▒█▓████▒ ░█▒█ ░█░░ // ░░░░░░░░░ ░░░ ░█▓░█▒█░█░▒▒█▓████▒ ░█▒█ ░█░░
// ░░░░░░░░░ ░░░░░ ░█▓▓███▒█ ▓▓▒▒▒▓▓▒ ▒█▒█▓██▓▓ // ░░░░░░░░░ ░░░░░ ░█▓▓███▒█ ▓▓▒▒▒▓▓▒ ▒█▒█▓██▓▓
// ░░░░░░░░░ ░░ ░▓▓███▓█▒░░█▒▓█▓██▓ ▒▓█▓▓▓█▒▒ // ░░░░░░░░░ ░░ ░▓▓███▓█▒░░█▒▓█▓██▓ ▒▓█▓▓▓█▒▒
// ░░░░░░░░░░ ░░ ▒▓█░█▒█░ ░▓▓██████░░██░░▒█ // ░░░░░░░░░░ ░░ ▒▓█░█▒█░ ░▓▓██████░░██░░▒█
// ░░░░░░░░░░ ░░░░ ░░ ▓▓█▒█▓█▒░ ▒▒▒█▒░█░ ███░░█ // ░░░░░░░░░░ ░░░░ ░░ ▓▓█▒█▓█▒░ ▒▒▒█▒░█░ ███░░█
// ░░░░░░░░░░ ░░ ▓▒▓███░█▒ ▒░░░▓▓█░░██░▓▒██ // ░░░░░░░░░░ ░░ ▓▒▓███░█▒ ▒░░░▓▓█░░██░▓▒██
// ░░░░░░░░░░ ░░ ░ ░░ ░░ ░░ ░░ ▒░ ░░░ ░░ // ░░░░░░░░░░ ░░ ░ ░░ ░░ ░░ ░░ ▒░ ░░░ ░░
// ░░░░░░░░░░ ░ ░░░ ░ ░ ░ ░ // ░░░░░░░░░░ ░ ░░░ ░ ░ ░ ░
// ░░░░░░░░░░ ░░░░ ░░ ░ // ░░░░░░░░░░ ░░░░ ░░ ░
// ░ ░░░░░░░░░ ░░░░ ░ ░ ░ ░░ ░░░ ░░░ ░ ░░ // ░ ░░░░░░░░░ ░░░░ ░ ░ ░ ░░ ░░░ ░░░ ░ ░░
// ░░░░░░░░░ ░ █████ ▒████ ░▓███▓░ ░████░ ░████▓ ░████░ ░▓███▓ ░████░ // ░░░░░░░░░ ░ █████ ▒████ ░▓███▓░ ░████░ ░████▓ ░████░ ░▓███▓ ░████░
//░░ ░░░░░░░░░ █▓▒██░░██▓██▒░░██▒██░░██▒▓██░▒██▒██ ██▓▓██ ░██▒██░ ██▒▓██ // ░░ ░░░░░░░░░ █▓▒██░░██▓██▒░░██▒██░░██▒▓██░▒██▒██ ██▓▓██ ░██▒██░ ██▒▓██
//░░ ░░░░░░░░░░ ██░▒█▓ ░██░██▒ ▒█▒░██ ░██ ██░ ▓█▒░██ ░██░██░ ▓█▒░██ ██░ // ░░ ░░░░░░░░░░ ██░▒█▓ ░██░██▒ ▒█▒░██ ░██ ██░ ▓█▒░██ ░██░██░ ▓█▒░██ ██░
// ░░░░░░░░░░░ ░▓███░▒█▓ ██░██░ ▒██▒██ ░██░██ ▒█▒░██ ░██░██░ ▒█▓▒█▓ ░██░ // ░░░░░░░░░░░ ░▓███░▒█▓ ██░██░ ▒██▒██ ░██░██ ▒█▒░██ ░██░██░ ▒█▓▒█▓ ░██░
// ░░░░░░░░░░ ░ ░▒███░ ▓████░▓█▒ ░██░██░ ▒██▒█▓ ░██░██ ░▒█▒░██ ░██░██░ ░██▒█▓ ░██░░ // ░░░░░░░░░░ ░ ░▒███░ ▓████░▓█▒ ░██░██░ ▒██▒█▓ ░██░██ ░▒█▒░██ ░██░██░ ░██▒█▓ ░██░░
// ░░░░░░░░░ ░░ ░ ░░██▓▓█▓░░██ ██░ ▒█▓▒█▓ ██░██░ ▒█▒░██ ░██░██░ ░██▒█▓ ░██░░ // ░░░░░░░░░ ░░ ░ ░░██▓▓█▓░░██ ██░ ▒█▓▒█▓ ██░██░ ▒█▒░██ ░██░██░ ░██▒█▓ ░██░░
// ░░░░░░░░░░ ░░░░ ░ ░███▒██ ░██ ██▒ ▓█▒▒██ ░██░██░ ██░░██░░██ ██▒ ▓█░░██ ░██ // ░░░░░░░░░░ ░░░░ ░ ░███▒██ ░██ ██▒ ▓█▒▒██ ░██░██░ ██░░██░░██ ██▒ ▓█░░██ ░██
// ░░░░░░░░░ ░░░ ░█████▒░█████▒▒▓█████ ░█████▒ ▒█████▓██████▓ ▒██▓██░ █████▒ // ░░░░░░░░░ ░░░ ░█████▒░█████▒▒▓█████ ░█████▒ ▒█████▓██████▓ ▒██▓██░ █████▒
// ░░░░░░░░░ ░░ ░████▒ ░░███▒░█▒▒███░ ░███▓ ▓███░▓█░███▓░ ▒███░ ░███▒ // ░░░░░░░░░ ░░ ░████▒ ░░███▒░█▒▒███░ ░███▓ ▓███░▓█░███▓░ ▒███░ ░███▒
// ░░░░░░░░░░ ░█░ ░ ░ ▒▒ ░ // ░░░░░░░░░░ ░█░ ░ ░ ▒▒ ░
// ░░░░░░░░░ ░ ░ // ░░░░░░░░░ ░ ░
// ░░░░░░░░░ ░░░░ // ░░░░░░░░░ ░░░░
// ░░░░░░░░░ ░░░░░ ░░ ░ ░ ░ ░ // ░░░░░░░░░ ░░░░░ ░░ ░ ░ ░ ░
// ░░░░░░░░░ ░░ ░░▒░░ ░▓▒ ░█▓ ░ ░ ░ // ░░░░░░░░░ ░░ ░░▒░░ ░▓▒ ░█▓ ░ ░ ░
// ░░░░░░░░░ ░░ ▒█████░ ░██ ░██ ░ ░ // ░░░░░░░░░ ░░ ▒█████░ ░██ ░██ ░ ░
// ░░░░░░░░░░░░░ ██▒▒███ ░░ ░ ░ ░░ ░░ ░██ // ░░░░░░░░░░░░░ ██▒▒███ ░░ ░ ░ ░░ ░░ ░██
// ░ ░░░░░░░░░░░░░░░ ██░ ░▓▓ ░████ ░████░░█▓ ▒███▓░░██ // ░ ░░░░░░░░░░░░░░░ ██░ ░▓▓ ░████ ░████░░█▓ ▒███▓░░██
// ░░░░░░░░░░░░░░░░░░░░░▓███░░ ░██████░██▓██▓▒██░██▓██▒▒██░ // ░░░░░░░░░░░░░░░░░░░░░▓███░░ ░██████░██▓██▓▒██░██▓██▒▒██░
// ░░░░░░░░░░░░░░░░░░░░ ░████░░█▓ ██░██ ▓▓░██ ░░▒██▓▒██░ ░ // ░░░░░░░░░░░░░░░░░░░░ ░████░░█▓ ██░██ ▓▓░██ ░░▒██▓▒██░ ░
// ░░░░░░░░░░░░░░░░░░░ ░░░░███▒█▓ ██▒██ ░░██░█████▓▒██░ ░░ ░ // ░░░░░░░░░░░░░░░░░░░ ░░░░███▒█▓ ██▒██ ░░██░█████▓▒██░ ░░ ░
// ░░░░░░░░░░░░░░░░░░ ░██▒ ░██▒██░░██░██ ░█▓░██░██░▓█▓░██░ ░ ██▓████████▓▒██▒ // ░░░░░░░░░░░░░░░░░░ ░██▒ ░██▒██░░██░██ ░█▓░██░██░▓█▓░██░ ░ ██▓████████▓▒██▒
// ░░░░░░░░░░░░░░░░░ ░██████▓ █████▓ █████▓▒██░█████▓░██░ ░ ░░░███████████▓█▓ // ░░░░░░░░░░░░░░░░░ ░██████▓ █████▓ █████▓▒██░█████▓░██░ ░ ░░░███████████▓█▓
// ░░░░░░░░░░░░░░░░░░ ░▓███▒ ░███▒ ░▓██▓ ░█▓ ▒██▓█▓░██░ ░████████████▓█░ // ░░░░░░░░░░░░░░░░░░ ░▓███▒ ░███▒ ░▓██▓ ░█▓ ▒██▓█▓░██░ ░████████████▓█░
// ░░░░░░░░░░░░░░░░░ ░ ░ ░█████████▓██ // ░░░░░░░░░░░░░░░░░ ░ ░ ░█████████▓██
// ░░░░░░░░░░░░░░ ░░ ▒███████▓█▓ // ░░░░░░░░░░░░░░ ░░ ▒███████▓█▓
// ░ ░░░░░░░░░░░░░░ ░░░░ ██▓██▓▓▓███ ░ // ░ ░░░░░░░░░░░░░░ ░░░░ ██▓██▓▓▓███ ░
// ░░░░░░░░░░░░░░░ ░░ ░ ░███████████ // ░░░░░░░░░░░░░░░ ░░ ░ ░███████████
// ░░░░░░░░░░░░ ░░ ░ ████████████ ▒ // ░░░░░░░░░░░░ ░░ ░ ████████████ ▒
// ░░░░░░░░░░░░ ░░▒▒░░ ░ ░██░▓█░ ░ ░░████ ▒██████ █ // ░░░░░░░░░░░░ ░░▒▒░░ ░ ░██░▓█░ ░ ░░████ ▒██████ █
// ░░░░░░░░░░░ ▓█████░ ░██ ██░░█▓░ ▓████ ▓█████ ░█ ▒▒ // ░░░░░░░░░░░ ▓█████░ ░██ ██░░█▓░ ▓████ ▓█████ ░█ ▒▒
// ░░░░░░░░░░░ ▒██░░██▒░░░░░ ░░░░░ ░░░░██ ░░░██░ █████ ▒█████ ▒█ ▒█░ // ░░░░░░░░░░░ ▒██░░██▒░░░░░ ░░░░░ ░░░░██ ░░░██░ █████ ▒█████ ▒█ ▒█░
// ░░░░░░░░░░░░░ ██▒ ░█▓░████░████░ ░█████ ██▒████ █████ ▓████▒ ████░ // ░░░░░░░░░░░░░ ██▒ ░█▓░████░████░ ░█████ ██▒████ █████ ▓████▒ ████░
// ░░░░░░░░░░░░ ██░ ▒███▓██▓▓██ ██▒▒██ ██░▒██░ ████▒ █████ ▒████████ // ░░░░░░░░░░░░ ██░ ▒███▓██▓▓██ ██▒▒██ ██░▒██░ ████▒ █████ ▒████████
// ░░░░░░░░░░ ██░ ░ ▒██░ ██▒▒██░██ ░██ ██ ░██░ ░████ ░█████ ▓██▓▒░░ // ░░░░░░░░░░ ██░ ░ ▒██░ ██▒▒██░██ ░██ ██ ░██░ ░████ ░█████ ▓██▓▒░░
// ░░░░░░░░░░ ██▒ ░▒░▒██ ░██████▒██ ░██░██░░██ ▒███▒ █████▒ // ░░░░░░░░░░ ██▒ ░▒░▒██ ░██████▒██ ░██░██░░██ ▒███▒ █████▒
// ░ ░░░░░░░░░ ▓█▓░ ▓█▓▒██ ░██░ ░░░██ ░██ ██░░██░ ░███░░░█████ // ░ ░░░░░░░░░ ▓█▓░ ▓█▓▒██ ░██░ ░░░██ ░██ ██░░██░ ░███░░░█████
// ░░░░░░░░░ ██████ ▒█▓ ▓█████ ██████ ██░░███░ ░██▒ █████▒ // ░░░░░░░░░ ██████ ▒█▓ ▓█████ ██████ ██░░███░ ░██▒ █████▒
// ░░░░░░░░░ ░▓███░░░█▓ ░░▒███░░░██▓█▓ ▓█░ ▒█▓░ ▓▓ ░█████ // ░░░░░░░░░ ░▓███░░░█▓ ░░▒███░░░██▓█▓ ▓█░ ▒█▓░ ▓▓ ░█████
// ░░░░░░░░░ ░░░░ ░░ ░░ ░ ░ ▓▒ ▓████░ // ░░░░░░░░░ ░░░░ ░░ ░░ ░ ░ ▓▒ ▓████░
// ░░░░░░░░ ░░ ░░ ░ ▒▓ ████▒ // ░░░░░░░░ ░░ ░░ ░ ▒▓ ████▒
// ░ ░░░░░ ░ ░ ▒░▓███▒ // ░ ░░░░░ ░ ░ ▒░▓███▒
// ░░░░ ░ ░▓▓██ // ░░░░ ░ ░▓▓██
// ░░░ // ░░░
// ░░░ // ░░░
// ░░ // ░░
unsigned char argc; unsigned char argc;
unsigned char flags; unsigned char flags;
unsigned short constsize; unsigned short constsize;
@@ -294,14 +294,14 @@ typedef struct noomV_CallFrame {
noom_bool_t isC; noom_bool_t isC;
union { union {
struct { struct {
// //
unsigned int pc; unsigned int pc;
// amount of varargs // amount of varargs
unsigned int varargc; unsigned int varargc;
} lua; } lua;
struct { struct {
noom_KFunction *resumeFunc; noom_KFunction* resumeFunc;
void *resumeCtx; void* resumeCtx;
} c; } c;
}; };
} noomV_CallFrame; } noomV_CallFrame;
@@ -311,26 +311,26 @@ typedef struct noomV_Thread {
unsigned int stacklen, calldepth; unsigned int stacklen, calldepth;
unsigned int stackcap, callcap; unsigned int stackcap, callcap;
// can have pointers! // can have pointers!
noomV_Value *stack; noomV_Value* stack;
noomV_CallFrame *calls; noomV_CallFrame* calls;
struct noomV_Thread *resumedBy; struct noomV_Thread* resumedBy;
struct noomV_Thread *resuming; struct noomV_Thread* resuming;
} noomV_Thread; } noomV_Thread;
struct noom_LuaVM { struct noom_LuaVM {
noomV_Object *heap; noomV_Object* heap;
noomV_Object *graySet; noomV_Object* graySet;
noomV_Table *globals; noomV_Table* globals;
noomV_Table *registry; noomV_Table* registry;
noomV_Thread *mainThread; noomV_Thread* mainThread;
noomV_Thread *currentThread; noomV_Thread* currentThread;
noom_LuaVersion version; noom_LuaVersion version;
}; };
// Allocating objects // Allocating objects
noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size); noomV_Object* noomV_allocObj(noom_LuaVM* vm, noomV_ObjTag tag, noom_uint_t size);
noomV_String *noomV_allocStr(noom_LuaVM *vm, const char *str, noom_uint_t len); noomV_String* noomV_allocStr(noom_LuaVM* vm, const char* str, noom_uint_t len);
noomV_Function *noomV_allocFunc(noom_LuaVM *vm, noomV_String *chunkname); noomV_Function* noomV_allocFunc(noom_LuaVM* vm, noomV_String* chunkname);
void noomV_freeObj(noomV_Object *obj); void noomV_freeObj(noomV_Object* obj);
#endif #endif