all lexer except atoi

This commit is contained in:
hugogogo
2026-04-29 00:27:46 +02:00
parent 5108571deb
commit 52c76767bd
2 changed files with 73 additions and 51 deletions

View File

@@ -32,7 +32,27 @@ int main(int ac, char **av)
i = 0; i = 0;
while (tokens[i].type != TOKEN_END) while (tokens[i].type != TOKEN_END)
{ {
ft_printf("token %i :\n type : %i\n value : ", i, tokens[i].type); ft_printf("token %2i - type : ", i);
if (tokens[i].type == TOKEN_VARIABLE)
ft_printf("%20s", "TOKEN_VARIABLE");
if (tokens[i].type == TOKEN_NUMBER)
ft_printf("%20s", "TOKEN_NUMBER");
if (tokens[i].type == TOKEN_POWER)
ft_printf("%20s", "TOKEN_POWER");
if (tokens[i].type == TOKEN_PLUS)
ft_printf("%20s", "TOKEN_PLUS");
if (tokens[i].type == TOKEN_MINUS)
ft_printf("%20s", "TOKEN_MINUS");
if (tokens[i].type == TOKEN_MULTIPLICATION)
ft_printf("%20s", "TOKEN_MULTIPLICATION");
if (tokens[i].type == TOKEN_DIVISION)
ft_printf("%20s", "TOKEN_DIVISION");
if (tokens[i].type == TOKEN_END)
ft_printf("%20s", "TOKEN_END");
ft_putstr(" - value : ");
if (tokens[i].type == TOKEN_NUMBER) if (tokens[i].type == TOKEN_NUMBER)
{ {
ft_printf("%d\n", i, tokens[i].num_value); ft_printf("%d\n", i, tokens[i].num_value);

View File

@@ -11,12 +11,9 @@ static int skip_whitespace(const char *input, int input_pos)
return input_pos; return input_pos;
} }
// any single letter is a valid variable, like "x" or "y"
static bool token_is_variable(const char *input, int input_pos, int *token_size) static bool token_is_variable(const char *input, int input_pos, int *token_size)
{ {
if (input[input_pos] == 'x' || input[input_pos] == 'X')
{
return false;
}
if (ft_isalpha(input[input_pos])) if (ft_isalpha(input[input_pos]))
{ {
*token_size = 1; *token_size = 1;
@@ -25,13 +22,13 @@ static bool token_is_variable(const char *input, int input_pos, int *token_size)
return false; return false;
} }
// number can be double "123.456"
static bool token_is_number(const char *input, int input_pos, int *token_size) static bool token_is_number(const char *input, int input_pos, int *token_size)
{ {
int number_size; int number_size;
int max_number_size; int max_number_size;
int is_number;
if (!ft_isnumber(input[input_pos])) if (!ft_isdigit(input[input_pos]))
{ {
return false; return false;
} }
@@ -40,14 +37,16 @@ static bool token_is_number(const char *input, int input_pos, int *token_size)
max_number_size = 129; // max size for double double is 128 bits, + the coma max_number_size = 129; // max size for double double is 128 bits, + the coma
while (number_size <= max_number_size) while (number_size <= max_number_size)
{ {
if (ft_isnumber(input[input_pos + number_size])) if (ft_isdigit(input[input_pos + number_size]))
{ {
number_size++; number_size++;
} }
else if (input[input_pos] == '.') else if (input[input_pos + number_size] == '.')
{ {
number_size++; number_size++;
} }
else
break;
} }
if (number_size > max_number_size) if (number_size > max_number_size)
{ {
@@ -57,6 +56,23 @@ static bool token_is_number(const char *input, int input_pos, int *token_size)
return true; return true;
} }
// power can be "^" and "**"
static bool token_is_power(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '^')
{
*token_size = 1;
return true;
}
else if (ft_memcmp(&input[input_pos], "**", 2) == 0)
{
*token_size = 2;
return true;
}
return false;
}
// detect a single "+" is valid
static bool token_is_plus(const char *input, int input_pos, int *token_size) static bool token_is_plus(const char *input, int input_pos, int *token_size)
{ {
if (input[input_pos] == '+') if (input[input_pos] == '+')
@@ -67,47 +83,30 @@ static bool token_is_plus(const char *input, int input_pos, int *token_size)
return false; return false;
} }
static bool token_is_plus(const char *input, int input_pos, int *token_size) // detect a single "-"
{ static bool token_is_minus(const char *input, int input_pos, int *token_size)
if (input[input_pos] == '+') {
{ if (input[input_pos] == '-')
*token_size = 1; {
return true; *token_size = 1;
} return true;
return false; }
} return false;
}
static bool token_is_plus(const char *input, int input_pos, int *token_size)
{ // detect a single "*"
if (input[input_pos] == '+') static bool token_is_multiplication(const char *input, int input_pos, int *token_size)
{ {
*token_size = 1; if (input[input_pos] == '*')
return true; {
} *token_size = 1;
return false; return true;
} }
return false;
static bool token_is_plus(const char *input, int input_pos, int *token_size) }
{
if (input[input_pos] == '+') // detect a single "/"
{ static bool token_is_division(const char *input, int input_pos, int *token_size)
*token_size = 1;
return true;
}
return false;
}
static bool token_is_plus(const char *input, int input_pos, int *token_size)
{
if (input[input_pos] == '+')
{
*token_size = 1;
return true;
}
return false;
}
static bool token_is_plus(const char *input, int input_pos, int *token_size)
{ {
if (input[input_pos] == '+') if (input[input_pos] == '+')
{ {
@@ -117,6 +116,9 @@ static bool token_is_plus(const char *input, int input_pos, int *token_size)
return false; return false;
} }
/**
* LEXER
*/
int lexerize(const char *input, token tokens[MAX_TOKENS]) int lexerize(const char *input, token tokens[MAX_TOKENS])
{ {
int token_count; int token_count;
@@ -143,7 +145,7 @@ int lexerize(const char *input, token tokens[MAX_TOKENS])
else if (token_is_number(input, input_pos, &token_size)) else if (token_is_number(input, input_pos, &token_size))
{ {
tokens[token_count].type = TOKEN_NUMBER; tokens[token_count].type = TOKEN_NUMBER;
tokens[token_count].num_value = ft_atoi(input[input_pos]); tokens[token_count].num_value = ft_atoi(&input[input_pos]);
} }
else if (token_is_power(input, input_pos, &token_size)) else if (token_is_power(input, input_pos, &token_size))
{ {