-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_handler.c
executable file
·55 lines (50 loc) · 1.16 KB
/
error_handler.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
extern int yylineno;
extern char buf[256];
typedef struct error_node {
char* msg;
int line;
struct error_node* next;
} error_node;
error_node *error_list;
void insert_error(char* msg) {
// create error node
error_node* target = (error_node*)malloc(sizeof(error_node));
target->msg = (char*)malloc(sizeof(char) * strlen(msg));
strcpy(target->msg, msg);
// insert the error node to error list
if (!error_list) {
error_list = target;
return;
}
error_node* iter = error_list;
while (iter->next) {
iter = iter->next;
}
iter->next = target;
}
void dump_error() {
if (!error_list) {
return;
}
error_node* iter;
error_node* prev;
iter = error_list;
prev = NULL;
// loop all error node and output to the terminal
while (iter) {
printf("\n|-----------------------------------------------|\n");
printf("| Error found in line %d: %s\n", yylineno, buf);
printf("| %s\n", iter->msg);
printf("|-----------------------------------------------|\n\n");
// free the previous error node
prev = iter;
iter = iter->next;
prev->next = NULL;
free(prev->msg);
free(prev);
}
error_list = NULL;
}