forked from infancy/devlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
252 lines (224 loc) · 6.29 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "MEM.h"
#include "DBG.h"
#include "crowbar.h"
extern char *yytext;
extern MessageFormat crb_compile_error_message_format[];
extern MessageFormat crb_runtime_error_message_format[];
typedef struct {
char *string;
} VString;
static void
clear_v_string(VString *v)
{
v->string = NULL;
}
int
my_strlen(char *str)
{
if (str == NULL) {
return 0;
}
return strlen(str);
}
static void
add_string(VString *v, char *str)
{
int new_size;
int old_len;
old_len = my_strlen(v->string);
new_size = old_len + strlen(str) + 1;
v->string = MEM_realloc(v->string, new_size);
strcpy(&v->string[old_len], str);
}
static void
add_character(VString *v, int ch)
{
int current_len;
current_len = my_strlen(v->string);
v->string = MEM_realloc(v->string, current_len + 2);
v->string[current_len] = ch;
v->string[current_len+1] = '\0';
}
typedef struct {
MessageArgumentType type;
char *name;
union {
int int_val;
double double_val;
char *string_val;
void *pointer_val;
int character_val;
} u;
} MessageArgument;
static void
create_message_argument(MessageArgument *arg, va_list ap)
{
int index = 0;
MessageArgumentType type;
while ((type = va_arg(ap, MessageArgumentType)) != MESSAGE_ARGUMENT_END) {
arg[index].type = type;
arg[index].name = va_arg(ap, char*);
switch (type) {
case INT_MESSAGE_ARGUMENT:
arg[index].u.int_val = va_arg(ap, int);
break;
case DOUBLE_MESSAGE_ARGUMENT:
arg[index].u.double_val = va_arg(ap, double);
break;
case STRING_MESSAGE_ARGUMENT:
arg[index].u.string_val = va_arg(ap, char*);
break;
case POINTER_MESSAGE_ARGUMENT:
arg[index].u.pointer_val = va_arg(ap, void*);
break;
case CHARACTER_MESSAGE_ARGUMENT:
arg[index].u.character_val = va_arg(ap, int);
break;
case MESSAGE_ARGUMENT_END:
assert(0);
break;
default:
assert(0);
}
index++;
assert(index < MESSAGE_ARGUMENT_MAX);
}
}
static void
search_argument(MessageArgument *arg_list,
char *arg_name, MessageArgument *arg)
{
int i;
for (i = 0; arg_list[i].type != MESSAGE_ARGUMENT_END; i++) {
if (!strcmp(arg_list[i].name, arg_name)) {
*arg = arg_list[i];
return;
}
}
assert(0);
}
static void
format_message(MessageFormat *format, VString *v, va_list ap)
{
int i;
char buf[LINE_BUF_SIZE];
int arg_name_index;
char arg_name[LINE_BUF_SIZE];
MessageArgument arg[MESSAGE_ARGUMENT_MAX];
MessageArgument cur_arg;
create_message_argument(arg, ap);
for (i = 0; format->format[i] != '\0'; i++) {
if (format->format[i] != '$') {
add_character(v, format->format[i]);
continue;
}
assert(format->format[i+1] == '(');
i += 2;
for (arg_name_index = 0; format->format[i] != ')';
arg_name_index++, i++) {
arg_name[arg_name_index] = format->format[i];
}
arg_name[arg_name_index] = '\0';
assert(format->format[i] == ')');
search_argument(arg, arg_name, &cur_arg);
switch (cur_arg.type) {
case INT_MESSAGE_ARGUMENT:
sprintf(buf, "%d", cur_arg.u.int_val);
add_string(v, buf);
break;
case DOUBLE_MESSAGE_ARGUMENT:
sprintf(buf, "%f", cur_arg.u.double_val);
add_string(v, buf);
break;
case STRING_MESSAGE_ARGUMENT:
strcpy(buf, cur_arg.u.string_val);
add_string(v, cur_arg.u.string_val);
break;
case POINTER_MESSAGE_ARGUMENT:
sprintf(buf, "%p", cur_arg.u.pointer_val);
add_string(v, buf);
break;
case CHARACTER_MESSAGE_ARGUMENT:
sprintf(buf, "%c", cur_arg.u.character_val);
add_string(v, buf);
break;
case MESSAGE_ARGUMENT_END:
assert(0);
break;
default:
assert(0);
}
}
}
void
self_check()
{
if (strcmp(crb_compile_error_message_format[0].format, "dummy") != 0) {
DBG_panic(("compile error message format error.\n"));
}
if (strcmp(crb_compile_error_message_format
[COMPILE_ERROR_COUNT_PLUS_1].format,
"dummy") != 0) {
DBG_panic(("compile error message format error. "
"COMPILE_ERROR_COUNT_PLUS_1..%d\n",
COMPILE_ERROR_COUNT_PLUS_1));
}
if (strcmp(crb_runtime_error_message_format[0].format, "dummy") != 0) {
DBG_panic(("runtime error message format error.\n"));
}
if (strcmp(crb_runtime_error_message_format
[RUNTIME_ERROR_COUNT_PLUS_1].format,
"dummy") != 0) {
DBG_panic(("runtime error message format error. "
"RUNTIME_ERROR_COUNT_PLUS_1..%d\n",
RUNTIME_ERROR_COUNT_PLUS_1));
}
}
void
crb_compile_error(CompileError id, ...)
{
va_list ap;
VString message;
int line_number;
self_check();
va_start(ap, id);
line_number = crb_get_current_interpreter()->current_line_number;
clear_v_string(&message);
format_message(&crb_compile_error_message_format[id],
&message, ap);
fprintf(stderr, "%3d:%s\n", line_number, message.string);
va_end(ap);
exit(1);
}
void
crb_runtime_error(int line_number, RuntimeError id, ...)
{
va_list ap;
VString message;
self_check();
va_start(ap, id);
clear_v_string(&message);
format_message(&crb_runtime_error_message_format[id],
&message, ap);
fprintf(stderr, "%3d:%s\n", line_number, message.string);
va_end(ap);
exit(1);
}
int
yyerror(char const *str)
{
char *near_token;
if (yytext[0] == '\0') {
near_token = "EOF";
} else {
near_token = yytext;
}
crb_compile_error(PARSE_ERR,
STRING_MESSAGE_ARGUMENT, "token", near_token,
MESSAGE_ARGUMENT_END);
return 0;
}