From 4792d1a7ab3216fa3d2c5e1e6ca0a03b6ab30242 Mon Sep 17 00:00:00 2001 From: omdxp Date: Sat, 17 Feb 2024 09:42:02 +0100 Subject: [PATCH] impl hex numbers --- lexer.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.c | 2 ++ 2 files changed, 57 insertions(+) diff --git a/lexer.c b/lexer.c index 728c7dd..fbc9136 100644 --- a/lexer.c +++ b/lexer.c @@ -443,6 +443,57 @@ char lex_get_escaped_char(char c) return co; } +void lexer_pop_token() +{ + vector_pop(lex_process->token_vec); +} + +bool is_hex_char(char c) +{ + c = tolower(c); + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); +} + +const char * +read_hex_number_str() +{ + struct buffer *buffer = buffer_create(); + char c = peekc(); + LEX_GETC_IF(buffer, c, is_hex_char(c)); + // write null terminator + buffer_write(buffer, 0x00); + return buffer_ptr(buffer); +} + +struct token * +token_make_special_number_hexadecimal() +{ + // skip the 'x' + nextc(); + + unsigned long number = 0; + const char *number_str = read_hex_number_str(); + number = strtol(number_str, 0, 16); + return token_make_number_for_value(number); +} + +struct token * +token_make_special_number() +{ + struct token *token = NULL; + struct token *last_token = lexer_last_token(); + + lexer_pop_token(); // poping off the first 0 + + char c = peekc(); + if (c == 'x') + { + token = token_make_special_number_hexadecimal(); + } + + return token; +} + struct token * token_make_quote() { @@ -487,6 +538,10 @@ struct token *read_next_token() token = token_make_symbol(); break; + case 'x': // '0' is already tokenized as a number + token = token_make_special_number(); + break; + case '"': token = token_make_string('"', '"'); break; diff --git a/test.c b/test.c index 744b252..b39d389 100644 --- a/test.c +++ b/test.c @@ -6,3 +6,5 @@ */ 'a' '\n' '\t' + + 0xFE12