build-a-programming-language / lesson-26.md
Lesson 26 · Evaluation

Comparisons and equality

Expressions that compare produce booleans. Today you evaluate the comparison operators on integers and the equality operators - and equality is where the boolean singletons finally earn their keep.

The goal

Evaluate comparison operators on integers and equality operators, returning the boolean singletons.

Start here - the target
TO DO
Scenario: Evaluating comparisons and equality
Givencomparison and equality expressions
Whenthe evaluator evaluates 1 < 2, then 2 == 2, then true == true, then (1 < 2) == true
Thenthe results are true, true, true, and true
Andboolean equality compares object identity, so true == true is true and true != false is true
Background

Comparison operators take integers and produce booleans: < and > compare values, and ==/!= test equality. Always return the shared TRUE/FALSE singletons, never fresh boolean objects - a small helper that maps a native bool to the singleton keeps this consistent.

Equality on booleans is where the singleton design pays off: because true is always the same object, true == true can be decided by comparing the objects themselves for identity, no value-unwrapping needed. And since a comparison like 1 < 2 already yields the TRUE singleton, (1 < 2) == true compares two identical objects and is true. That uniformity is exactly why you made them singletons back in lesson 22.

Make it work
// integer operands: compare .Value for < > == !=, return TRUE/FALSE
// boolean operands: compare the objects themselves for == and !=
// (works because TRUE and FALSE are singletons)
func nativeBool(b bool) *Boolean { if b { return TRUE }; return FALSE }
CheckpointDONE
Comparisons and equality evaluate to the boolean singletons. Commit.