35 lines
926 B
Go
35 lines
926 B
Go
package object
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// BinaryOpError is an binary operation error usually resulting from mismatched types
|
|
type BinaryOpError struct {
|
|
left, right Object
|
|
op string
|
|
}
|
|
|
|
func (e BinaryOpError) Error() string {
|
|
return fmt.Sprintf("unsupported types for binary operation: %s %s %s %s %s", e.left.Type(), e.left.Inspect(), e.op, e.right.Type(), e.right.Inspect())
|
|
}
|
|
|
|
// NewBinaryOpError returns a new BinaryOpError
|
|
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}
|
|
}
|