adding utils with enum_print
This commit is contained in:
137
src/utils/errors.c
Normal file
137
src/utils/errors.c
Normal file
@@ -0,0 +1,137 @@
|
||||
/* errors.c */
|
||||
|
||||
#include "computorv1.h"
|
||||
|
||||
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_dprintf(STDERR_FILENO, "token %2i - type : ", i);
|
||||
|
||||
if (tokens_g_err[i].type == TOKEN_VARIABLE)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_VARIABLE");
|
||||
else if (tokens_g_err[i].type == TOKEN_NUMBER_INT)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_NUMBER_INT");
|
||||
else if (tokens_g_err[i].type == TOKEN_NUMBER_DOUBLE)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_NUMBER_DOUBLE");
|
||||
else if (tokens_g_err[i].type == TOKEN_POWER)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_POWER");
|
||||
else if (tokens_g_err[i].type == TOKEN_SIGN_PLUS)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_SIGN_PLUS");
|
||||
else if (tokens_g_err[i].type == TOKEN_SIGN_MINUS)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_SIGN_MINUS");
|
||||
else if (tokens_g_err[i].type == TOKEN_FACTOR_MULT)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_FACTOR_MULT");
|
||||
else if (tokens_g_err[i].type == TOKEN_FACTOR_DIV)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_FACTOR_DIV");
|
||||
else if (tokens_g_err[i].type == TOKEN_EQUAL)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_EQUAL");
|
||||
else if (tokens_g_err[i].type == TOKEN_END)
|
||||
ft_dprintf(STDERR_FILENO, "%20s", "TOKEN_END");
|
||||
|
||||
ft_putstr(" - value : ");
|
||||
|
||||
if (tokens_g_err[i].tag == TOKEN_NUMBER)
|
||||
{
|
||||
dprintf(STDERR_FILENO, "%g\n", tokens_g_err[i].value_double);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "%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_dprintf(STDERR_FILENO, "term %2i - ", i);
|
||||
|
||||
// position
|
||||
ft_dprintf(STDERR_FILENO, "%10s : ", "position");
|
||||
if (terms_g_err[i].position == TERM_LEFT)
|
||||
ft_dprintf(STDERR_FILENO, "%10s", "TERM_LEFT");
|
||||
else if (terms_g_err[i].position == TERM_RIGHT)
|
||||
ft_dprintf(STDERR_FILENO, "%10s", "TERM_RIGHT");
|
||||
else
|
||||
ft_dprintf(STDERR_FILENO, "%10s", "");
|
||||
|
||||
// sign
|
||||
ft_dprintf(STDERR_FILENO, " | %10s : ", "sign");
|
||||
if (terms_g_err[i].sign == TERM_PLUS)
|
||||
ft_dprintf(STDERR_FILENO, "%10s", "TERM_PLUS");
|
||||
else if (terms_g_err[i].sign == TERM_MINUS)
|
||||
ft_dprintf(STDERR_FILENO, "%10s", "TERM_MINUS");
|
||||
else
|
||||
ft_dprintf(STDERR_FILENO, "%10s", "");
|
||||
|
||||
// coefficient
|
||||
dprintf(STDERR_FILENO, " | %10s : %13g", "coefficient", terms_g_err[i].coefficient);
|
||||
|
||||
// exponent
|
||||
ft_dprintf(STDERR_FILENO, " | %10s : %d\n", "exponent", 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);
|
||||
|
||||
// print the formatted details
|
||||
va_list args;
|
||||
va_start(args, details);
|
||||
ft_vdprintf(STDERR_FILENO, details, args);
|
||||
va_end(args);
|
||||
|
||||
ft_putchar_fd('\n', STDERR_FILENO);
|
||||
|
||||
exit(err);
|
||||
}
|
||||
59
src/utils/print_enums.c
Normal file
59
src/utils/print_enums.c
Normal file
@@ -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];
|
||||
}
|
||||
Reference in New Issue
Block a user