refactor: move type to separate file

This commit is contained in:
2026-07-03 19:26:16 +02:00
parent 11b05b8c18
commit 2164ab0d97
2 changed files with 132 additions and 118 deletions
+2 -118
View File
@@ -4,136 +4,20 @@
#include "furlang/arena.hpp"
#include "furvm/exceptions.hpp"
#include "furvm/fwd.hpp"
#include "furvm/module.hpp"
#include "furvm/type.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <functional>
#include <memory>
#include <new>
#include <stdexcept>
#include <utility>
#include <vector>
namespace furvm {
enum class type_t : std::uint32_t {
Primitive = 0,
Reference,
List,
};
using primitive_type = std::uint64_t;
using reference_type = std::shared_ptr<type>;
using list_type = std::shared_ptr<type>;
struct type {
type_t t;
union {
primitive_type primitive;
reference_type reference;
list_type list;
};
type(type_t type)
: t(type), primitive(0) {}
type(primitive_type primitive)
: t(type_t::Primitive), primitive(primitive) {}
type(const reference_type& reference)
: t(type_t::Reference), reference(reference) {}
static type make_list(const list_type& list) {
type type(type_t::List);
new (&type.list) list_type(list);
return type;
}
~type() {
switch (t) {
case type_t::Reference: reference.~reference_type(); break;
case type_t::List: list.~list_type(); break;
default: break;
}
}
type(type&& other) noexcept
: t(other.t) {
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = std::move(other.reference); break;
case type_t::List: list = std::move(other.list); break;
}
}
type& operator=(type&& other) noexcept {
if (this == &other) return *this;
t = other.t;
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = std::move(other.reference); break;
case type_t::List: list = std::move(other.list); break;
}
return *this;
}
type(const type& other)
: t(other.t) {
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = other.reference; break;
case type_t::List: list = other.list; break;
}
}
type& operator=(const type& other) {
if (this == &other) return *this;
t = other.t;
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = other.reference; break;
case type_t::List: list = other.list; break;
}
return *this;
}
};
using byte_t = std::int8_t; /**< A 1-byte integer. */
using short_t = std::int16_t; /**< A 2-byte integer. */
using int_t = std::int32_t; /**< A 4-byte integer. */
using long_t = std::int64_t; /**< An 8-byte integer. */
using reference_t = std::byte*;
struct list_t {
long_t size;
std::byte* data;
};
/**
* @brief A 1-byte integer thing type.
*/
inline static type byteType = { sizeof(byte_t) }; // NOLINT
/**
* @brief A 2-byte integer thing type.
*/
inline static type shortType = { sizeof(short_t) }; // NOLINT
/**
* @brief A 4-byte integer thing type.
*/
inline static type intType = { sizeof(int_t) }; // NOLINT
/**
* @brief An 8-byte integer thing type.
*/
inline static type longType = { sizeof(long_t) }; // NOLINT
template <template <typename> typename Allocator>
class thing final {
friend class executor;
+130
View File
@@ -0,0 +1,130 @@
#ifndef FURVM_TYPE_HPP
#define FURVM_TYPE_HPP
#include "furvm/fwd.hpp"
#include <cstdint>
#include <memory>
#include <utility>
namespace furvm {
enum class type_t : std::uint32_t {
Primitive = 0,
Reference,
List,
};
using primitive_type = std::uint64_t;
using reference_type = std::shared_ptr<type>;
using list_type = std::shared_ptr<type>;
struct type {
type_t t;
union {
primitive_type primitive;
reference_type reference;
list_type list;
};
type(type_t type)
: t(type), primitive(0) {}
type(primitive_type primitive)
: t(type_t::Primitive), primitive(primitive) {}
type(const reference_type& reference)
: t(type_t::Reference), reference(reference) {}
static type make_list(const list_type& list) {
type type(type_t::List);
new (&type.list) list_type(list);
return type;
}
~type() {
switch (t) {
case type_t::Reference: reference.~reference_type(); break;
case type_t::List: list.~list_type(); break;
default: break;
}
}
type(type&& other) noexcept
: t(other.t) {
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = std::move(other.reference); break;
case type_t::List: list = std::move(other.list); break;
}
}
type& operator=(type&& other) noexcept {
if (this == &other) return *this;
t = other.t;
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = std::move(other.reference); break;
case type_t::List: list = std::move(other.list); break;
}
return *this;
}
type(const type& other)
: t(other.t) {
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = other.reference; break;
case type_t::List: list = other.list; break;
}
}
type& operator=(const type& other) {
if (this == &other) return *this;
t = other.t;
switch (t) {
case type_t::Primitive: primitive = other.primitive; break;
case type_t::Reference: reference = other.reference; break;
case type_t::List: list = other.list; break;
}
return *this;
}
};
using byte_t = std::int8_t; /**< A 1-byte integer. */
using short_t = std::int16_t; /**< A 2-byte integer. */
using int_t = std::int32_t; /**< A 4-byte integer. */
using long_t = std::int64_t; /**< An 8-byte integer. */
using reference_t = std::byte*;
struct list_t {
long_t size;
std::byte* data;
};
/**
* @brief A 1-byte integer thing type.
*/
inline static type byteType = { sizeof(byte_t) }; // NOLINT
/**
* @brief A 2-byte integer thing type.
*/
inline static type shortType = { sizeof(short_t) }; // NOLINT
/**
* @brief A 4-byte integer thing type.
*/
inline static type intType = { sizeof(int_t) }; // NOLINT
/**
* @brief An 8-byte integer thing type.
*/
inline static type longType = { sizeof(long_t) }; // NOLINT
} // namespace furvm
#endif // FURVM_TYPE_HPP