diff --git a/headers/computorv1.h b/headers/computorv1.h index a913d3f..0e78682 100644 --- a/headers/computorv1.h +++ b/headers/computorv1.h @@ -100,5 +100,6 @@ int stop_errors(program_error err, const char *format, ...); extern char *input_g_err; extern token *tokens_g_err; +extern term *terms_g_err; #endif \ No newline at end of file diff --git a/src/computorv1.c b/src/computorv1.c index ad9ea4f..2ce94b0 100644 --- a/src/computorv1.c +++ b/src/computorv1.c @@ -8,6 +8,7 @@ char *input_g_err; token *tokens_g_err; +term *terms_g_err; /** * PROGRAM @@ -65,7 +66,7 @@ static void token_fill_null(token *tokens, size_t arg_len) int main(int ac, char **av) { - int i; + int ret; size_t arg_len; int terms_count_prediction; char *input; @@ -85,8 +86,8 @@ int main(int ac, char **av) token tokens[arg_len]; tokens_g_err = tokens; token_fill_null(tokens, arg_len); - int tokens_count = lexerize(input, tokens); - if (tokens_count == 0) + ret = lexerize(input, tokens); + if (ret == 0) { stop_errors(ERROR_TOKEN_COUNT, "lexer returned 0 token"); } @@ -94,42 +95,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 term terms[terms_count_prediction]; - int term_count = parse(tokens, terms, terms_count_prediction); + terms_g_err = terms; + ret = parse(tokens, terms, terms_count_prediction); + if (ret == 0) { stop_errors(ERROR_TERM_COUNT, "parser returned 0 term"); } - // DEBUG - ft_putstr("-> terms_count_prediction : "); - ft_putnbr(terms_count_prediction); - ft_putchar('\n'); - ft_putstr("-> terms_count : "); - ft_putnbr(term_count); - ft_putchar('\n'); - ft_putchar('\n'); - i = 0; - while (terms[i].position != TERM_END) - { - ft_printf("term %2i :\n", i); - // position - ft_printf(" position : "); - if (terms[i].position == TERM_LEFT) - ft_printf("%s\n", "TERM_LEFT"); - if (terms[i].position == TERM_RIGHT) - ft_printf("%s\n", "TERM_RIGHT"); - // sign - ft_printf(" sign : "); - if (terms[i].sign == TERM_PLUS) - ft_printf("%s\n", "TERM_PLUS"); - if (terms[i].sign == TERM_MINUS) - ft_printf("%s\n", "TERM_MINUS"); - // coefficient - printf(" coefficient : %g\n", terms[i].coefficient); - // exponent - ft_printf(" exponent : %d\n", terms[i].exponent); - i++; - } - ft_putchar('\n'); - return (0); } diff --git a/src/errors.c b/src/errors.c index bb757e9..9f02b89 100644 --- a/src/errors.c +++ b/src/errors.c @@ -2,10 +2,89 @@ #include "computorv1.h" -int stop_errors(program_error err, const char *details, ...) +static void print_context_input() +{ + ft_dprintf(STDERR_FILENO, "\ninput : %s\n", input_g_err); +} + +static void print_context_tokens() { int i; + ft_putchar('\n'); + i = 0; + while (tokens_g_err[i].type != TOKEN_END) + { + ft_printf("token %2i - type : ", i); + + if (tokens_g_err[i].type == TOKEN_VARIABLE) + ft_printf("%20s", "TOKEN_VARIABLE"); + else if (tokens_g_err[i].type == TOKEN_NUMBER_INT) + ft_printf("%20s", "TOKEN_NUMBER_INT"); + else if (tokens_g_err[i].type == TOKEN_NUMBER_DOUBLE) + ft_printf("%20s", "TOKEN_NUMBER_DOUBLE"); + else if (tokens_g_err[i].type == TOKEN_POWER) + ft_printf("%20s", "TOKEN_POWER"); + else if (tokens_g_err[i].type == TOKEN_SIGN_PLUS) + ft_printf("%20s", "TOKEN_SIGN_PLUS"); + else if (tokens_g_err[i].type == TOKEN_SIGN_MINUS) + ft_printf("%20s", "TOKEN_SIGN_MINUS"); + else if (tokens_g_err[i].type == TOKEN_FACTOR_MULT) + ft_printf("%20s", "TOKEN_FACTOR_MULT"); + else if (tokens_g_err[i].type == TOKEN_FACTOR_DIV) + ft_printf("%20s", "TOKEN_FACTOR_DIV"); + else if (tokens_g_err[i].type == TOKEN_EQUAL) + ft_printf("%20s", "TOKEN_EQUAL"); + else if (tokens_g_err[i].type == TOKEN_END) + ft_printf("%20s", "TOKEN_END"); + + ft_putstr(" - value : "); + + if (tokens_g_err[i].tag == TOKEN_NUMBER) + { + printf("%g\n", tokens_g_err[i].value_double); + } + else + { + ft_printf("%c\n", tokens_g_err[i].value_char); + } + i++; + } + ft_putchar('\n'); +} + +static void print_context_terms() +{ + int i; + + ft_putchar('\n'); + i = 0; + while (terms_g_err[i].position != TERM_END) + { + ft_printf("term %2i :\n", i); + // position + ft_printf(" position : "); + if (terms_g_err[i].position == TERM_LEFT) + ft_printf("%s\n", "TERM_LEFT"); + if (terms_g_err[i].position == TERM_RIGHT) + ft_printf("%s\n", "TERM_RIGHT"); + // sign + ft_printf(" sign : "); + if (terms_g_err[i].sign == TERM_PLUS) + ft_printf("%s\n", "TERM_PLUS"); + if (terms_g_err[i].sign == TERM_MINUS) + ft_printf("%s\n", "TERM_MINUS"); + // coefficient + printf(" coefficient : %g\n", terms_g_err[i].coefficient); + // exponent + ft_printf(" exponent : %d\n", terms_g_err[i].exponent); + i++; + } + ft_putchar('\n'); +} + +int stop_errors(program_error err, const char *details, ...) +{ // the base error message const char *msg = "error: error type is out of range"; @@ -29,52 +108,11 @@ int stop_errors(program_error err, const char *details, ...) // print context if (input_g_err) - { - ft_dprintf(STDERR_FILENO, "\ninput : %s\n", input_g_err); - } + print_context_input(); if (tokens_g_err) - { - ft_putchar('\n'); - i = 0; - while (tokens_g_err[i].type != TOKEN_END) - { - ft_printf("token %2i - type : ", i); - - if (tokens_g_err[i].type == TOKEN_VARIABLE) - ft_printf("%20s", "TOKEN_VARIABLE"); - else if (tokens_g_err[i].type == TOKEN_NUMBER_INT) - ft_printf("%20s", "TOKEN_NUMBER_INT"); - else if (tokens_g_err[i].type == TOKEN_NUMBER_DOUBLE) - ft_printf("%20s", "TOKEN_NUMBER_DOUBLE"); - else if (tokens_g_err[i].type == TOKEN_POWER) - ft_printf("%20s", "TOKEN_POWER"); - else if (tokens_g_err[i].type == TOKEN_SIGN_PLUS) - ft_printf("%20s", "TOKEN_SIGN_PLUS"); - else if (tokens_g_err[i].type == TOKEN_SIGN_MINUS) - ft_printf("%20s", "TOKEN_SIGN_MINUS"); - else if (tokens_g_err[i].type == TOKEN_FACTOR_MULT) - ft_printf("%20s", "TOKEN_FACTOR_MULT"); - else if (tokens_g_err[i].type == TOKEN_FACTOR_DIV) - ft_printf("%20s", "TOKEN_FACTOR_DIV"); - else if (tokens_g_err[i].type == TOKEN_EQUAL) - ft_printf("%20s", "TOKEN_EQUAL"); - else if (tokens_g_err[i].type == TOKEN_END) - ft_printf("%20s", "TOKEN_END"); - - ft_putstr(" - value : "); - - if (tokens_g_err[i].tag == TOKEN_NUMBER) - { - printf("%g\n", tokens_g_err[i].value_double); - } - else - { - ft_printf("%c\n", tokens_g_err[i].value_char); - } - i++; - } - ft_putchar('\n'); - } + print_context_tokens(); + if (terms_g_err) + print_context_terms(); // print the base message ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);