-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathlist_operations.cpp
More file actions
276 lines (255 loc) · 11.8 KB
/
list_operations.cpp
File metadata and controls
276 lines (255 loc) · 11.8 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
264
265
266
267
268
269
270
271
272
273
274
275
276
/* Copyright (c) 2024 Julian Benda
*
* This file is part of inkCPP which is released under MIT license.
* See file LICENSE.txt or go to
* https://github.com/JBenda/inkcpp for full license details.
*/
/// implements operations on lists
#include "stack.h"
#include "value.h"
#include "operations.h"
#include "list_table.h"
#define call4_Wrap(OP, RET, FUN) call4_Wrap_diff(OP, RET, RET, RET, RET, FUN)
#define call4_Wrap_diff(OP, RET0, RET1, RET2, RET3, FUN) \
void operation<Command::OP, value_type::list, void>::operator()( \
basic_eval_stack& stack, value* vals \
) \
{ \
call4(RET0, RET1, RET2, RET3, FUN); \
}
#define call4(RET0, RET1, RET2, RET3, FUN) \
if (vals[0].type() == value_type::list_flag) { \
if (vals[1].type() == value_type::list) { \
stack.push(value{}.set<value_type::RET0>( \
_list_table.FUN(vals[0].get<value_type::list_flag>(), vals[1].get<value_type::list>()) \
)); \
} else { \
inkAssert( \
vals[1].type() == value_type::list_flag, \
"list operation was called but second argument is not a list or list_flag" \
); \
stack.push(value{}.set<value_type::RET1>(_list_table.FUN( \
vals[0].get<value_type::list_flag>(), vals[1].get<value_type::list_flag>() \
))); \
} \
} else { \
inkAssert( \
vals[0].type() == value_type::list, \
"list operation was called but first argument is not a list or a list_flag!" \
); \
if (vals[1].type() == value_type::list) { \
stack.push(value{}.set<value_type::RET2>( \
_list_table.FUN(vals[0].get<value_type::list>(), vals[1].get<value_type::list>()) \
)); \
} else { \
inkAssert( \
vals[1].type() == value_type::list_flag, \
"list operation was called but second argument ist not a list or list_flag!" \
); \
stack.push(value{}.set<value_type::RET3>( \
_list_table.FUN(vals[0].get<value_type::list>(), vals[1].get<value_type::list_flag>()) \
)); \
} \
}
#define call2(OP, RET0, RET1, FUN) \
void operation<Command::OP, value_type::list, void>::operator()( \
basic_eval_stack& stack, value* vals \
) \
{ \
stack.push(value{}.set<value_type::RET0>(_list_table.FUN(vals[0].get<value_type::list>()))); \
} \
void operation<Command::OP, value_type::list_flag, void>::operator()( \
basic_eval_stack& stack, value* vals \
) \
{ \
stack.push(value{}.set<value_type::RET1>(_list_table.FUN(vals[0].get<value_type::list_flag>()) \
)); \
}
namespace ink::runtime::internal
{
void operation<Command::ADD, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
if (vals[1].type() == value_type::int32 || vals[1].type() == value_type::uint32) {
int i = vals[1].type() == value_type::int32
? vals[1].get<value_type::int32>()
: static_cast<int>(vals[1].get<value_type::uint32>());
inkAssert(
vals[0].type() == value_type::list,
"try to use list add function but value is not of type list"
);
stack.push(value{}.set<value_type::list>(_list_table.add(vals[0].get<value_type::list>(), i)));
} else {
call4(list, list, list, list, add);
}
}
void operation<Command::ADD, value_type::list_flag, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
inkAssert(
vals[0].type() == value_type::list_flag,
"try to use add function with list_flag results but first argument is not a list_flag!"
);
inkAssert(
vals[1].type() == value_type::int32 || vals[1].type() == value_type::uint32,
"try modify a list flag with a non intiger type!"
);
int i = vals[1].type() == value_type::int32 ? vals[1].get<value_type::int32>()
: static_cast<int>(vals[1].get<value_type::uint32>());
stack.push(
value{}.set<value_type::list_flag>(_list_table.add(vals[0].get<value_type::list_flag>(), i))
);
}
void operation<Command::SUBTRACT, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
if (vals[1].type() == value_type::int32 || vals[1].type() == value_type::uint32) {
int i = vals[1].type() == value_type::int32 ? vals[1].get<value_type::int32>()
: vals[1].get<value_type::uint32>();
inkAssert(
vals[0].type() == value_type::list,
"A in list resulting subtraction needs at leas one list as argument!"
);
stack.push(value{}.set<value_type::list>(_list_table.sub(vals[0].get<value_type::list>(), i)));
} else {
call4(list_flag, list_flag, list, list, sub);
}
}
void operation<Command::SUBTRACT, value_type::list_flag, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
inkAssert(
vals[0].type() == value_type::list_flag,
"subtraction resulting in list_flag needs a list_flag as first arguments!"
);
inkAssert(
vals[1].type() == value_type::int32 || vals[1].type() == value_type::uint32,
"Try to subtract non integer value from list_flag."
);
int i = vals[1].type() == value_type::int32 ? vals[1].get<value_type::int32>()
: vals[1].get<value_type::uint32>();
stack.push(
value{}.set<value_type::list_flag>(_list_table.sub(vals[0].get<value_type::list_flag>(), i))
);
}
void operation<Command::INTERSECTION, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
call4(list_flag, list_flag, list, list_flag, intersect);
}
call2(NOT, boolean, boolean, not_bool);
call2(LIST_COUNT, int32, int32, count);
call2(LIST_MIN, list_flag, list_flag, min);
call2(LIST_MAX, list_flag, list_flag, max);
call2(LIST_ALL, list, list, all);
call2(LIST_INVERT, list, list, invert);
call4_Wrap(LESS_THAN, boolean, less);
call4_Wrap(LESS_THAN_EQUALS, boolean, less_equal);
call4_Wrap(GREATER_THAN, boolean, greater);
call4_Wrap(GREATER_THAN_EQUALS, boolean, greater_equal);
call4_Wrap(IS_EQUAL, boolean, equal);
call4_Wrap(NOT_EQUAL, boolean, not_equal);
call4_Wrap(HAS, boolean, has);
call4_Wrap(HASNT, boolean, hasnt);
void operation<Command::lrnd, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
stack.push(
value{}.set<value_type::list_flag>(_list_table.lrnd(vals[0].get<value_type::list>(), _prng))
);
}
void operation<Command::lrnd, value_type::list_flag, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
stack.push(
value{}.set<value_type::list_flag>(_list_table.lrnd(vals[0].get<value_type::list_flag>()))
);
}
void operation<Command::LIST_INT, value_type::string, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
inkAssert(
vals[0].type() == value_type::string,
"list_flag construction needs the list name as string as first argument!"
);
inkAssert(
vals[1].type() == value_type::int32,
"list_flag construction needs the flag numeric value as second argument!"
);
list_flag entry = _list_table.get_list_id(vals[0].get<value_type::string>());
entry.flag = static_cast<int16_t>(vals[1].get<value_type::int32>());
entry = _list_table.external_fvalue_to_internal(entry);
stack.push(value{}.set<value_type::list_flag>(entry));
}
int get_limit(const value& val, const list_table& lists)
{
if (val.type() == value_type::int32) {
return val.get<value_type::int32>();
} else {
inkAssert(val.type() == value_type::list_flag, "flag value must be a integer or a list_flag");
return lists.get_flag_value(val.get<value_type::list_flag>());
}
}
void operation<Command::LIST_RANGE, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
inkAssert(vals[0].type() == value_type::list, "Can't get range of non list type!");
stack.push(value{}.set<value_type::list>(_list_table.range(
vals[0].get<value_type::list>(), get_limit(vals[1], _list_table),
get_limit(vals[2], _list_table)
)));
}
void convert_bools(value* vals, const list_table& lists, bool* res)
{
for (int i = 0; i < 2; ++i) {
switch (vals[i].type()) {
case value_type::list: res[i] = lists.to_bool(vals[i].get<value_type::list>()); break;
case value_type::list_flag:
res[i] = lists.to_bool(vals[i].get<value_type::list_flag>());
break;
default: res[i] = casting::numeric_cast<value_type::boolean>(vals[i]);
}
}
}
void operation<Command::AND, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
bool res[2];
convert_bools(vals, _list_table, res);
stack.push(value{}.set<value_type::boolean>(res[0] && res[1]));
}
void operation<Command::AND, value_type::list_flag, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
bool res[2];
convert_bools(vals, _list_table, res);
stack.push(value{}.set<value_type::boolean>(res[0] && res[1]));
}
void operation<Command::OR, value_type::list, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
bool res[2];
convert_bools(vals, _list_table, res);
stack.push(value{}.set<value_type::boolean>(res[0] || res[1]));
}
void operation<Command::OR, value_type::list_flag, void>::operator()(
basic_eval_stack& stack, value* vals
)
{
bool res[2];
convert_bools(vals, _list_table, res);
stack.push(value{}.set<value_type::boolean>(res[0] || res[1]));
}
} // namespace ink::runtime::internal