From 647c9a54b109442c7633e724b593c4b460ff7d0e Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 10 May 2026 02:17:25 +0200 Subject: [PATCH] wip improve printer --- headers/computorv1.h | 18 +++++------ src/launcher.c | 49 ++--------------------------- src/utils/print_enums.c | 69 +++++++++++++++++++++++------------------ 3 files changed, 51 insertions(+), 85 deletions(-) diff --git a/headers/computorv1.h b/headers/computorv1.h index 1738d3f..3c2bf44 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -33,6 +33,7 @@ void launch_computorv1(char *input); typedef enum { + TOKEN_END = 0, // null (end of input) TOKEN_VARIABLE, // x, y, etc. TOKEN_NUMBER_INT, // int TOKEN_NUMBER_DOUBLE, // double @@ -43,12 +44,11 @@ typedef enum TOKEN_FACTOR_MULT, // * TOKEN_FACTOR_DIV, // / or : TOKEN_EQUAL, // = - TOKEN_END // null (end of input) } e_token_type; typedef enum { - TOKEN_NO_TAG, + TOKEN_NO_TAG = 0, TOKEN_NUMBER, TOKEN_SIGN, TOKEN_FACTOR, @@ -74,16 +74,16 @@ void lexerize(const char *input, s_token *tokens); typedef enum { - TERM_LEFT, // a in "a = b" - TERM_RIGHT, // b in "a = b" - TERM_POS_END, // last term + TERM_POS_END = 0, // for last term + TERM_LEFT, // a in "a = b" + TERM_RIGHT, // b in "a = b" } e_term_position; typedef enum { - TERM_PLUS = '+', // + - TERM_MINUS = '-', // - - TERM_SIGN_END, // last term + TERM_SIGN_END = 0, // for last term + TERM_PLUS = '+', // + + TERM_MINUS = '-', // - } e_term_sign; typedef struct @@ -115,9 +115,9 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent); typedef enum { + DELTA_ZERO = 0, DELTA_PLUS = 1, DELTA_MINUS = -1, - DELTA_ZERO = 0, } e_delta_sign; typedef struct diff --git a/src/launcher.c b/src/launcher.c index 7f9bffd..69b70fa 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -38,35 +38,6 @@ static size_t count_any_of(const char *s, const char *set) return count; } -static void tokens_fill_null(s_token *tokens, size_t arg_len) -{ - size_t i; - - i = 0; - while (i < arg_len) - { - tokens[i].type = TOKEN_END; - tokens[i].tag = TOKEN_NO_TAG; - tokens[i].value_char = '\0'; - i++; - } -} - -static void terms_fill_null(s_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_POS_END; - terms[i].sign = TERM_SIGN_END; - i++; - } -} - static int get_max_exponent(s_term *terms) { int i; @@ -115,20 +86,6 @@ static int get_number_of_exponents(s_term *terms, int max_exponent) return nbr_of_exponent; } -static void polynom_fill_null(s_polynom *polynom, int len) -{ - int i; - - i = 0; - while (i < len) - { - polynom[i].coefficient = 0.0; - polynom[i].exponent = 0; - polynom[i].sign = TERM_SIGN_END; - i++; - } -} - void launch_computorv1(char *input) { int max_exponent; @@ -146,7 +103,7 @@ void launch_computorv1(char *input) print_debug("\n-> tokens[%i]\n", arg_len); s_token tokens[arg_len]; tokens_g_err = tokens; - tokens_fill_null(tokens, arg_len); + ft_bzero(tokens, sizeof(tokens)); lexerize(input, tokens); // parse @@ -154,7 +111,7 @@ void launch_computorv1(char *input) print_debug("-> terms[%i]\n\n", terms_count_prediction); s_term terms[terms_count_prediction]; terms_g_err = terms; - terms_fill_null(terms, terms_count_prediction); + ft_bzero(terms, sizeof(terms)); parse(tokens, terms, terms_count_prediction); // reduce @@ -164,7 +121,7 @@ void launch_computorv1(char *input) print_debug("-> nbr_of_exponents: %i\n\n", nbr_of_exponents); s_polynom polynom[nbr_of_exponents + 2]; // +1 for last term, +1 for the degree (eg. degree 2 means 3 terms) polynom_g_err = polynom; - polynom_fill_null(polynom, nbr_of_exponents + 2); + ft_bzero(polynom, sizeof(polynom)); degree = reduce(terms, polynom, max_exponent); print_debug("-> degree: %i\n\n", degree); diff --git a/src/utils/print_enums.c b/src/utils/print_enums.c index 624de96..30a4df0 100644 --- a/src/utils/print_enums.c +++ b/src/utils/print_enums.c @@ -4,47 +4,56 @@ const char *token_type_to_str(e_token_type enum_value) { - if (enum_value > TOKEN_END || enum_value < TOKEN_VARIABLE) + if (enum_value == TOKEN_VARIABLE) + return "TOKEN_VARIABLE"; + else if (enum_value == TOKEN_NUMBER_INT) + return "TOKEN_NUMBER_INT"; + else if (enum_value == TOKEN_NUMBER_DOUBLE) + return "TOKEN_NUMBER_DOUBLE"; + else if (enum_value == TOKEN_NUMBER_INT_SUPER) + return "TOKEN_NUMBER_INT_SUPER"; + else if (enum_value == TOKEN_POWER) + return "TOKEN_POWER"; + else if (enum_value == TOKEN_SIGN_PLUS) + return "TOKEN_SIGN_PLUS"; + else if (enum_value == TOKEN_SIGN_MINUS) + return "TOKEN_SIGN_MINUS"; + else if (enum_value == TOKEN_FACTOR_MULT) + return "TOKEN_FACTOR_MULT"; + else if (enum_value == TOKEN_FACTOR_DIV) + return "TOKEN_FACTOR_DIV"; + else if (enum_value == TOKEN_EQUAL) + return "TOKEN_EQUAL"; + else if (enum_value == TOKEN_END) + return "TOKEN_END"; + else 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_NUMBER_INT_SUPER] = "TOKEN_NUMBER_INT_SUPER", - [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(e_token_tag enum_value) { - if (enum_value > TOKEN_FACTOR || enum_value < TOKEN_NO_TAG) + if (enum_value == TOKEN_NO_TAG) + return "TOKEN_NO_TAG"; + else if (enum_value == TOKEN_NUMBER) + return "TOKEN_NUMBER"; + else if (enum_value == TOKEN_SIGN) + return "TOKEN_SIGN"; + else if (enum_value == TOKEN_FACTOR) + return "TOKEN_FACTOR"; + else 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(e_term_position enum_value) { - if (enum_value > TERM_POS_END || enum_value < TERM_LEFT) + if (enum_value == TERM_LEFT) + return "TERM_LEFT"; + else if (enum_value == TERM_RIGHT) + return "TERM_RIGHT"; + else if (enum_value == TERM_POS_END) + return "TERM_POS_END"; + else return "invalid enum value"; - - static const char *const term_position_str[TERM_POS_END + 1] = { - [TERM_LEFT] = "TERM_LEFT", - [TERM_RIGHT] = "TERM_RIGHT", - [TERM_POS_END] = "TERM_POS_END"}; - return term_position_str[enum_value]; } const char *term_sign_to_str(e_term_sign enum_value)