-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy patherror.c
More file actions
83 lines (69 loc) · 1.72 KB
/
error.c
File metadata and controls
83 lines (69 loc) · 1.72 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
The One Programming Language
File: builtins/error.c
_ _
/ \ |\ | |_ Max Base
\_/ | \| |_ Copyright 2021; One Language Contributors
**/
#include "../parser/lexer/lexer.h"
#include "error.h"
// Global variable(s)
extern Lexer lexer;
/*
* @function: error
* @description: Occur a error/warning at the runtime, If that was not a warning so we will exit the program immediately
* @arguments: ErrorType, int line, const char* file, const char* function, const char* format varg...
* @return: void; nothing
*/
void error(ErrorType type, int line, const char* file, const char* function, const char* format, ...)
{
debug("error");
va_list args;
va_start(args, format);
// TODO: Show error type with another custom color!
fprintf(stderr, "%s: ", error_name(type));
vfprintf(stderr, format, args);
va_end(args);
// fprintf(stderr, "[at %s line %d in %s]", file, line, function);
Location loc;
char* path = NULL;
#ifdef _ONE_PARSER_
Token* current_token = (*parser.tokens);
loc = current_token->pos;
path = parser.path;
#elif defined _ONE_LEXER_
loc = lexer.loc;
#endif
path = (path == NULL) ? "REPL" : path;
fprintf(stderr, " at %s:%zu:%zu", path, loc.line, loc.column);
fputs("\n", stderr);
if (type != ERROR_WARNING)
{
exit(1);
}
}
/*
* @function: error_name
* @description: Convert a ErrorType value to char*, mapping to its name
* @arguments: ErrorType
* @return: char*
*/
char* error_name(ErrorType type)
{
debug("error_name");
switch (type)
{
case ERROR_TREE:
return "TREE";
case ERROR_WARNING:
return "WARNING";
case ERROR_PANIC:
return "ERROR";
case ERROR_TOKEN:
return "TOKEN ERROR";
case ERROR_PARSER:
return "PARSER ERROR";
default:
return "UNKNOWN ERROR";
}
}