-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
93 lines (79 loc) · 1.82 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
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
void exit_error(const int err_num, const char *err_msg)
{
fprintf(stderr, "ERROR %d: %s\n", err_num, err_msg);
/*
if(pInfile != NULL) { fclose(pInfile); }
if(pOutfile != NULL) { fclose(pOutfile); }
*/
exit(EXIT_FAILURE);
}
void warning_error(const int err_num, const char *err_msg)
{
fprintf(stderr, "WARNING: %d: %s\n", err_num, err_msg);
}
void compiler_error(const int comp_num, const char *err_msg, char *pS, char *pC, char pT[])
{
char *pTemp = pC;
char *pCount = pC;
int sp_count = 0; /* space count */
int line_num = 1;
int token_len = 0;
printf("\n");
fflush(stdout);
/* backup pointer to token to beginning of previous token. */
token_len = strlen(pT);
do {
pC--;
} while (strncmp(pT, pC, token_len) != 0);
pTemp = pC;
/* set pTemp to beginning of current line */
while(pTemp > pS && *pTemp != '\n')
{
sp_count++;
pTemp--;
}
/* count line number to beginning of file print for user later */
while(pS <= pC)
{
if(*pC == '\n') { line_num++; }
pC--;
}
pTemp++; /* move past newline */
pCount = pTemp;
/* print last line to stderr */
while((*pTemp != '\n') && (*pTemp != '\0'))
{
putc(*pTemp, stderr);
pTemp++;
}
fprintf(stderr, "\n");
sp_count--; /* off by one because of arrow printed at end of line*/
/* print arrow pointing to token compiler cannot comprehend */
while(sp_count >= 0)
{
if(sp_count == 0)
{
fprintf(stderr, "^\n");
} else {
if(*pCount == '\t')
{
fprintf(stderr, "\t");
} else {
fprintf(stderr, " ");
}
}
sp_count--;
pCount++;
}
fprintf(stderr, "ERROR %d: %s On Line %d.\n\n", comp_num, err_msg, line_num);
exit(EXIT_FAILURE);
}
void line_notification(const int line_num)
{
fprintf(stderr, "ERROR FOUND ON LINE: %d.\n", line_num);
}