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

Evaluating a sequence of statements

A program is many statements, but evaluating one produces one value. Today you define what a whole program evaluates to - the value of its last statement - and the same rule for the statements inside a block.

The goal

Evaluate a program and a block to the value of their final statement.

Start here - the target
TO DO
Scenario: A program evaluates to its last statement
Giventhe source 1; 2; 3
Whenthe evaluator evaluates the program
Thenthe result is 3, the value of the last statement
Andevaluating 5 alone yields 5, and an empty program yields null
Background

So far you have evaluated single expressions. A real program is a list of statements, so you must decide what the whole thing produces. The rule here is simple and useful in a REPL: evaluate the statements in order and the program’s value is the value of the last one. An empty program is null.

Blocks - the { ... } bodies inside if and while - follow the same rule, so write one helper that folds a statement list down to its final value and reuse it for both the top-level program and blocks. This is what lets if (true) { 10 } produce 10 in the very next lessons: the consequence block evaluates to its last statement. There is one subtlety with return that jumps out early - you will handle that in two lessons.

Make it work
func evalStatements(stmts []Statement) Object {
var result Object = NULL
for _, s := range stmts {
result = Eval(s)
}
return result
}
CheckpointDONE
A program and a block evaluate to the value of their final statement. Commit.