From e0ecca12d56c8025c75033195ae352acaae4fbed Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Fri, 13 Mar 2026 20:31:20 +0100 Subject: [PATCH] script: lex now returns proper tokens --- src/l_lsl.c | 70 +++++++++++++++++++++++++++++++++-------------- src/l_lsl.h | 19 +++++++------ src/r_render.c | 2 +- src/tests/t_lsl.c | 13 +++++---- 4 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/l_lsl.c b/src/l_lsl.c index db8d366..5af644d 100644 --- a/src/l_lsl.c +++ b/src/l_lsl.c @@ -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); } diff --git a/src/l_lsl.h b/src/l_lsl.h index e61ed87..394e6cc 100644 --- a/src/l_lsl.h +++ b/src/l_lsl.h @@ -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 diff --git a/src/r_render.c b/src/r_render.c index b493929..e24d045 100644 --- a/src/r_render.c +++ b/src/r_render.c @@ -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; } diff --git a/src/tests/t_lsl.c b/src/tests/t_lsl.c index 7a3a9d1..bd0636e 100644 --- a/src/tests/t_lsl.c +++ b/src/tests/t_lsl.c @@ -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; } -- 2.30.2