misc fixes
This commit is contained in:
@@ -18,3 +18,17 @@ func (e BinaryOpError) Error() string {
|
||||
func NewBinaryOpError(left, right Object, op string) BinaryOpError {
|
||||
return BinaryOpError{left, right, op}
|
||||
}
|
||||
|
||||
// DivisionByZeroError is an error returned when dividing by zero
|
||||
type DivisionByZeroError struct {
|
||||
left Object
|
||||
}
|
||||
|
||||
func (e DivisionByZeroError) Error() string {
|
||||
return fmt.Sprintf("cannot divide %s by zero", e.left)
|
||||
}
|
||||
|
||||
// NewDivisionByZeroError returns a new DivisionByZeroError
|
||||
func NewDivisionByZeroError(left Object) DivisionByZeroError {
|
||||
return DivisionByZeroError{left}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,9 @@ func (i Integer) Mul(other Object) (Object, error) {
|
||||
func (i Integer) Div(other Object) (Object, error) {
|
||||
switch obj := other.(type) {
|
||||
case Integer:
|
||||
if obj.Value == 0 {
|
||||
return nil, NewDivisionByZeroError(i)
|
||||
}
|
||||
return Integer{i.Value / obj.Value}, nil
|
||||
default:
|
||||
return nil, NewBinaryOpError(i, other, "/")
|
||||
|
||||
Reference in New Issue
Block a user