fix parser get sign
This commit is contained in:
94
src/parser.c
94
src/parser.c
@@ -32,38 +32,42 @@
|
|||||||
|
|
||||||
static term_sign get_sign(token *tokens, int i, int *token_count)
|
static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||||
{
|
{
|
||||||
// forbidden tokens
|
// // forbidden tokens
|
||||||
if (tokens[i].type == TOKEN_POWER)
|
// 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);
|
// 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)
|
// 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);
|
// 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)
|
// 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);
|
// 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)
|
||||||
{
|
{
|
||||||
*token_count = 1;
|
*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
|
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;
|
*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
|
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;
|
*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)
|
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;
|
coefficient = 1.0;
|
||||||
|
|
||||||
// forbidden tokens
|
// // forbidden tokens
|
||||||
if (tokens[i].type == TOKEN_POWER)
|
// 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);
|
// 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)
|
// 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);
|
// 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)
|
// 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);
|
// 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)
|
// 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);
|
// 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,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)
|
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
|
// first reach VARIABLE
|
||||||
if (tokens[i].type == TOKEN_VARIABLE)
|
if (tokens[i].type == TOKEN_VARIABLE)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ static void print_context_terms()
|
|||||||
ft_dprintf(STDERR_FILENO, "term %2i - ", i);
|
ft_dprintf(STDERR_FILENO, "term %2i - ", i);
|
||||||
|
|
||||||
// position
|
// position
|
||||||
ft_dprintf(STDERR_FILENO, "%10s : ", "position");
|
ft_dprintf(STDERR_FILENO, "%8s : ", "position");
|
||||||
if (terms_g_err[i].position == TERM_LEFT)
|
if (terms_g_err[i].position == TERM_LEFT)
|
||||||
ft_dprintf(STDERR_FILENO, "%10s", "TERM_LEFT");
|
ft_dprintf(STDERR_FILENO, "%10s", "TERM_LEFT");
|
||||||
else if (terms_g_err[i].position == TERM_RIGHT)
|
else if (terms_g_err[i].position == TERM_RIGHT)
|
||||||
@@ -73,7 +73,7 @@ static void print_context_terms()
|
|||||||
ft_dprintf(STDERR_FILENO, "%10s", "");
|
ft_dprintf(STDERR_FILENO, "%10s", "");
|
||||||
|
|
||||||
// sign
|
// sign
|
||||||
ft_dprintf(STDERR_FILENO, " | %10s : ", "sign");
|
ft_dprintf(STDERR_FILENO, " | %4s : ", "sign");
|
||||||
if (terms_g_err[i].sign == TERM_PLUS)
|
if (terms_g_err[i].sign == TERM_PLUS)
|
||||||
ft_dprintf(STDERR_FILENO, "%10s", "TERM_PLUS");
|
ft_dprintf(STDERR_FILENO, "%10s", "TERM_PLUS");
|
||||||
else if (terms_g_err[i].sign == TERM_MINUS)
|
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);
|
dprintf(STDERR_FILENO, " | %10s : %13g", "coefficient", terms_g_err[i].coefficient);
|
||||||
|
|
||||||
// exponent
|
// 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++;
|
i++;
|
||||||
}
|
}
|
||||||
ft_putchar('\n');
|
ft_putchar('\n');
|
||||||
|
|||||||
Reference in New Issue
Block a user