improved errors

This commit is contained in:
hugogogo
2026-05-02 22:30:31 +02:00
parent 3977e6a6bb
commit c24461cb33
4 changed files with 109 additions and 49 deletions

View File

@@ -1,15 +1,16 @@
#include "parser.h"
/**
TOKEN_VARIABLE, // x, y, etc.
TOKEN_NUMBER_INT, // int
TOKEN_NUMBER_DOUBLE, // double
TOKEN_POWER, // ^ or **
TOKEN_SIGN, // + or -
TOKEN_MULTIPLICATION, // *
TOKEN_DIVISION, // /
TOKEN_EQUAL, // =
TOKEN_END // null (end of input)
TOKEN_VARIABLE, // x, y, etc.
TOKEN_NUMBER_INT, // int
TOKEN_NUMBER_DOUBLE, // double
TOKEN_POWER, // ^ or **
TOKEN_SIGN_PLUS, // +
TOKEN_SIGN_MINUS, // -
TOKEN_FACTOR_MULT, // *
TOKEN_FACTOR_DIV, // / or :
TOKEN_EQUAL, // =
TOKEN_END // null (end of input)
1. VAR | NUMBER_I | NUMBER_D | ! POW | SIGN_P | SIGN_M | ! FACTOR_MUL | ! FACTOR_DIV | ! EQUAL | END
NT | NUMBER | NT | SIGN | ! FACTOR | NT | NT
@@ -17,8 +18,8 @@
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 | NUMBER_I | NUMBER_D | ! POW | SIGN_P | SIGN_M | FACTOR_MUL | FACTOR_DIV | EQUAL | END
NT | NUMBER | NT | SIGN | FACTOR | NT | NT
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;
@@ -32,15 +33,15 @@ 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 begining of term, we should not have a power token : " + tokens[i].value_char);
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'power' : %c", tokens[i].value_char);
}
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);
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'factor' : %c", 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);
stop_errors(ERROR_TOKEN_POSITION, "at sign place, we should not have a token 'equal' : %c", tokens[i].value_char);
}
// sign
@@ -60,7 +61,7 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
return '+';
}
return stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a sign token " + tokens[i].value_char);
return stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign' : %c", tokens[i].value_char);
}
static double get_coefficient(token *tokens, int i, int *token_count)
@@ -72,19 +73,19 @@ static double get_coefficient(token *tokens, int i, int *token_count)
// forbidden tokens
if (tokens[i].type == TOKEN_POWER)
{
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a power token : " + tokens[i].value_char);
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'power' : %c", tokens[i].value_char);
}
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);
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'factor' : %c", tokens[i].value_char);
}
if (tokens[i].type == TOKEN_EQUAL)
{
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have an equal token : " + tokens[i].value_char);
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'equal' : %c", tokens[i].value_char);
}
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);
stop_errors(ERROR_TOKEN_POSITION, "at coefficient place, we should not have a token 'sign' : %c", tokens[i].value_char);
}
// if not coefficient token
@@ -128,9 +129,41 @@ static double get_coefficient(token *tokens, int i, int *token_count)
static int get_exponent(token *tokens, int i, int *token_count)
{
if (tokens[i].type) // placeholder
*token_count = 1; // placeholder
return 1; // placeholder
/**
* power
* number
* sign
* equal
* factor_div
*/
// 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);
}
// if 'var' -> token_count + 1
// else if '*' + 'var' -> token_count + 2
token_count += 0; // placeholder
return 1; // placeholder
}
int parse(token *tokens, term *terms, int terms_count_max)
@@ -139,13 +172,29 @@ int parse(token *tokens, term *terms, int terms_count_max)
int terms_count;
int token_count;
term_position term_position;
char var;
terms_count = 0;
token_count = 0;
i = 0;
term_position = TERM_LEFT;
var = 0;
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
{
// variable -> all variables must be the same
if (tokens[i].type == TOKEN_VARIABLE)
{
if (!var)
{
var = tokens[i].value_char;
}
else if (var != tokens[i].value_char)
{
stop_errors(ERROR_VAR_DIFF, "old var : '%c' - new var : '%c'", var, tokens[i].value_char);
}
}
// equal
if (tokens[i].type == TOKEN_EQUAL)
{
term_position = TERM_RIGHT;