BFC-Compiler
A C23 Brainfuck compiler
Loading...
Searching...
No Matches
bfc_error.h
Go to the documentation of this file.
1
8#ifndef BFC_ERROR_H
9#define BFC_ERROR_H
10
11#include "bfc_token.h"
12
13#define COL_OFF "\033[m"
14#define COL_INFO "\033[1;1m"
15#define COL_ERROR "\033[1;31m"
16
20#define ERROR_LIST \
21 X(ERR_OK) \
22 X(ERR_ARGS) \
23 X(ERR_IO) \
24 X(ERR_MISMATCHED_BRACKET) \
25 X(ERR_MISSING_BRACKET) \
26 X(ERR_ALLOC) \
27 X(ERR_INTERNAL)
28
29typedef enum
30{
31#define X(name) name,
33#undef X
34} bfc_err_code_t;
35
41typedef struct [[nodiscard("bfc_error_t result must be checked")]]
42{
43 bfc_err_code_t code;
44 char msg[512];
45 bfc_token_t token;
47
48#define BFC_ERR_OK ((bfc_error_t) {.code = ERR_OK, .msg = {0}, .token = {0}})
49
50#define BFC_ERR_ALLOC \
51 ((bfc_error_t) {.code = ERR_ALLOC, .msg = "Memory allocation failure!", .token = {0}})
52
53struct bfc_program_t;
54
63[[gnu::nonnull(2), gnu::format(printf, 2, 3)]]
64bfc_error_t bfc_make_errorf(bfc_err_code_t error_code, const char* format, ...);
65
75[[gnu::nonnull(3), gnu::format(printf, 3, 4)]]
77bfc_make_errorf_with_token(bfc_err_code_t error_code, bfc_token_t token, const char* format, ...);
78
82bfc_error_t bfc_make_error(bfc_err_code_t const error_code, char const* msg);
83
88 bfc_err_code_t const error_code,
89 char const* msg,
90 bfc_token_t const token
91);
92
98[[nodiscard, gnu::const, gnu::returns_nonnull]]
99char const* bfc_get_error_code(bfc_err_code_t const error_code);
100
107[[gnu::cold]]
108void bfc_log_error(bfc_error_t err, const struct bfc_program_t* program);
109
110#endif // BFC_ERROR_H
bfc_error_t bfc_make_errorf(bfc_err_code_t error_code, const char *format,...)
Creates an error using printf-style formatting.
Definition bfc_error.c:22
bfc_error_t bfc_make_errorf_with_token(bfc_err_code_t error_code, bfc_token_t token, const char *format,...)
Creates a source-associated formatted error.
Definition bfc_error.c:42
#define ERROR_LIST
Single source of truth for compiler error codes.
Definition bfc_error.h:20
bfc_error_t bfc_make_error_with_token(bfc_err_code_t const error_code, char const *msg, bfc_token_t const token)
Creates a token-associated error by copying an existing message string.
Definition bfc_error.c:80
void bfc_log_error(bfc_error_t err, const struct bfc_program_t *program)
Prints a user-facing compiler diagnostic to standard error.
Definition bfc_error.c:120
bfc_error_t bfc_make_error(bfc_err_code_t const error_code, char const *msg)
Creates an error by copying an existing message string.
Definition bfc_error.c:62
char const * bfc_get_error_code(bfc_err_code_t const error_code)
Returns the symbolic name of an error code.
Definition bfc_error.c:98
Brainfuck token definitions and token-stream ownership.
Error value propagated through compiler stages.
Definition bfc_error.h:42
Owning representation of a loaded source file.
Definition bfc_io.h:21
One Brainfuck token and its one-based source location.
Definition bfc_token.h:41