Comparisons like < and == bind looser than arithmetic, so 1 + 2 < 3 compares the sum. Today you add two more precedence levels for the comparison and equality operators.
Parse comparison and equality operators at their own precedence levels, below arithmetic.
Comparisons slot in as two more precedence levels beneath arithmetic: < and
> (call it LESSGREATER) bind looser than +/-, and ==/!= (EQUALS) looser
still. Because arithmetic binds tighter, 1 + 2 < 3 naturally parses the 1 + 2
first and then compares the result, giving ((1 + 2) < 3).
The satisfying part: you write no new parsing code. The Pratt loop from the last
lesson already consults the precedence table, so adding operators is just adding
rows to that table. 5 > 4 == 3 < 4 grouping as ((5 > 4) == (3 < 4)) falls out
for free - a good sign your precedence machinery is right rather than
special-cased.
const ( LOWEST = iota; EQUALS; LESSGREATER; SUM; PRODUCT )// add to the precedence map:// "==" and "!=" -> EQUALS// "<" and ">" -> LESSGREATER// the parseInfix code does not change - only the level table does