script: next_tok now returns liner tokens instead of values
authorLukas Krickl <lukas@krickl.dev>
Mon, 16 Mar 2026 13:40:08 +0000 (14:40 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 16 Mar 2026 13:40:08 +0000 (14:40 +0100)
This will make parsing easier when it is time to implement functions.

src/l_lsl.c
src/l_lsl.h

index c6b8fc8de82293de28f5f1f3f8b4fb27480ea4ef..8c47b6c3f5b0c08bac8ba2929e20d0107edf651a 100644 (file)
@@ -83,24 +83,24 @@ lrts_bool l_lsl_isident(char c) {
        (code)[++v->lex_pos]
 
 /* writes a char to token and advances token_len */
-#define l_lsl_lex_write_token(tokbuf, token_len, c) \
-                       tokbuf[*token_len] = c; \
-                       *token_len += 1;
+#define l_lsl_lex_write_token(t, c) \
+                       t.val[t.len] = c; \
+                       t.len += 1;
 
-#define l_lsl_lex_term_token(tokbuf, token_len) \
-               tokbuf[*token_len] = '\0';
+#define l_lsl_lex_term_token(t) \
+               t.val[t.len] = '\0';
 
 char tokbuf[L_LSL_TOK_MAX];
-struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
-               u32 *token_len) {
+struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, const char *code) {
        char c = 0;
-       struct l_lsl_value *value = l_lsl_value_alloc(v);
+       struct l_lsl_token t;
+       u_memset(&t, 0, sizeof(t));
+       t.val = tokbuf;
 
-       *token_len = 0;
        
        c = l_lsl_lex_peek(v, code);
        if (l_lsl_isnum(c)) {
-               value->type = L_LSL_TYPE_INT;
+               t.type = L_LSL_TOK_INT;
                if (c == '0') {
                        /* special base */
                        /* specific base number */
@@ -109,61 +109,51 @@ struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
                        if (l_lsl_isnum(c)) {
                                /* octal */
                                while (l_lsl_isoct(c)) {
-                                       l_lsl_lex_write_token(tokbuf, token_len, c);
+                                       l_lsl_lex_write_token(t, c);
                                        c = l_lsl_lex_advance(v, code);
                                }
                                c = l_lsl_lex_advance(v, code);
-                               l_lsl_lex_term_token(tokbuf, token_len);
-                               value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 8);
+                               t.base = 8;
                        } else if (c == 'x') {
                                /* hex */
                                c = l_lsl_lex_advance(v, code);
                                while (l_lsl_ishexnum(c)) {
-                                       l_lsl_lex_write_token(tokbuf, token_len, c);
+                                       l_lsl_lex_write_token(t, c);
                                        c = l_lsl_lex_advance(v, code);
                                }
-                               l_lsl_lex_term_token(tokbuf, token_len);
-                               value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 16);
+                               t.base = 16;
                        } else if (c == 'b') {
                                /* bin */
                                c = l_lsl_lex_advance(v, code);
                                while (l_lsl_isbin(c)) {
-                                       l_lsl_lex_write_token(tokbuf, token_len, c);
+                                       l_lsl_lex_write_token(t, c);
                                        c = l_lsl_lex_advance(v, code);
                                }
                                c = l_lsl_lex_advance(v, code);
-                               l_lsl_lex_term_token(tokbuf, token_len);
-                               value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 2);
+                               t.base = 2;
                        } else {
                                l_lsl_err(v, L_LSL_ERR_INVALID_NUMBER_BASE, 
                                                "unknown number base: 0%c\n", c);
-                               return LRTS_NULL;
+                               t.type = L_LSL_TOK_NONE;
                        }
                } else {
                        /* decimal number */
                        while (l_lsl_isnum(c)) {
-                               l_lsl_lex_write_token(tokbuf, token_len, c);
+                               l_lsl_lex_write_token(t, c);
                                c = l_lsl_lex_advance(v, code);
                        }
-                       l_lsl_lex_term_token(tokbuf, token_len);
-
-                       value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 10);
+                       t.base = 10;
                }
        } else if (l_lsl_isident(c)) {
                /* identifier */
        } else {
                        l_lsl_err(v, L_LSL_ERR_LEX, "unknown char: %c\n", c);
-                       return LRTS_NULL;
+                       t.type = L_LSL_TOK_NONE;
        }
+                               
+       l_lsl_lex_term_token(t);
 
-
-       /* now we have a token
-        * return a value type based
-        * on the token type 
-        */
-
-
-       return value;
+       return t;
 }
 
 struct l_lsl_value* l_lsl_value_alloc(struct l_lsl_vm *v) {
@@ -188,12 +178,22 @@ void l_lsl_value_free(struct l_lsl_vm *v,
 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;
+       struct l_lsl_token tok;
        /* TODO: length check */
        LRTS_UNUSED(len);
        
 
-       val = l_lsl_next_token(v, code, &token_len);
+       tok = l_lsl_next_token(v, code);
+
+       switch (tok.type) {
+               case L_LSL_TOK_INT:
+                       val = l_lsl_value_alloc(v);
+                       val->type = L_LSL_TYPE_INT;
+                       val->data.int_val = u_strtol(tok.val, LRTS_NULL, tok.base);
+                       break;
+               default:
+                       v->err = L_LSL_ERR_LEX;
+       }
 
        return val;
 }
index d23a23d16915ab8b793607fe2b3ba0d3cb68f271..2ad3f761d95a9fd5265943053eafb925b742cb8f 100644 (file)
 #define L_LWL_TOK_STR_SQUOTE "\'"
 
 enum l_token_type {
-       L_LWL_TOK_LPAREN,
-       L_LWL_TOK_RPAREN,
-       L_LWL_TOK_DQUOTE,
-       L_LWL_TOK_SQUOTE,
-       L_LWL_TOK_NUMBER,
-       L_LWL_TOK_STRING,
-       L_LWL_TOK_IDENT
+       L_LSL_TOK_NONE,
+       L_LSL_TOK_LPAREN,
+       L_LSL_TOK_RPAREN,
+       L_LSL_TOK_DQUOTE,
+       L_LSL_TOK_SQUOTE,
+       L_LSL_TOK_INT,
+       L_LSL_TOK_STRING,
+       L_LSL_TOK_IDENT
+};
+
+struct l_lsl_token {
+       char *val;
+       enum l_token_type type;
+       u32 len;
+       /* for int values only */
+       int base;
 };
 
 enum l_lsl_flags {
@@ -107,8 +116,7 @@ void l_lsl_value_free(struct l_lsl_vm *v,
  * the result token is placed into an internal string buffer and should not be modified 
  * or saved.
  */
-struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code, 
-               u32 *token_len);
+struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, const char *code);
 
 /* compiles a program 
  * returns the resulting program's head as a list