using double everywhere
This commit is contained in:
@@ -8,8 +8,9 @@ typedef enum
|
||||
ERROR_BASIC = 0,
|
||||
ERROR_UNKNOWN_TOKEN = -1,
|
||||
ERROR_NUMBER_TOO_BIG = -2,
|
||||
ERROR_PARSING = -3,
|
||||
} program_error;
|
||||
|
||||
int stop_errors(int err);
|
||||
int stop_errors(int err, char *details);
|
||||
|
||||
#endif
|
||||
@@ -7,14 +7,13 @@
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TOKEN_VARIABLE, // x, y, etc.
|
||||
TOKEN_NUMBER_INT, // int
|
||||
TOKEN_NUMBER_DOUBLE, // double
|
||||
TOKEN_POWER, // ^ or **
|
||||
TOKEN_SIGN, // + or -
|
||||
TOKEN_FACTOR, // * or /
|
||||
TOKEN_EQUAL, // =
|
||||
TOKEN_END // null (end of input)
|
||||
TOKEN_VARIABLE, // x, y, etc.
|
||||
TOKEN_NUMBER, // int or double -> all converted into double
|
||||
TOKEN_POWER, // ^ or **
|
||||
TOKEN_SIGN, // + or -
|
||||
TOKEN_FACTOR, // * or /
|
||||
TOKEN_EQUAL, // =
|
||||
TOKEN_END // null (end of input)
|
||||
} token_type;
|
||||
|
||||
typedef struct
|
||||
@@ -23,7 +22,6 @@ typedef struct
|
||||
union
|
||||
{
|
||||
char value_char;
|
||||
int value_int;
|
||||
double value_double;
|
||||
};
|
||||
} token;
|
||||
|
||||
@@ -18,25 +18,14 @@ typedef enum
|
||||
TERM_MINUS, // -
|
||||
} term_sign;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TYPE_INT, // "123"
|
||||
TYPE_DOUBLE, // "123.456"
|
||||
} coefficient_type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
term_position position;
|
||||
term_sign sign;
|
||||
coefficient_type coefficient_type;
|
||||
union
|
||||
{
|
||||
int coefficient_int;
|
||||
double coefficient_double;
|
||||
};
|
||||
double coefficient;
|
||||
int exponent;
|
||||
} term;
|
||||
|
||||
int parse(token *tokens, term *terms);
|
||||
int parse(token *tokens, term *terms, int terms_count_max);
|
||||
|
||||
#endif
|
||||
@@ -88,28 +88,22 @@ int main(int ac, char **av)
|
||||
|
||||
if (tokens[i].type == TOKEN_VARIABLE)
|
||||
ft_printf("%20s", "TOKEN_VARIABLE");
|
||||
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
ft_printf("%20s", "TOKEN_NUMBER_INT");
|
||||
if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
ft_printf("%20s", "TOKEN_NUMBER_DOUBLE");
|
||||
if (tokens[i].type == TOKEN_POWER)
|
||||
else if (tokens[i].type == TOKEN_NUMBER)
|
||||
ft_printf("%20s", "TOKEN_NUMBER");
|
||||
else if (tokens[i].type == 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");
|
||||
if (tokens[i].type == TOKEN_FACTOR)
|
||||
else if (tokens[i].type == 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");
|
||||
if (tokens[i].type == TOKEN_END)
|
||||
else if (tokens[i].type == TOKEN_END)
|
||||
ft_printf("%20s", "TOKEN_END");
|
||||
|
||||
ft_putstr(" - value : ");
|
||||
|
||||
if (tokens[i].type == TOKEN_NUMBER_INT)
|
||||
{
|
||||
printf("%i\n", tokens[i].value_int);
|
||||
}
|
||||
else if (tokens[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
if (tokens[i].type == TOKEN_NUMBER)
|
||||
{
|
||||
printf("%g\n", tokens[i].value_double);
|
||||
}
|
||||
@@ -129,7 +123,7 @@ int main(int ac, char **av)
|
||||
ft_putchar('\n'); // debug
|
||||
|
||||
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_putnbr(term_count); // debug
|
||||
|
||||
14
src/errors.c
14
src/errors.c
@@ -1,19 +1,25 @@
|
||||
#include "errors.h"
|
||||
|
||||
int stop_errors(int err)
|
||||
int stop_errors(int err, char *details)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case ERROR_UNKNOWN_TOKEN:
|
||||
ft_putstr_fd("error: unknown token\n", STDERR_FILENO);
|
||||
ft_putstr_fd("error: unknown token, details : ", STDERR_FILENO);
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
ft_putstr_fd("unknown error\n", STDERR_FILENO);
|
||||
ft_putstr_fd("unknown error, details : ", STDERR_FILENO);
|
||||
break;
|
||||
}
|
||||
|
||||
ft_putstr_fd(details, STDERR_FILENO);
|
||||
ft_putchar_fd('\n', STDERR_FILENO);
|
||||
|
||||
exit(err);
|
||||
}
|
||||
88
src/lexer.c
88
src/lexer.c
@@ -11,48 +11,8 @@ static bool token_is_variable(const char *input, int input_pos, int *token_size)
|
||||
return false;
|
||||
}
|
||||
|
||||
// number can be int "123"
|
||||
static bool token_is_number_int(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)
|
||||
// number can be int "123" or double "123.456"
|
||||
static bool token_is_number(const char *input, int input_pos, int *token_size)
|
||||
{
|
||||
int 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;
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// number is not a valid double, it has 2 dots
|
||||
// number is not a valid token, it has 2 dots
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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)
|
||||
{
|
||||
stop_errors(ERROR_NUMBER_TOO_BIG);
|
||||
stop_errors(ERROR_NUMBER_TOO_BIG, &input[input_pos]);
|
||||
}
|
||||
*token_size = number_size;
|
||||
return true;
|
||||
@@ -106,7 +66,7 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
|
||||
*token_size = 1;
|
||||
return true;
|
||||
}
|
||||
else if (ft_memcmp(&input[input_pos], "**", 2) == 0)
|
||||
if (ft_memcmp(&input[input_pos], "**", 2) == 0)
|
||||
{
|
||||
*token_size = 2;
|
||||
return true;
|
||||
@@ -117,7 +77,12 @@ static bool token_is_power(const char *input, int input_pos, int *token_size)
|
||||
// sign can be '+' or '-'
|
||||
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;
|
||||
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 ':'
|
||||
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;
|
||||
return true;
|
||||
@@ -172,14 +147,9 @@ int lexerize(const char *input, token *tokens)
|
||||
tokens[tokens_count].type = TOKEN_VARIABLE;
|
||||
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].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].type = TOKEN_NUMBER;
|
||||
tokens[tokens_count].value_double = ft_atof(&input[input_pos]);
|
||||
}
|
||||
else if (token_is_power(input, input_pos, &token_size))
|
||||
@@ -204,15 +174,13 @@ int lexerize(const char *input, token *tokens)
|
||||
}
|
||||
else
|
||||
{
|
||||
// tmp
|
||||
token_size = 1;
|
||||
// stop_errors(ERROR_UNKNOWN_TOKEN);
|
||||
stop_errors(ERROR_UNKNOWN_TOKEN, &input[input_pos]);
|
||||
}
|
||||
|
||||
tokens_count++;
|
||||
if (token_size == 0)
|
||||
{
|
||||
stop_errors(ERROR_UNKNOWN_TOKEN);
|
||||
stop_errors(ERROR_UNKNOWN_TOKEN, &input[input_pos]);
|
||||
}
|
||||
input_pos += token_size;
|
||||
}
|
||||
|
||||
51
src/parser.c
51
src/parser.c
@@ -17,11 +17,7 @@
|
||||
term_position position;
|
||||
term_sign sign;
|
||||
coefficient_type coefficient_type;
|
||||
union
|
||||
{
|
||||
int coefficient_int;
|
||||
double coefficient_double;
|
||||
};
|
||||
int coefficient;
|
||||
int exponent;
|
||||
*/
|
||||
|
||||
@@ -32,10 +28,11 @@ static term_sign get_sign(token *tokens, int *token_count)
|
||||
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
|
||||
*token_count = 1; // placeholder
|
||||
if (tokens) // placeholder
|
||||
*token_count = 1; // placeholder
|
||||
return 1.0; // placeholder
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
int parse(token *tokens, term *terms)
|
||||
int parse(token *tokens, term *terms, int terms_count_max)
|
||||
{
|
||||
/*
|
||||
if (tokens)
|
||||
@@ -56,22 +53,16 @@ int parse(token *tokens, term *terms)
|
||||
*/
|
||||
|
||||
int i;
|
||||
int term_count;
|
||||
int terms_count;
|
||||
int token_count;
|
||||
int coefficient_int;
|
||||
double coefficient_double;
|
||||
term_position term_position;
|
||||
coefficient_type coefficient_type;
|
||||
|
||||
term_count = 0;
|
||||
terms_count = 0;
|
||||
token_count = 0;
|
||||
i = 0;
|
||||
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)
|
||||
{
|
||||
term_position = TERM_RIGHT;
|
||||
@@ -80,30 +71,28 @@ int parse(token *tokens, term *terms)
|
||||
}
|
||||
|
||||
// position
|
||||
terms[term_count].position = term_position;
|
||||
terms[terms_count].position = term_position;
|
||||
|
||||
// sign
|
||||
terms[term_count].sign = get_sign(&tokens[i], &token_count);
|
||||
terms[terms_count].sign = get_sign(&tokens[i], &token_count);
|
||||
i += token_count;
|
||||
|
||||
// coefficient
|
||||
get_coefficient(&tokens[i], &token_count, &coefficient_type, &coefficient_int, &coefficient_double);
|
||||
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;
|
||||
}
|
||||
terms[terms_count].coefficient = get_coefficient(&tokens[i], &token_count);
|
||||
i += token_count;
|
||||
|
||||
// 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++;
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user