27 #define DBG_SF LOG_STREAM(debug, log_scripting_formula)
28 #define LOG_SF LOG_STREAM(info, log_scripting_formula)
29 #define WRN_SF LOG_STREAM(warn, log_scripting_formula)
30 #define ERR_SF LOG_STREAM(err, log_scripting_formula)
47 std::ostringstream ss;
49 ss <<
"TYPE ERROR: expected " << message <<
" but found "
68 : type_(value->get_type())
195 must_be(formula_variant::type::list);
198 return value_cast<variant_list>()->get_container().at(
n);
199 }
catch(std::out_of_range&) {
211 auto& map = value_cast<variant_map>()->get_container();
213 auto i = map.find(v);
221 std::vector<variant> slice;
223 slice.push_back((*
this)[v[
i]]);
226 return variant(std::move(slice));
227 }
else if(v.
as_int() < 0) {
239 must_be(formula_variant::type::map);
241 std::vector<variant> tmp;
242 for(
const auto&
i : value_cast<variant_map>()->get_container()) {
243 tmp.push_back(
i.first);
246 return variant(std::move(tmp));
251 must_be(formula_variant::type::map);
253 std::vector<variant> tmp;
254 for(
const auto&
i : value_cast<variant_map>()->get_container()) {
255 tmp.push_back(
i.second);
258 return variant(std::move(tmp));
263 return value_->make_iterator().begin();
268 return value_->make_iterator().end();
273 return value_->is_empty();
282 return value_->num_elements();
288 if(
auto obj = value_cast<variant_callable>()->get_callable()) {
289 return obj->query_value(name);
302 if(
is_null()) {
return fallback; }
305 must_be(formula_variant::type::integer);
306 return value_cast<variant_int>()->get_numeric_value();
312 return value_cast<variant_decimal>()->get_numeric_value();
314 return value_cast<variant_int>()->get_numeric_value() * 1000;
329 must_be(formula_variant::type::string);
330 return value_cast<variant_string>()->get_string();
335 must_be(formula_variant::type::list);
336 return value_cast<variant_list>()->get_container();
341 must_be(formula_variant::type::map);
342 return value_cast<variant_map>()->get_container();
348 auto& list = value_cast<variant_list>()->get_container();
351 std::vector<variant> res;
352 res.reserve(list.size() + other_list.size());
354 for(
const auto& member : list) {
355 res.push_back(member);
358 for(
const auto& member : other_list) {
359 res.push_back(member);
362 return variant(std::move(res));
366 std::map<variant, variant> res = value_cast<variant_map>()->get_container();
369 res[member.first] = member.second;
372 return variant(std::move(res));
401 if(long_int%10 >= 5) {
419 if(denominator == 0) {
420 throw type_error(
"decimal divide by zero error");
427 long_int /= denominator;
429 if(long_int%10 >= 5) {
439 const int numerator =
as_int();
440 const int denominator = v.
as_int();
442 if(denominator == 0) {
446 return variant(numerator / denominator);
454 if(denominator == 0) {
460 const int numerator =
as_int();
461 const int denominator = v.
as_int();
462 if(denominator == 0) {
466 return variant(numerator % denominator);
476 if(std::isnan(res)) {
483 return variant(
static_cast<int>(std::round(std::pow(
static_cast<double>(
as_int()), v.
as_int()))));
552 template<
typename Func>
558 if(lhs.size() != rhs.size()) {
559 throw type_error(
"zip_transform requires two lists of the same length");
562 std::vector<variant> res;
563 res.reserve(lhs.size());
565 for(std::size_t
i = 0;
i < lhs.size(); ++
i) {
566 res.push_back(std::invoke(op_func, lhs[
i], rhs[
i]));
569 return variant(std::move(res));
601 v.
must_be(formula_variant::type::list);
603 std::vector<variant> res;
607 res.push_back((*
this)[
i]);
614 return variant(std::move(res));
616 v.
must_be(formula_variant::type::string);
628 return value_cast<variant_int>()->build_range_variant(v.
as_int());
638 return value_cast<variant_list>()->contains(v);
640 return value_cast<variant_map>()->contains(v);
663 return value_->get_serialized_string();
678 return value_->string_cast();
685 return value_->get_debug_string(seen_stack, verbose);
688 return value_->get_debug_string(*seen, verbose);
693 std::stack<variant> vars;
702 std::vector<variant> made_moves;
704 while(!vars.empty()) {
706 if(vars.top().is_null()) {
712 variant res = action->execute_self(*
this);
714 made_moves.push_back(vars.top());
716 }
else if(vars.top().is_string() && vars.top().as_string() ==
"continue") {
718 made_moves.push_back(vars.top());
726 }
else if(vars.top().is_string() && (vars.top().as_string() ==
"end_turn" || vars.top().as_string() ==
"end")) {
730 ERR_SF <<
"UNRECOGNIZED MOVE: " << vars.top().to_debug_string();
736 return variant(std::move(made_moves));
std::unique_ptr< PangoAttribute, void(*)(PangoAttribute *)> value_
Iterator class for the variant.
bool operator!=(const variant_iterator &that) const
const variant_value_base * container_
variant operator*() const
bool operator==(const variant_iterator &that) const
variant_iterator & operator--()
variant_iterator()
Constructor for a no-op iterator.
variant_iterator & operator++()
const variant_map_raw & get_container() const
Base class for all variant types.
virtual bool iterator_equals(const utils::any &, const utils::any &) const
Implements the equality functionality of variant_iterator for a value of this type.
virtual variant deref_iterator(const utils::any &iter) const
Implements the dereference functionality of variant_iterator for a value of this type.
virtual void iterator_dec(utils::any &) const
Implements the decrement functionality of variant_iterator for a value of this type.
virtual void iterator_inc(utils::any &) const
Implements the increment functionality of variant_iterator for a value of this type.
variant execute_variant(const variant &to_exec)
value_base_ptr value_
Variant value.
std::shared_ptr< T > value_cast() const
variant operator-() const
variant operator^(const variant &) const
variant concatenate(const variant &v) const
variant_iterator begin() const
int as_int(int fallback=0) const
Returns the variant's value as an integer.
variant operator/(const variant &) const
variant get_values() const
void serialize_from_string(const std::string &str)
int as_decimal(int fallback=0) const
Returns the variant's internal representation of decimal number: ie, 1.234 is represented as 1234.
bool operator>=(const variant &) const
void must_both_be(formula_variant::type t, const variant &second) const
std::size_t num_elements() const
formula_variant::type type() const
bool operator!=(const variant &) const
variant operator+(const variant &) const
bool contains(const variant &other) const
bool operator==(const variant &) const
bool operator>(const variant &) const
variant operator*(const variant &) const
variant get_member(const std::string &name) const
variant list_elements_mul(const variant &v) const
std::string serialize_to_string() const
variant list_elements_add(const variant &v) const
const std::string & as_string() const
std::string string_cast() const
variant list_elements_div(const variant &v) const
variant_iterator end() const
bool as_bool() const
Returns a boolean state of the variant value.
bool operator<(const variant &) const
variant operator[](std::size_t n) const
variant build_range(const variant &v) const
std::string type_string() const
Gets string name of the current value type.
variant operator%(const variant &) const
const std::vector< variant > & as_list() const
bool operator<=(const variant &) const
void must_be(formula_variant::type t) const
std::string to_debug_string(bool verbose=false, formula_seen_stack *seen=nullptr) const
const std::map< variant, variant > & as_map() const
bool is_null() const
Functions to test the type of the internal value.
variant list_elements_sub(const variant &v) const
Standard logging facilities (interface).
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
variant zip_transform(const variant &v1, const variant &v2, const Func &op_func)
Applies the provided function to the corresponding variants in both lists.
std::shared_ptr< variant_value_base > value_base_ptr
static std::string variant_type_to_string(formula_variant::type type)
std::vector< variant > variant_vector
std::vector< const_formula_callable_ptr > formula_seen_stack
static std::string was_expecting(const std::string &message, const variant &v)
static value_base_ptr null_value(new variant_value_base)
The base template for associating string values with enum values.
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.
type_error(const std::string &str)
static map_location::direction n
static lg::log_domain log_scripting_formula("scripting/formula")