-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.h
362 lines (312 loc) · 9.91 KB
/
header.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <initializer_list>
#include <variant>
#include <cassert>
#include <cstdio>
#include <cstring>
#include "fix32.h"
#include <unordered_map>
#include <functional>
#include <memory>
using namespace z8;
#define assertm(exp, msg) assert(((void)msg, exp))
// must match with variant type order
const uint8_t TT_NUM = 0;
const uint8_t TT_STR = 1;
const uint8_t TT_TAB = 2;
const uint8_t TT_NULL = 3;
const uint8_t TT_BOOL = 4;
const uint8_t TT_FN = 5;
class TValue;
class SpecialTable;
void print(const TValue t);
class FWrapper {
// this std::function is 24 bytes, which makes the variant 24 bytes
// which makes every number / etc 24 bytes. due to the million unnecessary copies
// that are currently triggered, it makes some actions significantly slower
// Wrapping it in a pointer makes function accesses slower, but those are infrequent
public:
using ftype = std::function<TValue(std::vector<TValue> args)>;
ftype func;
FWrapper(ftype val) : func(val) {}
};
class TValue {
public:
using func = std::function<TValue(std::vector<TValue> args)>;
using myvariant = std::variant<fix32,
const char*,
SpecialTable*,
std::nullptr_t,
bool,
FWrapper*
>;
myvariant data;
// size_t hash = 0;
operator int16_t() const {
return std::get<fix32>(data);
};
operator uint8_t() const {
//assertm(n<(int16_t)255, "Tried to cast >255 to int8_t");
//assertm(n>(int16_t)0, "Tried to cast <0 to int8_t");
return std::get<fix32>(data);
};
operator int8_t() const {
//assertm(n<(int16_t)128, "Tried to cast >128 to int8_t");
//assertm(n>(int16_t)-127, "Tried to cast <-127 to int8_t");
return std::get<fix32>(data);
};
inline operator fix32() const {
return std::get<fix32>(data);
};
operator const char*() const {
return std::get<const char*>(data);
};
inline operator bool() const {
if(data.index()==TT_NULL) return false;
if(data.index()==TT_BOOL) return std::get<bool>(data);
return true;
};
inline bool operator >(fix32 o) {
return std::get<fix32>(data) > o;
}
inline bool operator >(TValue o) {
return std::get<fix32>(data) > std::get<fix32>(o.data);
}
inline bool operator <(TValue o) {
return std::get<fix32>(data) < std::get<fix32>(o.data);
}
inline bool operator >=(fix32 o) {
return std::get<fix32>(data) >= o;
}
inline bool operator >(int o) {
return std::get<fix32>(data) > fix32(o);
}
inline bool operator >=(int o) {
return std::get<fix32>(data) >= fix32(o);
}
inline bool operator <=(int o) {
return std::get<fix32>(data) <= fix32(o);
}
inline bool operator <=(fix32 o) {
return std::get<fix32>(data) <= o;
}
inline bool operator <=(TValue o) {
return std::get<fix32>(data) <= std::get<fix32>(o.data);
}
inline bool operator <(fix32 o) {
return std::get<fix32>(data) < o;
}
inline bool operator ==(int o) {
return std::get<fix32>(data) == fix32(o);
}
inline bool operator ==(fix32 o) {
return std::get<fix32>(data) == o;
}
inline bool operator !=(fix32 o) {
return std::get<fix32>(data) != o;
}
inline TValue operator %(fix32 o) {
return std::get<fix32>(data) % o;
}
inline TValue operator *(fix32 o) {
return std::get<fix32>(data) * o;
}
inline TValue operator *(TValue o) {
return std::get<fix32>(data) * std::get<fix32>(o.data);
}
inline TValue operator +(TValue o) {
return TValue(std::get<fix32>(data) + std::get<fix32>(o.data));
}
inline TValue operator +(int o) {
return TValue(std::get<fix32>(data) + fix32(o));
}
inline TValue operator +(fix32 o) {
return TValue(std::get<fix32>(data) + o);
}
inline TValue operator -(fix32 o) {
return TValue(std::get<fix32>(data) - o);
}
inline TValue operator -() {
return TValue(-std::get<fix32>(data));
}
inline TValue operator /(fix32 o) {
return TValue(std::get<fix32>(data) / o);
}
inline TValue operator -=(fix32 o) {
data = std::get<fix32>(data) - o;
return *this;
}
inline TValue operator +=(fix32 o) {
data = std::get<fix32>(data) + o;
return *this;
}
inline TValue operator()(std::vector<TValue> args) {
return std::get<TT_FN>(data)->func(args);
}
bool operator ==(TValue other) {
if(data.index()!=other.data.index()) return false;
switch(data.index()) {
case TT_NUM:
return std::get<fix32>(data) == std::get<fix32>(other.data);
case TT_STR: // FIXME: by pointer identity
return std::get<const char*>(data) == std::get<const char*>(other.data);
case TT_BOOL:
return std::get<bool>(data) == std::get<bool>(other.data);
case TT_NULL:
return true;
case TT_TAB: // by pointer identity
return std::get<SpecialTable*>(data) == std::get<SpecialTable*>(other.data);
case TT_FN:
assertm(false, "Can't compare functions");
return false;
}
assertm(false, "Nothing to compare");
return false;
}
bool operator ==(const TValue other) const {
if(data.index()!=other.data.index()) return false;
switch(data.index()) {
case TT_NUM:
return std::get<fix32>(data) == std::get<fix32>(other.data);
case TT_STR: // FIXME: by pointer identity
return std::get<const char*>(data) == std::get<const char*>(other.data);
case TT_BOOL:
return std::get<bool>(data) == std::get<bool>(other.data);
case TT_NULL:
return true;
case TT_TAB: // by pointer identity
return std::get<SpecialTable*>(data) == std::get<SpecialTable*>(other.data);
case TT_FN:
assertm(false, "Can't compare functions");
return false;
}
assertm(false, "Nothing to compare");
return false;
}
TValue& operator=(fix32 val) {
data = val;
return *this;
}
TValue& operator=(int val) {
data = fix32(val);
return *this;
}
TValue& operator=(bool val) {
data = val;
return *this;
}
TValue& operator=(SpecialTable* val) {
data = val;
return *this;
}
TValue() {
data = nullptr;
}
TValue(fix32 val) {
data = val;
}
TValue(int val) {
data = fix32(val);
}
TValue(bool val) {
data = val ? 1 : 0;
}
TValue(const char* val) {
data = val;
}
TValue(SpecialTable* val) {
data = val;
}
TValue(std::function<TValue(std::vector<TValue>)> val) {
data = new FWrapper(val);
}
TValue& operator= (std::function<TValue(std::vector<TValue>)> val) {
data = new FWrapper(val);
return *this;
}
/*
TValue(TValue&& other) {
data = other.data;
other.data = nullptr;
}
TValue& operator=(TValue&& other) {
if(this != &other) {
data = other.data;
other.data = nullptr;
}
return *this;
}
*/
}; // TValue;
TValue operator* (fix32 x, const TValue& y)
{
return std::get<fix32>(y.data) * x;
}
TValue operator< (fix32 x, const TValue& y)
{
return std::get<fix32>(y.data) < x;
}
TValue operator< (TValue x, int o)
{
return std::get<fix32>(x.data) < fix32(o);
}
template<>
struct std::hash<TValue> {
inline std::size_t operator()(TValue const& s) const noexcept {
switch(s.data.index()) {
case TT_NUM:
return std::get<fix32>(s.data).bits();
case TT_TAB:
return (size_t)std::get<SpecialTable*>(s.data);
case TT_NULL:
return 0x5a5a5a5a;
case TT_BOOL:
return std::get<bool>(s.data);
case TT_STR:
{
auto c = std::get<const char*>(s.data);
return (c[0] | c[1] << 1); // unless the strings are empty; they always have 2 bytes+
}
case TT_FN:
assertm(false, "Did not match any tag on hash");
return 0;
}
assertm(false, "Did not match any tag on hash");
return 0;
}
};
class Table
{
public:
using pair = std::pair<TValue, TValue*>;
using cpair = std::pair<const TValue, TValue*>;
std::unordered_map<TValue, TValue*> fields;
Table* metatable = NULL;
uint16_t last_auto_index = 0;
void prepopulate(std::initializer_list<cpair> values) {
for(auto [key, val]: values) {
if(fields.count(key)) {
// pre-populated part of the map with a reference
// to a local class member
*fields[key] = *val;
} else {
// dynamic key
fields[key] = val;
}
}
}
};
TValue get_with_default(const std::vector<TValue>& v, uint8_t idx) {
if(idx>= v.size())
return TValue();
return v[idx];
}
inline TValue _and(TValue left, TValue right) {
// The operator and returns its first argument if it is false; otherwise, it returns its second argument.
if (!left) return left;
return right;
}
inline TValue _or(TValue left, TValue right) {
// The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
if (left) return left;
return right;
}