-
Notifications
You must be signed in to change notification settings - Fork 15
/
error.c
125 lines (101 loc) · 1.54 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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "compiler.h"
/* Eventually remove stdio usage */
static char buf[20];
static char *uitoa(unsigned int i)
{
register char *p = buf + sizeof(buf);
register int c;
*--p = '\0';
do {
c = i % 10;
i /= 10;
*--p = '0' + c;
} while(i);
return p;
}
unsigned errors;
static void writes(const char *p)
{
write(2, p, strlen(p));
}
static void writec(const char c)
{
write(2, &c, 1);
}
static void writeval(unsigned n)
{
writes(uitoa(n));
}
void format_error(unsigned line, const char *p, const unsigned c)
{
writes(filename);
writec(':');
writeval(line);
writes(" - ");
writes(p);
if (c) {
writes(" \"");
writec(c);
writec('"');
}
writec('\n');
}
void warningline(unsigned line, const char *p)
{
format_error(line, p, 0);
}
void warning(const char *p)
{
format_error(line_num, p, 0);
}
void errorline(unsigned line, const char *p)
{
format_error(line, p, 0);
errors++;
}
void error(const char *p)
{
format_error(line_num, p, 0);
errors++;
}
void fatal(const char *p)
{
error(p);
exit(255);
}
void errorc(const unsigned c, const char *p)
{
format_error(line_num, p, c);
errors++;
}
void needlval(void)
{
error("lvalue required");
}
void badtype(void)
{
error("bad type");
}
void indirections(void)
{
error("too many indirections");
}
void typemismatch(void)
{
error("type mismatch");
}
void invalidtype(void)
{
error("invalid type");
}
void divzero(void)
{
warning("division by zero");
}
void notconst(void)
{
error("must evaluate to a constant");
}