-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.h
More file actions
65 lines (57 loc) · 4.2 KB
/
error.h
File metadata and controls
65 lines (57 loc) · 4.2 KB
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
/* IFJ20 - Error lib
* Authors:
* Juraj Marticek, xmarti97
*/
#ifndef ERROR_H
#define ERROR_H
#include <stdarg.h>
#include <stdlib.h>
// Enum consisting of error types
typedef enum
{
LEXICAL_ERROR = 1,
SYNTAX_ERROR,
DEFINITION_ERROR,
TYPE_DEFINITION_ERROR,
INCOMPATIBLE_EXPRESSION_ERROR,
FUNCTION_DEFINITION_ERROR,
OTHER_SEMANTIC_ERROR,
ZERO_DIVISION_ERROR = 9,
INTERNAL_ERROR = 99,
} ErrorTypes;
// Macro for DEBUG print, prints message to stdout
// params: fmt, rest
// fmt: format string, printf format
// rest: rest of params which are feeded into format string
#define print(fmt, ...) \
do \
{ \
printf("\n"); \
fprintf(stderr, "#\033[32m[DEBUG]\033[1m %s:%d\033[0m In function \033[1m‘%s’\033[0m: ", __FILE__, __LINE__, __func__); \
fprintf(stderr, fmt, ##__VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
// Macro for error throw -> prints error message to stderr
#define throw_error(err_no, fmt, ...) \
do \
{ \
printf("\n"); \
char *errorMsg = errTypeToString(err_no); \
fprintf(stderr, "\033[31m[%s]\033[1m %s:%d\033[0m In function \033[1m‘%s’\033[0m: ", errorMsg, __FILE__, __LINE__, __func__); \
fprintf(stderr, fmt, ##__VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
// Macro for fata error throw -> prints error message to stderr, then exits process
#define throw_error_fatal(err_no, fmt, ...) \
do \
{ \
printf("\n"); \
char *errorMsg = errTypeToString(err_no); \
fprintf(stderr, "\033[31m[%s (FATAL)]\033[1m %s:%d\033[0m In function \033[1m‘%s’\033[0m: ", errorMsg, __FILE__, __LINE__, __func__); \
fprintf(stderr, fmt, ##__VA_ARGS__); \
fprintf(stderr, "\n"); \
fprintf(stderr, "Exiting...\n"); \
exit(err_no); \
} while (0)
char *errTypeToString(ErrorTypes err_no);
#endif