-
Notifications
You must be signed in to change notification settings - Fork 0
/
define.cpp
174 lines (171 loc) · 5.05 KB
/
define.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "define.h"
auto Class_Function(std::string &prettyFun) -> std::string {
unsigned int i = 0;
while (prettyFun[i++] != '(');
i -= 2;
unsigned int j = i;
while (j != 0)
if (prettyFun[j--] == ' ')break;
j == 0 ?: j += 2;
std::string tmp;
for (unsigned int k = j; k <= i; k++) {
tmp.push_back(prettyFun[k]);
}
return tmp;
}
bool suffix(const std::string &src, const std::string &s) {
bool tmp = false;
if (src.size() <= s.size())return false;
for (UInt64 i = src.size() - 1; i >= src.size() - s.size(); i--)
tmp |= src[i] != s[i - src.size() + s.size()];
return !tmp;
}
bool prefix(const std::string &src, const std::string &s) {
bool tmp = false;
if (src.size() <= s.size())return false;
for (UInt64 i = 0; i < s.size(); i++)
tmp |= src[i] != s[i];
return !tmp;
}
bool isInteger(const std::string &num) {
UInt64 len = 0;
while (num[len++]);
len--;
if ((len == 1) && (('0' <= num[0]) && (num[0] <= '9')))
return true;
if (('1' <= num[0]) && (num[0] <= '9')) {
UInt8 end = 0;
if (suffix(num, "ull"))end = 3;
else if (suffix(num, "ul") || suffix(num, "ll"))end = 2;
else if (suffix(num, "l") || suffix(num, "u"))end = 1;
for (UInt64 i = 1; i < len - end; i++) {
if (!(('0' <= num[i]) && (num[i] <= '9')))return false;
}
} else return false;
return true;
}
bool isFloating(const std::string &num) {
UInt64 len = 0;
while (num[len++]);
len--;
if ((len == 1) && (('0' <= num[0]) && (num[0] <= '9')))
return true;
bool decimalPoint = false;
UInt64 decimalIndex = 0;
for (UInt64 i = 1; i < len; i++) {
if (num[i] == '.') {
if (!decimalPoint) {
decimalPoint = true;
decimalIndex = i;
} else return false;
}
}
std::string integer;
integer.append(num, decimalIndex == 0 ? len : decimalIndex);
if (isInteger(integer)) {
UInt8 end = 0;
if (suffix(num, "l") || suffix(num, "f"))end = 1;
for (UInt64 i = decimalIndex + 1; i < len - end; i++) {
if (!(('0' <= num[i]) && (num[i] <= '9')))return false;
}
} else
return false;
return true;
}
bool isNumber(const std::string& num) {
UInt64 len = 0;
while (num[len++]);
len--;
bool tmp = isInteger(num) || isFloating(num);
if (!tmp) {
UInt8 end = 0;
if (suffix(num, "ull"))end = 3;
else if (suffix(num, "ul") || suffix(num, "ll"))end = 2;
else if (suffix(num, "l") || suffix(num, "u"))end = 1;
if (prefix(num, "0x")) {
for (UInt64 i = 2; i < len - end; i++) {
if (
(!(('0' <= num[i]) && (num[i] <= '9'))) &&
(!(('a' <= num[i]) && (num[i] <= 'f'))) &&
(!(('A' <= num[i]) && (num[i] <= 'Z')))
)
return false;
}
return true;
} else if (prefix(num, "0b")) {
for (UInt64 i = 2; i < len - end; i++) {
if ((!((num[i] == '0') || (num[i] == '1'))))
return false;
}
return true;
} else if (prefix(num, "0")) {
for (UInt64 i = 1; i < len - end; i++) {
if (!(('0' <= num[i]) && (num[i] <= '7')))return false;
}
return true;
}
} else return true;
return false;
}
bool isASymbol(char c) {
return (
(
((33 <= c) && (c <= 47)) ||
((58 <= c) && (c <= 64)) ||
((91 <= c) && (c <= 96)) ||
((123 <= c) && (c <= 126))
) && (c != '_')
);
}
std::string UInt64ToString(UInt64 num) {
std::string tmp;
if (num == 0)tmp = "0";
while (num != 0) {
tmp += (char) ((num % 10) + 48);
num /= 10;
}
std::string ans;
for (UInt64 i = tmp.size() - 1; i > 0; i--)
ans += tmp[i];
ans += tmp[0];
return ans;
}
std::string toString(TokenType tp) {
if (tp == tt_identifier) {
return "标识符";
}
if (tp == tt_BInteger) {
return "2进制整数";
}
if (tp == tt_OInteger) {
return "8进制整数";
}
if (tp == tt_DInteger) {
return "10进制整数";
}
if (tp == tt_HInteger) {
return "16进制整数";
}
if (tp == tt_float) {
return "浮点数";
}
if (tp == tt_scienceNumber) {
return "科学计数法";
}
if (tp == tt_symbol) {
return "符号";
}
if (tp == tt_string) {
return "字符串";
}
if (tp == tt_char) {
return "字符";
}
if (tp == tt_noe) {
return "空";
}
return "未知类型";
}
bool isACharNum(Int8 c) {
return (isalnum(c) || (c == '_') || ((-128 <= c) && (c <= -1)));
}