-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaedalusCall.h
643 lines (532 loc) · 14.8 KB
/
DaedalusCall.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#include <expected>
#include <unordered_map>
#include <optional>
#include <vector>
#include <ranges>
#include <algorithm>
#include <array>
#define DCLikely [[likely]]
#define DCUnlikely [[unlikely]]
namespace GOTHIC_ENGINE
{
enum class eCallFuncError
{
WRONG_SYMBOL,
WRONG_ARGS_SIZE,
WRONG_ARG_TYPE,
WRONG_RET_VAL
};
enum class eClearStack
{
CLEAR,
NO
};
struct DaedalusVoid {};
struct IgnoreReturn {};
struct DCStringView
{
explicit constexpr DCStringView(const char* t_str)
: m_view(t_str)
{
}
explicit constexpr DCStringView(const char* t_str, const size_t t_num)
: m_view(t_str, t_num)
{
}
constexpr DCStringView(std::string_view t_view)
: m_view(t_view)
{
}
constexpr DCStringView(const std::string& t_str)
: m_view(t_str)
{
}
DCStringView(DCStringView&&) = default;
DCStringView(const DCStringView&) = default;
constexpr auto& operator=(const auto& t_str)
{
m_view = t_str;
return *this;
}
operator std::string_view() const
{
return m_view;
}
std::string_view m_view;
};
//same array is used in zSTRING::ToUpper
inline constexpr std::array<unsigned char, 256> ToUpperArray =
{
0,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,'A','B','C','D','E',
'F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y',
'Z', 91, 92, 93, 94, 95, 96,'A','B','C',
'D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W',
'X','Y','Z',123,124,125,126,127,128,154,
144,182,142,183,143,128,210,211,212,216,
215,221,142,143,144,146,226,153,227,234,
235,190,152,153,154,157,156,157,158,159,
181,214,224,233,165,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,198,
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,229,229,
230,232,232,233,208,235,237,237,238,239,
240,241,242,243,244,245,246,247,248,249,
250,251,252,253,254,255
};
template<std::size_t N>
struct DCFixedStr
{
using CharArray = const char(&)[N];
std::array<char, N> m_array;
consteval DCFixedStr(const CharArray t_array)
{
for (size_t i = 0; i < N; i++)
{
m_array[i] = ToUpperArray[static_cast<unsigned char>(t_array[i])];
}
}
constexpr size_t Size() const
{
return N;
}
constexpr operator DCStringView() const
{
return DCStringView(m_array.data(), N);
}
};
template<size_t N>
consteval auto DCFunction(const char(&t_str)[N])
{
return DCFixedStr{ t_str };
}
constexpr auto DCFunction(const auto& t_str)
{
return DCStringView{ t_str };
}
static inline constexpr char CharToUpperSimple(const char t_char)
{
return static_cast<char>(ToUpperArray[static_cast<unsigned char>(t_char)]);
}
//use Zengin way to get upper string
inline constexpr std::string StrViewToUpperZengin(const std::string_view t_name)
{
return t_name | std::views::transform(CharToUpperSimple) | std::ranges::to<std::string>();
}
template<bool ToUpper = true>
inline constexpr int ParserGetIndex(zCParser* const t_parser, std::string_view t_name)
{
//TODO use better way
[[maybe_unused]]
const auto upperName = ToUpper ? StrViewToUpperZengin(t_name) : std::string{};
if constexpr (ToUpper)
{
t_name = std::string_view{ upperName };
}
const auto& symbTab = t_parser->symtab.table;
for (int i = 0; i < symbTab.GetNum(); i++)
{
if (std::string_view(symbTab[i]->name, symbTab[i]->name.Length()) == t_name)
{
return i;
}
}
return -1;
}
inline constexpr zCPar_Symbol* ParserGetSymbol(zCParser* const t_parser, const int t_index)
{
if (t_index < 0 || t_index >= t_parser->symtab.table.GetNum())
{
return {};
}
return t_parser->symtab.table[t_index];
}
inline constexpr zCPar_Symbol* ParserGetSymbol(zCParser* const t_parser, const std::string_view t_name)
{
const auto index = ParserGetIndex(t_parser, t_name);
if (index == -1)
{
return {};
}
return t_parser->symtab.table[index];
}
struct DaedalusFunction
{
explicit constexpr DaedalusFunction(const int t_index)
: m_index(t_index)
{
};
zCPar_Symbol* GetSymbol(zCParser* const t_parser) const
{
auto const symb = ParserGetSymbol(t_parser, m_index);
if (!symb)
{
return nullptr;
}
if (symb->type != zPAR_TYPE_FUNC)
//|| !(symb->flags & zPAR_FLAG_CONST))
{
return nullptr;
}
return symb;
}
constexpr std::strong_ordering operator<=>(const DaedalusFunction& t_function) const = default;
int m_index{ -1 };
};
template<typename T>
concept DaedalusData =
std::is_same_v<std::decay_t<T>, int>
|| std::is_same_v<std::decay_t<T>, DaedalusFunction>
|| std::is_same_v<std::decay_t<T>, float>
|| std::is_same_v<std::decay_t<T>, zSTRING>
|| (std::is_pointer_v<std::decay_t<T>> && !std::is_pointer_v<std::remove_pointer_t<T>>);
template<typename T>
concept DaedalusReturn =
std::is_same_v<T, int>
|| std::is_same_v<T, DaedalusFunction>
|| std::is_same_v<T, float>
|| std::is_same_v<T, zSTRING>
|| std::is_same_v<T, DaedalusVoid>
|| std::is_same_v<T, IgnoreReturn>
|| (std::is_pointer_v<std::decay_t<T>> && !std::is_pointer_v<std::remove_pointer_t<T>>);
struct CallFuncContext
{
CallFuncContext(zCParser* const t_par, const DaedalusFunction t_function)
: m_parser(t_par),
m_function(t_function)
{
m_symbol = ParserGetSymbol(m_parser, t_function.m_index);
}
template<DaedalusReturn T>
static inline constexpr int TypeToEnum()
{
if constexpr (std::is_same_v<T, int>)
{
return zPAR_TYPE_INT;
}
else if constexpr (std::is_same_v<T, DaedalusFunction>)
{
return zPAR_TYPE_FUNC;
}
else if constexpr (std::is_same_v<T, zSTRING>)
{
return zPAR_TYPE_STRING;
}
else if constexpr (std::is_same_v<T, float>)
{
return zPAR_TYPE_FLOAT;
}
else if constexpr (std::is_pointer_v<T>)
{
return zPAR_TYPE_INSTANCE;
}
else if constexpr (std::is_same_v<T, DaedalusVoid>)
{
return zPAR_TYPE_VOID;
}
}
template<DaedalusReturn T>
static inline constexpr int ShouldReturn()
{
return !(std::is_same_v<T, DaedalusVoid> || std::is_same_v<T, IgnoreReturn>);
}
template<DaedalusData... Args>
inline bool CheckAllTypes() const
{
size_t counter{};
bool valid{ true };
(((!CheckType<Args>(counter++)
? (valid = false, false) : true)
&& ...));
return valid;
}
inline void PushOne(DaedalusData auto&& t_argument, [[maybe_unused]] const size_t t_index) const
{
using ArgType = std::decay_t<decltype(t_argument)>;
if constexpr (std::is_pointer_v<ArgType>)
{
auto const argumentSymbol = ParserGetSymbol(m_parser, t_index);
argumentSymbol->offset = reinterpret_cast<int>(t_argument);
m_parser->datastack.Push(t_index);
}
else if constexpr (std::is_same_v<ArgType, int>)
{
m_parser->datastack.Push(t_argument);
m_parser->datastack.Push(zPAR_TOK_PUSHINT);
}
else if constexpr (std::is_same_v<ArgType, DaedalusFunction>)
{
m_parser->datastack.Push(t_argument.m_index);
m_parser->datastack.Push(zPAR_TOK_PUSHINT);
}
else if constexpr (std::is_same_v<ArgType, float>)
{
m_parser->datastack.Push(std::bit_cast<int>(t_argument));
m_parser->datastack.Push(zPAR_TOK_PUSHINT);
}
else if constexpr (std::is_same_v<ArgType, zSTRING>)
{
m_parser->datastack.Push(reinterpret_cast<int>(&t_argument));
m_parser->datastack.Push(zPAR_TOK_PUSHSTR);
}
}
template<DaedalusReturn T>
inline auto ReturnScriptValue() const
{
if constexpr (std::is_same_v<T, DaedalusVoid>)
{
return {};
}
else if constexpr (std::is_pointer_v<T>)
{
//TODO check index?
return static_cast<T>(m_parser->GetInstance());
}
else if constexpr (std::is_same_v<T, zSTRING>)
{
return std::cref(*m_parser->PopString());
}
else if constexpr (std::is_same_v<T, int>)
{
return m_parser->PopDataValue();
}
else if constexpr (std::is_same_v<T, float>)
{
return m_parser->PopFloatValue();
}
}
inline void PopReturnValue() const
{
switch (m_symbol->offset)
{
case zPAR_TYPE_INT:
(void)ReturnScriptValue<int>();
break;
case zPAR_TYPE_STRING:
(void)ReturnScriptValue<zSTRING>();
break;
case zPAR_TYPE_FLOAT:
(void)ReturnScriptValue<float>();
break;
case zPAR_TYPE_INSTANCE:
(void)ReturnScriptValue<void*>();
break;
case zPAR_TYPE_VOID:
break;
default:
break;
}
}
template<DaedalusReturn T>
inline bool CheckType(const size_t t_offset) const
{
return m_parser->symtab.table[m_function.m_index + t_offset]->type == static_cast<unsigned int>(TypeToEnum<T>());
}
template<DaedalusReturn T, DaedalusData... Args>
inline std::optional<eCallFuncError> CheckDaedalusCallError() const
{
if (!m_symbol)
{
return eCallFuncError::WRONG_SYMBOL;
}
if (sizeof...(Args) != m_symbol->ele)
{
return eCallFuncError::WRONG_ARGS_SIZE;
}
const bool hasReturn = (m_symbol->flags & zPAR_FLAG_RETURN) != 0;
if (!hasReturn)
{
if constexpr (ShouldReturn<T>())
{
return eCallFuncError::WRONG_RET_VAL;
}
}
if constexpr (!std::is_same_v<T, IgnoreReturn>)
{
if (m_symbol->offset != TypeToEnum<std::decay_t<T>>())
{
return eCallFuncError::WRONG_RET_VAL;
}
}
if constexpr (sizeof...(Args))
{
if (!CheckAllTypes<Args...>())
{
return eCallFuncError::WRONG_ARG_TYPE;
}
}
return {};
}
zCParser* m_parser;
zCPar_Symbol* m_symbol;
DaedalusFunction m_function;
};
struct string_hash
{
using is_transparent = void;
[[nodiscard]] size_t operator()(const char* txt) const
{
return std::hash<std::string_view>{}(txt);
}
[[nodiscard]] size_t operator()(std::string_view txt) const
{
return std::hash<std::string_view>{}(txt);
}
[[nodiscard]] size_t operator()(const std::string& txt) const
{
return std::hash<std::string>{}(txt);
}
};
class CallFuncStringCache
{
public:
CallFuncStringCache(const zCParser* t_parser)
: m_parser(t_parser)
{
}
__declspec(noinline) void Add(std::string t_functionName, const DaedalusFunction t_function)
{
m_cache.emplace(std::move(t_functionName), t_function);
}
[[nodiscard]] std::optional<DaedalusFunction> FindCache(const std::string_view t_name) const noexcept
{
if (const auto it = m_cache.find(t_name);
it != std::end(m_cache))
{
return it->second;
}
return {};
}
[[nodiscard]] static CallFuncStringCache& Get(const zCParser* t_parser)
{
const auto SameKey = [t_parser](const auto& t_object)
{
return t_object.m_parser == t_parser;
};
auto& value = [&]() -> CallFuncStringCache&
{
if (const auto it = std::ranges::find_if(s_cache, SameKey);
it != std::end(s_cache)) DCLikely
{
return *it;
}
else DCUnlikely
{
return s_cache.emplace_back(CallFuncStringCache{t_parser});
}
}();
return value;
}
private:
const zCParser* m_parser;
std::unordered_map<std::string, DaedalusFunction, string_hash, std::equal_to<>> m_cache;
inline static std::vector<CallFuncStringCache> s_cache;
};
//TODO do not copy zSTRINGS
template<DaedalusReturn T = IgnoreReturn, bool SafeCall = true>
std::expected<T, eCallFuncError> DaedalusCall(zCParser* const t_par, const DaedalusFunction t_function, const eClearStack t_clearStack, DaedalusData auto... t_args)
{
const CallFuncContext contex{ t_par,t_function };
if constexpr (SafeCall)
{
if (const auto error = contex.CheckDaedalusCallError<T, std::decay_t<decltype(t_args)>...>();
error.has_value())
{
return std::unexpected{ *error };
}
}
if (t_clearStack == eClearStack::CLEAR)
{
t_par->datastack.Clear();
}
auto PushFunc = [&, counter = 0]() mutable
{
((contex.PushOne(std::move(t_args), t_function.m_index + counter++)), ...);
};
PushFunc();
if (contex.m_symbol->flags & zPAR_FLAG_EXTERNAL) DCUnlikely
{
const auto cur_par = zCParser::cur_parser;
const auto cur_instance = zCPar_Symbol::instance_sym;
zCParser::cur_parser = t_par;
auto const Func = reinterpret_cast<int(*)()>(contex.m_symbol->single_intdata);
Func();
zCParser::cur_parser = cur_par;
zCPar_Symbol::SetUseInstance(cur_instance);
}
else DCLikely
{
t_par->DoStack(contex.m_symbol->single_intdata);
}
if constexpr (std::is_same_v<T, IgnoreReturn>)
{
contex.PopReturnValue();
return{};
}
else
{
return std::expected<T, eCallFuncError>{ contex.ReturnScriptValue<T>() };
}
}
template<DaedalusReturn T = IgnoreReturn, bool Cache = true, bool Upper = true>
constexpr std::expected<T, eCallFuncError> DaedalusCall(zCParser* const t_par, DCStringView t_name, const eClearStack t_clearStack, DaedalusData auto... t_args)
{
if constexpr (Cache == false)
{
return DaedalusCall<T, true>(t_par, ParserGetIndex<Upper>(t_par, t_name), t_clearStack, std::move(t_args)...);
}
//TODO simplify
[[maybe_unused]]
std::string upper{};
if constexpr (Upper)
{
upper = StrViewToUpperZengin(t_name);
t_name = upper;
}
auto& cache = CallFuncStringCache::Get(t_par);
auto index = cache.FindCache(t_name).value_or(DaedalusFunction{ -1 });
if (index == DaedalusFunction{ -1 })
{
const auto callError = [&]() -> std::optional<eCallFuncError>
{
index = DaedalusFunction{ ParserGetIndex<false>(t_par, t_name) };
if (const auto error = CallFuncContext{ t_par,index }.CheckDaedalusCallError<T, std::decay_t<decltype(t_args)>...>();
error.has_value())
{
return error;
}
cache.Add(Upper ? std::move(upper) : std::string{ t_name }, index);
return{};
}();
if (callError.has_value())
{
return std::unexpected{ *callError };
}
}
return DaedalusCall<T, false>(t_par, index, t_clearStack, std::move(t_args)...);
}
template<DaedalusReturn T = IgnoreReturn, bool Cache = true, typename ZSTR = zSTRING>
//hack for implicit zSTRING conversion
requires(std::same_as<ZSTR, zSTRING>)
__forceinline constexpr std::expected<T, eCallFuncError> DaedalusCall(zCParser* const t_par, const ZSTR& t_name, const eClearStack t_clearStack, DaedalusData auto... t_args)
{
return DaedalusCall<T, Cache>(t_par, std::string_view{ t_name.ToChar(), static_cast<size_t>(t_name.Length()) }, t_clearStack, std::move(t_args)...);
}
template<DaedalusReturn T = IgnoreReturn, size_t N>
__forceinline constexpr std::expected<T, eCallFuncError> DaedalusCall(zCParser* const t_par, const DCFixedStr<N> t_name, const eClearStack t_clearStack, DaedalusData auto... t_args)
{
return DaedalusCall<T, true, false>(t_par, t_name, t_clearStack, std::move(t_args)...);
}
}
#undef DCLikely
#undef DCUnlikely