hydroforth/include/hydroforth/result.h
2023-08-04 21:42:01 +02:00

61 lines
2 KiB
C

#ifndef __HF__RESULT_H__
#define __HF__RESULT_H__
enum hf__error {
HF__ERROR__PARSER__UNEXPECTED,
HF__ERROR__PARSER__INVALID_NUMBER,
HF__ERROR__PARSER__INVALID_CHAR,
HF__ERROR__PARSER__WORD_DEF_INCOMPLETE,
HF__ERROR__PARSER__WORD_DEF_INVALID_NAME,
HF__ERROR__PARSER__WORD_DEF_IS_KEYWORD,
HF__ERROR__INTERPRETER__UNKNOWN_WORD,
HF__ERROR__INTERPRETER__WORD_ALREADY_DEF,
HF__ERROR__INTERPRETER__STACK_UNDERFLOW,
__HF__ERROR__N,
};
struct hf__error_wrapper {
enum hf__error error;
bool msg_is_freeable;
char *msg;
};
static const char *const HF__ERROR_STR[__HF__ERROR__N] = {
[HF__ERROR__PARSER__UNEXPECTED] = "Unexpected token",
[HF__ERROR__PARSER__INVALID_NUMBER] = "Invalid number",
[HF__ERROR__PARSER__INVALID_CHAR] = "Invalid char",
[HF__ERROR__PARSER__WORD_DEF_INCOMPLETE] = "Word definition imcomplete",
[HF__ERROR__PARSER__WORD_DEF_INVALID_NAME] =
"Invalid token type for word name",
[HF__ERROR__PARSER__WORD_DEF_IS_KEYWORD] = "Word name is already a keyword",
[HF__ERROR__INTERPRETER__UNKNOWN_WORD] = "Unknown word",
[HF__ERROR__INTERPRETER__WORD_ALREADY_DEF] = "Word is already defined",
[HF__ERROR__INTERPRETER__STACK_UNDERFLOW] = "Stack underflow",
};
extern void hf__print_error(struct hf__error_wrapper *error);
struct hf__result {
bool ok;
struct hf__error_wrapper error;
};
#define HF__OK ((struct hf__result){.ok = true})
#define HF__ERR(err) \
((struct hf__result){ \
.ok = false, \
.error = {.error = err, .msg = NULL}, \
})
#define HF__ERR_CUSTOM(err, message, freeable) \
((struct hf__result){ \
.ok = false, \
.error = {.error = err, .msg_is_freeable = freeable, .msg = message}, \
})
#endif