Handle chains: "3+5+2" -> 10. After reading the first number, loop: while there's a character left, read the operator and the next number, apply it.
check("3+5+2", 10);
check("9-3-1", 5);
fn eval(input: []const u8) i64 {
source = input;
pos = 0;
var val: i64 = digit(cur());
pos += 1;
while (cur() != 0) {
const op: u8 = cur();
pos += 1;
const right: i64 = digit(cur());
pos += 1;
val = switch (op) {
'+' => val + right,
'-' => val - right,
'*' => val * right,
'/' => @divTrunc(val, right),
else => val,
};
}
return val;
}
Look at that -- we went from fixed positions to a loop, and suddenly we can handle arbitrarily long expressions.