parse sign

This commit is contained in:
hugogogo
2026-05-01 22:39:15 +02:00
parent 416d170ed2
commit bcbd3b2abb
6 changed files with 117 additions and 31 deletions

View File

@@ -10,9 +10,9 @@
TOKEN_EQUAL, // =
TOKEN_END // null (end of input)
1. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
2. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
3. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
1. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | ! POWER | SIGN | ! FACTOR | ! EQUAL | END
2. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
3. VARIABLE | NUMBER_INT | NUMBER_DOUBLE | POWER | SIGN | FACTOR | EQUAL | END
term_position position;
term_sign sign;
@@ -21,23 +21,52 @@
int exponent;
*/
static term_sign get_sign(token *tokens, int *token_count)
static term_sign get_sign(token *tokens, int i, int *token_count)
{
if (tokens) // placeholder
*token_count = 1; // placeholder
return '+'; // placeholder
// forbidden tokens
if (tokens[i].type == TOKEN_POWER)
{
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)
{
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should not have a factor token : " + tokens[i].value_char);
}
if (tokens[i].type == TOKEN_EQUAL)
{
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should not have an equal token : " + tokens[i].value_char);
}
// sign
if (tokens[i].type == TOKEN_SIGN)
{
*token_count = 1;
return tokens[i].value_char;
}
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 = 1;
return '+';
}
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 = 1;
return '+';
}
return stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a sign token " + tokens[i].value_char);
}
static double get_coefficient(token *tokens, int *token_count)
static double get_coefficient(token *tokens, int i, int *token_count)
{
if (tokens) // placeholder
if (tokens[i].type) // placeholder
*token_count = 1; // placeholder
return 1.0; // placeholder
}
static int get_exponent(token *tokens, int *token_count)
static int get_exponent(token *tokens, int i, int *token_count)
{
if (tokens) // placeholder
if (tokens[i].type) // placeholder
*token_count = 1; // placeholder
return 1; // placeholder
}
@@ -66,22 +95,22 @@ int parse(token *tokens, term *terms, int terms_count_max)
terms[terms_count].position = term_position;
// sign
terms[terms_count].sign = get_sign(&tokens[i], &token_count);
terms[terms_count].sign = get_sign(tokens, i, &token_count);
i += token_count;
// coefficient
terms[terms_count].coefficient = get_coefficient(&tokens[i], &token_count);
terms[terms_count].coefficient = get_coefficient(tokens, i, &token_count);
i += token_count;
// exponent
terms[terms_count].exponent = get_exponent(&tokens[i], &token_count);
terms[terms_count].exponent = get_exponent(tokens, i, &token_count);
i += token_count;
i++;
terms_count++;
}
// last token is TOKEN_END, and terms[] should have at least one more spot for a null
// last token is TOKEN_END, and terms[] should have at least one more spot for the END term
if (tokens[i].type == TOKEN_END && terms_count < terms_count_max)
{
terms[terms_count].position = TERM_END;