package subex import ( "main/walk" "errors" ) func binopAdd(values []walk.Value) ([]walk.Value, error) { if len(values) != 2 { return nil, errors.New("Tried to sum a weird number of values") } lhs, lhsIsNumber := values[0].(walk.NumberValue) if !lhsIsNumber { return nil, errors.New("Tried to sum a lhs that is not a number") } rhs, rhsIsNumber := values[1].(walk.NumberValue) if !rhsIsNumber { return nil, errors.New("Tried to sum a rhs that is not a number") } return []walk.Value{walk.NumberValue(float64(lhs) + float64(rhs))}, nil } func binopMultiply(values []walk.Value) ([]walk.Value, error) { if len(values) != 2 { return nil, errors.New("Tried to multiply a weird number of values") } lhs, lhsIsNumber := values[0].(walk.NumberValue) if !lhsIsNumber { return nil, errors.New("Tried to multiply a lhs that is not a number") } rhs, rhsIsNumber := values[1].(walk.NumberValue) if !rhsIsNumber { return nil, errors.New("Tried to multiply a rhs that is not a number") } return []walk.Value{walk.NumberValue(float64(lhs) * float64(rhs))}, nil } func binopDivide(values []walk.Value) ([]walk.Value, error) { if len(values) != 2 { return nil, errors.New("Tried to divide a weird number of values") } lhs, lhsIsNumber := values[0].(walk.NumberValue) if !lhsIsNumber { return nil, errors.New("Tried to divide a lhs that is not a number") } rhs, rhsIsNumber := values[1].(walk.NumberValue) if !rhsIsNumber { return nil, errors.New("Tried to divide a rhs that is not a number") } return []walk.Value{walk.NumberValue(float64(lhs) / float64(rhs))}, nil } func arithmeticSum(values []walk.Value) ([]walk.Value, error) { var total float64 = 0 for _, value := range values { n, isNumber := value.(walk.NumberValue) if !isNumber { return nil, errors.New("Tried to sum non-number value") } total += float64(n) } return []walk.Value{walk.NumberValue(total)}, nil } func arithmeticProduct(values []walk.Value) ([]walk.Value, error) { var product float64 = 0 for _, value := range values { n, isNumber := value.(walk.NumberValue) if !isNumber { return nil, errors.New("Tried to sum non-number value") } product *= float64(n) } return []walk.Value{walk.NumberValue(product)}, nil }