/
variant_value.cpp
364 lines (284 loc) · 8.42 KB
/
variant_value.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
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
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "formula/variant.hpp"
#include "formula/variant_value.hpp"
#include "formula/callable.hpp"
#include "formula/function.hpp"
namespace wfl
{
boost::iterator_range<variant_iterator> variant_value_base::make_iterator() const
{
return {variant_iterator(), variant_iterator()};
}
variant variant_value_base::deref_iterator(const boost::any& /*iter*/) const
{
return variant();
}
variant variant_int::build_range_variant(int limit) const
{
const int len = std::abs(limit - value_) + 1;
std::vector<variant> res;
res.reserve(len);
for(int i = value_; res.size() != res.capacity(); value_ < limit ? ++i : --i) {
res.emplace_back(i);
}
return variant(res);
}
std::string variant_decimal::to_string_impl(const bool sign_value) const
{
std::ostringstream ss;
int fractional = value_ % 1000;
int integer = (value_ - fractional) / 1000;
if(sign_value) {
// Make sure we get the sign on small negative values.
if(integer == 0 && value_ < 0) {
ss << '-';
}
}
ss << integer << ".";
fractional = std::abs(fractional);
if(fractional < 100) {
if(fractional < 10) {
ss << "00";
} else {
ss << 0;
}
}
ss << fractional;
return ss.str();
}
variant_callable::variant_callable(const formula_callable* callable)
: callable_(callable)
{}
std::string variant_callable::get_serialized_string() const
{
// TODO: make serialize return a string.
std::string str;
if(callable_) {
callable_->serialize(str);
}
return str;
}
std::string variant_callable::get_debug_string(formula_seen_stack& seen, bool verbose) const
{
std::ostringstream ss;
ss << "{";
if(!callable_) {
ss << "null";
} else if(std::find(seen.begin(), seen.end(), callable_) == seen.end()) {
if(!verbose) {
seen.push_back(callable_);
}
formula_input_vector v = callable_->inputs();
bool first = true;
for(const auto& input : v) {
if(!first) {
ss << ", ";
}
first = false;
ss << input.name << " ";
if(input.access == FORMULA_READ_WRITE) {
ss << "(read-write) ";
} else if(input.access == FORMULA_WRITE_ONLY) {
ss << "(writeonly) ";
}
ss << "-> " << callable_->query_value(input.name).to_debug_string(verbose, &seen);
}
} else {
ss << "...";
}
ss << "}";
return ss.str();
}
bool variant_callable::equals(variant_value_base& other) const
{
variant_callable& other_ref = value_ref_cast<variant_callable>(other);
return callable_ ? callable_->equals(other_ref.callable_) : callable_ == other_ref.callable_;
}
bool variant_callable::less_than(variant_value_base& other) const
{
variant_callable& other_ref = value_ref_cast<variant_callable>(other);
return callable_ ? callable_->less(other_ref.callable_) : other_ref.callable_ != nullptr;
}
boost::iterator_range<variant_iterator> variant_callable::make_iterator() const
{
if(!callable_) {
return variant_value_base::make_iterator();
}
if(inputs.empty()) {
callable_->get_inputs(inputs);
}
return {variant_iterator(this, inputs.cbegin()), variant_iterator(this, inputs.cend())};
}
variant variant_callable::deref_iterator(const boost::any& iter) const
{
if(!callable_) {
return variant();
}
return callable_->query_value(boost::any_cast<const formula_input_vector::const_iterator&>(iter)->name);
}
void variant_callable::iterator_inc(boost::any& iter) const
{
++boost::any_cast<formula_input_vector::const_iterator&>(iter);
}
void variant_callable::iterator_dec(boost::any& iter) const
{
--boost::any_cast<formula_input_vector::const_iterator&>(iter);
}
std::string variant_string::get_serialized_string() const
{
std::ostringstream ss;
ss << "'";
for(const auto& c : string_) {
switch(c) {
case '\'':
ss << "[']";
break;
case '[':
ss << "[(]";
break;
case ']':
ss << "[)]";
break;
default:
ss << c;
break;
}
}
ss << "'";
return ss.str();
}
template<typename T>
std::string variant_container<T>::to_string_impl(bool annotate, bool annotate_empty, mod_func_t mod_func) const
{
std::ostringstream ss;
if(annotate) {
ss << "[";
}
bool first_time = true;
for(const auto& member : container_) {
if(!first_time) {
ss << ", ";
}
first_time = false;
ss << to_string_detail(member, mod_func);
}
// TODO: evaluate if this really needs to be separately conditional.
if(annotate_empty && container_.empty()) {
ss << "->";
}
if(annotate) {
ss << "]";
}
return ss.str();
}
template<typename T>
std::string variant_container<T>::string_cast() const
{
return to_string_impl(false, false, [](const variant& v) { return v.string_cast(); });
}
template<typename T>
std::string variant_container<T>::get_serialized_string() const
{
return to_string_impl(true, true, [](const variant& v) { return v.serialize_to_string(); });
}
template<typename T>
std::string variant_container<T>::get_debug_string(formula_seen_stack& seen, bool verbose) const
{
return to_string_impl(true, false, [&](const variant& v) { return v.to_debug_string(verbose, &seen); });
}
template<typename T>
void variant_container<T>::iterator_inc(boost::any& iter) const
{
++boost::any_cast<typename T::const_iterator&>(iter);
}
template<typename T>
void variant_container<T>::iterator_dec(boost::any& iter) const
{
--boost::any_cast<typename T::const_iterator&>(iter);
}
template<typename T>
boost::iterator_range<variant_iterator> variant_container<T>::make_iterator() const
{
return {variant_iterator(this, get_container().cbegin()), variant_iterator(this, get_container().cend())};
}
template<typename T>
bool variant_container<T>::iterator_equals(const boost::any& first, const boost::any& second) const {
return boost::any_cast<typename T::const_iterator>(first) == boost::any_cast<typename T::const_iterator>(second);
}
// Force compilation of the following template instantiations
template class variant_container<variant_vector>;
template class variant_container<variant_map_raw>;
variant variant_list::list_op(value_base_ptr second, std::function<variant(variant&, variant&)> op_func)
{
const auto& other_list = value_cast<variant_list>(second);
if(num_elements() != other_list->num_elements()) {
throw type_error("List op requires two lists of the same length");
}
std::vector<variant> res;
res.reserve(num_elements());
for(size_t i = 0; i < num_elements(); ++i) {
res.push_back(op_func(get_container()[i], other_list->get_container()[i]));
}
return variant(res);
}
bool variant_list::equals(variant_value_base& other) const
{
const auto& other_container = value_ref_cast<variant_list>(other).get_container();
if(num_elements() != other.num_elements()) {
return false;
}
for(size_t n = 0; n < num_elements(); ++n) {
if(get_container()[n] != other_container[n]) {
return false;
}
}
return true;
}
bool variant_list::less_than(variant_value_base& other) const
{
const auto& other_container = value_ref_cast<variant_list>(other).get_container();
for(size_t n = 0; n != num_elements() && n != other.num_elements(); ++n) {
if(get_container()[n] < other_container[n]) {
return true;
} else if(get_container()[n] > other_container[n]) {
return false;
}
}
return num_elements() < other.num_elements();
}
variant variant_list::deref_iterator(const boost::any& iter) const
{
return *boost::any_cast<const variant_vector::const_iterator&>(iter);
}
std::string variant_map::to_string_detail(const variant_map_raw::value_type& container_val, mod_func_t mod_func) const
{
std::ostringstream ss;
ss << mod_func(container_val.first);
ss << "->";
ss << mod_func(container_val.second);
return ss.str();
}
bool variant_map::equals(variant_value_base& other) const
{
return get_container() == value_ref_cast<variant_map>(other).get_container();
}
bool variant_map::less_than(variant_value_base& other) const
{
return get_container() < value_ref_cast<variant_map>(other).get_container();
}
variant variant_map::deref_iterator(const boost::any& iter) const
{
const variant_map_raw::value_type& p = *boost::any_cast<const variant_map_raw::const_iterator&>(iter);
key_value_pair* the_pair = new key_value_pair(p.first, p.second);
return variant(the_pair);
}
} // namespace wfl