using double everywhere

This commit is contained in:
hugogogo
2026-05-01 10:42:45 +02:00
parent 2eeed8580f
commit 7519b8e615
7 changed files with 78 additions and 133 deletions

View File

@@ -8,8 +8,9 @@ typedef enum
ERROR_BASIC = 0, ERROR_BASIC = 0,
ERROR_UNKNOWN_TOKEN = -1, ERROR_UNKNOWN_TOKEN = -1,
ERROR_NUMBER_TOO_BIG = -2, ERROR_NUMBER_TOO_BIG = -2,
ERROR_PARSING = -3,
} program_error; } program_error;
int stop_errors(int err); int stop_errors(int err, char *details);
#endif #endif

View File

@@ -7,14 +7,13 @@
typedef enum typedef enum
{ {
TOKEN_VARIABLE, // x, y, etc. TOKEN_VARIABLE, // x, y, etc.
TOKEN_NUMBER_INT, // int TOKEN_NUMBER, // int or double -> all converted into double
TOKEN_NUMBER_DOUBLE, // double TOKEN_POWER, // ^ or **
TOKEN_POWER, // ^ or ** TOKEN_SIGN, // + or -
TOKEN_SIGN, // + or - TOKEN_FACTOR, // * or /
TOKEN_FACTOR, // * or / TOKEN_EQUAL, // =
TOKEN_EQUAL, // = TOKEN_END // null (end of input)
TOKEN_END // null (end of input)
} token_type; } token_type;
typedef struct typedef struct
@@ -23,7 +22,6 @@ typedef struct
union union
{ {
char value_char; char value_char;
int value_int;
double value_double; double value_double;
}; };
} token; } token;

View File

@@ -18,25 +18,14 @@ typedef enum
TERM_MINUS, // - TERM_MINUS, // -
} term_sign; } term_sign;
typedef enum
{
TYPE_INT, // "123"
TYPE_DOUBLE, // "123.456"
} coefficient_type;
typedef struct typedef struct
{ {
term_position position; term_position position;
term_sign sign; term_sign sign;
coefficient_type coefficient_type; double coefficient;
union
{
int coefficient_int;
double coefficient_double;
};
int exponent; int exponent;
} term; } term;
int parse(token *tokens, term *terms); int parse(token *tokens, term *terms, int terms_count_max);
#endif #endif

View File

@@ -88,28 +88,22 @@ int main(int ac, char **av)
if (tokens[i].type == TOKEN_VARIABLE) if (tokens[i].type == TOKEN_VARIABLE)
ft_printf("%20s", "TOKEN_VARIABLE"); ft_printf("%20s", "TOKEN_VARIABLE");
if (tokens[i].type == TOKEN_NUMBER_INT) else if (tokens[i].type == TOKEN_NUMBER)
ft_printf("%20s", "TOKEN_NUMBER_INT"); ft_printf("%20s", "TOKEN_NUMBER");
if (tokens[i].type == TOKEN_NUMBER_DOUBLE) else if (tokens[i].type == TOKEN_POWER)
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
if (tokens[i].type == TOKEN_POWER)
ft_printf("%20s", "TOKEN_POWER"); ft_printf("%20s", "TOKEN_POWER");
if (tokens[i].type == TOKEN_SIGN) else if (tokens[i].type == TOKEN_SIGN)
ft_printf("%20s", "TOKEN_SIGN"); ft_printf("%20s", "TOKEN_SIGN");
if (tokens[i].type == TOKEN_FACTOR) else if (tokens[i].type == TOKEN_FACTOR)
ft_printf("%20s", "TOKEN_FACTOR"); ft_printf("%20s", "TOKEN_FACTOR");
if (tokens[i].type == TOKEN_EQUAL) else if (tokens[i].type == TOKEN_EQUAL)
ft_printf("%20s", "TOKEN_EQUAL"); ft_printf("%20s", "TOKEN_EQUAL");
if (tokens[i].type == TOKEN_END) else if (tokens[i].type == TOKEN_END)
ft_printf("%20s", "TOKEN_END"); ft_printf("%20s", "TOKEN_END");
ft_putstr(" - value : "); ft_putstr(" - value : ");
if (tokens[i].type == TOKEN_NUMBER_INT) if (tokens[i].type == TOKEN_NUMBER)
{
printf("%i\n", tokens[i].value_int);
}
else if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
{ {
printf("%g\n", tokens[i].value_double); printf("%g\n", tokens[i].value_double);
} }
@@ -129,7 +123,7 @@ int main(int ac, char **av)
ft_putchar('\n'); // debug ft_putchar('\n'); // debug
term terms[terms_count_prediction]; term terms[terms_count_prediction];
int term_count = parse(tokens, terms); int term_count = parse(tokens, terms, terms_count_prediction);
ft_putstr("-> terms_count : "); // debug ft_putstr("-> terms_count : "); // debug
ft_putnbr(term_count); // debug ft_putnbr(term_count); // debug

View File

@@ -1,19 +1,25 @@
#include "errors.h" #include "errors.h"
int stop_errors(int err) int stop_errors(int err, char *details)
{ {
switch (err) switch (err)
{ {
case ERROR_UNKNOWN_TOKEN: case ERROR_UNKNOWN_TOKEN:
ft_putstr_fd("error: unknown token\n", STDERR_FILENO); ft_putstr_fd("error: unknown token, details : ", STDERR_FILENO);
break; break;
case ERROR_NUMBER_TOO_BIG: case ERROR_NUMBER_TOO_BIG:
ft_putstr_fd("error: number is too big\n", STDERR_FILENO); ft_putstr_fd("error: number is too big, details : ", STDERR_FILENO);
break;
case ERROR_PARSING:
ft_putstr_fd("error: too much terms to parse, details : ", STDERR_FILENO);
break; break;
default: default:
ft_putstr_fd("unknown error\n", STDERR_FILENO); ft_putstr_fd("unknown error, details : ", STDERR_FILENO);
break; break;
} }
ft_putstr_fd(details, STDERR_FILENO);
ft_putchar_fd('\n', STDERR_FILENO);
exit(err); exit(err);
} }

View File

@@ -11,48 +11,8 @@ static bool token_is_variable(const char *input, int input_pos, int *token_size)
return false; return false;
} }
// number can be int "123" // number can be int "123" or double "123.456"
static bool token_is_number_int(const char *input, int input_pos, int *token_size) static bool token_is_number(const char *input, int input_pos, int *token_size)
{
int number_size;
int max_number_size;
if (!ft_isdigit(input[input_pos]))
{
return false;
}
number_size = 1;
max_number_size = 16; // max size for int
while (number_size <= max_number_size)
{
if (ft_isdigit(input[input_pos + number_size]))
{
number_size++;
}
else if (input[input_pos + number_size] == '.')
{
if (ft_isdigit(input[input_pos + number_size + 1]))
{
// number is double
return false;
}
else
break;
}
else
break;
}
if (number_size > max_number_size)
{
stop_errors(ERROR_NUMBER_TOO_BIG);
}
*token_size = number_size;
return true;
}
// number can be double "123.456"
static bool token_is_number_double(const char *input, int input_pos, int *token_size)
{ {
int number_size; int number_size;
int max_number_size; int max_number_size;
@@ -65,7 +25,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
has_dot = false; has_dot = false;
number_size = 1; number_size = 1;
max_number_size = 129; // max size for double double is 128 bits, + the coma max_number_size = 24; // max char needed to represent double : 1 sign + 1 point + 17 fractinoal part + 5 exponent
while (number_size <= max_number_size) while (number_size <= max_number_size)
{ {
if (ft_isdigit(input[input_pos + number_size])) if (ft_isdigit(input[input_pos + number_size]))
@@ -76,12 +36,12 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
{ {
if (has_dot) if (has_dot)
{ {
// number is not a valid double, it has 2 dots // number is not a valid token, it has 2 dots
return false; return false;
} }
if (!ft_isdigit(input[input_pos + number_size + 1])) if (!ft_isdigit(input[input_pos + number_size + 1]))
{ {
// number is not a double, it has no number after the dot // number is not valid token, it has a dot with no number after the dot
return false; return false;
} }
has_dot = true; has_dot = true;
@@ -92,7 +52,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
} }
if (number_size > max_number_size) if (number_size > max_number_size)
{ {
stop_errors(ERROR_NUMBER_TOO_BIG); stop_errors(ERROR_NUMBER_TOO_BIG, &input[input_pos]);
} }
*token_size = number_size; *token_size = number_size;
return true; return true;
@@ -106,7 +66,7 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
*token_size = 1; *token_size = 1;
return true; return true;
} }
else if (ft_memcmp(&input[input_pos], "**", 2) == 0) if (ft_memcmp(&input[input_pos], "**", 2) == 0)
{ {
*token_size = 2; *token_size = 2;
return true; return true;
@@ -117,7 +77,12 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
// sign can be '+' or '-' // sign can be '+' or '-'
static bool token_is_sign(const char *input, int input_pos, int *token_size) static bool token_is_sign(const char *input, int input_pos, int *token_size)
{ {
if (input[input_pos] == '+' || input[input_pos] == '-') if (input[input_pos] == '+')
{
*token_size = 1;
return true;
}
if (input[input_pos] == '-')
{ {
*token_size = 1; *token_size = 1;
return true; return true;
@@ -128,7 +93,17 @@ static bool token_is_sign(const char *input, int input_pos, int *token_size)
// factor can be '*' or '/' or ':' // factor can be '*' or '/' or ':'
static bool token_is_factor(const char *input, int input_pos, int *token_size) static bool token_is_factor(const char *input, int input_pos, int *token_size)
{ {
if (input[input_pos] == '*' || input[input_pos] == '/' || input[input_pos] == ':') if (input[input_pos] == '*')
{
*token_size = 1;
return true;
}
if (input[input_pos] == '/')
{
*token_size = 1;
return true;
}
if (input[input_pos] == ':')
{ {
*token_size = 1; *token_size = 1;
return true; return true;
@@ -172,14 +147,9 @@ int lexerize(const char *input, token *tokens)
tokens[tokens_count].type = TOKEN_VARIABLE; tokens[tokens_count].type = TOKEN_VARIABLE;
tokens[tokens_count].value_char = 'x'; tokens[tokens_count].value_char = 'x';
} }
else if (token_is_number_int(input, input_pos, &token_size)) else if (token_is_number(input, input_pos, &token_size))
{ {
tokens[tokens_count].type = TOKEN_NUMBER_INT; tokens[tokens_count].type = TOKEN_NUMBER;
tokens[tokens_count].value_int = ft_atoi(&input[input_pos]);
}
else if (token_is_number_double(input, input_pos, &token_size))
{
tokens[tokens_count].type = TOKEN_NUMBER_DOUBLE;
tokens[tokens_count].value_double = ft_atof(&input[input_pos]); tokens[tokens_count].value_double = ft_atof(&input[input_pos]);
} }
else if (token_is_power(input, input_pos, &token_size)) else if (token_is_power(input, input_pos, &token_size))
@@ -204,15 +174,13 @@ int lexerize(const char *input, token *tokens)
} }
else else
{ {
// tmp stop_errors(ERROR_UNKNOWN_TOKEN, &input[input_pos]);
token_size = 1;
// stop_errors(ERROR_UNKNOWN_TOKEN);
} }
tokens_count++; tokens_count++;
if (token_size == 0) if (token_size == 0)
{ {
stop_errors(ERROR_UNKNOWN_TOKEN); stop_errors(ERROR_UNKNOWN_TOKEN, &input[input_pos]);
} }
input_pos += token_size; input_pos += token_size;
} }

View File

@@ -17,11 +17,7 @@
term_position position; term_position position;
term_sign sign; term_sign sign;
coefficient_type coefficient_type; coefficient_type coefficient_type;
union int coefficient;
{
int coefficient_int;
double coefficient_double;
};
int exponent; int exponent;
*/ */
@@ -32,10 +28,11 @@ static term_sign get_sign(token *tokens, int *token_count)
return '+'; // placeholder return '+'; // placeholder
} }
static void get_coefficient(token *tokens, int *token_count, coefficient_type *coefficient_type, int *coefficient_int, double *coefficient_double) static double get_coefficient(token *tokens, int *token_count)
{ {
if (tokens && coefficient_type && coefficient_double && coefficient_int) // placeholder if (tokens) // placeholder
*token_count = 1; // 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 *token_count)
@@ -45,7 +42,7 @@ static int get_exponent(token *tokens, int *token_count)
return 1; // placeholder return 1; // placeholder
} }
int parse(token *tokens, term *terms) int parse(token *tokens, term *terms, int terms_count_max)
{ {
/* /*
if (tokens) if (tokens)
@@ -56,22 +53,16 @@ int parse(token *tokens, term *terms)
*/ */
int i; int i;
int term_count; int terms_count;
int token_count; int token_count;
int coefficient_int;
double coefficient_double;
term_position term_position; term_position term_position;
coefficient_type coefficient_type;
term_count = 0; terms_count = 0;
token_count = 0; token_count = 0;
i = 0; i = 0;
term_position = TERM_LEFT; term_position = TERM_LEFT;
while (tokens[i].type != TOKEN_END) while (tokens[i].type != TOKEN_END && terms_count < terms_count_max)
{ {
coefficient_int = 0;
coefficient_double = 0.0;
if (tokens[i].type == TOKEN_EQUAL) if (tokens[i].type == TOKEN_EQUAL)
{ {
term_position = TERM_RIGHT; term_position = TERM_RIGHT;
@@ -80,30 +71,28 @@ int parse(token *tokens, term *terms)
} }
// position // position
terms[term_count].position = term_position; terms[terms_count].position = term_position;
// sign // sign
terms[term_count].sign = get_sign(&tokens[i], &token_count); terms[terms_count].sign = get_sign(&tokens[i], &token_count);
i += token_count; i += token_count;
// coefficient // coefficient
get_coefficient(&tokens[i], &token_count, &coefficient_type, &coefficient_int, &coefficient_double); terms[terms_count].coefficient = get_coefficient(&tokens[i], &token_count);
if (coefficient_type == TYPE_INT)
{
terms[term_count].coefficient_int = coefficient_int;
}
else if (coefficient_type == TYPE_DOUBLE)
{
terms[term_count].coefficient_double = coefficient_double;
}
i += token_count; i += token_count;
// exponent // exponent
terms[term_count].exponent = get_exponent(&tokens[i], &token_count); terms[terms_count].exponent = get_exponent(&tokens[i], &token_count);
i += token_count; i += token_count;
i++; i++;
terms_count++;
} }
return term_count; if (tokens[i].type != TOKEN_END && terms_count >= terms_count_max)
{
stop_errors(ERROR_PARSING, "deal with that later");
}
return terms_count;
} }