30 #define DBG_SF LOG_STREAM(debug, log_scripting_formula)
31 #define LOG_SF LOG_STREAM(info, log_scripting_formula)
32 #define WRN_SF LOG_STREAM(warn, log_scripting_formula)
33 #define ERR_SF LOG_STREAM(err, log_scripting_formula)
50 std::ostringstream ss;
52 ss <<
"TYPE ERROR: expected " << message <<
" but found "
71 : type_(value->get_type())
189 must_be(formula_variant::type::list);
192 return value_cast<variant_list>()->get_container().at(
n);
193 }
catch(std::out_of_range&) {
205 auto& map = value_cast<variant_map>()->get_container();
207 auto i = map.find(v);
215 std::vector<variant> slice;
217 slice.push_back((*
this)[v[
i]]);
221 }
else if(v.
as_int() < 0) {
233 must_be(formula_variant::type::map);
235 std::vector<variant> tmp;
236 for(
const auto&
i : value_cast<variant_map>()->get_container()) {
237 tmp.push_back(
i.first);
245 must_be(formula_variant::type::map);
247 std::vector<variant> tmp;
248 for(
const auto&
i : value_cast<variant_map>()->get_container()) {
249 tmp.push_back(
i.second);
257 return value_->make_iterator().begin();
262 return value_->make_iterator().end();
267 return value_->is_empty();
276 return value_->num_elements();
282 if(
auto obj = value_cast<variant_callable>()->get_callable()) {
283 return obj->query_value(name);
299 must_be(formula_variant::type::integer);
300 return value_cast<variant_int>()->get_numeric_value();
306 return value_cast<variant_decimal>()->get_numeric_value();
308 return value_cast<variant_int>()->get_numeric_value() * 1000;
323 must_be(formula_variant::type::string);
324 return value_cast<variant_string>()->get_string();
329 must_be(formula_variant::type::list);
330 return value_cast<variant_list>()->get_container();
335 must_be(formula_variant::type::map);
336 return value_cast<variant_map>()->get_container();
342 auto& list = value_cast<variant_list>()->get_container();
345 std::vector<variant> res;
346 res.reserve(list.size() + other_list.size());
348 for(
const auto& member : list) {
349 res.push_back(member);
352 for(
const auto& member : other_list) {
353 res.push_back(member);
360 std::map<variant, variant> res = value_cast<variant_map>()->get_container();
363 res[member.first] = member.second;
395 if(long_int%10 >= 5) {
413 if(denominator == 0) {
414 throw type_error(
"decimal divide by zero error");
421 long_int /= denominator;
423 if(long_int%10 >= 5) {
433 const int numerator =
as_int();
434 const int denominator = v.
as_int();
436 if(denominator == 0) {
440 return variant(numerator / denominator);
448 if(denominator == 0) {
454 const int numerator =
as_int();
455 const int denominator = v.
as_int();
456 if(denominator == 0) {
460 return variant(numerator % denominator);
470 if(std::isnan(res)) {
477 return variant(
static_cast<int>(std::round(std::pow(
static_cast<double>(
as_int()), v.
as_int()))));
542 return value_cast<variant_list>()->list_op(v.
value_, std::plus<variant>());
548 return value_cast<variant_list>()->list_op(v.
value_, std::minus<variant>());
554 return value_cast<variant_list>()->list_op(v.
value_, std::multiplies<variant>());
560 return value_cast<variant_list>()->list_op(v.
value_, std::divides<variant>());
566 v.
must_be(formula_variant::type::list);
568 std::vector<variant> res;
572 res.push_back((*
this)[
i]);
581 v.
must_be(formula_variant::type::string);
593 return value_cast<variant_int>()->build_range_variant(v.
as_int());
603 return value_cast<variant_list>()->contains(v);
605 return value_cast<variant_map>()->contains(v);
628 return value_->get_serialized_string();
643 return value_->string_cast();
650 return value_->get_debug_string(seen_stack, verbose);
653 return value_->get_debug_string(*seen, verbose);
658 std::stack<variant> vars;
667 std::vector<variant> made_moves;
669 while(!vars.empty()) {
671 if(vars.top().is_null()) {
677 variant res = action->execute_self(*
this);
679 made_moves.push_back(vars.top());
681 }
else if(vars.top().is_string() && vars.top().as_string() ==
"continue") {
683 made_moves.push_back(vars.top());
691 }
else if(vars.top().is_string() && (vars.top().as_string() ==
"end_turn" || vars.top().as_string() ==
"end")) {
695 ERR_SF <<
"UNRECOGNIZED MOVE: " << vars.top().to_debug_string();
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++()
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
int as_decimal() const
Returns variant's internal representation of decimal number: ie, 1.234 is represented as 1234.
variant concatenate(const variant &v) const
variant_iterator begin() const
variant operator/(const variant &) const
variant get_values() const
void serialize_from_string(const std::string &str)
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).
General math utility functions.
std::string get_unknown_exception_type()
Utility function for finding the type of thing caught with catch(...).
std::shared_ptr< variant_value_base > value_base_ptr
static std::string variant_type_to_string(formula_variant::type type)
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")