@ 0d159351c7131f98bbe83fa0f45b743e0d28b3f9 | history
package eval
import "government/sexpr"
// and and or are special forms rather than functions because they must be
// able to skip evaluating their remaining operands. As in Scheme they
// return the deciding value, not just T or F, so (or x default) is the
// idiom for "x, or default if x is F".
// (and x...) returns F at the first F operand, else the last value.
func evalAnd(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
var result sexpr.Value = sexpr.T
for _, operand := range operands {
v, err := Eval(operand, env)
if err != nil {
return nil, err
}
if !isTrue(v) {
return sexpr.F, nil
}
result = v
}
return result, nil
}
// (or x...) returns the first operand that is not F, else F.
func evalOr(operands []sexpr.Value, env *Env) (sexpr.Value, error) {
for _, operand := range operands {
v, err := Eval(operand, env)
if err != nil {
return nil, err
}
if isTrue(v) {
return v, nil
}
}
return sexpr.F, nil
}