The Snake Eats Its Tail
test-self-three-plus-five

Test the self-hosted compiler on a simple expression. Feed it "3 + 5" as input and check the WAT output.

We can do this entirely in the Zig test harness:

fn test_self_hosted(compiler_src: []const u8, input: []const u8) void {
    // Step 1: Compile the compiler from our language to WAT
    c_program(compiler_src);
    const compiler_wat: []u8 = out[0..out_len];

    // Step 2: Run the compiler WAT in the VM, with 'input' as its stdin
    // Load input into vm_memory at address 0
    for (input, 0..) |c, i| { vm_memory[i] = c; }
    // Set up read_input to return the length
    const result_wat: []const u8 = vm_run_compiler(compiler_wat, input.len);

    // Step 3: The VM's output (in vm_memory at 100000+) is the compiled WAT of 'input'
    print("Self-hosted output:\n{s}\n", .{result_wat});
}

The output should be:

(module
(memory (export "memory") 1)
(func (export "main") (result i64)
  i64.const 3
  i64.const 5
  i64.add
)
)

If it is: the self-hosted compiler, running as WAT inside our VM, produced correct WAT for 3 + 5. The compiler compiled.