scripting: reworked and simplified the scripting types
authorLukas Krickl <lukas@krickl.dev>
Wed, 11 Mar 2026 05:59:31 +0000 (06:59 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 11 Mar 2026 05:59:31 +0000 (06:59 +0100)
src/l_lsl.c
src/l_lsl.h

index be76603c009db3328d82a9799f436621f63d143b..3c449a94776ef862a59fe5002c9b2cf5c99636db 100644 (file)
@@ -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;
 }
index 249d8dcaee5b58db7584fdd43954f32c55ece599..52483c4ee640a6df95cfa0f665b6624cacb67553 100644 (file)
@@ -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