Feeding the Snake
emit-wat-in-language

Write a program in our language that outputs WAT for a hardcoded expression:

    check(
        \\fn emit_byte(b: i64) i64 {
        \\    store8(50000 + out_len, b);
        \\    out_len = out_len + 1;
        \\    return 0;
        \\}
        \\fn emit_s(s: i64) i64 {
        \\    var addr: i64 = s / 65536;
        \\    var len: i64 = s - addr * 65536;
        \\    var i: i64 = 0;
        \\    while (i < len) {
        \\        emit_byte(load8(addr + i));
        \\        i = i + 1;
        \\    }
        \\    return 0;
        \\}
        \\var out_len: i64 = 0;
        \\emit_s("  i64.const 3\n");
        \\emit_s("  i64.const 5\n");
        \\emit_s("  i64.add\n");
        \\out_len
    , 39);

That program writes WAT instructions into memory starting at address 50000. The emit_byte and emit_s functions are the compiler's output mechanism -- the same emit_byte and emit_str we wrote in Zig, now expressed in our own language.

The test checks that 39 bytes were written (three lines of WAT). To verify the content, we could read back the bytes with load8 -- or we could trust the string tests and move on.