Now "3+4+5" should give 12. Read the first digit, then loop: skip the +, read the next digit, add it.
Same shape will turn up everywhere.
pub fn main() void {
const input: [:0]const u8 = "3+4+5";
var input_pos: usize = 0;
var result: i64 = @as(i64, input[input_pos]) - '0';
input_pos += 1;
// YOU: while input_pos < input.len:
// skip the '+', read the next digit, add it to result.
printString("interpreter result for ");
printString(input);
printString(" : ");
printNumber(result);
printChar('\n');
// (the WAT-toggle / VM section from compile-to-wat is still below.
// the hand-written WAT and the compiler still only know "3+4",
// so the vm result will be wrong until we fix them.)
}
while (input_pos < input.len) {
input_pos += 1;
const next_digit: i64 = @as(i64, input[input_pos]) - '0';
input_pos += 1;
result = result + next_digit;
}
The input_pos += 1 at the top skips the +. Then we read the next digit and bump past it.
interpreter result for 3+4+5 : 12. The vm result: line will still say 7 until the next two problems catch up.