feat: introduce array and get instruction
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "furvm/fwd.hpp"
|
||||
#include "furvm/instruction.hpp"
|
||||
#include "furvm/thing.hpp"
|
||||
#include "furvm/type.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
@@ -94,6 +95,32 @@ void executor::step() {
|
||||
push_thing({ *m_context->at("core")->type_at(2), m_context, m_context->thing_alloc() })->get<int_t>() =
|
||||
frame.mod->byte(frame.position++);
|
||||
} break;
|
||||
case instruction_t::Array: {
|
||||
type_id typeId = static_cast<type_id>(frame.mod->byte(frame.position)) |
|
||||
(static_cast<type_id>(frame.mod->byte(frame.position + 1)) << 8) |
|
||||
(static_cast<type_id>(frame.mod->byte(frame.position + 2)) << 16) |
|
||||
(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)
|
||||
throw std::runtime_error("invalid array type");
|
||||
|
||||
auto array = push_thing({ type, m_context, m_context->thing_alloc() });
|
||||
|
||||
if (type->array.size == 0) {
|
||||
auto sizeThing = pop_thing();
|
||||
long_t size = sizeThing->integer();
|
||||
array->resize(size);
|
||||
}
|
||||
|
||||
push_thing(std::move(array));
|
||||
} break;
|
||||
case instruction_t::Get: {
|
||||
auto index = pop_thing();
|
||||
auto array = pop_thing();
|
||||
push_thing(array->at(index->integer()));
|
||||
} break;
|
||||
case instruction_t::Drop: {
|
||||
pop_thing();
|
||||
} break;
|
||||
|
||||
+41
-6
@@ -10,13 +10,33 @@
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
|
||||
auto rsv = thing.resolve();
|
||||
switch (rsv.type()->t) {
|
||||
case furvm::type_t::Primitive: {
|
||||
std::cout << rsv.integer();
|
||||
} break;
|
||||
case furvm::type_t::Array: {
|
||||
std::cout << "{ ";
|
||||
for (furvm::long_t i = 0; i < rsv.size(); ++i) {
|
||||
if (i > 0) std::cout << ", ";
|
||||
print_thing(rsv.at(i));
|
||||
}
|
||||
std::cout << " }\n";
|
||||
} break;
|
||||
default: std::cerr << "{Something went wrong}";
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
auto context = std::make_shared<furvm::context>();
|
||||
|
||||
#if 0 // NOLINT
|
||||
if (argc != 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " <mod.fmod>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto context = std::make_shared<furvm::context>();
|
||||
|
||||
std::ifstream file(argv[1]);
|
||||
if (!file.is_open()) {
|
||||
@@ -24,14 +44,29 @@ int main(int argc, char** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
furvm::mod_h mod = context->emplace("main", furvm::mod::load(file));
|
||||
furvm::function_h func = mod->function_at("main");
|
||||
furvm::mod_h mod = context->emplace("main", std::move(furvm::mod::load(file)));
|
||||
#else
|
||||
static const furvm::byte s_bytecode[] = {
|
||||
static_cast<furvm::byte>(furvm::instruction_t::Array),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
static_cast<furvm::byte>(furvm::instruction_t::Call),
|
||||
1,
|
||||
0,
|
||||
static_cast<furvm::byte>(furvm::instruction_t::Return),
|
||||
};
|
||||
|
||||
mod->set_native_function("print",
|
||||
[](furvm::executor& executor) { std::cout << executor.load_thing(0)->integer() << '\n'; });
|
||||
furvm::mod_h mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode));
|
||||
mod->emplace_type(std::make_shared<furvm::type>(context->at("core")->type_at(2), 10)).dispatch();
|
||||
auto mainFunc = mod->emplace_function("main", 0, 0);
|
||||
mod->emplace_function_private("print", 1, "print").dispatch();
|
||||
#endif
|
||||
mod->set_native_function("print", [](furvm::executor& executor) { print_thing(*executor.load_thing(0)); });
|
||||
|
||||
furvm::executor_h executor = context->emplace_executor(context);
|
||||
executor->push_frame(mod, *func);
|
||||
executor->push_frame(mod, *mainFunc);
|
||||
|
||||
while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) {
|
||||
executor->step();
|
||||
|
||||
Reference in New Issue
Block a user