script: Added hex literals
authorLukas Krickl <lukas@krickl.dev>
Mon, 16 Mar 2026 12:17:07 +0000 (13:17 +0100)
committerLukas Krickl <lukas@krickl.dev>
Mon, 16 Mar 2026 12:17:07 +0000 (13:17 +0100)
src/l_lsl.c
src/tests/lsl/hexliteral.lsl [new file with mode: 0644]
src/tests/t_lsl.c
src/u_file.c
src/u_string.c

index f86996149456dc459e90e805ed69070e310edea0..847e5c1ee160e27f31a54614e295e53ac79ec717 100644 (file)
@@ -72,6 +72,11 @@ lrts_bool l_lsl_isbin(char c) {
 #define l_lsl_lex_advance(v, code) \
        (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_term_token(tokbuf, token_len) \
                tokbuf[*token_len] = '\0';
 
@@ -84,39 +89,45 @@ struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
        *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;
+               if (c == '0') {
+                       /* special base */
+                       /* specific base number */
                        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);
-       } else if (c == '0') {
-               value->type = L_LSL_TYPE_INT;
-               /* specific base number */
-               c = l_lsl_lex_advance(v, code);
 
-               if (l_lsl_isnum(c)) {
-                       /* octal */
-                       l_lsl_lex_term_token(tokbuf, token_len);
-               } else if (c == 'x') {
-                       /* hex */
-                       l_lsl_lex_term_token(tokbuf, token_len);
-               } else if (c == 'b') {
-                       /* bin */
-                       l_lsl_lex_term_token(tokbuf, token_len);
+                       if (l_lsl_isnum(c)) {
+                               /* octal */
+                               c = l_lsl_lex_advance(v, code);
+                               l_lsl_lex_term_token(tokbuf, token_len);
+                       } 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);
+                                       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);
+                       } else if (c == 'b') {
+                               /* bin */
+                               c = l_lsl_lex_advance(v, code);
+                               l_lsl_lex_term_token(tokbuf, token_len);
+                       } else {
+                               l_lsl_err(v, L_LSL_ERR_INVALID_NUMBER_BASE, 
+                                               "unknown number base: 0%c\n", c);
+                               return LRTS_NULL;
+                       }
                } else {
-                       l_lsl_err(v, L_LSL_ERR_INVALID_NUMBER_BASE, 
-                                       "unknown number base: 0%c\n", c);
-                       return LRTS_NULL;
-               }
-
+                       /* decimal number */
+                       while (l_lsl_isnum(c)) {
+                               l_lsl_lex_write_token(tokbuf, token_len, 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);
+               }
        } else {
                        l_lsl_err(v, L_LSL_ERR_LEX, "unknown char: %c\n", c);
                        return LRTS_NULL;
diff --git a/src/tests/lsl/hexliteral.lsl b/src/tests/lsl/hexliteral.lsl
new file mode 100644 (file)
index 0000000..dadf9ff
--- /dev/null
@@ -0,0 +1 @@
+0xabc123
index 94d5097249864778889214e95f27e80d8f59aad8..73d2994f3677ccd4e312f424e2465a3eea1db18a 100644 (file)
@@ -22,6 +22,8 @@ int t_lsl_assert(struct l_lsl_value expect_value, enum l_lsl_error expect_err,
        struct l_lsl_vm v = l_lsl_vm_init();
        struct l_lsl_value *res = l_lsl_exec(&v, src_file);
        enum l_lsl_error err = v.err;
+       
+       u_fprintf(u_stderr, "%s\n", src_file);
 
        l_lsl_vm_free(&v);
        
@@ -41,9 +43,16 @@ int t_lsl_assert(struct l_lsl_value expect_value, enum l_lsl_error expect_err,
 int t_test_lsl() {
        struct l_lsl_value v = l_lsl_value_init();
        
+       /* simple int value */
        v.type = L_LSL_TYPE_INT;
        v.data.int_val = 123;
        T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/intliteral.lsl", 
                        t_lsl_vlue_assert_int);
+
+       /* simple hex value */
+       v.type = L_LSL_TYPE_INT;
+       v.data.int_val = 0xabc123;
+       T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/hexliteral.lsl", 
+                       t_lsl_vlue_assert_int);
        return 0;
 }
index f84309b21f6802ee8674ca33bbe4a831f64d44ef..0603c542bb7f980d0d16fa1b0b404d925b56b6ef 100644 (file)
@@ -25,6 +25,5 @@ const char *u_file_read(const char *path) {
                buf = u_realloc(buf, len);
        }
        buf[index] = '\0';
-
        return buf;
 }
index eb521079972d87bf6079e21965fd71c6c97b32dc..e0735ebee5602792bef5dad8fc4b0a6a44ece72f 100644 (file)
@@ -2,7 +2,7 @@
 
 int u_strnlen(const char *s, u32 n) {
        u32 i = 0;
-       while (s[i++] && i < n) {
+       while (s[i] && i < n) {
                i++;
        }
        return i;