build-an-expression-evaluator / lesson-17.md
Lesson 17 · Evaluating the AST

Evaluating unary minus

The negation node negates the value it wraps, and evaluating it confirms the precedence choice you made for -2^2. Today you handle the one prefix node and see the convention produce its number.

The goal

Evaluate a Un negation node by negating its operand.

Start here - the target
TO DO
Scenario: Unary minus negates its operand
Giventhe expression string "-5"
Whenit is evaluated with EvalString
Thenthe result is -5
Andevaluating "-2 ^ 2" gives -4 (the power binds first), and "-(2 + 3)" gives -5
Background

A Un node evaluates by evaluating its single operand and negating the result. Like the binary case, the evaluator does nothing clever: it trusts the tree. The interesting result is -2 ^ 2, which evaluates to -4. That is the convention you chose back when you set the binding powers, where the power binds tighter than the leading minus, so the tree is -(2 ^ 2). The evaluator squares 2 to 4 and then negates it. A different convention would have given 4, and the difference lives entirely in the parse tree, not here.

-(2 + 3) giving -5 shows the same thing with grouping: the parentheses forced the addition into the operand, and negation applied to the whole sum. With this, every arithmetic expression the parser can build now evaluates to a number. The next lesson puts the whole numeric core through its paces at once.

Make it work
case *Un:
v, err := Eval(n.Operand, env)
if err != nil { return 0, err }
return -v, nil // the only prefix operator is negation
CheckpointDONE
The evaluator negates values, and -2 ^ 2 evaluates to -4 as chosen. Commit and stop here.