This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherror.c
171 lines (158 loc) · 4.21 KB
/
error.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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ14
*
* Repository:
* https://github.com/Dasio/IFJ
*
* Team:
* Dávid Mikuš (xmikus15)
* Peter Hostačný (xhosta03)
* Tomáš Kello (xkello00)
* Adam Lučanský (xlucan01)
* Michaela Lukášová (xlukas09)
*/
#include "error.h"
#include "token.h"
/* Global variable in which is stored last error */
SError error;
extern Token *token;
/* Error code to return code conversion table */
static const int returnCodeTable[] = {
[ERR_None] = 0,
[ERR_Allocation] = 99,
[ERR_CannotOpenFile] = 99,
// Lexical analysis
[ERR_Lexical] = 1,
[ERR_LexicalConversion] = 1,
// Parser
[ERR_Syntax] = 2,
[ERR_RedefVar] = 3,
[ERR_RedefFunc] = 3,
[ERR_DeclrFunc] = 3,
[ERR_BadDefArg] = 3,
[ERR_NoDefFunc] = 3,
[ERR_VarAsFunc] = 3,
[ERR_ReadBool] = 4,
[ERR_FuncAsVar] = 3,
// Expression
[ERR_SyntaxExpr] = 2,
[ERR_PrecedenceTable] = 2,
[ERR_Reduction] = 2,
[ERR_TypeCompatibility] = 4,
[ERR_TypeCompatibilityArg] = 4,
[ERR_UndefVarOrFunction]= 3,
[ERR_ArgCount] = 4,
// Interpreter
[ERR_UnknownInstruction]= 99,
[ERR_UnitializedAccess] = 7,
[ERR_DivisionByZero] = 8,
[ERR_ReadInput] = 6,
// Other
[ERR_OutOfRange] = 9,
[ERR_Unknown] = 9
};
void printError()
{
switch(error.state) {
case ERR_None:
return;
case ERR_CannotOpenFile:
printErrorDetails("Cannot open specified file.");
break;
case ERR_Lexical:
printErrorDetails("Invalid input file, "
"lexical analysis cannot proceed:");
break;
case ERR_Syntax:
printErrorDetails("Syntax error:");
break;
case ERR_Unknown:
printErrorDetails("Unknown error:");
break;
case ERR_RedefVar:
printErrorDetails("Variable was redefined:");
break;
case ERR_RedefFunc:
printErrorDetails("Function was already defined, can't be defined again:");
break;
case ERR_DeclrFunc:
printErrorDetails("Function can't be declared more than one time:");
break;
case ERR_BadDefArg:
printErrorDetails("Header of function definition don't match with declaration:");
break;
case ERR_NoDefFunc:
printErrorDetails("Function was declared, but afterward it wasn't defined:");
break;
case ERR_SyntaxExpr:
printErrorDetails("Error in syntax of expression:");
break;
case ERR_PrecedenceTable:
printErrorDetails("Error in syntax detected by precedence table:");
break;
case ERR_Reduction:
printErrorDetails("Error in syntax detected during reduction:");
break;
case ERR_TypeCompatibility:
printErrorDetails("Error in data type of some operand:");
break;
case ERR_TypeCompatibilityArg:
printErrorDetails("Error in data type of some argument:");
break;
case ERR_UndefVarOrFunction:
printErrorDetails("Undefined variable or function:");
break;
case ERR_ArgCount:
printErrorDetails("Bad number of arguments in function call:");
break;
case ERR_DivisionByZero:
printErrorDetails("You can't divide by zero !");
break;
case ERR_UnitializedAccess:
printErrorDetails("Uninitialized acces.");
break;
case ERR_ReadBool:
printErrorDetails("Cant't read input into bool variable:");
break;
case ERR_VarAsFunc:
printErrorDetails("Local variable name can't be same as function name:");
break;
case ERR_FuncAsVar:
printErrorDetails("Expected variable instead of function");
break;
case ERR_ReadInput:
printErrorDetails("Wrong input:");
break;
case ERR_OutOfRange:
printErrorDetails("Out of range:");
break;
default:
printErrorDetails("Unrecognized error:");
}
}
void printErrorDetails(const char *description)
{
fprintf(stderr,"%s in file \"%s\" at line: %u\n",description,error.file,error.line);
if(error.state == ERR_Syntax || error.state == ERR_SyntaxExpr)
{
fprintf(stderr,"Current token: %s\n",stringifyToken(token));
}
}
void setErrorDetails(EErrorStates state, unsigned line, char *file)
{
error.state = state;
error.line = line;
error.file = file;
}
EErrorStates getError()
{
return error.state;
}
int getReturnError()
{
if(error.state >= ERR_None || error.state <= ERR_Unknown)
return returnCodeTable[error.state];
else
return returnCodeTable[ERR_Unknown];
}