Files
monkey/internal/object/int.go
Chuck Smith 3b6df3e813
Some checks failed
Build / build (push) Failing after 5m54s
Publish Image / publish (push) Failing after 44s
Test / build (push) Failing after 5m21s
Refactor VM and operators
2024-03-29 17:59:34 -04:00

130 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) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "+")
}
return &Integer{i.Value + other.(*Integer).Value}, nil
}
func (i *Integer) Sub(other Object) (Object, error) {
if !AssertTypes(other, IntegerType) {
return nil, NewBinaryOpError(i, other, "-")
}
return &Integer{i.Value - other.(*Integer).Value}, nil
}
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 {
if obj, ok := other.(*Integer); ok {
switch {
case i.Value < obj.Value:
return -1
case i.Value > obj.Value:
return 1
default:
return 0
}
}
return -1
}