refactor: replace type_p with type_ref in thing
Damn, this was such a great call. Closes: #46
This commit is contained in:
+16
-14
@@ -83,6 +83,10 @@ thing_h executor::load_thing(variable_t variable) const {
|
||||
return frame.variables[variable];
|
||||
}
|
||||
|
||||
static type_ref get_type_ref(const mod_h& mod, type_id id) {
|
||||
return { mod, mod->type_at(id) };
|
||||
}
|
||||
|
||||
void executor::step() {
|
||||
if ((m_flags & executor_flags::Suspended) == executor_flags::Suspended) return;
|
||||
|
||||
@@ -92,7 +96,7 @@ void executor::step() {
|
||||
switch (instr) {
|
||||
case instruction_t::NoOperation: break;
|
||||
case instruction_t::PushB2I: {
|
||||
push_thing({ *m_context->at("core")->type_at(2), m_context, m_context->thing_alloc() })->get<int_t>() =
|
||||
push_thing({ get_type_ref(m_context->at("core"), 2), m_context, m_context->thing_alloc() })->get<int_t>() =
|
||||
frame.mod->byte(frame.position++);
|
||||
} break;
|
||||
case instruction_t::Array: {
|
||||
@@ -102,8 +106,8 @@ void executor::step() {
|
||||
(static_cast<type_id>(frame.mod->byte(frame.position + 3)) << 24);
|
||||
frame.position += 4;
|
||||
|
||||
auto type = furvm::thing<>::resolve_type(*frame.mod->type_at(typeId), m_context);
|
||||
if (type == nullptr || type->t != type_t::Array || *type->array.type == nullptr)
|
||||
auto type = furvm::thing<>::resolve_type(get_type_ref(frame.mod, typeId), m_context);
|
||||
if (*type.type == nullptr || type->t != type_t::Array || *type->array.type == nullptr)
|
||||
throw std::runtime_error("invalid array type");
|
||||
|
||||
auto array = push_thing({ type, m_context, m_context->thing_alloc() });
|
||||
@@ -190,29 +194,27 @@ void executor::step() {
|
||||
push_thing(lhs->greater_equals(*rhs));
|
||||
} break;
|
||||
case instruction_t::Pointerof: {
|
||||
auto thing = pop_thing()->resolve();
|
||||
auto ptr = push_thing({ *m_context->at("core")->type_at(4), m_context, m_context->thing_alloc() });
|
||||
switch (furvm::thing<>::resolve_type(thing.type(), m_context)->t) {
|
||||
case type_t::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing.raw()); break;
|
||||
auto thing = pop_thing();
|
||||
auto ptr = push_thing({ get_type_ref(m_context->at("core"), 4), m_context, m_context->thing_alloc() });
|
||||
switch (furvm::thing<>::resolve_type(thing->type(), m_context)->t) {
|
||||
case type_t::Primitive: ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->raw()); break;
|
||||
case type_t::Array:
|
||||
ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing.get<array_t>().data);
|
||||
ptr->get<std::uintptr_t>() = reinterpret_cast<std::uintptr_t>(thing->get<array_t>().data);
|
||||
break;
|
||||
case type_t::Reference:
|
||||
case type_t::Import:
|
||||
default: throw std::runtime_error("unreachable");
|
||||
}
|
||||
} break;
|
||||
case instruction_t::Sizeof: {
|
||||
auto thing = pop_thing()->resolve();
|
||||
auto size = push_thing({ *m_context->at("core")->type_at(3), m_context, m_context->thing_alloc() });
|
||||
auto type = furvm::thing<>::resolve_type(thing.type(), m_context);
|
||||
auto thing = pop_thing();
|
||||
auto size = push_thing({ get_type_ref(m_context->at("core"), 3), m_context, m_context->thing_alloc() });
|
||||
auto type = furvm::thing<>::resolve_type(thing->type(), m_context);
|
||||
switch (type->t) {
|
||||
case type_t::Primitive: size->get<long_t>() = static_cast<long_t>(type->primitive); break;
|
||||
case type_t::Array:
|
||||
size->get<long_t>() =
|
||||
(type->array.size == 0) ? thing.get<array_t>().dynamic.size : static_cast<long_t>(type->array.size);
|
||||
(type->array.size == 0) ? thing->get<array_t>().dynamic.size : static_cast<long_t>(type->array.size);
|
||||
break;
|
||||
case type_t::Reference:
|
||||
case type_t::Import:
|
||||
default: throw std::runtime_error("unreachable");
|
||||
}
|
||||
|
||||
+5
-5
@@ -11,16 +11,15 @@
|
||||
#include <sstream>
|
||||
|
||||
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
||||
auto rsv = thing.resolve();
|
||||
switch (rsv.type()->t) {
|
||||
switch (thing.type()->t) {
|
||||
case furvm::type_t::Primitive: {
|
||||
std::cout << rsv.integer();
|
||||
std::cout << thing.integer();
|
||||
} break;
|
||||
case furvm::type_t::Array: {
|
||||
std::cout << "{ ";
|
||||
for (furvm::long_t i = 0; i < rsv.size(); ++i) {
|
||||
for (furvm::long_t i = 0; i < thing.size(); ++i) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
print_thing(rsv.at(i));
|
||||
print_thing(thing.at(i));
|
||||
}
|
||||
std::cout << " }\n";
|
||||
} break;
|
||||
@@ -52,6 +51,7 @@ int main(int argc, char** argv) {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
static_cast<furvm::byte>(furvm::instruction_t::Reference),
|
||||
static_cast<furvm::byte>(furvm::instruction_t::Call),
|
||||
1,
|
||||
0,
|
||||
|
||||
@@ -56,7 +56,6 @@ std::ostream& mod::serialize(std::ostream& os) const {
|
||||
auto type = *m_types.at(id);
|
||||
switch (type->t) {
|
||||
case type_t::Primitive: detail::serialize(os, type->primitive); break;
|
||||
case type_t::Reference: throw std::runtime_error("reference type serialization is unimplemented");
|
||||
case type_t::Array: {
|
||||
detail::serialize(os, type->array.size);
|
||||
detail::serialize(os, type->array.type.id());
|
||||
@@ -134,7 +133,6 @@ mod mod::load(std::istream& is) {
|
||||
detail::load(is, primitive);
|
||||
mod.emplace_type(id, std::make_shared<class type>(primitive)).dispatch();
|
||||
} break;
|
||||
case type_t::Reference: throw std::runtime_error("reference type serialization is unimplemented");
|
||||
case type_t::Array: {
|
||||
std::size_t size = 0;
|
||||
detail::load(is, size);
|
||||
|
||||
Reference in New Issue
Block a user