The VM
vm-split-lines

Split WAT text into lines. We'll store them in a global array and work with them by index.

var vm_lines: [4096][]const u8 = undefined;
var vm_line_count: usize = 0;

fn vm_split(wat: []const u8) void {
    vm_line_count = 0;
    var start: usize = 0;
    for (wat, 0..) |c, i| {
        if (c == '\n') {
            if (i > start) {
                vm_lines[vm_line_count] = wat[start..i];
                vm_line_count += 1;
            }
            start = i + 1;
        }
    }
    if (start < wat.len) {
        vm_lines[vm_line_count] = wat[start..wat.len];
        vm_line_count += 1;
    }
}

Test it:

    vm_split("  i64.const 2\n  i64.const 3\n  i64.add\n");
    print("lines: {d}\n", .{vm_line_count});   // 3