diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 8bb39142..2817ff1a 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -109,49 +109,49 @@ namespace chaiscript { const Type_Info &inp_ = t_bv.get_type_info(); - if (inp_ == typeid(int)) { + if (inp_ == user_type()) { return get_common_type(sizeof(int), true); - } else if (inp_ == typeid(double)) { + } else if (inp_ == user_type()) { return Common_Types::t_double; - } else if (inp_ == typeid(long double)) { + } else if (inp_ == user_type()) { return Common_Types::t_long_double; - } else if (inp_ == typeid(float)) { + } else if (inp_ == user_type()) { return Common_Types::t_float; - } else if (inp_ == typeid(char)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(char), std::is_signed::value); - } else if (inp_ == typeid(unsigned char)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(unsigned char), false); - } else if (inp_ == typeid(unsigned int)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(unsigned int), false); - } else if (inp_ == typeid(long)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(long), true); - } else if (inp_ == typeid(long long)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(long long), true); - } else if (inp_ == typeid(unsigned long)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(unsigned long), false); - } else if (inp_ == typeid(unsigned long long)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(unsigned long long), false); - } else if (inp_ == typeid(std::int8_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_int8; - } else if (inp_ == typeid(std::int16_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_int16; - } else if (inp_ == typeid(std::int32_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_int32; - } else if (inp_ == typeid(std::int64_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_int64; - } else if (inp_ == typeid(std::uint8_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_uint8; - } else if (inp_ == typeid(std::uint16_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_uint16; - } else if (inp_ == typeid(std::uint32_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_uint32; - } else if (inp_ == typeid(std::uint64_t)) { + } else if (inp_ == user_type()) { return Common_Types::t_uint64; - } else if (inp_ == typeid(wchar_t)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(wchar_t), std::is_signed::value); - } else if (inp_ == typeid(char16_t)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(char16_t), std::is_signed::value); - } else if (inp_ == typeid(char32_t)) { + } else if (inp_ == user_type()) { return get_common_type(sizeof(char32_t), std::is_signed::value); } else { throw chaiscript::detail::exception::bad_any_cast(); @@ -160,8 +160,8 @@ namespace chaiscript - template - static auto go(Operators::Opers t_oper, const Boxed_Value &t_bv, LHS *t_lhs, const T &c_lhs, const T &c_rhs) + template + static auto go(Operators::Opers t_oper, const Boxed_Value &t_bv, LHS *t_lhs, const LHS &c_lhs, const RHS &c_rhs) { switch (t_oper) { case Operators::Opers::equals: @@ -188,7 +188,7 @@ namespace chaiscript } - if constexpr (!std::is_floating_point::value) { + if constexpr (!std::is_floating_point::value && !std::is_floating_point::value) { switch (t_oper) { case Operators::Opers::shift_left: return const_var(c_lhs << c_rhs); @@ -226,7 +226,7 @@ namespace chaiscript return t_bv; } - if constexpr (!std::is_floating_point::value) { + if constexpr (!std::is_floating_point::value && !std::is_floating_point::value) { switch (t_oper) { case Operators::Opers::assign_bitwise_and: check_divide_by_zero(c_rhs); @@ -254,155 +254,89 @@ namespace chaiscript throw chaiscript::detail::exception::bad_any_cast(); } - template - static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs, const Boxed_Value &t_rhs) - { - using common_type = typename std::common_type::type; - - auto *lhs = [&]() -> LHS *{ - if (!t_lhs.is_const() && !t_lhs.is_return_value()) { - return static_cast(t_lhs.get_ptr()); - } else { - return nullptr; - } - }(); - - return go(t_oper, t_lhs, lhs, get_as_aux(t_lhs), get_as_aux(t_rhs)); - } - - // Unary - template - static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs) - { - auto *lhs = [&]() -> LHS *{ - if (!t_lhs.is_const() && !t_lhs.is_return_value()) { - return static_cast(t_lhs.get_ptr()); - } else { - return nullptr; - } - }(); - - const LHS &c_lhs = (*static_cast(t_lhs.get_const_ptr())); - - if (lhs) { - switch (t_oper) { - case Operators::Opers::pre_increment: - ++(*lhs); - return t_lhs; - case Operators::Opers::pre_decrement: - --(*lhs); - return t_lhs; - } - } - - switch (t_oper) { - case Operators::Opers::unary_minus: - return const_var(-c_lhs); - case Operators::Opers::unary_plus: - return const_var(+c_lhs); - } - - if constexpr (!std::is_floating_point::value) { - switch (t_oper) { - case Operators::Opers::bitwise_complement: - return const_var(~c_lhs); - } - } - - throw chaiscript::detail::exception::bad_any_cast(); - } - - - template - inline static Boxed_Value oper_rhs(Operators::Opers t_oper, const Boxed_Value &t_lhs, const Boxed_Value &t_rhs) + template + inline static auto visit(const Boxed_Value &bv, Callable &&callable) { - switch (get_common_type(t_rhs)) { + switch (get_common_type(bv)) { case Common_Types::t_int32: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_uint8: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_int8: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_uint16: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_int16: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_uint32: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_uint64: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_int64: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_double: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_float: - return go(t_oper, t_lhs, t_rhs); + return callable(*static_cast(bv.get_const_ptr())); case Common_Types::t_long_double: - return go(t_oper, t_lhs, t_rhs); - } + return callable(*static_cast(bv.get_const_ptr())); + default: + throw chaiscript::detail::exception::bad_any_cast(); - throw chaiscript::detail::exception::bad_any_cast(); + } } inline static Boxed_Value oper(Operators::Opers t_oper, const Boxed_Value &t_lhs) { - switch (get_common_type(t_lhs)) { - case Common_Types::t_int32: - return go(t_oper, t_lhs); - case Common_Types::t_uint8: - return go(t_oper, t_lhs); - case Common_Types::t_int8: - return go(t_oper, t_lhs); - case Common_Types::t_uint16: - return go(t_oper, t_lhs); - case Common_Types::t_int16: - return go(t_oper, t_lhs); - case Common_Types::t_uint32: - return go(t_oper, t_lhs); - case Common_Types::t_uint64: - return go(t_oper, t_lhs); - case Common_Types::t_int64: - return go(t_oper, t_lhs); - case Common_Types::t_double: - return go(t_oper, t_lhs); - case Common_Types::t_float: - return go(t_oper, t_lhs); - case Common_Types::t_long_double: - return go(t_oper, t_lhs); - } + auto unary_operator = [t_oper, &t_lhs](const auto &c_lhs){ + auto *lhs = static_cast *>(t_lhs.get_ptr()); - throw chaiscript::detail::exception::bad_any_cast(); + if (lhs) { + switch (t_oper) { + case Operators::Opers::pre_increment: + ++(*lhs); + return t_lhs; + case Operators::Opers::pre_decrement: + --(*lhs); + return t_lhs; + } + } + + switch (t_oper) { + case Operators::Opers::unary_minus: + return const_var(-c_lhs); + case Operators::Opers::unary_plus: + return const_var(+c_lhs); + } + + if constexpr (!std::is_floating_point_v>) { + switch (t_oper) { + case Operators::Opers::bitwise_complement: + return const_var(~c_lhs); + } + } + + throw chaiscript::detail::exception::bad_any_cast(); + }; + + return visit(t_lhs, unary_operator); } inline static Boxed_Value oper(Operators::Opers t_oper, const Boxed_Value &t_lhs, const Boxed_Value &t_rhs) { - switch (get_common_type(t_lhs)) { - case Common_Types::t_int32: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_uint8: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_int8: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_uint16: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_int16: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_uint32: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_uint64: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_int64: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_double: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_float: - return oper_rhs(t_oper, t_lhs, t_rhs); - case Common_Types::t_long_double: - return oper_rhs(t_oper, t_lhs, t_rhs); - } - throw chaiscript::detail::exception::bad_any_cast(); + auto lhs_visit = [t_oper, &t_lhs, &t_rhs](const auto &c_lhs){ + auto *lhs = static_cast *>(t_lhs.get_ptr()); + + auto rhs_visit = [t_oper, &t_lhs, lhs, &c_lhs](const auto &c_rhs) { + return go(t_oper, t_lhs, lhs, c_lhs, c_rhs); + }; + + return visit(t_rhs, rhs_visit); + }; + + return visit(t_lhs, lhs_visit); } template @@ -445,11 +379,11 @@ namespace chaiscript { const Type_Info &inp_ = t_bv.get_type_info(); - if (inp_ == typeid(double)) { + if (inp_ == user_type()) { return true; - } else if (inp_ == typeid(long double)) { + } else if (inp_ == user_type()) { return true; - } else if (inp_ == typeid(float)) { + } else if (inp_ == user_type()) { return true; } else { return false; @@ -458,49 +392,49 @@ namespace chaiscript Boxed_Number get_as(const Type_Info &inp_) const { - if (inp_.bare_equal_type_info(typeid(int))) { + if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(double))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(float))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(long double))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(char))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(unsigned char))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(wchar_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(char16_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(char32_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(unsigned int))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(long))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(long long))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(unsigned long))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(unsigned long long))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(int8_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(int16_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(int32_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(int64_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(uint8_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(uint16_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(uint32_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); - } else if (inp_.bare_equal_type_info(typeid(uint64_t))) { + } else if (inp_.bare_equal(user_type())) { return Boxed_Number(get_as()); } else { throw chaiscript::detail::exception::bad_any_cast(); @@ -632,7 +566,7 @@ namespace chaiscript static void validate_boxed_number(const Boxed_Value &v) { const Type_Info &inp_ = v.get_type_info(); - if (inp_ == typeid(bool)) + if (inp_ == user_type()) { throw chaiscript::detail::exception::bad_any_cast(); }