implemented ErrorCodePage

This commit is contained in:
2025-11-10 11:42:25 +05:00
parent 6a1067a612
commit 972f244ae5
5 changed files with 82 additions and 5 deletions

View File

@@ -18,15 +18,31 @@ typedef struct ErrorCallPos {
typedef struct Error {
str msg;
bool is_msg_on_heap;
u16 error_code_page;
u32 error_code;
List(ErrorCallPos) call_stack;
} Error;
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p,
u16 error_code_page, u32 error_code);
void Error_free(Error* e);
void Error_addCallPos(Error* e, ErrorCallPos p);
str Error_toStr(Error* e);
void Error_printAndExit(Error* e) ATTRIBUTE_NORETURN;
#define ErrorCodePage_name(name) ErrorCodePage_##name
#define ErrorCodePage_declare(name) extern u16 ErrorCodePage_name(name);
#define ErrorCodePage_define(name) u16 ErrorCodePage_name(name) = 0;
void _ErrorCodePage_register(u16* error_code_page_ptr);
#define ErrorCodePage_register(name) _ErrorCodePage_register(&ErrorCodePage_name(name));
typedef enum TLIBC_ERROR {
TLIBC_ERROR_UNKNOWN
} TLIBC_ERROR;
#define ErrorCodePage_NONE 0
ErrorCodePage_declare(TLIBC_ERROR);
ErrorCodePage_declare(LIBC_ERRNO);
typedef struct Result_ {
Error* error;
union {
@@ -46,9 +62,21 @@ typedef struct Result_ {
///USAGE: IGNORE_RESULT trySomething();
#define IGNORE_RESULT Result_ __ignored_##__LINE__ ATTRIBUTE_UNUSED =
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) }
#define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(FORMAT ,##ARGS), true)
#define RESULT_ERROR_ERRNO() RESULT_ERROR(strerror(errno), false)
#define RESULT_ERROR_CODE(CODE_PAGE, CODE, MSG, IS_MSG_ON_HEAP) (Result_){ \
.error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here(), \
ErrorCodePage_name(CODE_PAGE), CODE), \
.u = 0 \
}
#define RESULT_ERROR_CODE_FMT(CODE_PAGE, CODE, FORMAT, ARGS...) \
RESULT_ERROR_CODE(CODE_PAGE, CODE, sprintf_malloc(FORMAT ,##ARGS), true)
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) \
RESULT_ERROR_CODE(NONE, 0, MSG, IS_MSG_ON_HEAP);
#define RESULT_ERROR_FMT(FORMAT, ARGS...) \
RESULT_ERROR_CODE_FMT(NONE, 0, FORMAT ,##ARGS)
#define RESULT_ERROR_ERRNO() \
RESULT_ERROR_CODE(LIBC_ERRNO, errno, strerror(errno), false)
#define RESULT_VOID (Result_){ .error = NULL, .u = 0 }
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }

5
include/tlibc/tlibc.h Normal file
View File

@@ -0,0 +1,5 @@
#include "tlibc/errors.h"
/// call once at the start of main()
Result(void) tlibc_init();
void tlibc_deinit();