fix parse coefficient
This commit is contained in:
@@ -206,7 +206,7 @@ int lexerize(const char *input, token *tokens)
|
||||
{
|
||||
tokens[tokens_count].type = TOKEN_NUMBER_INT;
|
||||
tokens[tokens_count].tag = TOKEN_NUMBER;
|
||||
tokens[tokens_count].value_double = ft_atoi(&input[input_pos]);
|
||||
tokens[tokens_count].value_int = ft_atoi(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_number_double(input, input_pos, &token_size))
|
||||
{
|
||||
|
||||
84
src/parser.c
84
src/parser.c
@@ -3,17 +3,6 @@
|
||||
#include "computorv1.h"
|
||||
|
||||
/**
|
||||
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
|
||||
|
||||
@@ -22,12 +11,6 @@
|
||||
|
||||
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;
|
||||
coefficient_type coefficient_type;
|
||||
int coefficient;
|
||||
int exponent;
|
||||
*/
|
||||
|
||||
static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
@@ -50,7 +33,6 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
if (tokens[i].tag == TOKEN_SIGN)
|
||||
{
|
||||
*token_count = 1;
|
||||
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
|
||||
{
|
||||
@@ -64,10 +46,24 @@ static term_sign get_sign(token *tokens, int i, int *token_count)
|
||||
}
|
||||
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);
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign', not '%s' (token[%i])", token_type_to_str(tokens[i].type), i);
|
||||
}
|
||||
|
||||
return TERM_PLUS; // default, but should not even be reached
|
||||
return tokens[i].type == TOKEN_SIGN_PLUS ? TERM_PLUS : TERM_MINUS;
|
||||
}
|
||||
|
||||
static double get_double_value(token token)
|
||||
{
|
||||
if (token.tag != TOKEN_NUMBER)
|
||||
{
|
||||
stop_errors(ERROR_UNKNOWN_TOKEN, "this was suppose to be a number, instead got a %s", token_type_to_str(token.type));
|
||||
}
|
||||
if (token.type == TOKEN_NUMBER_DOUBLE)
|
||||
{
|
||||
return token.value_double;
|
||||
}
|
||||
// else it's an int
|
||||
return token.value_int;
|
||||
}
|
||||
|
||||
static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
@@ -104,30 +100,30 @@ static double get_coefficient(token *tokens, int i, int *token_count)
|
||||
// if coefficient tokens
|
||||
if (tokens[i].tag == TOKEN_NUMBER)
|
||||
{
|
||||
(*token_count)++;
|
||||
coefficient = tokens[i].value_double;
|
||||
*token_count = 1;
|
||||
coefficient = get_double_value(tokens[i]);
|
||||
}
|
||||
|
||||
// detect more coefficients, like "3 * 2 / 5" etc
|
||||
i++;
|
||||
while (tokens[i].tag == TOKEN_FACTOR)
|
||||
{
|
||||
i++;
|
||||
if (tokens[i].tag == TOKEN_NUMBER)
|
||||
{
|
||||
*token_count += 2;
|
||||
if (tokens[i - 1].type == TOKEN_FACTOR_MULT)
|
||||
{
|
||||
coefficient *= tokens[i].value_double;
|
||||
}
|
||||
else if (tokens[i - 1].type == TOKEN_FACTOR_DIV)
|
||||
{
|
||||
coefficient /= tokens[i].value_double;
|
||||
}
|
||||
}
|
||||
else
|
||||
i++; // to check if token after factor is number
|
||||
if (tokens[i].tag != TOKEN_NUMBER)
|
||||
{
|
||||
return coefficient;
|
||||
}
|
||||
|
||||
*token_count += 2;
|
||||
if (tokens[i - 1].type == TOKEN_FACTOR_MULT)
|
||||
{
|
||||
coefficient *= get_double_value(tokens[i]);
|
||||
}
|
||||
else if (tokens[i - 1].type == TOKEN_FACTOR_DIV)
|
||||
{
|
||||
coefficient /= get_double_value(tokens[i]);
|
||||
}
|
||||
i++; // to check if next token is a factor
|
||||
}
|
||||
|
||||
return coefficient;
|
||||
@@ -193,12 +189,7 @@ static int get_exponent(token *tokens, int i, int *token_count)
|
||||
}
|
||||
|
||||
// then get exponent
|
||||
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
{
|
||||
i++;
|
||||
(*token_count)++;
|
||||
}
|
||||
else
|
||||
if (tokens[i].type != TOKEN_NUMBER_INT)
|
||||
{
|
||||
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
|
||||
}
|
||||
@@ -246,6 +237,8 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
term_position = TERM_LEFT;
|
||||
while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
|
||||
{
|
||||
ft_printf("- token[%i]\n", i); // debug
|
||||
|
||||
// equal
|
||||
if (tokens[i].type == TOKEN_EQUAL)
|
||||
{
|
||||
@@ -261,21 +254,20 @@ int parse(token *tokens, term *terms, int terms_count_max)
|
||||
term_sign ret_sign = get_sign(tokens, i, &token_count);
|
||||
terms[terms_count].sign = ret_sign;
|
||||
i += token_count;
|
||||
ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count);
|
||||
ft_printf("term[%i] get_sign: (%i)[%s], token_count: [%d]\n", terms_count, ret_sign, term_sign_to_str(ret_sign), token_count); // debug
|
||||
|
||||
// coefficient
|
||||
double ret_coefficient = get_coefficient(tokens, i, &token_count);
|
||||
terms[terms_count].coefficient = ret_coefficient;
|
||||
i += token_count;
|
||||
printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count);
|
||||
printf("term[%i] get_coefficient: [%g], token_count: [%d]\n", terms_count, ret_coefficient, token_count); // debug
|
||||
|
||||
// exponent
|
||||
int ret_exponent = get_exponent(tokens, i, &token_count);
|
||||
terms[terms_count].exponent = ret_exponent;
|
||||
i += token_count;
|
||||
ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count);
|
||||
ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug
|
||||
|
||||
i++;
|
||||
terms_count++;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,11 @@ static void print_context_tokens()
|
||||
|
||||
ft_putstr(" - value : ");
|
||||
|
||||
if (tokens_g_err[i].tag == TOKEN_NUMBER)
|
||||
if (tokens_g_err[i].type == TOKEN_NUMBER_INT)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "%i\n", tokens_g_err[i].value_int);
|
||||
}
|
||||
else if (tokens_g_err[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%g\n", tokens_g_err[i].value_double);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user