From: Lukas Krickl Date: Wed, 11 Mar 2026 05:59:31 +0000 (+0100) Subject: scripting: reworked and simplified the scripting types X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=63deabd5094ee1a71df9e54e3801e2aba684b186;p=lrts%2F.git scripting: reworked and simplified the scripting types --- diff --git a/src/l_lsl.c b/src/l_lsl.c index be76603..3c449a9 100644 --- a/src/l_lsl.c +++ b/src/l_lsl.c @@ -16,31 +16,37 @@ void l_lsl_vm_free(struct l_lsl_vm *vm) { LRTS_UNUSED(vm); } -enum l_lsl_error l_lsl_compile_file(struct l_lsl_vm *v, const char *path) { - enum l_lsl_error err = L_LSL_ERR_OK; +struct l_lsl_value l_lsl_value_init(void) { + struct l_lsl_value val; + u_memset(&val, 0, sizeof(val)); + return val; +} + +struct l_lsl_value l_lsl_compile_file(struct l_lsl_vm *v, const char *path) { + struct l_lsl_value val = l_lsl_value_init(); LRTS_UNUSED(v); LRTS_UNUSED(path); - return err; + return val; } -enum l_lsl_error l_lsl_run(struct l_lsl_vm *v, const char *entry) { - enum l_lsl_error err = L_LSL_ERR_OK; +struct l_lsl_value l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program) { + struct l_lsl_value val = l_lsl_value_init(); LRTS_UNUSED(v); - LRTS_UNUSED(entry); + LRTS_UNUSED(program); - return err; + return val; } -enum l_lsl_error l_lsl_exec(struct l_lsl_vm *v, const char *path) { - enum l_lsl_error err = L_LSL_ERR_OK; +struct l_lsl_value l_lsl_exec(struct l_lsl_vm *v, const char *path) { + struct l_lsl_value val = l_lsl_value_init(); - err = l_lsl_compile_file(v, path); - if (err) { - return err; + l_lsl_compile_file(v, path); + if (v->err) { + return val; } - err = l_lsl_run(v, LRTS_NULL); + l_lsl_run(v, LRTS_NULL); - return err; + return val; } diff --git a/src/l_lsl.h b/src/l_lsl.h index 249d8dc..52483c4 100644 --- a/src/l_lsl.h +++ b/src/l_lsl.h @@ -28,100 +28,47 @@ enum l_lsl_flags { }; enum l_lsl_data_type { + L_LSL_TYPE_NIL = 0, L_LSL_TYPE_INT, L_LSL_TYPE_STRING, - L_LSL_TYPE_TABLE, - L_LSL_FUNCTION + L_LSL_FUNCTION, + L_LSL_NATIVE_CALL }; enum l_lsl_error { L_LSL_ERR_OK = 0 }; -/* all string constants are stored in this string table. - * strings should be unique and deduplicated */ -struct l_lsl_strings { - const char **strings; - u32 index; - u32 max_len; -}; - struct l_lsl_table; -union l_lsl_tableent_data { +union l_lsl_value_data { i32 int_val; char *str_val; - struct l_lsl_table *table; - /* start of code in bytecode table */ - u32 function_code_index; }; -/* a table entry is a single value +/* a signle value single value * of a specific data type */ -struct l_lsl_tableent { - /* string table entry */ - u32 name; +struct l_lsl_value { + /* name might be null for literals */ + const char *name; /* reference count for the * current entry. * if rc is 0 this entry should be freed * rc should increase when: - * - entry is assigned to a table - * - entry is pushed to a function + * - entry is assigned to a list * rc should decrease when - * - a function returns where the table is assigned - * - a table entry is re-sassigned to a different reference - * - a table that contains this entry as a member is freed + * - entry is removed from a list */ u32 rc; enum l_lsl_data_type type; - union l_lsl_tableent_data data; -}; - -/* - * a table is a mapping of - * names to values - * table entries can contain more tables - */ -struct l_lsl_table { - u32 index; - u32 max_len; - struct l_lsl_tableent *entries; - struct l_lsl_table *parent; -}; - -/* list of all valid opcodes */ -enum l_lsl_op { - L_LSL_OP_NOP, - L_LSL_OP_ADD, - L_LSL_OP_SUB, - L_LSL_OP_MUL, - L_LSL_OP_DIV, - - L_LSL_OP_RET, - L_LSL_OP_CALL -}; - -/* some opcodes might need data - * to perform their action - * opcode data should not exceed - * 32 bits - */ -union l_lsl_op_data { - /* string table index of function name to call */ - u32 call; -}; - -struct l_lsl_instruction { - enum l_lsl_op opcode; - union l_lsl_op_data data; + union l_lsl_value_data data; + struct l_lsl_value *next; }; struct l_lsl_vm { - enum l_lsl_error error; + enum l_lsl_error err; enum l_lsl_flags flags; - struct l_lsl_table global_table; - u_malloc_fp malloc; u_free_fp free; u_realloc_fp realloc; @@ -132,6 +79,13 @@ struct l_lsl_vm l_lsl_vm_init(); void l_lsl_vm_free(struct l_lsl_vm *vm); +struct l_lsl_value l_lsl_value_init(void); + +/* frees a value if rc == 0 + * if rc != 0 decreases rc by one + */ +void l_lsl_value_free(struct l_lsl_value *v); + /* * gets the next token returns tone's length in token_len * the result token is placed into an interla string buffer and should not be modified @@ -139,17 +93,19 @@ void l_lsl_vm_free(struct l_lsl_vm *vm); */ const char* l_lsl_next_token(const char *code, u32 len, u32 *token_len); -/* compiles a program and places its byte code into the vm memory */ -enum l_lsl_error l_lsl_compile(struct l_lsl_vm *v, const char *code, u32 len); +/* compiles a program + * returns the resulting program's head as a list + */ +struct l_lsl_value l_lsl_compile(struct l_lsl_vm *v, const char *code, u32 len); /* runs a program from a given entry point function * if the entry point is NULL the global scope is executed */ -enum l_lsl_error l_lsl_run(struct l_lsl_vm *v, const char *entry); +struct l_lsl_value l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program); /* compiles from a file */ -enum l_lsl_error l_lsl_compile_file(struct l_lsl_vm *v, const char *path); +struct l_lsl_value l_lsl_compile_file(struct l_lsl_vm *v, const char *path); /* loads a file, compiles it and runs it */ -enum l_lsl_error l_lsl_exec(struct l_lsl_vm *v, const char *path); +struct l_lsl_value l_lsl_exec(struct l_lsl_vm *v, const char *path); #endif