Compiler version of chain-interpret. Same loop shape, but the body emits text.
We restart input_pos at 0 because the interpreter already walked it to the end. After the first const-emit, loop: skip the +, emit the next const, emit add.
} else {
input_pos = 0;
appendStringLiteral(&wat_buffer, &wat_length, "i64.const ");
appendChar(&wat_buffer, &wat_length, input[input_pos]);
appendChar(&wat_buffer, &wat_length, '\n');
input_pos += 1;
// YOU: while input_pos < input.len:
// skip the '+', emit "i64.const X\n" for the next digit, emit "i64.add\n".
}
Set write_wat_manually = false and run.
while (input_pos < input.len) {
input_pos += 1;
appendStringLiteral(&wat_buffer, &wat_length, "i64.const ");
appendChar(&wat_buffer, &wat_length, input[input_pos]);
appendChar(&wat_buffer, &wat_length, '\n');
appendStringLiteral(&wat_buffer, &wat_length, "i64.add\n");
input_pos += 1;
}
vm result: 12. Three engines, one answer. Try flipping write_wat_manually -- both paths produce the same WAT.