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;
}
};
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;
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
*/
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