Files
monkey/internal/object/int.go
csmith f9e6e164b0
Some checks failed
Build / build (push) Successful in 10m40s
Publish Image / publish (push) Failing after 34s
Test / build (push) Has been cancelled
further improvements
2024-04-01 17:02:44 -04:00

135 lines
2.9 KiB
Go

package object
import "fmt"
type Integer struct {
Value int64
}
func (i Integer) Bool() bool {
return i.Value != 0
}
func (i Integer) Type() Type {
return IntegerType
}
func (i Integer) Inspect() string {
return fmt.Sprintf("%d", i.Value)
}
func (i Integer) Clone() Object {
return Integer{Value: i.Value}
}
func (i Integer) String() string {
return i.Inspect()
}
func (i Integer) Add(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Integer{i.Value + obj.Value}, nil
default:
return nil, NewBinaryOpError(i, other, "+")
}
}
func (i Integer) Sub(other Object) (Object, error) {
switch obj := other.(type) {
case Integer:
return Integer{i.Value - obj.Value}, nil
default:
return nil, NewBinaryOpError(i, other, "-")
}
}
func (i Integer) Mul(other Object) (Object, error) {
switch other.Type() {
case IntegerType:
return Integer{i.Value * other.(Integer).Value}, nil
case StringType:
return other.(Mul).Mul(i)
case ArrayType:
return other.(Mul).Mul(i)
default:
return nil, NewBinaryOpError(i, other, "*")
}
}
func (i Integer) Div(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "/")
}
return Integer{i.Value / other.(Integer).Value}, nil
}
func (i Integer) Mod(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "%")
}
return Integer{i.Value % other.(Integer).Value}, nil
}
func (i Integer) BitwiseOr(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "|")
}
return Integer{i.Value | other.(Integer).Value}, nil
}
func (i Integer) BitwiseXor(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "^")
}
return Integer{i.Value ^ other.(Integer).Value}, nil
}
func (i Integer) BitwiseAnd(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "")
}
return Integer{i.Value & other.(Integer).Value}, nil
}
func (i Integer) BitwiseNot() Object {
return Integer{^i.Value}
}
func (i Integer) LeftShift(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "<<")
}
return Integer{i.Value << other.(Integer).Value}, nil
}
func (i Integer) RightShift(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, ">>")
}
return Integer{i.Value >> other.(Integer).Value}, nil
}
func (i Integer) LogicalNot() Object {
return Boolean{!i.Bool()}
}
func (i Integer) Negate() Object {
return Integer{-i.Value}
}
func (i Integer) Compare(other Object) int {
switch obj := other.(type) {
case Integer:
if i.Value < obj.Value {
return -1
}
if i.Value > obj.Value {
return 1
}
return 0
default:
return -1
}
}