Added hex support

This commit is contained in:
Xavier Del Campo Romero 2020-07-14 23:36:56 +02:00
parent 63845cd736
commit f8d2b43c38
2 changed files with 39 additions and 2 deletions

39
lexer.c
View File

@ -17,6 +17,7 @@ static unsigned int line, start_column, column;
X(TOKEN_EQ) \
X(TOKEN_MOV) \
X(TOKEN_INT) \
X(TOKEN_HEX) \
X(TOKEN_FLOAT) \
X(TOKEN_GT) \
X(TOKEN_LT) \
@ -192,6 +193,8 @@ static int alloc_token(const enum token_id id,
switch (id)
{
case TOKEN_HEX:
/* Fall through. */
case TOKEN_FLOAT:
/* Fall through. */
case TOKEN_SYMBOL:
@ -244,6 +247,36 @@ static int is_integer(const char *const symbol)
return 1;
}
static int is_hex(const char *const symbol)
{
const size_t len = strlen(symbol);
const size_t minlen = strlen("0x");
if (len >= minlen && symbol[0] == '0' &&
(symbol[1] == 'x' || symbol[1] == 'X'))
{
if (len > minlen)
{
for (const char *s = &symbol[2]; *s; s++)
{
if ((*s >= 'a' && *s <= 'f')
|| (*s >= 'A' && *s <= 'F')
|| (*s >= '0' && *s <= '9'))
continue;
else
return 0;
}
}
else
FATAL_ERROR("invalid hex number at line %u, column %u",
line, start_column);
}
else
return 0;
return 1;
}
static int is_float(const char *const symbol)
{
int count = 0;
@ -295,6 +328,10 @@ static int process_symbol(const char *const symbol)
{
if (alloc_token(TOKEN_INT, symbol)) return 1;
}
else if (is_hex(symbol))
{
if (alloc_token(TOKEN_HEX, symbol)) return 1;
}
else if (is_float(symbol))
if (alloc_token(TOKEN_FLOAT, symbol)) return 1;
}
@ -345,8 +382,8 @@ start:
goto next;
case '\n':
/* Fall through. */
line++;
/* Fall through. */
case '\r':
/* Support for CR-only. */
if (*c == '\r' && (*(c + 1) != '\n'))

View File

@ -1,6 +1,6 @@
let x = 1+2/4.0.13 # comentari
let aj = -12.3456
let y = 1234 / #1.44
let y = 0x1234 / #1.44
if a + b >= 2 else
for x = 1, x <= 151 < 2, a > 0
{