feat(furvm): introduce length of operator

Closes: #54
This commit is contained in:
2026-07-11 23:17:59 +02:00
parent cd715ae779
commit e1b75ccc8e
4 changed files with 19 additions and 7 deletions
+5
View File
@@ -125,6 +125,11 @@ enum class instruction_t : byte {
*/
Sizeof,
/**
* @brief Pushes a length of popped-off thing onto the stack.
*/
Lengthof,
/**
* @brief Pushes a variable onto the stack.
*
+1 -1
View File
@@ -453,7 +453,7 @@ public:
return { *m_type.value.array.type, data + (index * elementSize), m_allocator };
}
std::size_t size() const {
thing_type::u64 length() const {
if (m_type.type != thing_type::Array) throw bad_thing_access();
return m_type.value.array.size == 0 ? get<array>().dynamic.size : m_type.value.array.size;
}
+9 -3
View File
@@ -245,13 +245,19 @@ void executor::step() {
break;
case thing_type::Ptr: size->get<thing_type::u64>() = static_cast<thing_type::u64>(sizeof(void*)); break;
case thing_type::Array:
size->get<thing_type::u64>() = static_cast<thing_type::u64>(
(thing->type().value.array.size == 0) ? thing->get<furvm::thing<>::array>().dynamic.size
: thing->type().value.array.size);
/* TODO: Return actual memory size of the array
* By the memory size I mean the length times sizeof single element.
*/
size->get<thing_type::u64>() = thing->length();
break;
default: throw std::runtime_error("unreachable");
}
} break;
case instruction_t::Lengthof: {
auto thing = pop_thing();
auto length = push_thing({ (struct thing_type){ thing_type::U64 }, m_context->thing_alloc() });
length->get<thing_type::u64>() = thing->length();
} break;
case instruction_t::Load: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
+4 -3
View File
@@ -24,7 +24,7 @@ static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
case thing_type::U64: std::cout << thing.get<thing_type::u64>(); break;
case furvm::thing_type::Array:
std::cout << "{ ";
for (thing_type::u64 i = 0; i < thing.size(); ++i) {
for (thing_type::u64 i = 0; i < thing.length(); ++i) {
if (i > 0) std::cout << ", ";
print_thing(thing.at(i));
}
@@ -58,7 +58,7 @@ int main(int argc, char** argv) {
0,
0,
0,
static_cast<furvm::byte>(furvm::instruction_t::Pointerof),
static_cast<furvm::byte>(furvm::instruction_t::Lengthof),
static_cast<furvm::byte>(furvm::instruction_t::Call),
1,
0,
@@ -68,8 +68,9 @@ int main(int argc, char** argv) {
auto mod = context->emplace("main", s_bytecode, s_bytecode + sizeof(s_bytecode));
auto charType = mod->emplace_type(furvm::mod_type::S8);
auto arrayType = mod->emplace_type(charType.id(), 10);
auto u64Type = mod->emplace_type(furvm::mod_type::U64);
auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0);
mod->emplace_function(furvm::function_sig{ { arrayType } }, "print").dispatch();
mod->emplace_function(furvm::function_sig{ { u64Type } }, "print").dispatch();
#endif
mod->set_native_function("print", [](furvm::executor& executor) {
print_thing(*executor.load_thing(0));