refactor(furvm): move thing operators to thing.cpp

This commit is contained in:
2026-06-11 12:18:47 +02:00
parent 00ec0b8f52
commit 5472b77ea0
2 changed files with 64 additions and 54 deletions
+5 -54
View File
@@ -4,8 +4,6 @@
#include "furvm/context.hpp" // IWYU pragma: keep #include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/fwd.hpp" #include "furvm/fwd.hpp"
#include <stdexcept>
namespace furvm { namespace furvm {
enum class thing_t : std::uint8_t { enum class thing_t : std::uint8_t {
@@ -93,7 +91,6 @@ public:
thing(const thing&) = delete; thing(const thing&) = delete;
thing& operator=(const thing&) = delete; thing& operator=(const thing&) = delete;
public: public:
#define x(lType, rType) ((std::uint16_t)((((std::uint8_t)(lType)) << 8) | ((std::uint8_t)(rType))))
/** /**
* @brief Adds two things together. * @brief Adds two things together.
* *
@@ -101,16 +98,7 @@ public:
* @param rhs Right-hand-side thing. * @param rhs Right-hand-side thing.
* @return Shared pointer to result thing. * @return Shared pointer to result thing.
*/ */
friend thing_p operator+(const thing_p& lhs, const thing_p& rhs) { friend thing_p operator+(const thing_p& lhs, const thing_p& rhs);
switch (x(lhs->m_type, rhs->m_type)) {
case x(thing_t::Int32, thing_t::Int32): {
auto res = create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() + rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
/** /**
* @brief Subtracts two things together. * @brief Subtracts two things together.
@@ -119,16 +107,7 @@ public:
* @param rhs Right-hand-side thing. * @param rhs Right-hand-side thing.
* @return Shared pointer to result thing. * @return Shared pointer to result thing.
*/ */
friend thing_p operator-(const thing_p& lhs, const thing_p& rhs) { friend thing_p operator-(const thing_p& lhs, const thing_p& rhs);
switch (x(lhs->m_type, rhs->m_type)) {
case x(thing_t::Int32, thing_t::Int32): {
auto res = create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() - rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
/** /**
* @brief Multiplies two things together. * @brief Multiplies two things together.
@@ -137,16 +116,7 @@ public:
* @param rhs Right-hand-side thing. * @param rhs Right-hand-side thing.
* @return Shared pointer to result thing. * @return Shared pointer to result thing.
*/ */
friend thing_p operator*(const thing_p& lhs, const thing_p& rhs) { friend thing_p operator*(const thing_p& lhs, const thing_p& rhs);
switch (x(lhs->m_type, rhs->m_type)) {
case x(thing_t::Int32, thing_t::Int32): {
auto res = create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() * rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
/** /**
* @brief Divides two things together. * @brief Divides two things together.
@@ -155,16 +125,7 @@ public:
* @param rhs Right-hand-side thing. * @param rhs Right-hand-side thing.
* @return Shared pointer to result thing. * @return Shared pointer to result thing.
*/ */
friend thing_p operator/(const thing_p& lhs, const thing_p& rhs) { friend thing_p operator/(const thing_p& lhs, const thing_p& rhs);
switch (x(lhs->m_type, rhs->m_type)) {
case x(thing_t::Int32, thing_t::Int32): {
auto res = create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() / rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
/** /**
* @brief Modulos two things together. * @brief Modulos two things together.
@@ -173,17 +134,7 @@ public:
* @param rhs Right-hand-side thing. * @param rhs Right-hand-side thing.
* @return Shared pointer to result thing. * @return Shared pointer to result thing.
*/ */
friend thing_p operator%(const thing_p& lhs, const thing_p& rhs) { friend thing_p operator%(const thing_p& lhs, const thing_p& rhs);
switch (x(lhs->m_type, rhs->m_type)) {
case x(thing_t::Int32, thing_t::Int32): {
auto res = create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() % rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
#undef x
public: public:
/** /**
* @brief Returns a new thing. * @brief Returns a new thing.
+59
View File
@@ -82,6 +82,65 @@ thing& thing::operator=(thing&& other) noexcept {
return *this; return *this;
} }
static constexpr std::uint16_t thing_type_pair(thing_t lhs, thing_t rhs) {
return (static_cast<std::uint16_t>(lhs) << 8) | static_cast<std::uint16_t>(rhs);
}
thing_p operator+(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() + rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
thing_p operator-(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() - rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
thing_p operator*(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() * rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
thing_p operator/(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() / rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
thing_p operator%(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() % rhs->int32();
return res;
}
default: throw std::runtime_error("unexpected operator");
}
}
thing_p thing::clone(const thing_p& thing) { thing_p thing::clone(const thing_p& thing) {
thing_handle id = thing->m_context->m_things.size(); thing_handle id = thing->m_context->m_things.size();
if (!thing->m_context->m_deadThings.empty()) { if (!thing->m_context->m_deadThings.empty()) {