Write vm_run_wat(wat) -- the top-level entry point. Split lines, scan for functions, find main, run it.
fn vm_run_wat(wat: []const u8) i64 {
vm_split(wat);
vm_scan_functions();
vm_sp = 0;
vm_local_count = 0;
vm_block_depth = 0;
vm_call_depth = 0;
// find main
for (0..vm_fn_count) |i| {
if (streq(vm_fn_names[i], "main")) {
return vm_run_at(vm_fn_start[i], vm_fn_end[i]);
}
}
return 0;
}
Test it with hand-written WAT:
const result: i64 = vm_run_wat(
\\(func (export "main") (result i64)
\\ i64.const 3
\\ i64.const 5
\\ i64.add
\\)
);
print("vm result: {d}\n", .{result}); // 8
If that prints 8, your VM runs WebAssembly. Not all of WebAssembly -- just the subset our compiler produces. But it's enough.