hydroforth/include/hydroforth/result.h

77 lines
2.7 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__CALL_STACK_TOO_BIG,
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__CALL_STACK_TOO_BIG] = "Call stack is too big",
[HF__ERROR__INTERPRETER__UNKNOWN_WORD] = "Unknown word",
[HF__ERROR__INTERPRETER__WORD_ALREADY_DEF] = "Word is already defined",
[HF__ERROR__INTERPRETER__STACK_UNDERFLOW] = "Stack underflow",
};
static const bool HF__ERROR_PANIC[__HF__ERROR__N] = {
[HF__ERROR__PARSER__UNEXPECTED] = false,
[HF__ERROR__PARSER__INVALID_NUMBER] = false,
[HF__ERROR__PARSER__INVALID_CHAR] = false,
[HF__ERROR__PARSER__WORD_DEF_INCOMPLETE] = false,
[HF__ERROR__PARSER__WORD_DEF_INVALID_NAME] = false,
[HF__ERROR__PARSER__WORD_DEF_IS_KEYWORD] = false,
[HF__ERROR__INTERPRETER__CALL_STACK_TOO_BIG] = true,
[HF__ERROR__INTERPRETER__UNKNOWN_WORD] = false,
[HF__ERROR__INTERPRETER__WORD_ALREADY_DEF] = false,
[HF__ERROR__INTERPRETER__STACK_UNDERFLOW] = false,
};
extern void hf__handle_error_light(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