Skip to content

Commit

Permalink
impl hex numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Feb 17, 2024
1 parent f9b8a4a commit 4792d1a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*/

'a' '\n' '\t'

0xFE12

0 comments on commit 4792d1a

Please sign in to comment.