-
Notifications
You must be signed in to change notification settings - Fork 9
/
error.cpp
148 lines (124 loc) · 2.7 KB
/
error.cpp
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
#include <stdio.h> // printf()
#include <stdlib.h> // exit()
#include "common.h"
#include "tokens.h"
#include "ast.h"
#include "typecheck.h"
extern config_t config;
extern location_t loc;
extern const char *filename;
// IMPORTANT: The decisions below may _significantly_ slow down compilation.
// Essentialy, we're left off with bad C++... There are 2 choices. Either this
// or printf-like functions, which are horrible as well.
static void log(int i) {
printf("%d", i);
}
static void log(size_t s) {
printf("%zd", s);
}
static void log(char c) {
printf("%c", c);
}
static void log(const char *s) {
printf("%s", s);
}
static void log(location_t loc) {
printf("%s: %d: ", filename, loc.ln);
}
static void log(TOK kind) {
printf("%s", token_name(kind));
}
static void log(token_t tok) {
printf("%s", token_name(tok.kind));
if (tok.kind == TOK::INTLIT) {
printf(" %d", tok.val);
} else if (tok.kind == TOK::ID) {
printf(" %s", tok.id);
}
}
static void log(EXPR kind) {
printf("%s", expr_name(kind));
}
static void red_on() {
if (config.ansi_style) {
printf("\x1b[31m");
}
}
static void yellow_on() {
if (config.ansi_style) {
printf("\x1b[93m");
}
}
static void yellow_off() {
if (config.ansi_style) {
printf("\x1b[0m");
}
}
static void red_off() {
if (config.ansi_style) {
printf("\x1b[0m");
}
}
static void bold_on() {
if (config.ansi_style) {
printf("\x1b[1m");
}
}
static void bold_off() {
if (config.ansi_style) {
printf("\x1b[0m");
}
}
// TODO: Find a way to iteratve over instead
// of recurse.
template <typename T, typename... Args>
static void log(T t, Args... args) {
log(t);
log(args...);
}
template <typename T, typename... Args>
static void debug_log(T t, Args... args) {
if (config.log) {
log(t);
log(args...);
}
}
template <typename... Args>
static void internal_compiler_error(Args... args) {
bold_on();
printf("Internal Compiler Error: ");
bold_off();
log(args...);
printf("\n");
}
template <typename... Args>
static void warning(Args... args) {
log(loc);
bold_on();
printf(" Warning: ");
bold_off();
log(args...);
printf("\n");
}
template <typename... Args>
static void syntax_error_no_ln(Args... args) {
log(loc);
red_on();
bold_on();
log(" Syntax Error: ");
bold_off();
red_off();
log(args...);
}
template <typename... Args>
static void syntax_error(Args... args) {
syntax_error_no_ln(args...);
printf("\n");
}
template <typename... Args>
static void fatal_error(Args... args) {
printf("Fatal error: ");
log(args...);
printf("\n");
exit(1);
}