this code is a mess and I am losing my mind
This commit does not work and I am so down I don't even bother committing to conventional commits until I fix this mess.
This commit is contained in:
@@ -111,7 +111,7 @@ using function_id = std::uint16_t;
|
||||
/**
|
||||
* @brief A handle to a furvm function.
|
||||
*/
|
||||
using function_h = handle<function, refcount_header<function_id>>;
|
||||
using function_h = handle<function, generic_header<function_id>>;
|
||||
|
||||
// module.hpp
|
||||
|
||||
|
||||
@@ -76,6 +76,11 @@ enum class instruction_t : byte {
|
||||
*/
|
||||
Duplicate,
|
||||
|
||||
/**
|
||||
* @brief Swaps two top elements of the stack.
|
||||
*/
|
||||
Swap,
|
||||
|
||||
/**
|
||||
* @brief Clones top element on the stack.
|
||||
*/
|
||||
|
||||
@@ -502,6 +502,32 @@ public:
|
||||
if (m_type.type != thing_type::Ref || *m_type.value.typeRef != thing.type()) throw bad_thing_access();
|
||||
m_data = thing.m_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Self-explainatory.
|
||||
*
|
||||
* TODO: Document
|
||||
*/
|
||||
void assign(thing&& thing) {
|
||||
class thing rhs = std::move(thing);
|
||||
if (true_type() != rhs.true_type()) throw std::runtime_error("thing type mismatch");
|
||||
// TODO: Move this to another function
|
||||
switch (true_type().type) {
|
||||
case thing_type::S8:
|
||||
case thing_type::S16:
|
||||
case thing_type::S32:
|
||||
case thing_type::S64:
|
||||
case thing_type::U8:
|
||||
case thing_type::U16:
|
||||
case thing_type::U32:
|
||||
case thing_type::U64: std::memcpy(m_data, rhs.m_data, m_size); return;
|
||||
case thing_type::Ptr:
|
||||
case thing_type::Ref:
|
||||
case thing_type::Array: throw std::runtime_error("unimplemented");
|
||||
case thing_type::Count: break;
|
||||
}
|
||||
throw std::runtime_error("unreachable");
|
||||
}
|
||||
private:
|
||||
static void copy_list(const thing_type& arrayType, array& dst, const array& src) {
|
||||
if (arrayType.type != thing_type::Array || arrayType.value.array.type == nullptr)
|
||||
|
||||
+14
-6
@@ -10,6 +10,7 @@
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace furvm {
|
||||
@@ -122,7 +123,7 @@ void executor::store_thing(variable_t variable, thing_h&& thing) {
|
||||
|
||||
thing_h executor::load_thing(variable_t variable) const {
|
||||
const auto& frame = m_frames.top();
|
||||
return frame.variables[variable];
|
||||
return { frame.variables[variable] };
|
||||
}
|
||||
|
||||
void executor::step() {
|
||||
@@ -186,10 +187,10 @@ void executor::step() {
|
||||
push_thing(array->at(index->integer()));
|
||||
} break;
|
||||
case instruction_t::Set: {
|
||||
auto index = pop_thing();
|
||||
auto array = pop_thing();
|
||||
auto element = pop_thing();
|
||||
array->at(index->integer()) = std::move(element->clone());
|
||||
auto element = pop_thing();
|
||||
auto index = pop_thing();
|
||||
auto array = pop_thing();
|
||||
array->at(index->integer()).assign(std::move(element->clone()));
|
||||
} break;
|
||||
case instruction_t::Drop: {
|
||||
pop_thing();
|
||||
@@ -197,6 +198,12 @@ void executor::step() {
|
||||
case instruction_t::Duplicate: {
|
||||
push_thing(thing());
|
||||
} break;
|
||||
case instruction_t::Swap: {
|
||||
auto thing1 = pop_thing();
|
||||
auto thing2 = pop_thing();
|
||||
push_thing(std::move(thing1));
|
||||
push_thing(std::move(thing2));
|
||||
} break;
|
||||
case instruction_t::Clone: {
|
||||
push_thing(std::move(thing()->clone()));
|
||||
} break;
|
||||
@@ -316,7 +323,8 @@ void executor::step() {
|
||||
push_frame(frame.mod, *frame.mod->function_at(funcId));
|
||||
} break;
|
||||
case instruction_t::Jump: {
|
||||
frame.position += ((std::int8_t)frame.mod->byte(frame.position)) + 1;
|
||||
std::int8_t offset = static_cast<std::int8_t>(frame.mod->byte(frame.position++));
|
||||
frame.position += offset;
|
||||
} break;
|
||||
case instruction_t::JumpNotZero: {
|
||||
byte offset = frame.mod->byte(frame.position++);
|
||||
|
||||
+2
-2
@@ -75,8 +75,8 @@ int main(int argc, char** argv) {
|
||||
mod->emplace_function(furvm::function_sig{ { u64Type }, u64Type }, "print").dispatch();
|
||||
#endif
|
||||
mod->set_native_function("println", [](furvm::executor& executor) {
|
||||
auto arg = std::move(*executor.load_thing(0));
|
||||
print_thing(arg);
|
||||
auto arg = executor.load_thing(0);
|
||||
print_thing(*arg);
|
||||
std::cout << '\n';
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user