script: lex now returns proper tokens
authorLukas Krickl <lukas@krickl.dev>
Fri, 13 Mar 2026 19:31:20 +0000 (20:31 +0100)
committerLukas Krickl <lukas@krickl.dev>
Fri, 13 Mar 2026 19:31:20 +0000 (20:31 +0100)
src/l_lsl.c
src/l_lsl.h
src/r_render.c
src/tests/t_lsl.c

index db8d3664d73123c42ccd3c04b529d11be410badc..5af644dade1bf54ca9cb6c17836d1259ec14f669 100644 (file)
@@ -72,24 +72,30 @@ lrts_bool l_lsl_isbin(char c) {
 #define l_lsl_lex_advance(v, code) \
        (code)[++v->lex_pos]
 
+#define l_lsl_lex_term_token(tokbuf, token_len) \
+               tokbuf[*token_len] = '\0';
 
 char tokbuf[L_LSL_TOK_MAX];
-const char* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
+struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
                u32 *token_len) {
        char c = 0;
+       struct l_lsl_value *value = l_lsl_value_alloc(v);
 
        *token_len = 0;
        
        c = l_lsl_lex_peek(v, code);
 
        if (l_lsl_isnum(c)) {
+               value->type = L_LSL_TYPE_INT;
                /* decimal number */
                while (l_lsl_isnum(c)) {
                        tokbuf[*token_len] = c;
                        *token_len += 1;
                        c = l_lsl_lex_advance(v, code);
                }
+               l_lsl_lex_term_token(tokbuf, token_len);
        } else if (c == '0') {
+               value->type = L_LSL_TYPE_INT;
                /* specific base number */
                c = l_lsl_lex_advance(v, code);
 
@@ -111,23 +117,51 @@ const char* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
                        return LRTS_NULL;
        }
 
-       tokbuf[*token_len] = '\0';
 
-       return tokbuf;
+       /* now we have a token
+        * return a value type based
+        * on the token type 
+        */
+
+       return value;
 }
 
 struct l_lsl_value* l_lsl_value_alloc(struct l_lsl_vm *v) {
        struct l_lsl_value *val;
        val = v->malloc(sizeof(*val));
+
+       lrts_assert(v);
+       lrts_assert(val);
+
        *val = l_lsl_value_init();
        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();
-       const char *src = u_file_read(path);
+void l_lsl_value_free(struct l_lsl_vm *v, 
+               struct l_lsl_value *val) {
+       lrts_assert(v);
+       lrts_assert(val);
+
+       v->free(val);
+}
+
+struct l_lsl_value* l_lsl_compile(struct l_lsl_vm *v, const char *code, 
+               u32 len) {      
+       struct l_lsl_value *val = LRTS_NULL;
        u32 token_len;
-       const char *token;
+       /* TODO: length check */
+       LRTS_UNUSED(len);
+       
+
+       val = l_lsl_next_token(v, code, &token_len);
+
+       return val;
+}
+
+struct l_lsl_value* l_lsl_compile_file(struct l_lsl_vm *v, const char *path) {
+       const char *src = u_file_read(path);
+       struct l_lsl_value *val = LRTS_NULL;
+
        u32 prev_lex_pos = v->lex_pos;
        const char *prev_file_name = v->path;
 
@@ -140,9 +174,7 @@ struct l_lsl_value l_lsl_compile_file(struct l_lsl_vm *v, const char *path) {
                return val;
        }
 
-
-       token = l_lsl_next_token(v, src, &token_len);
-       u_printf("%s\n", token);
+       val = l_lsl_compile(v, src, u_strnlen(src, L_LSL_SOURCE_MAX));
 
        u_free((void*)src);
 
@@ -151,23 +183,19 @@ struct l_lsl_value l_lsl_compile_file(struct l_lsl_vm *v, const char *path) {
        return val;
 }
 
-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();
+struct l_lsl_value* l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program) {
+       struct l_lsl_value *val = LRTS_NULL;
        LRTS_UNUSED(v);
-       LRTS_UNUSED(program);
+       LRTS_UNUSED(val);
 
-       return val;
+       return program;
 }
 
-struct l_lsl_value l_lsl_exec(struct l_lsl_vm *v, const char *path) {
-       struct l_lsl_value val = l_lsl_value_init();
-
-       l_lsl_compile_file(v, path);
+struct l_lsl_value* l_lsl_exec(struct l_lsl_vm *v, const char *path) {
+       struct l_lsl_value *val = l_lsl_compile_file(v, path);
        if (v->err) {
                return val;
        }
 
-       l_lsl_run(v, LRTS_NULL);
-
-       return val;
+       return l_lsl_run(v, val);
 }
index e61ed877568e5617973368bae9b8a2346c86f360..394e6cc81c329879d39ddac73e24fbed4465364e 100644 (file)
@@ -90,31 +90,32 @@ 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);
+struct l_lsl_value* l_lsl_value_alloc(struct l_lsl_vm *v);
+
+void l_lsl_value_free(struct l_lsl_vm *v, 
+               struct l_lsl_value *val);
 
 /* 
  * gets the next token returns tone's length in token_len
  * the result token is placed into an internal string buffer and should not be modified 
  * or saved.
  */
-const char* l_lsl_next_token(struct l_lsl_vm *v, const char *code, u32 *token_len);
+struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code, 
+               u32 *token_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);
+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 */
-struct l_lsl_value l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program);
+struct l_lsl_value* l_lsl_run(struct l_lsl_vm *v, struct l_lsl_value *program);
 
 /* compiles from a file */
-struct l_lsl_value 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 */
-struct l_lsl_value 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
index b493929128d7c10291e96d6deeeb5c00f1df447f..e24d045f4aeb3dab80858540149061c11428bfde 100644 (file)
@@ -18,7 +18,7 @@ void r_draw_pixel(struct r_framebuffer *fb, i32 x, i32 y, r_color color) {
        /* drop the draw if out of bounds */
        if (x < 0 || x >= r_target->w) {
                return;
-       }
+}
        if (y < 0 || y >= r_target->h) {
                return;
        }
index 7a3a9d1b9d1b1db7546c22a6659dbc80fc33a37d..bd0636ed29bb349825bc5a20753594fe4524ca78 100644 (file)
@@ -7,21 +7,24 @@
 int t_lsl_assert(struct l_lsl_value expect_value, enum l_lsl_error expect_err, 
                const char *src_file) {
        struct l_lsl_vm v = l_lsl_vm_init();
-       struct l_lsl_value res = l_lsl_exec(&v, src_file);
+       struct l_lsl_value *res = l_lsl_exec(&v, src_file);
        enum l_lsl_error err = v.err;
 
        l_lsl_vm_free(&v);
-
+       
+       T_ASSERT(res != LRTS_NULL, ("Invalid result value"))
        T_ASSERT(err == expect_err, ("Unexpected error code %d\n", err));
-       T_ASSERT(res.type == expect_value.type, ("Unexpected return value"));
-
+       T_ASSERT(res->type == expect_value.type, ("Unexpected return value\n"));
+       
+       l_lsl_value_free(&v, res);
                
        return 0;
 }
 
 int t_test_lsl() {
        struct l_lsl_value v = l_lsl_value_init();
-
+       
+       v.type = L_LSL_TYPE_INT;
        T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/intliteral.lsl");
        return 0;
 }