Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hex number support #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions alexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <limits.h>

#define ALEXER_ARRAY_LEN(xs) (sizeof(xs)/(sizeof((xs)[0])))
#define alexer_return_defer(value) do { result = (value); goto defer; } while(0)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions example.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int main()
const char *content =
"#include <stdio.h>\n"
"if (a == 17*2 + 35) { // single line comment\n"
"if (x == 0xff) { // testing hex support\n"
" /* multi\n"
" * line\n"
" * comment\n"
Expand Down