Now conditionals actually choose. Today you evaluate an if expression - test the condition's truthiness, run the matching block, and decide what an if with no matching branch produces.
Evaluate an if expression by running the branch its condition selects.
Evaluating an if ties together two things you already built: truthiness
(from the bang lesson) decides which way to go, and block evaluation (from the
last lesson) produces the chosen branch’s value. Evaluate the condition, and if
it is truthy run the consequence; otherwise run the alternative if there is one.
The boundary case is an if whose condition is falsy and that has no else:
there is nothing to run, so it evaluates to null. That is why if is an
expression in this language - it always produces a value, even if that value is
null. This same truthiness test will drive while at the end of the chapter.
func evalIf(ie *IfExpression) Object {cond := Eval(ie.Condition)if isTruthy(cond) {return Eval(ie.Consequence)} else if ie.Alternative != nil {return Eval(ie.Alternative)}return NULL}