Refactor VM and operators
This commit is contained in:
@@ -2,6 +2,7 @@ package object
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -18,30 +19,30 @@ func (ao *Array) Bool() bool {
|
||||
return len(ao.Elements) > 0
|
||||
}
|
||||
|
||||
func (a *Array) PopLeft() Object {
|
||||
if len(a.Elements) > 0 {
|
||||
e := a.Elements[0]
|
||||
a.Elements = a.Elements[1:]
|
||||
func (ao *Array) PopLeft() Object {
|
||||
if len(ao.Elements) > 0 {
|
||||
e := ao.Elements[0]
|
||||
ao.Elements = ao.Elements[1:]
|
||||
return e
|
||||
}
|
||||
return &Null{}
|
||||
}
|
||||
|
||||
func (a *Array) PopRight() Object {
|
||||
if len(a.Elements) > 0 {
|
||||
e := a.Elements[(len(a.Elements) - 1)]
|
||||
a.Elements = a.Elements[:(len(a.Elements) - 1)]
|
||||
func (ao *Array) PopRight() Object {
|
||||
if len(ao.Elements) > 0 {
|
||||
e := ao.Elements[(len(ao.Elements) - 1)]
|
||||
ao.Elements = ao.Elements[:(len(ao.Elements) - 1)]
|
||||
return e
|
||||
}
|
||||
return &Null{}
|
||||
}
|
||||
|
||||
func (a *Array) Prepend(obj Object) {
|
||||
a.Elements = append([]Object{obj}, a.Elements...)
|
||||
func (ao *Array) Prepend(obj Object) {
|
||||
ao.Elements = append([]Object{obj}, ao.Elements...)
|
||||
}
|
||||
|
||||
func (a *Array) Append(obj Object) {
|
||||
a.Elements = append(a.Elements, obj)
|
||||
func (ao *Array) Append(obj Object) {
|
||||
ao.Elements = append(ao.Elements, obj)
|
||||
}
|
||||
|
||||
func (ao *Array) Inspect() string {
|
||||
@@ -69,6 +70,63 @@ func (ao *Array) Less(i, j int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (ao *Array) Add(other Object) (Object, error) {
|
||||
if other.Type() != ao.Type() {
|
||||
return nil, NewBinaryOpError(ao, other, "+")
|
||||
}
|
||||
|
||||
var elements []Object
|
||||
elements = append(elements, ao.Elements...)
|
||||
elements = append(elements, other.(*Array).Elements...)
|
||||
|
||||
return &Array{Elements: elements}, nil
|
||||
|
||||
}
|
||||
|
||||
func (ao *Array) Mul(other Object) (Object, error) {
|
||||
if other.Type() != IntegerType {
|
||||
return nil, NewBinaryOpError(ao, other, "*")
|
||||
}
|
||||
|
||||
var elements []Object
|
||||
N := int(other.(*Integer).Value)
|
||||
for i := 0; i < N; i++ {
|
||||
elements = append(elements, ao.Elements...)
|
||||
}
|
||||
|
||||
return &Array{Elements: elements}, nil
|
||||
}
|
||||
|
||||
func (ao *Array) Get(index Object) (Object, error) {
|
||||
if !AssertTypes(index, IntegerType) {
|
||||
return nil, fmt.Errorf("invalid type for array index, expected Integer got %s", index.Type())
|
||||
}
|
||||
|
||||
i := index.(*Integer).Value
|
||||
N := int64(len(ao.Elements))
|
||||
if i < 0 || i >= N {
|
||||
return &Null{}, nil
|
||||
}
|
||||
|
||||
return ao.Elements[i], nil
|
||||
}
|
||||
|
||||
func (ao *Array) Set(index, other Object) error {
|
||||
if !AssertTypes(index, IntegerType) {
|
||||
return fmt.Errorf("invalid type for array index, expected Integer got %s", index.Type())
|
||||
}
|
||||
|
||||
i := index.(*Integer).Value
|
||||
N := int64(len(ao.Elements))
|
||||
if i < 0 || i >= N {
|
||||
return fmt.Errorf("index out of bounds %d with array length %d", i, N)
|
||||
}
|
||||
|
||||
ao.Elements[i] = other
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ao *Array) Compare(other Object) int {
|
||||
if obj, ok := other.(*Array); ok {
|
||||
if len(ao.Elements) != len(obj.Elements) {
|
||||
|
||||
Reference in New Issue
Block a user