Now grow parseNumber to actually parse multi-digit numbers, not just one.
A small helper to keep the digit check readable -- we'll reach for it more than once:
fn isDigit(c: u8) bool {
return c >= '0' and c <= '9';
}
Hint: loop while position is in range AND the current char isDigit, doing value = value * 10 + digit each step. To parse 123: 1*10+2=12, then 12*10+3=123. Stop on anything that isn't a digit. The bounds check in the loop condition stops us from running off the end of buffer -- needed because parseNumber will be called with non-sentinel slices later (the WAT lines in the VM).
const input: [:0]const u8 = "12+3"; // 15
const input: [:0]const u8 = "100+23"; // 123
const input: [:0]const u8 = "12*12"; // 144
fn parseNumber(
buffer: []const u8,
position: *usize,
) i64 {
var value: i64 = 0;
while (position.* < buffer.len and isDigit(buffer[position.*])) {
value = value * 10 + (@as(i64, buffer[position.*]) - '0');
position.* += 1;
}
return value;
}
The interpreter call sites didn't change. That's the helper paying off.
The compiler and the VM still live in single-digit land -- they're next.