feat: introduce array and get instruction
This commit is contained in:
+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