fix parser get sign

This commit is contained in:
hugogogo
2026-05-03 20:57:20 +02:00
parent 187c26083a
commit 6157fdb59f
2 changed files with 63 additions and 37 deletions

View File

@@ -32,38 +32,42 @@
static term_sign get_sign(token *tokens, int i, int *token_count)
{
// forbidden tokens
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);
}
// // forbidden tokens
// 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
if (tokens[i].tag == TOKEN_SIGN)
{
*token_count = 1;
return tokens[i].value_char;
return tokens[i].type == TOKEN_SIGN_PLUS ? TERM_PLUS : TERM_MINUS;
}
else if (i == 0) // if most left term, the sign can be ommited for a '+' sign in front of a number or variable
{
*token_count = 0;
return '+';
return TERM_PLUS;
}
else if (tokens[i - 1].type == TOKEN_EQUAL) // if first token after 'equal', the sign can be ommited for a '+' sign in front of a number or variable
{
*token_count = 0;
return '+';
return TERM_PLUS;
}
else
{
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign', not : '%c' (token number %i)", tokens[i].value_char, i);
}
return stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign', not : '%c' (token number %i)", tokens[i].value_char, i);
return TERM_PLUS; // default, but should not even be reached
}
static double get_coefficient(token *tokens, int i, int *token_count)
@@ -72,23 +76,23 @@ static double get_coefficient(token *tokens, int i, int *token_count)
coefficient = 1.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);
}
// // 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 (tokens[i].type == TOKEN_VARIABLE)
@@ -131,6 +135,28 @@ static double get_coefficient(token *tokens, int i, int *token_count)
static int get_exponent(token *tokens, int i, int *token_count)
{
// // forbidden tokens
// 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
if (tokens[i].type == TOKEN_VARIABLE)
{