From 187c26083aaf3a55e2e5b5ae68f7087de01a341b Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 3 May 2026 20:42:18 +0200 Subject: [PATCH] adding utils with enum_print --- Makefile | 8 ++++-- headers/computorv1.h | 26 ++++++++++++------ src/computorv1.c | 34 +++++++++++++++++++---- src/lexer.c | 2 +- src/parser.c | 16 +++++++---- src/{ => utils}/errors.c | 0 src/utils/print_enums.c | 59 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 23 deletions(-) rename src/{ => utils}/errors.c (100%) create mode 100644 src/utils/print_enums.c diff --git a/Makefile b/Makefile index 4911849..bfbcf14 100644 --- a/Makefile +++ b/Makefile @@ -35,11 +35,13 @@ NAME = computorv1 D_LIB = ./libft D_HEADERS = ./headers HEADERS = computorv1.h -D_SRCS = ./src +D_SRCS = ./src \ + ./src/utils SRCS = computorv1.c \ - errors.c \ lexer.c \ - parser.c + parser.c \ + errors.c \ + print_enums.c # COMPILATION CONFIG : CC = gcc diff --git a/headers/computorv1.h b/headers/computorv1.h index 0e78682..4551bcb 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -12,7 +12,7 @@ * LEXER.C */ -typedef enum +typedef enum t_token_type { TOKEN_VARIABLE, // x, y, etc. TOKEN_NUMBER_INT, // int @@ -26,7 +26,7 @@ typedef enum TOKEN_END // null (end of input) } token_type; -typedef enum +typedef enum t_token_tag { TOKEN_NO_TAG, TOKEN_NUMBER, @@ -41,6 +41,7 @@ typedef struct union { char value_char; + int value_int; double value_double; }; } token; @@ -51,35 +52,35 @@ int lexerize(const char *input, token *tokens); * PARSER.C */ -typedef enum +typedef enum t_term_position { TERM_LEFT, // a in "a = b" TERM_RIGHT, // b in "a = b" TERM_END, // last term } term_position; -typedef enum +typedef enum t_term_sign { TERM_PLUS, // + TERM_MINUS, // - TERM_NULL, // null -> for the last term } term_sign; -typedef struct +typedef struct t_term { term_position position; term_sign sign; double coefficient; - double exponent; + int exponent; } term; int parse(token *tokens, term *terms, int terms_count_max); /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * ERRORS.C + * UTILS/ERRORS.C */ -typedef enum +typedef enum t_program_error { ERROR_BASE = 1, // start at 1 to avoid exit(0) for errors ERROR_TOKEN_COUNT, @@ -94,6 +95,15 @@ typedef enum int stop_errors(program_error err, const char *format, ...); +/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * UTILS/PRINT_ENUMS.C + */ + +const char *token_type_to_str(token_type type); +const char *token_tag_to_str(token_tag tag); +const char *term_position_to_str(term_position pos); +const char *term_sign_to_str(term_sign sign); + /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * GLOBALS */ diff --git a/src/computorv1.c b/src/computorv1.c index 2ce94b0..ce56cd7 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -37,9 +37,9 @@ static void remove_spaces(char *s) } } -static int count_any_of(const char *s, const char *set) +static size_t count_any_of(const char *s, const char *set) { - int count = 0; + size_t count = 0; for (; *s != '\0'; s++) { if (ft_strchr(set, *s) != NULL) @@ -50,7 +50,7 @@ static int count_any_of(const char *s, const char *set) return count; } -static void token_fill_null(token *tokens, size_t arg_len) +static void tokens_fill_null(token *tokens, size_t arg_len) { size_t i; @@ -64,11 +64,26 @@ static void token_fill_null(token *tokens, size_t arg_len) } } +static void terms_fill_null(term *terms, size_t terms_count_prediction) +{ + size_t i; + + i = 0; + while (i < terms_count_prediction) + { + terms[i].coefficient = 0.0; + terms[i].exponent = 0; + terms[i].position = TERM_END; + terms[i].sign = TERM_NULL; + i++; + } +} + int main(int ac, char **av) { int ret; size_t arg_len; - int terms_count_prediction; + size_t terms_count_prediction; char *input; if (ac < 2) @@ -80,12 +95,15 @@ int main(int ac, char **av) input = av[1]; input_g_err = input; remove_spaces(input); - arg_len = ft_strlen(input) + 1; // +1 for last END token // lexerize + arg_len = ft_strlen(input) + 1; // +1 for last END token + + ft_printf("-> tokens[%i]\n", arg_len); // debug + token tokens[arg_len]; tokens_g_err = tokens; - token_fill_null(tokens, arg_len); + tokens_fill_null(tokens, arg_len); ret = lexerize(input, tokens); if (ret == 0) { @@ -94,8 +112,12 @@ int main(int ac, char **av) // parse terms_count_prediction = count_any_of(input, "-+=") + 2; // +1 for first term that can have no leading '+', +1 for last term == NULL + + ft_printf("-> terms[%i]\n", terms_count_prediction); // debug + term terms[terms_count_prediction]; terms_g_err = terms; + terms_fill_null(terms, terms_count_prediction); ret = parse(tokens, terms, terms_count_prediction); if (ret == 0) { diff --git a/src/lexer.c b/src/lexer.c index d98e88c..a2343d5 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -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_atof(&input[input_pos]); // we keep info it's an int, but treat it as a double + tokens[tokens_count].value_double = ft_atoi(&input[input_pos]); } else if (token_is_number_double(input, input_pos, &token_size)) { diff --git a/src/parser.c b/src/parser.c index afb0618..6c08723 100644 --- a/src/parser.c +++ b/src/parser.c @@ -129,7 +129,7 @@ static double get_coefficient(token *tokens, int i, int *token_count) return coefficient; } -static double get_exponent(token *tokens, int i, int *token_count) +static int get_exponent(token *tokens, int i, int *token_count) { // first reach VARIABLE if (tokens[i].type == TOKEN_VARIABLE) @@ -177,7 +177,7 @@ static double get_exponent(token *tokens, int i, int *token_count) 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); } - return tokens[i].value_double; + return tokens[i].value_int; } static void check_variables(token *tokens) @@ -232,16 +232,22 @@ int parse(token *tokens, term *terms, int terms_count_max) terms[terms_count].position = term_position; // sign - terms[terms_count].sign = get_sign(tokens, i, &token_count); + 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); // coefficient - terms[terms_count].coefficient = get_coefficient(tokens, i, &token_count); + 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); // exponent - terms[terms_count].exponent = get_exponent(tokens, i, &token_count); + 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); i++; terms_count++; diff --git a/src/errors.c b/src/utils/errors.c similarity index 100% rename from src/errors.c rename to src/utils/errors.c diff --git a/src/utils/print_enums.c b/src/utils/print_enums.c new file mode 100644 index 0000000..894237b --- /dev/null +++ b/src/utils/print_enums.c @@ -0,0 +1,59 @@ +/* print_enums.c */ + +#include "computorv1.h" + +const char *token_type_to_str(token_type enum_value) +{ + if (enum_value > TOKEN_END || enum_value < TOKEN_VARIABLE) + return "invalid enum value"; + + static const char *const token_type_str[TOKEN_END + 1] = { + [TOKEN_VARIABLE] = "TOKEN_VARIABLE", + [TOKEN_NUMBER_INT] = "TOKEN_NUMBER_INT", + [TOKEN_NUMBER_DOUBLE] = "TOKEN_NUMBER_DOUBLE", + [TOKEN_POWER] = "TOKEN_POWER", + [TOKEN_SIGN_PLUS] = "TOKEN_SIGN_PLUS", + [TOKEN_SIGN_MINUS] = "TOKEN_SIGN_MINUS", + [TOKEN_FACTOR_MULT] = "TOKEN_FACTOR_MULT", + [TOKEN_FACTOR_DIV] = "TOKEN_FACTOR_DIV", + [TOKEN_EQUAL] = "TOKEN_EQUAL", + [TOKEN_END] = "TOKEN_END"}; + return token_type_str[enum_value]; +} + +const char *token_tag_to_str(token_tag enum_value) +{ + if (enum_value > TOKEN_FACTOR || enum_value < TOKEN_NO_TAG) + return "invalid enum value"; + + static const char *const token_tag_str[TOKEN_FACTOR + 1] = { + [TOKEN_NO_TAG] = "TOKEN_NO_TAG", + [TOKEN_NUMBER] = "TOKEN_NUMBER", + [TOKEN_SIGN] = "TOKEN_SIGN", + [TOKEN_FACTOR] = "TOKEN_FACTOR"}; + return token_tag_str[enum_value]; +} + +const char *term_position_to_str(term_position enum_value) +{ + if (enum_value > TERM_END || enum_value < TERM_LEFT) + return "invalid enum value"; + + static const char *const term_position_str[TERM_END + 1] = { + [TERM_LEFT] = "TERM_LEFT", + [TERM_RIGHT] = "TERM_RIGHT", + [TERM_END] = "TERM_END"}; + return term_position_str[enum_value]; +} + +const char *term_sign_to_str(term_sign enum_value) +{ + if (enum_value > TERM_NULL || enum_value < TERM_PLUS) + return "invalid enum value"; + + static const char *const term_sign_str[TERM_NULL + 1] = { + [TERM_PLUS] = "TERM_PLUS", + [TERM_MINUS] = "TERM_MINUS", + [TERM_NULL] = "TERM_NULL"}; + return term_sign_str[enum_value]; +} \ No newline at end of file