1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#include "errors.h"
static const char* error_msgs[] = {
[ERR_UNKNOWN] = "error: something unexpected happened",
[ERR_UNMATCHED_STRING] = "error: unmatched string delimiter",
[ERR_UNMATCHED_PAREN] = "error: unbalanced parentheses",
[ERR_UNKNOWN_TOK_TYPE] = "error: unknown token type",
[ERR_MALFORMED_NUMBER] = "error: malformed number token",
[ERR_MALFORMED_EXPR] = "error: malformed expression",
[ERR_UNIMPLEMENTED] = "error: not implemented",
[ERR_NOT_A_NUMBER] = "error: expected a number",
[ERR_NOT_A_SYMBOL] = "error: expected a symbol",
[ERR_NOT_A_STRING] = "error: expected a string",
[ERR_NOT_A_TYPE] = "error: expected a type",
[ERR_NOT_A_BOOL] = "error: expected a bool",
[ERR_NOT_A_LPAREN] = "error: expected opening parentheses (lparen)",
[ERR_NOT_A_RPAREN] = "error: expected closing parentheses (rparen)",
[ERR_SYMBOL_REDEF] = "error: symbol redefinition",
[ERR_UNKNOWN_SYMBOL] = "error: unknown symbol",
[ERR_TYPE_REDEF] = "error: type redefinition",
[ERR_UNKNOWN_TYPE] = "error: unknown type",
[ERR_WRONG_RET_TYPE] = "error: return type don't match type signature",
[ERR_WRONG_COND_TYPE] = "error: conditional expression is not boolean",
[ERR_WRONG_TYPE_T_F] = "error: unmatched types between true and false expression",
[ERR_WRONG_TYPE_NUM] = "error: non numeric argument types",
[ERR_WRONG_TYPE_BOOL] = "error: non bool argument types",
[ERR_WRONG_TYPE_FUN] = "error: not a function",
[ERR_BAD_ARGS] = "error: arguments don't match expected types",
[ERR_TYPE_MISMATCH] = "error: type mismatch",
};
static Error current_error = {.value = ERR_OK};
void
push_error(ErrorType type, ErrorValue value, size_t line, size_t col) {
if (has_errors()) {
return;
}
current_error.type = type;
current_error.value = value;
current_error.line = line;
current_error.col = col;
}
bool
has_errors(void) {
return current_error.value != ERR_OK;
}
void
check_errors(const char *file_name) {
if (!has_errors()) {
return;
}
fprintf(stderr, "%s", file_name);
if (current_error.line != 0) {
fprintf(stderr, ":%ld:%ld", current_error.line, current_error.col);
}
switch (current_error.type) {
case ERR_TYPE_LEXER: { fprintf(stderr, ": [lexer] "); } break;
case ERR_TYPE_PARSER: { fprintf(stderr, ": [parser] "); } break;
default: break;
}
fprintf(stderr, "%s\n", error_msgs[current_error.value]);
exit(EXIT_FAILURE);
}
|