From 275e2e618265435143c60b2e4764e321a8c16d5f Mon Sep 17 00:00:00 2001 From: 0XF00D Date: Sun, 24 Nov 2024 08:29:33 -0500 Subject: [PATCH] Add hex number support - Added hex number parsing (0xFF format) - Added helper function for hex digit checking --- alexer.h | 38 ++++++++++++++++++++++++++++++++++---- example.c | 1 + 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/alexer.h b/alexer.h index 33b30c8..acd1ec6 100644 --- a/alexer.h +++ b/alexer.h @@ -9,6 +9,7 @@ #include #include #include +#include #define ALEXER_ARRAY_LEN(xs) (sizeof(xs)/(sizeof((xs)[0]))) #define alexer_return_defer(value) do { result = (value); goto defer; } while(0) @@ -171,6 +172,9 @@ void alexer_ignore_diagf(Alexer_Loc loc, const char *level, const char *fmt, ... bool alexer_expect_id(Alexer *l, Alexer_Token t, uint64_t id); bool alexer_expect_one_of_ids(Alexer *l, Alexer_Token t, uint64_t *ids, size_t ids_count); +// Helper function to check if char is a hex digit +static bool alexer_is_hex_digit(char c); + #endif // ALEXER_H_ #ifdef ALEXER_IMPLEMENTATION @@ -311,10 +315,30 @@ bool alexer_get_token(Alexer *l, Alexer_Token *t) // Int if (isdigit(l->content[l->cur])) { t->id = ALEXER_INT; - while (l->cur < l->size && isdigit(l->content[l->cur])) { - t->int_value = t->int_value*10 + l->content[l->cur] - '0'; - t->end += 1; - alexer_chop_char(l); + + // Check for hex + if (l->content[l->cur] == '0' && + l->cur + 1 < l->size && + tolower(l->content[l->cur + 1]) == 'x') { + + t->end += 2; + alexer_chop_chars(l, 2); // skip 0x + + // Just parse the hex digits, worry about overflow later maybe + while (l->cur < l->size && alexer_is_hex_digit(l->content[l->cur])) { + char c = tolower(l->content[l->cur]); + int digit = isdigit(c) ? (c - '0') : (c - 'a' + 10); + t->int_value = (t->int_value << 4) | digit; + t->end += 1; + alexer_chop_char(l); + } + } else { + // Regular decimal number + while (l->cur < l->size && isdigit(l->content[l->cur])) { + t->int_value = t->int_value * 10 + (l->content[l->cur] - '0'); + t->end += 1; + alexer_chop_char(l); + } } return true; } @@ -487,4 +511,10 @@ void alexer_rewind(Alexer *l, Alexer_State s) l->row = s.row; } +static bool alexer_is_hex_digit(char c) +{ + c = tolower(c); + return isdigit(c) || (c >= 'a' && c <= 'f'); +} + #endif // ALEXER_IMPLEMENTATION diff --git a/example.c b/example.c index 10aa1d9..1777446 100644 --- a/example.c +++ b/example.c @@ -52,6 +52,7 @@ int main() const char *content = "#include \n" "if (a == 17*2 + 35) { // single line comment\n" + "if (x == 0xff) { // testing hex support\n" " /* multi\n" " * line\n" " * comment\n"