From 6648161d9a94c59955bfb6120aa86bf5e36b0d4e Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 3 May 2026 22:28:26 +0200 Subject: [PATCH] parse working --- headers/computorv1.h | 1 + src/computorv1.c | 11 +++++------ src/parser.c | 12 ++++++------ src/utils/errors.c | 22 ++++++++++++++-------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/headers/computorv1.h b/headers/computorv1.h index 4551bcb..0ab95ca 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -93,6 +93,7 @@ typedef enum t_program_error ERROR_SENTINEL, // last token not used, only for enum count } program_error; +void print_state(); int stop_errors(program_error err, const char *format, ...); /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * diff --git a/src/computorv1.c b/src/computorv1.c index ce56cd7..3dc2546 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -97,10 +97,8 @@ int main(int ac, char **av) remove_spaces(input); // lexerize - arg_len = ft_strlen(input) + 1; // +1 for last END token - + 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; tokens_fill_null(tokens, arg_len); @@ -112,9 +110,7 @@ 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 - + 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); @@ -124,5 +120,8 @@ int main(int ac, char **av) stop_errors(ERROR_TERM_COUNT, "parser returned 0 term"); } + // debug + print_state(); + return (0); } diff --git a/src/parser.c b/src/parser.c index 863dab4..674e274 100644 --- a/src/parser.c +++ b/src/parser.c @@ -237,14 +237,14 @@ 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 + // ft_printf("- token[%i]\n", i); // debug // equal if (tokens[i].type == TOKEN_EQUAL) { term_position = TERM_RIGHT; i++; - break; + continue; } // position @@ -254,19 +254,19 @@ 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); // debug + // 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); // debug + // 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); // debug + // ft_printf("term[%i] get_exponent: [%i], token_count: [%d]\n", terms_count, ret_exponent, token_count); // debug terms_count++; } @@ -281,7 +281,7 @@ int parse(token *tokens, term *terms, int terms_count_max) } else { - stop_errors(ERROR_PARSING, "deal with that later"); + stop_errors(ERROR_PARSING, "terms_count: %i, terms_count_max: %i, tokens[%i].type: %s", terms_count, terms_count_max, i, token_type_to_str(tokens[i].type)); } return terms_count; diff --git a/src/utils/errors.c b/src/utils/errors.c index 3b322de..367c716 100644 --- a/src/utils/errors.c +++ b/src/utils/errors.c @@ -15,7 +15,7 @@ static void print_context_tokens() i = 0; while (tokens_g_err[i].type != TOKEN_END) { - ft_dprintf(STDERR_FILENO, "token %2i - type : ", i); + ft_dprintf(STDERR_FILENO, "token[%2i] - type : ", i); if (tokens_g_err[i].type == TOKEN_VARIABLE) ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_VARIABLE"); @@ -65,7 +65,7 @@ static void print_context_terms() i = 0; while (terms_g_err[i].position != TERM_END) { - ft_dprintf(STDERR_FILENO, "term %2i - ", i); + ft_dprintf(STDERR_FILENO, "term[%2i] - ", i); // position ft_dprintf(STDERR_FILENO, "%8s : ", "position"); @@ -95,6 +95,17 @@ static void print_context_terms() ft_putchar('\n'); } +void print_state() +{ + ft_dprintf(STDERR_FILENO, "\nSTATE :\n"); + if (input_g_err) + print_context_input(); + if (tokens_g_err) + print_context_tokens(); + if (terms_g_err) + print_context_terms(); +} + int stop_errors(program_error err, const char *details, ...) { // the base error message @@ -119,12 +130,7 @@ int stop_errors(program_error err, const char *details, ...) } // print context - if (input_g_err) - print_context_input(); - if (tokens_g_err) - print_context_tokens(); - if (terms_g_err) - print_context_terms(); + print_state(); // print the base message ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);