changed subtypes for tags

This commit is contained in:
hugogogo
2026-05-02 10:25:25 +02:00
parent a250a170cb
commit 3977e6a6bb
4 changed files with 104 additions and 79 deletions

View File

@@ -87,41 +87,29 @@ int main(int ac, char **av)
ft_printf("token %2i - type : ", i);
if (tokens[i].type == TOKEN_VARIABLE)
ft_printf("%14s%30s", "TOKEN_VARIABLE", "");
else if (tokens[i].type == TOKEN_NUMBER)
{
ft_printf("%14s", "TOKEN_NUMBER");
if (tokens[i].subtype == TOKEN_NUMBER_INT)
ft_printf("%30s", "TOKEN_NUMBER_INT");
else if (tokens[i].subtype == TOKEN_NUMBER_DOUBLE)
ft_printf("%30s", "TOKEN_NUMBER_DOUBLE");
}
ft_printf("%20s", "TOKEN_VARIABLE");
else if (tokens[i].type == TOKEN_NUMBER_INT)
ft_printf("%20s", "TOKEN_NUMBER_INT");
else if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
else if (tokens[i].type == TOKEN_POWER)
ft_printf("%14s%30s", "TOKEN_POWER", "");
else if (tokens[i].type == TOKEN_SIGN)
{
ft_printf("%14s", "TOKEN_SIGN");
if (tokens[i].subtype == TOKEN_SIGN_PLUS)
ft_printf("%30s", "TOKEN_SIGN_PLUS");
else if (tokens[i].subtype == TOKEN_SIGN_MINUS)
ft_printf("%30s", "TOKEN_SIGN_MINUS");
}
else if (tokens[i].type == TOKEN_FACTOR)
{
ft_printf("%14s", "TOKEN_FACTOR");
if (tokens[i].subtype == TOKEN_FACTOR_MULTIPLICATION)
ft_printf("%30s", "TOKEN_FACTOR_MULTIPLICATION");
else if (tokens[i].subtype == TOKEN_FACTOR_DIVISION)
ft_printf("%30s", "TOKEN_FACTOR_DIVISION");
}
ft_printf("%20s", "TOKEN_POWER");
else if (tokens[i].type == TOKEN_SIGN_PLUS)
ft_printf("%20s", "TOKEN_SIGN_PLUS");
else if (tokens[i].type == TOKEN_SIGN_MINUS)
ft_printf("%20s", "TOKEN_SIGN_MINUS");
else if (tokens[i].type == TOKEN_FACTOR_MULT)
ft_printf("%20s", "TOKEN_FACTOR_MULT");
else if (tokens[i].type == TOKEN_FACTOR_DIV)
ft_printf("%20s", "TOKEN_FACTOR_DIV");
else if (tokens[i].type == TOKEN_EQUAL)
ft_printf("%14s%30s", "TOKEN_EQUAL", "");
ft_printf("%20s", "TOKEN_EQUAL");
else if (tokens[i].type == TOKEN_END)
ft_printf("%14s%30s", "TOKEN_END", "");
ft_printf("%20s", "TOKEN_END");
ft_putstr(" - value : ");
if (tokens[i].type == TOKEN_NUMBER)
if (tokens[i].tag == TOKEN_NUMBER)
{
printf("%g\n", tokens[i].value_double);
}

View File

@@ -197,55 +197,55 @@ int lexerize(const char *input, token *tokens)
if (token_is_variable(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_VARIABLE;
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
tokens[tokens_count].tag = TOKEN_NO_TAG;
tokens[tokens_count].value_char = 'x';
}
else if (token_is_number_int(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_NUMBER;
tokens[tokens_count].subtype = TOKEN_NUMBER_INT;
tokens[tokens_count].type = TOKEN_NUMBER_INT;
tokens[tokens_count].tag = TOKEN_NUMBER;
tokens[tokens_count].value_double = ft_atof(&input[input_pos]); // we keep info it's an int, but treat it as a double
}
else if (token_is_number_double(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_NUMBER;
tokens[tokens_count].subtype = TOKEN_NUMBER_DOUBLE;
tokens[tokens_count].type = TOKEN_NUMBER_DOUBLE;
tokens[tokens_count].tag = TOKEN_NUMBER;
tokens[tokens_count].value_double = ft_atof(&input[input_pos]);
}
else if (token_is_power(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_POWER;
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
tokens[tokens_count].tag = TOKEN_NO_TAG;
tokens[tokens_count].value_char = '^';
}
else if (token_is_sign_plus(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_SIGN;
tokens[tokens_count].subtype = TOKEN_SIGN_PLUS;
tokens[tokens_count].type = TOKEN_SIGN_PLUS;
tokens[tokens_count].tag = TOKEN_SIGN;
tokens[tokens_count].value_char = input[input_pos];
}
else if (token_is_sign_minus(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_SIGN;
tokens[tokens_count].subtype = TOKEN_SIGN_MINUS;
tokens[tokens_count].type = TOKEN_SIGN_MINUS;
tokens[tokens_count].tag = TOKEN_SIGN;
tokens[tokens_count].value_char = input[input_pos];
}
else if (token_is_factor_multiplication(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_FACTOR;
tokens[tokens_count].subtype = TOKEN_FACTOR_MULTIPLICATION;
tokens[tokens_count].type = TOKEN_FACTOR_MULT;
tokens[tokens_count].tag = TOKEN_FACTOR;
tokens[tokens_count].value_char = input[input_pos];
}
else if (token_is_factor_division(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_FACTOR;
tokens[tokens_count].subtype = TOKEN_FACTOR_DIVISION;
tokens[tokens_count].type = TOKEN_FACTOR_DIV;
tokens[tokens_count].tag = TOKEN_FACTOR;
tokens[tokens_count].value_char = input[input_pos];
}
else if (token_is_equal(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_EQUAL;
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
tokens[tokens_count].tag = TOKEN_NO_TAG;
tokens[tokens_count].value_char = '=';
}
else
@@ -262,7 +262,7 @@ int lexerize(const char *input, token *tokens)
}
tokens[tokens_count].type = TOKEN_END;
tokens[tokens_count].subtype = TOKEN_NO_SUBTYPE;
tokens[tokens_count].tag = TOKEN_NO_TAG;
tokens[tokens_count].value_char = '\0';
return tokens_count;

View File

@@ -11,14 +11,14 @@
TOKEN_EQUAL, // =
TOKEN_END // null (end of input)
1. VAR | NUM | ! POW | SIGN | ! FACTOR | ! EQUAL | END
NS | NUM_I | NUM_D | NS | SIGN_P | SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | NS | NS
1. VAR | NUMBER_I | NUMBER_D | ! POW | SIGN_P | SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | ! EQUAL | END
NT | NUMBER | NT | SIGN | ! FACTOR | NT | NT
2. VAR | NUM | ! POW | ! SIGN | ! FACTOR | ! EQUAL | END
NS | NUM_I | NUM_D | NS | ! SIGN_P | ! SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | NS | NS
2. VAR | NUMBER_I | NUMBER_D | ! POW | ! SIGN_P | ! SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | ! EQUAL | END
NT | NUMBER | NT | ! SIGN | ! FACTOR | NT | NT
3. VAR | NUM | ! POW | SIGN | FACTOR | ! EQUAL | END
NS | NUM_I | NUM_D | NS | SIGN_P | SIGN_M | FACTOR_MUL | ! FACTOR_DIV | NS | NS
3. VAR | NUMBER_I | NUMBER_D | ! POW | SIGN_P | SIGN_M | FACTOR_MUL | FACTOR_DIV | EQUAL | END
NT | NUMBER | NT | SIGN | FACTOR | NT | NT
term_position position;
term_sign sign;
@@ -34,7 +34,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
{
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should not have a power token : " + tokens[i].value_char);
}
if (tokens[i].type == TOKEN_FACTOR)
if (tokens[i].tag == TOKEN_FACTOR)
{
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should not have a factor token : " + tokens[i].value_char);
}
@@ -44,7 +44,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
}
// sign
if (tokens[i].type == TOKEN_SIGN)
if (tokens[i].tag == TOKEN_SIGN)
{
*token_count = 1;
return tokens[i].value_char;
@@ -74,7 +74,7 @@ static double get_coefficient(token *tokens, int i, int *token_count)
{
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a power token : " + tokens[i].value_char);
}
if (tokens[i].type == TOKEN_FACTOR)
if (tokens[i].tag == TOKEN_FACTOR)
{
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a factor token : " + tokens[i].value_char);
}
@@ -82,7 +82,7 @@ static double get_coefficient(token *tokens, int i, int *token_count)
{
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have an equal token : " + tokens[i].value_char);
}
if (tokens[i].type == TOKEN_SIGN)
if (tokens[i].tag == TOKEN_SIGN)
{
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a sign token : " + tokens[i].value_char);
}
@@ -95,24 +95,24 @@ static double get_coefficient(token *tokens, int i, int *token_count)
}
// if coefficient tokens
if (tokens[i].type == TOKEN_NUMBER)
if (tokens[i].tag == TOKEN_NUMBER)
{
(*token_count)++;
coefficient = tokens[i].value_double;
}
// detect more coefficients, like "3 * 2 / 5" etc
i++;
while (tokens[i].type == TOKEN_FACTOR)
while (tokens[i].tag == TOKEN_FACTOR)
{
i++;
if (tokens[i].type == TOKEN_NUMBER)
if (tokens[i].tag == TOKEN_NUMBER)
{
*token_count += 2;
if (tokens[i - 1].subtype == TOKEN_FACTOR_MULTIPLICATION)
if (tokens[i - 1].type == TOKEN_FACTOR_MULT)
{
coefficient *= tokens[i].value_double;
}
else if (tokens[i - 1].subtype == TOKEN_FACTOR_DIVISION)
else if (tokens[i - 1].type == TOKEN_FACTOR_DIV)
{
coefficient /= tokens[i].value_double;
}