Refactor VM and operators
Some checks failed
Build / build (push) Failing after 5m54s
Publish Image / publish (push) Failing after 44s
Test / build (push) Failing after 5m21s

This commit is contained in:
Chuck Smith
2024-03-29 17:59:34 -04:00
parent 7435a993d9
commit 3b6df3e813
11 changed files with 979 additions and 639 deletions

View File

@@ -26,6 +26,94 @@ 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 {