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())
186 must_be(formula_variant::type::list);
189 return value_cast<variant_list>()->get_container().at(
n);
190 }
catch(std::out_of_range&) {
202 auto& map = value_cast<variant_map>()->get_container();
204 auto i = map.find(v);
212 std::vector<variant> slice;
214 slice.push_back((*
this)[v[
i]]);
218 }
else if(v.
as_int() < 0) {
230 must_be(formula_variant::type::map);
232 std::vector<variant> tmp;
233 for(
const auto&
i : value_cast<variant_map>()->get_container()) {
234 tmp.push_back(
i.first);
242 must_be(formula_variant::type::map);
244 std::vector<variant> tmp;
245 for(
const auto&
i : value_cast<variant_map>()->get_container()) {
246 tmp.push_back(
i.second);
254 return value_->make_iterator().begin();
259 return value_->make_iterator().end();
264 return value_->is_empty();
273 return value_->num_elements();
279 if(
auto obj = value_cast<variant_callable>()->get_callable()) {
280 return obj->query_value(name);
296 must_be(formula_variant::type::integer);
297 return value_cast<variant_int>()->get_numeric_value();
303 return value_cast<variant_decimal>()->get_numeric_value();
305 return value_cast<variant_int>()->get_numeric_value() * 1000;
320 must_be(formula_variant::type::string);
321 return value_cast<variant_string>()->get_string();
326 must_be(formula_variant::type::list);
327 return value_cast<variant_list>()->get_container();
332 must_be(formula_variant::type::map);
333 return value_cast<variant_map>()->get_container();
339 auto& list = value_cast<variant_list>()->get_container();
342 std::vector<variant> res;
343 res.reserve(list.size() + other_list.size());
345 for(
const auto& member : list) {
346 res.push_back(member);
349 for(
const auto& member : other_list) {
350 res.push_back(member);
357 std::map<variant, variant> res = value_cast<variant_map>()->get_container();
360 res[member.first] = member.second;
392 if(long_int%10 >= 5) {
410 if(denominator == 0) {
411 throw type_error(
"decimal divide by zero error");
418 long_int /= denominator;
420 if(long_int%10 >= 5) {
430 const int numerator =
as_int();
431 const int denominator = v.
as_int();
433 if(denominator == 0) {
437 return variant(numerator / denominator);
445 if(denominator == 0) {
451 const int numerator =
as_int();
452 const int denominator = v.
as_int();
453 if(denominator == 0) {
457 return variant(numerator % denominator);
467 if(std::isnan(res)) {
474 return variant(
static_cast<int>(std::round(std::pow(
static_cast<double>(
as_int()), v.
as_int()))));
539 return value_cast<variant_list>()->list_op(v.
value_, std::plus<variant>());
545 return value_cast<variant_list>()->list_op(v.
value_, std::minus<variant>());
551 return value_cast<variant_list>()->list_op(v.
value_, std::multiplies<variant>());
557 return value_cast<variant_list>()->list_op(v.
value_, std::divides<variant>());
563 v.
must_be(formula_variant::type::list);
565 std::vector<variant> res;
569 res.push_back((*
this)[
i]);
578 v.
must_be(formula_variant::type::string);
590 return value_cast<variant_int>()->build_range_variant(v.
as_int());
600 return value_cast<variant_list>()->contains(v);
602 return value_cast<variant_map>()->contains(v);
625 return value_->get_serialized_string();
640 return value_->string_cast();
647 return value_->get_debug_string(seen_stack, verbose);
650 return value_->get_debug_string(*seen, verbose);
655 std::stack<variant> vars;
664 std::vector<variant> made_moves;
666 while(!vars.empty()) {
668 if(vars.top().is_null()) {
674 variant res = action->execute_self(*
this);
676 made_moves.push_back(vars.top());
678 }
else if(vars.top().is_string() && vars.top().as_string() ==
"continue") {
680 made_moves.push_back(vars.top());
688 }
else if(vars.top().is_string() && (vars.top().as_string() ==
"end_turn" || vars.top().as_string() ==
"end")) {
692 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).
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")