handle superscript powers

This commit is contained in:
hugogogo
2026-05-06 20:01:41 +02:00
parent 4b6ad34720
commit 16c57c9bea
8 changed files with 130 additions and 44 deletions

View File

@@ -54,7 +54,7 @@ static double get_double_value(s_token token)
return token.value_int;
}
static double get_coefficient_absolute(s_token *tokens, int i, int *token_count)
static double get_coefficient(s_token *tokens, int i, int *token_count)
{
double coefficient;
@@ -140,13 +140,18 @@ static int get_exponent(s_token *tokens, int i, int *token_count)
return 0;
}
// then get power sign '^'
// then get power sign '^' or directly superscript power like '²'
if (tokens[i].type == TOKEN_POWER)
{
i++;
*token_count += 1;
}
// else if (tokens[i].type == TOKEN_NUMBER_INT_POWER){}
else if (tokens[i].type == TOKEN_NUMBER_INT_SUPER)
{
*token_count += 1;
// return exponent directly
return tokens[i].value_int;
}
else
{
// if token is 'x' not followed by '^' -> it's an exponent 1
@@ -201,13 +206,15 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
check_variables(tokens);
print_debug("PARSER STEPS :\n"); // debug
terms_count = 0;
token_count = 0;
i = 0;
term_position = TERM_LEFT;
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
{
// ft_printf("- token[%i]\n", i); // debug
print_debug("- token[%i]\n", i); // debug
// equal
if (tokens[i].type == TOKEN_EQUAL)
@@ -229,19 +236,19 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
sign = -1;
}
i += token_count;
// ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
print_debug("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
// coefficient
double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count);
double ret_coefficient = get_coefficient(tokens, i, &token_count);
terms[terms_count].coefficient = ret_coefficient * sign;
i += token_count;
// printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
print_debug("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
// exponent
int ret_exponent = get_exponent(tokens, i, &token_count);
terms[terms_count].exponent = ret_exponent;
i += token_count;
// ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
print_debug("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
terms_count++;
}