forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nierr.c
239 lines (223 loc) · 7.21 KB
/
nierr.c
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ13.
*
* Codename:
* INI: Ni Interpreter
*
* Description:
* https://wis.fit.vutbr.cz/FIT/st/course-files-st.php/course/IFJ-IT/projects/ifj2013.pdf
*
* Project's GitHub repository:
* https://github.com/metthal/IFJ-Projekt
*
* Team:
* Marek Milkovič (xmilko01)
* Lukáš Vrabec (xvrabe07)
* Ján Spišiak (xspisi03)
* Ivan Ševčík (xsevci50)
* Marek Bertovič (xberto00)
*/
#include "nierr.h"
/** Global interpret error variable. */
NiError niErr;
#ifdef DEBUG
#include "token_vector.h"
#include <unistd.h>
#define STD_ERR_INT 2
#define MAX_TOKEN_PEEK 4
#define NORMAL_COLOR "\033[0m"
#define ERR_COLOR "\033[1m\033[31m"
#define INFO_COLOR "\033[1m\033[33m"
char errorPrinted = 1;
extern ConstTokenVectorIterator tokensIt;
void printToken(const Token *token) {
switch (token->type) {
case STT_Plus:
fprintf(stderr, "+"); break;
case STT_Minus:
fprintf(stderr, "-"); break;
case STT_Multiply:
fprintf(stderr, "*"); break;
case STT_Divide:
fprintf(stderr, "/"); break;
case STT_Dot:
fprintf(stderr, "."); break;
case STT_Less:
fprintf(stderr, "<"); break;
case STT_Greater:
fprintf(stderr, ">"); break;
case STT_LessEqual:
fprintf(stderr, "<="); break;
case STT_GreaterEqual:
fprintf(stderr, ">="); break;
case STT_Equal:
fprintf(stderr, "==="); break;
case STT_NotEqual:
fprintf(stderr, "!=="); break;
case STT_LeftBracket:
fprintf(stderr, "("); break;
case STT_RightBracket:
fprintf(stderr, ")"); break;
case STT_Identifier:
fprintf(stderr, "%s", token->str.data); break;
case STT_Comma:
fprintf(stderr, ","); break;
case STT_EOF:
fprintf(stderr, "EOF"); break;
case STT_Variable:
fprintf(stderr, "$%s", token->str.data); break;
case STT_Number:
fprintf(stderr, "%d", token->n); break;
case STT_Double:
fprintf(stderr, "%g", token->d); break;
case STT_Bool:
fprintf(stderr, "%s", token->n == 0 ? "false" : "true"); break;
case STT_Null:
fprintf(stderr, "null"); break;
case STT_String:
fprintf(stderr, "\"%s\"", token->str.data); break;
case STT_Semicolon:
fprintf(stderr, ";"); break;
case STT_LeftCurlyBracket:
fprintf(stderr, "{"); break;
case STT_RightCurlyBracket:
fprintf(stderr, "}"); break;
case STT_Empty:
break;
case STT_Keyword:
switch (token->keywordType) {
case KTT_If:
fprintf(stderr, "if"); break;
case KTT_Else:
fprintf(stderr, "else"); break;
case KTT_Elseif:
fprintf(stderr, "elseif"); break;
case KTT_Continue:
fprintf(stderr, "continue"); break;
case KTT_Break:
fprintf(stderr, "break"); break;
case KTT_While:
fprintf(stderr, "while"); break;
case KTT_For:
fprintf(stderr, "for"); break;
case KTT_Return:
fprintf(stderr, "return"); break;
case KTT_Function:
fprintf(stderr, "function"); break;
default:
break;
}
break;
case STT_Assignment:
fprintf(stderr, "="); break;
case STT_Php:
fprintf(stderr, "<?php"); break;
case STT_And:
fprintf(stderr, "&&"); break;
case STT_Or:
fprintf(stderr, "||"); break;
case STT_Not:
fprintf(stderr, "!"); break;
default:
break;
}
}
void printErrorF(const char *str)
{
if (isatty(STD_ERR_INT)) {
fprintf(stderr, ERR_COLOR "Error at %s:%d:%s: %s" INFO_COLOR,
niErr.file, niErr.line, niErr.fun, str);
}
else {
fprintf(stderr, "Error at %s:%d:%s: %s",
niErr.file, niErr.line, niErr.fun, str);
}
if ((niErr.type == ERR_Syntax || niErr.type == ERR_UndefVariable) && (tokensIt != NULL)) {
fprintf(stderr, "Tokens around error:\n");
ConstTokenVectorIterator beginIt = tokensIt;
ConstTokenVectorIterator endIt = tokensIt;
for (int i = 0; beginIt->type != STT_Php && i < MAX_TOKEN_PEEK; i++)
beginIt--;
for (int i = 0; endIt->type != STT_EOF && i < MAX_TOKEN_PEEK; i++)
endIt++;
endIt++; // To point behind last token
for (;beginIt != endIt; beginIt++) {
printToken(beginIt);
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
if (isatty(STD_ERR_INT)) {
fprintf(stderr, NORMAL_COLOR);
}
}
void printError()
{
if (niErr.type == ERR_None || errorPrinted)
return;
switch (niErr.type) {
case ERR_Unknown:
printErrorF("Unknown error.\n");
break;
case ERR_LexFile:
printErrorF("Scanner couldn't process file.\n");
break;
case ERR_Allocation:
printErrorF("Allocation failed.\n");
break;
case ERR_Range:
printErrorF("Index was outside of allowed range.\n");
break;
case ERR_Internal:
printErrorF("Internal error.\n");
break;
case ERR_Convert:
printErrorF("Conversion failed.\n");
break;
case ERR_Syntax:
printErrorF("Syntax error.\n");
break;
case ERR_UndefFunction:
printErrorF("Undefined function used.\n");
break;
case ERR_RedefFunction:
printErrorF("Redefinition of function.\n");
break;
case ERR_UndefVariable:
printErrorF("Undefined variable used.\n");
break;
case ERR_RedefParameter:
printErrorF("Redefinition of function parameter.\n");
break;
case ERR_BadParamCount:
printErrorF("Function called with wrong number of parameters.\n");
break;
case ERR_DefArgOrder:
printErrorF("Non-default parameter appeared after default one.\n");
break;
case ERR_BadDefArg:
printErrorF("Invalid default value of parameter.\n");
break;
case ERR_ISTGenerator:
printErrorF("Instruction generator error.\n");
break;
case ERR_CycleControl:
printErrorF("Break or continue used outside cycle.\n");
break;
case ERR_Substr:
printErrorF("Substring parameters are wrong.\n");
break;
case ERR_OperandTypes:
printErrorF("Invalid operand type.\n");
break;
case ERR_DivideByZero:
printErrorF("Division by zero.\n");
break;
default:
printErrorF("Undocumented error.\n");
break;
}
errorPrinted = 1;
}
#endif