Formulas compare values too, and comparisons bind more loosely than arithmetic so the sides are computed first. Today you add the comparison operators at the lowest precedence level, which IF will later depend on.
Parse the comparison operators at a precedence below arithmetic.
Spreadsheet formulas do not just compute numbers, they ask questions: is A1
greater than zero? The six comparison operators - >, <, >=, <=, =
(equal), and <> (not equal) - slot straight into the precedence machinery you
already have. The only decision is where they sit, and the answer is below
arithmetic: they get the lowest binding power.
That ordering is what makes 1+2>3 mean “is the sum greater than three” rather than
“is 1 plus the comparison”. With comparison at power 10 and + at 20, the parser
folds the addition into a subtree first, then makes it the left side of the
comparison - printing ((1 + 2) > 3). Comparisons produce a yes/no result, which is
why we built the Bool value kind back in Chapter 1; the evaluator will turn these
nodes into booleans later, and IF will branch on them.
// comparisons get the LOWEST binding power so arithmetic groups firstfunc bp(k TokKind) int {switch k {case TStar, TSlash: return 30case TPlus, TMinus: return 20case TGt, TLt, TGe, TLe, TEq, TNe: return 10}return 0}