Skip whitespace. Write skip() that advances past whitespace characters. Treat any of these four bytes as whitespace: space ' ', tab '\t', newline '\n', carriage return '\r'.
Strategy: we want the cursor to always be sitting on a non-whitespace character when we're about to read something. So we call skip() once at the start of eval to handle leading whitespace, and after every consume so the next reader doesn't have to think about spaces.
check(" 3 + 5 ", 8);
check("100 + 23", 123);
fn is_whitespace(c: u8) bool {
return c == ' ' or c == '\n' or c == '\t' or c == '\r';
}
fn skip() void {
while (is_whitespace(cur())) {
pos += 1;
}
}
You might worry about an input that ends with whitespace -- the loop keeps advancing pos past the last character. But cur() already handles end-of-input: once pos >= source.len, it returns 0, and is_whitespace(0) is false, so the loop stops on its own. No bounds check needed here.
Three places to add skip();:
1. At the start of eval, before parsing begins.
2. At the end of number(), before returning.
3. After the pos += 1 that consumes the operator character. Easy to forget -- the operator is just one byte, so there's no read_operator() helper that would skip for us.