about summary refs log tree commit diff
path: root/boot
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2025-07-06 15:25:47 +0200
committerMel <mel@rnrd.eu>2025-07-06 15:25:47 +0200
commit3620cabd1d722e4acc761c7278aa44aba902006c (patch)
treed14002e134c4fab723d3db400067b1eb045a8c0f /boot
parent660808af50780dae5292c8064d5bddd77246c52b (diff)
downloadcatskill-3620cabd1d722e4acc761c7278aa44aba902006c.tar.zst
catskill-3620cabd1d722e4acc761c7278aa44aba902006c.zip
Print formatted lex token value next to token type
Signed-off-by: Mel <mel@rnrd.eu>
Diffstat (limited to 'boot')
-rw-r--r--boot/catboot.c3
-rw-r--r--boot/lex.c18
-rw-r--r--boot/tests/lex/basic.cskt2
3 files changed, 21 insertions, 2 deletions
diff --git a/boot/catboot.c b/boot/catboot.c
index e2e5315..53b2bed 100644
--- a/boot/catboot.c
+++ b/boot/catboot.c
@@ -87,7 +87,8 @@ debug_lex_pass(struct String source)
     struct Token token;
     do {
         token = lexer_next(&lexer);
-        printf("%s ", token_kind_to_string(token.kind));
+        token_print(&token);
+        printf(" ");
     } while (token.kind != TOKEN_END_OF_FILE);
 
     return 0;
diff --git a/boot/lex.c b/boot/lex.c
index 82d3567..c705f26 100644
--- a/boot/lex.c
+++ b/boot/lex.c
@@ -429,6 +429,24 @@ token_can_begin_declaration(const struct Token* t)
     return token_is(t, TOKEN_WORD_VAR) || token_is(t, TOKEN_WORD_LET);
 }
 
+void
+token_print(const struct Token* t)
+{
+    printf("%s", token_kind_to_string(t->kind));
+
+    if (token_is(t, TOKEN_NAME)) {
+        printf("(%s)", t->value.name.data);
+    } else if (token_is(t, TOKEN_LITERAL_INTEGER)) {
+        printf("(%ld)", t->value.literal_integer);
+    } else if (token_is(t, TOKEN_LITERAL_FLOAT)) {
+        printf("(%f)", t->value.literal_float);
+    } else if (token_is(t, TOKEN_LITERAL_STRING)) {
+        printf("(%s)", t->value.name.data);
+    } else if (token_is(t, TOKEN_SOMETHING)) {
+        printf("(%c)", t->value.unknown_character);
+    }
+}
+
 bool
 ascii_in_range(ascii c, ascii from, ascii to)
 {
diff --git a/boot/tests/lex/basic.cskt b/boot/tests/lex/basic.cskt
index 7380498..26b41f3 100644
--- a/boot/tests/lex/basic.cskt
+++ b/boot/tests/lex/basic.cskt
@@ -9,4 +9,4 @@ main = fun () int {
 
 >>>
 
-NAME ASSIGN WORD_FUN ROUND_OPEN ROUND_CLOSE NAME CURLY_OPEN NEWLINE WORD_RETURN LITERAL_INTEGER PLUS LITERAL_INTEGER NEWLINE CURLY_CLOSE END_OF_FILE
+NAME(main) ASSIGN WORD_FUN ROUND_OPEN ROUND_CLOSE NAME(int) CURLY_OPEN NEWLINE WORD_RETURN LITERAL_INTEGER(1) PLUS LITERAL_INTEGER(2) NEWLINE CURLY_CLOSE END_OF_FILE