-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.c
More file actions
91 lines (65 loc) · 2.2 KB
/
errors.c
File metadata and controls
91 lines (65 loc) · 2.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <wchar.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include<stdio.h>
#include <inttypes.h>
#include "errors.h"
void errorInitContents(Error *error) {
error->lineNumber = 0;
error->functionName = NULL;
error->fileName = NULL;
error->type = E_NONE;
}
void printError(Error *error) {
char* fileName = basename((char *) error->fileName);
printf("(debug) error at %s(%s:%" PRIu64 "): %ls\n", error->functionName, fileName, error->lineNumber, error->message);
}
/*
* Error factories
*/
RetVal memoryError(Error *error, char *desc) {
error->type = E_MEMORY;
swprintf(error->message, ERROR_MSG_LENGTH, L"failed to %s\n", desc);
if (DEBUG) { printError(error); }
return R_ERROR;
}
RetVal ioError(Error *error, char *desc) {
error->type = E_IO;
error->lexer.position = 0;
swprintf(error->message, ERROR_MSG_LENGTH, L"failed to %s -> '%s'\n", desc, strerror(errno));
if (DEBUG) { printError(error); }
return R_ERROR;
}
RetVal internalError(Error *error, char *desc) {
error->type = E_INTERNAL;
swprintf(error->message, ERROR_MSG_LENGTH, L"encountered internal failure, probably a bug: %s\n", desc, strerror(errno));
if (DEBUG) { printError(error); }
return R_ERROR;
}
RetVal tokenizationError(Error *error, unsigned long position, char *desc) {
error->type = E_LEXER;
error->lexer.position = position;
swprintf(error->message, ERROR_MSG_LENGTH, L"failed to tokenize stream -> %s\n", desc);
if (DEBUG) { printError(error); }
return R_ERROR;
}
RetVal syntaxError(Error *error, unsigned long position, char *desc) {
error->type = E_SYNTAX;
error->lexer.position = position;
swprintf(error->message, ERROR_MSG_LENGTH, L"invalid syntax encountered -> %s\n", desc);
if (DEBUG) { printError(error); }
return R_ERROR;
}
RetVal runtimeError(Error *error, char *desc) {
error->type = E_RUNTIME;
swprintf(error->message, ERROR_MSG_LENGTH, L"vm failure encountered -> %s\n", desc);
if (DEBUG) { printError(error); }
return R_ERROR;
}
RetVal compilerError(Error *error, char *desc) {
error->type = E_RUNTIME;
swprintf(error->message, ERROR_MSG_LENGTH, L"compiler error encountered -> %s\n", desc);
if (DEBUG) { printError(error); }
return R_ERROR;
}