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

Booleans and null

Alongside integers the language has boolean values and a null. Today you add those objects - and since there are only ever two booleans and one null, you make them shared singletons.

The goal

Evaluate boolean literals into shared true and false singleton objects, and define a single null.

Start here - the target
TO DO
Scenario: Evaluating boolean literals
Giventhe source true
Whenthe evaluator evaluates the program
Thenthe result is the boolean object whose value is true and whose Inspect is true
Andevaluating true twice returns the very same object both times
Background

Integers are unbounded, so each one is its own object. But there are only ever two booleans and one null, so allocating a fresh object for each is wasteful and, more importantly, makes comparison awkward. Instead, create them once as package-level singletons - TRUE, FALSE, NULL - and always return those same instances.

This pays off immediately in the next lessons: because true is always the exact same object, you can later test boolean equality by comparing object identity rather than unwrapping values. Evaluating true twice returning the identical object is the observable proof the singletons are shared, not copied.

Make it work
type Boolean struct { Value bool }
type Null struct {}
var (
TRUE = &Boolean{Value: true}
FALSE = &Boolean{Value: false}
NULL = &Null{}
)
// a Boolean literal node -> return TRUE or FALSE (never a fresh object)
CheckpointDONE
Boolean literals evaluate to shared singletons, and null exists. Commit.