-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtypes.h
More file actions
263 lines (244 loc) · 5.9 KB
/
types.h
File metadata and controls
263 lines (244 loc) · 5.9 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//
// Project: CMiniLang
// Author: bajdcc
//
#ifndef CMINILANG_TYPES_H
#define CMINILANG_TYPES_H
#include <string>
#include <unordered_map>
using string_t = std::string;
template<class K, class V> using map_t = std::unordered_map<K, V>;
namespace clib {
#if __APPLE__ && __MACH__
using int8 = int8_t;
using uint8 = uint8_t;
using int16 = int16_t;
using uint16 = uint16_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int64 = int64_t;
using uint64 = uint64_t;
#else
using int8 = signed __int8;
using uint8 = unsigned __int8;
using int16 = signed __int16;
using uint16 = unsigned __int16;
using int32 = signed __int32;
using uint32 = unsigned __int32;
using int64 = signed __int64;
using uint64 = unsigned __int64;
#endif
// 这里暂不支持64位程序
#if __x86_64__
#warning "Not support x86_64"
#endif
#if 1
using sint = int32;
using uint = uint32;
using slong = long long;
using ulong = unsigned long long;
#else
using sint = int64;
using uint = uint64;
using slong = long;
using ulong = unsigned long;
#endif
using byte = uint8;
using size_t = uint;
enum lexer_t {
l_none,
l_ptr,
l_error,
l_char,
l_uchar,
l_short,
l_ushort,
l_int,
l_uint,
l_long,
l_ulong,
l_float,
l_double,
l_operator,
l_keyword,
l_identifier,
l_string,
l_comment,
l_space,
l_newline,
l_end,
};
enum keyword_t {
k__start,
k_auto,
k_bool,
k_break,
k_case,
k_char,
k_const,
k_continue,
k_default,
k_do,
k_double,
k_else,
k_enum,
k_extern,
k_false,
k_float,
k_for,
k_goto,
k_if,
k_int,
k_long,
k_register,
k_return,
k_short,
k_signed,
k_sizeof,
k_static,
k_struct,
k_switch,
k_true,
k_typedef,
k_union,
k_unsigned,
k_void,
k_volatile,
k_while,
k__end
};
enum operator_t {
op__start,
op_assign,
op_equal,
op_plus,
op_plus_assign,
op_minus,
op_minus_assign,
op_times,
op_times_assign,
op_divide,
op_div_assign,
op_bit_and,
op_and_assign,
op_bit_or,
op_or_assign,
op_bit_xor,
op_xor_assign,
op_mod,
op_mod_assign,
op_less_than,
op_less_than_or_equal,
op_greater_than,
op_greater_than_or_equal,
op_logical_not,
op_not_equal,
op_escape,
op_query,
op_bit_not,
op_lparan,
op_rparan,
op_lbrace,
op_rbrace,
op_lsquare,
op_rsquare,
op_comma,
op_dot,
op_semi,
op_colon,
op_plus_plus,
op_minus_minus,
op_logical_and,
op_logical_or,
op_pointer,
op_left_shift,
op_right_shift,
op_left_shift_assign,
op_right_shift_assign,
op_ellipsis,
op__end,
};
enum error_t {
e__start,
e_invalid_char,
e_invalid_operator,
e_invalid_comment,
e_invalid_digit,
e_invalid_string,
e__end
};
template<lexer_t>
struct base_t {
using type = void *;
};
template<class T>
struct base_lexer_t {
static const lexer_t type = l_none;
};
#define DEFINE_BASE_TYPE(t, obj) \
template<> \
struct base_t<t> \
{ \
using type = obj; \
static const int size = sizeof(obj); \
};
DEFINE_BASE_TYPE(l_ptr, void*)
DEFINE_BASE_TYPE(l_char, char)
DEFINE_BASE_TYPE(l_uchar, unsigned char)
DEFINE_BASE_TYPE(l_short, short)
DEFINE_BASE_TYPE(l_ushort, unsigned short)
DEFINE_BASE_TYPE(l_int, int)
DEFINE_BASE_TYPE(l_uint, unsigned int)
DEFINE_BASE_TYPE(l_long, slong)
DEFINE_BASE_TYPE(l_ulong, ulong)
DEFINE_BASE_TYPE(l_float, float)
DEFINE_BASE_TYPE(l_double, double)
DEFINE_BASE_TYPE(l_operator, operator_t)
DEFINE_BASE_TYPE(l_keyword, keyword_t)
DEFINE_BASE_TYPE(l_identifier, string_t)
DEFINE_BASE_TYPE(l_string, string_t)
DEFINE_BASE_TYPE(l_comment, string_t)
DEFINE_BASE_TYPE(l_space, uint)
DEFINE_BASE_TYPE(l_newline, uint)
DEFINE_BASE_TYPE(l_error, error_t)
#undef DEFINE_BASE_TYPE
#define DEFINE_CONV_TYPE(t, obj) \
template<> \
struct base_lexer_t<obj> \
{ \
static const lexer_t type = t; \
};
DEFINE_CONV_TYPE(l_ptr, void*)
DEFINE_CONV_TYPE(l_char, char)
DEFINE_CONV_TYPE(l_uchar, unsigned char)
DEFINE_CONV_TYPE(l_short, short)
DEFINE_CONV_TYPE(l_ushort, unsigned short)
DEFINE_CONV_TYPE(l_int, int)
DEFINE_CONV_TYPE(l_uint, unsigned int)
DEFINE_CONV_TYPE(l_long, slong)
DEFINE_CONV_TYPE(l_ulong, ulong)
DEFINE_CONV_TYPE(l_float, float)
DEFINE_CONV_TYPE(l_double, double)
DEFINE_CONV_TYPE(l_string, string_t)
DEFINE_CONV_TYPE(l_error, error_t)
#undef DEFINE_CONV_TYPE
const string_t &lexer_typestr(lexer_t);
const string_t &lexer_keywordstr(keyword_t);
const string_t &lexer_opstr(operator_t);
const string_t &lexer_opnamestr(operator_t);
const string_t &lexer_errstr(error_t);
int lexer_operatorpred(operator_t);
int lexer_op2ins(operator_t);
extern string_t keyword_string_list[];
#define LEX_T(t) base_t<l_##t>::type
#define LEX_CONV_T(t) base_lexer_t<t>::type
#define LEX_SIZEOF(t) base_t<l_##t>::size
#define LEX_STRING(t) lexer_typestr(t)
#define KEYWORD_STRING(t) lexer_keywordstr(t)
#define OPERATOR_STRING(t) lexer_opnamestr(t)
#define OP_STRING(t) lexer_opstr(t)
#define ERROR_STRING(t) lexer_errstr(t)
#define OPERATOR_PRED(t) lexer_operatorpred(t)
#define OP_INS(t) lexer_op2ins(t)
}
#endif //CMINILANG_TYPES_H