adding debug output for terms
This commit is contained in:
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
94
src/errors.c
94
src/errors.c
@@ -2,38 +2,15 @@
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
int stop_errors(program_error err, const char *details, ...)
|
||||
{
|
||||
int i;
|
||||
|
||||
// the base error message
|
||||
const char *msg = "error: error type is out of range";
|
||||
|
||||
// map error codes to messages
|
||||
const char *error_messages[ERROR_SENTINEL] = {
|
||||
[ERROR_BASE] = "undefined error",
|
||||
[ERROR_UNKNOWN_TOKEN] = "LEXER - unknown token",
|
||||
[ERROR_NUMBER_TOO_BIG] = "LEXER - number is too big",
|
||||
[ERROR_PARSING] = "PARSER - too much terms to parse",
|
||||
[ERROR_TOKEN_POSITION] = "PARSER - token position is not good in grammar",
|
||||
[ERROR_VAR_DIFF] = "PARSER - expression must only contain one variable",
|
||||
[ERROR_TOKEN_COUNT] = "LEXER - token count error",
|
||||
[ERROR_TERM_COUNT] = "PARSER - term count error",
|
||||
};
|
||||
|
||||
// override msg if err is in the error_messages array
|
||||
if (err >= ERROR_BASE && err < ERROR_SENTINEL)
|
||||
{
|
||||
msg = error_messages[err];
|
||||
}
|
||||
|
||||
// print context
|
||||
if (input_g_err)
|
||||
static void print_context_input()
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "\ninput : %s\n", input_g_err);
|
||||
}
|
||||
if (tokens_g_err)
|
||||
|
||||
static void print_context_tokens()
|
||||
{
|
||||
int i;
|
||||
|
||||
ft_putchar('\n');
|
||||
i = 0;
|
||||
while (tokens_g_err[i].type != TOKEN_END)
|
||||
@@ -76,6 +53,67 @@ int stop_errors(program_error err, const char *details, ...)
|
||||
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";
|
||||
|
||||
// map error codes to messages
|
||||
const char *error_messages[ERROR_SENTINEL] = {
|
||||
[ERROR_BASE] = "undefined error",
|
||||
[ERROR_UNKNOWN_TOKEN] = "LEXER - unknown token",
|
||||
[ERROR_NUMBER_TOO_BIG] = "LEXER - number is too big",
|
||||
[ERROR_PARSING] = "PARSER - too much terms to parse",
|
||||
[ERROR_TOKEN_POSITION] = "PARSER - token position is not good in grammar",
|
||||
[ERROR_VAR_DIFF] = "PARSER - expression must only contain one variable",
|
||||
[ERROR_TOKEN_COUNT] = "LEXER - token count error",
|
||||
[ERROR_TERM_COUNT] = "PARSER - term count error",
|
||||
};
|
||||
|
||||
// override msg if err is in the error_messages array
|
||||
if (err >= ERROR_BASE && err < ERROR_SENTINEL)
|
||||
{
|
||||
msg = error_messages[err];
|
||||
}
|
||||
|
||||
// print context
|
||||
if (input_g_err)
|
||||
print_context_input();
|
||||
if (tokens_g_err)
|
||||
print_context_tokens();
|
||||
if (terms_g_err)
|
||||
print_context_terms();
|
||||
|
||||
// print the base message
|
||||
ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user