-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.h
59 lines (48 loc) · 1.84 KB
/
error.h
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
//
// Created by Kevin Tan on 2021/10/23.
//
#ifndef CODE_ERROR_H
#define CODE_ERROR_H
#include <iostream>
#include <queue>
#define ERROR_EXPECTED_GOT(expected, got) do { cerr << "In " << __func__ << " line " << __LINE__ << " source code line " << (*(got))->line << ", expected "#expected", got " << **(got) << endl; exit(-1); } while (0)
#define ERROR_NOT_SUPPORTED(got) do { cerr << "In " << __func__ << " line " << __LINE__ << ", "#got" is not supported." << endl; exit(-1); } while (0)
#define ERROR_LIMITED_SUPPORT_WITH_LINE(line, support) do { cerr << "In " << __func__ << " line " << __LINE__ << " source code line " << (line) << ", only supports "#support << endl; exit(-1); } while (0)
#define ERROR_LIMITED_SUPPORT(support) do { cerr << "In " << __func__ << " line " << __LINE__ << ", only supports "#support << endl; exit(-1); } while (0)
using namespace std;
enum class ErrorCode {
ILLEGAL_CHAR,
IDENT_REDEFINED,
IDENT_UNDEFINED,
PARAM_AMOUNT_MISMATCH,
PARAM_TYPE_MISMATCH,
RETURN_TYPE_MISMATCH,
MISSING_RETURN,
CANNOT_MODIFY_CONST,
MISSING_SEMICN,
MISSING_RPAREN,
MISSING_RBRACK,
FORMAT_STRING_ARGUMENT_MISMATCH,
BREAK_CONTINUE_NOT_IN_LOOP
};
using Pair = pair<int, char>;
using heapq = priority_queue<Pair, vector<Pair>, bool (*)(Pair &, Pair &)>;
class Error {
static bool cmp(Pair &a, Pair &b) {
return a.first == b.first ? a.second > b.second : a.first > b.first;
}
public:
heapq errors = heapq(cmp);
inline void operator()(ErrorCode err, int line) {
errors.emplace(line, 'a' + (char) err);
}
friend ostream &operator<<(ostream &out, Error &e) {
while (!e.errors.empty()) {
auto top = e.errors.top();
out << top.first << ' ' << top.second << endl;
e.errors.pop();
}
return out;
}
};
#endif //CODE_ERROR_H