Files
monkey/internal/object/int.go
Charles Smith aebbe43999
Some checks failed
Build / build (push) Successful in 10m25s
Publish Image / publish (push) Failing after 39s
Test / build (push) Successful in 11m19s
Fix VM memory allocation optimizations by reducing what we allocate on the heap
2024-03-31 20:44:50 -04:00

130 lines
2.8 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
}