diff --git a/src/parser.c b/src/parser.c index 6c08723..57f859a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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) { diff --git a/src/utils/errors.c b/src/utils/errors.c index d6dc6be..0683043 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -64,7 +64,7 @@ static void print_context_terms() ft_dprintf(STDERR_FILENO, "term %2i - ", i); // position - ft_dprintf(STDERR_FILENO, "%10s : ", "position"); + ft_dprintf(STDERR_FILENO, "%8s : ", "position"); if (terms_g_err[i].position == TERM_LEFT) ft_dprintf(STDERR_FILENO, "%10s", "TERM_LEFT"); else if (terms_g_err[i].position == TERM_RIGHT) @@ -73,7 +73,7 @@ static void print_context_terms() ft_dprintf(STDERR_FILENO, "%10s", ""); // sign - ft_dprintf(STDERR_FILENO, " | %10s : ", "sign"); + ft_dprintf(STDERR_FILENO, " | %4s : ", "sign"); if (terms_g_err[i].sign == TERM_PLUS) ft_dprintf(STDERR_FILENO, "%10s", "TERM_PLUS"); else if (terms_g_err[i].sign == TERM_MINUS) @@ -85,7 +85,7 @@ static void print_context_terms() dprintf(STDERR_FILENO, " | %10s : %13g", "coefficient", terms_g_err[i].coefficient); // exponent - ft_dprintf(STDERR_FILENO, " | %10s : %d\n", "exponent", terms_g_err[i].exponent); + ft_dprintf(STDERR_FILENO, " | %8s : %d\n", "exponent", terms_g_err[i].exponent); i++; } ft_putchar('\n');