fix parser when no exponent
This commit is contained in:
95
src/parser.c
95
src/parser.c
@@ -15,19 +15,7 @@
|
|||||||
|
|
||||||
static e_term_sign get_sign(s_token *tokens, int i, int *token_count)
|
static e_term_sign get_sign(s_token *tokens, int i, int *token_count)
|
||||||
{
|
{
|
||||||
// // forbidden tokens
|
*token_count = 0;
|
||||||
// if (tokens[i].type == TOKEN_POWER)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'power' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].tag == TOKEN_FACTOR)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'factor' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].type == TOKEN_EQUAL)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'equal' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sign
|
// sign
|
||||||
if (tokens[i].tag == TOKEN_SIGN)
|
if (tokens[i].tag == TOKEN_SIGN)
|
||||||
@@ -71,24 +59,7 @@ static double get_coefficient_absolute(s_token *tokens, int i, int *token_count)
|
|||||||
double coefficient;
|
double coefficient;
|
||||||
|
|
||||||
coefficient = 1.0;
|
coefficient = 1.0;
|
||||||
|
*token_count = 0;
|
||||||
// // forbidden tokens
|
|
||||||
// if (tokens[i].type == TOKEN_POWER)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'power' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].tag == TOKEN_FACTOR)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'factor' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].type == TOKEN_EQUAL)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'equal' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].tag == TOKEN_SIGN)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'sign' : '%c' (token number %i)", tokens[i].value_char, i);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if not coefficient token
|
// if not coefficient token
|
||||||
if (tokens[i].type == TOKEN_VARIABLE)
|
if (tokens[i].type == TOKEN_VARIABLE)
|
||||||
@@ -131,41 +102,32 @@ static double get_coefficient_absolute(s_token *tokens, int i, int *token_count)
|
|||||||
|
|
||||||
static int get_exponent(s_token *tokens, int i, int *token_count)
|
static int get_exponent(s_token *tokens, int i, int *token_count)
|
||||||
{
|
{
|
||||||
// // forbidden tokens
|
*token_count = 0;
|
||||||
// if (tokens[i].type == TOKEN_POWER)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'power' : %c", tokens[i].value_char);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].tag == TOKEN_NUMBER)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'number' : %c", tokens[i].value_char);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].tag == TOKEN_SIGN)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'sign' : %c", tokens[i].value_char);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].type == TOKEN_EQUAL)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'equal' : %c", tokens[i].value_char);
|
|
||||||
// }
|
|
||||||
// if (tokens[i].type == TOKEN_FACTOR_DIV)
|
|
||||||
// {
|
|
||||||
// stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should not have a token 'division' : %c", tokens[i].value_char);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// first reach VARIABLE
|
// valide :
|
||||||
|
// - '*x^2' -> exponent 2
|
||||||
|
// - '*x²' -> exponent 2
|
||||||
|
// - 'x^2' -> exponent 2
|
||||||
|
// - '*x' -> exponent 1
|
||||||
|
// - 'x' -> exponent 1
|
||||||
|
// - '' -> exponent 0
|
||||||
|
|
||||||
|
// first reach VARIABLE : 'x' or '*x'
|
||||||
if (tokens[i].type == TOKEN_VARIABLE)
|
if (tokens[i].type == TOKEN_VARIABLE)
|
||||||
{
|
{
|
||||||
|
// token is 'x'
|
||||||
i++;
|
i++;
|
||||||
(*token_count)++;
|
*token_count = 1;
|
||||||
}
|
}
|
||||||
else if (tokens[i].type == TOKEN_FACTOR_MULT)
|
else if (tokens[i].type == TOKEN_FACTOR_MULT)
|
||||||
{
|
{
|
||||||
|
// token is '*'
|
||||||
i++;
|
i++;
|
||||||
if (tokens[i].type == TOKEN_VARIABLE)
|
if (tokens[i].type == TOKEN_VARIABLE)
|
||||||
{
|
{
|
||||||
|
// tokens are '*x'
|
||||||
i++;
|
i++;
|
||||||
(*token_count) += 2;
|
*token_count = 2;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -174,22 +136,29 @@ static int get_exponent(s_token *tokens, int i, int *token_count)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, the first tokens should be 'x' or '*x', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
// if token are neither 'x' or '*x', then exponent is 0
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// then get power sign '^'
|
// then get power sign '^'
|
||||||
if (tokens[i].type == TOKEN_POWER)
|
if (tokens[i].type == TOKEN_POWER)
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
(*token_count)++;
|
*token_count += 1;
|
||||||
}
|
}
|
||||||
|
// else if (tokens[i].type == TOKEN_NUMBER_INT_POWER){}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, after variable we should have '^', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
// if token is 'x' not followed by '^' -> it's an exponent 1
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// then get exponent
|
// then get exponent
|
||||||
if (tokens[i].type != TOKEN_NUMBER_INT)
|
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||||
|
{
|
||||||
|
*token_count += 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||||
}
|
}
|
||||||
@@ -238,7 +207,7 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
|
|||||||
term_position = TERM_LEFT;
|
term_position = TERM_LEFT;
|
||||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
||||||
{
|
{
|
||||||
// ft_printf("- token[%i]\n", i); // debug
|
ft_printf("- token[%i]\n", i); // debug
|
||||||
|
|
||||||
// equal
|
// equal
|
||||||
if (tokens[i].type == TOKEN_EQUAL)
|
if (tokens[i].type == TOKEN_EQUAL)
|
||||||
@@ -260,19 +229,19 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
|
|||||||
sign = -1;
|
sign = -1;
|
||||||
}
|
}
|
||||||
i += token_count;
|
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
|
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
|
||||||
|
|
||||||
// coefficient
|
// coefficient
|
||||||
double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count);
|
double ret_coefficient = get_coefficient_absolute(tokens, i, &token_count);
|
||||||
terms[terms_count].coefficient = ret_coefficient * sign;
|
terms[terms_count].coefficient = ret_coefficient * sign;
|
||||||
i += token_count;
|
i += token_count;
|
||||||
// printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
|
printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
|
||||||
|
|
||||||
// exponent
|
// exponent
|
||||||
int ret_exponent = get_exponent(tokens, i, &token_count);
|
int ret_exponent = get_exponent(tokens, i, &token_count);
|
||||||
terms[terms_count].exponent = ret_exponent;
|
terms[terms_count].exponent = ret_exponent;
|
||||||
i += token_count;
|
i += token_count;
|
||||||
// ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
|
ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
|
||||||
|
|
||||||
terms_count++;
|
terms_count++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user