rearrange builtins
This commit is contained in:
51
object/array.go
Normal file
51
object/array.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Array is the array literal type that holds a slice of Object(s)
|
||||
type Array struct {
|
||||
Elements []Object
|
||||
}
|
||||
|
||||
func (ao *Array) Type() ObjectType {
|
||||
return ARRAY_OBJ
|
||||
}
|
||||
func (ao *Array) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
var elements []string
|
||||
for _, e := range ao.Elements {
|
||||
elements = append(elements, e.Inspect())
|
||||
}
|
||||
|
||||
out.WriteString("[")
|
||||
out.WriteString(strings.Join(elements, ", "))
|
||||
out.WriteString("]")
|
||||
|
||||
return out.String()
|
||||
}
|
||||
func (ao *Array) String() string {
|
||||
return ao.Inspect()
|
||||
}
|
||||
func (ao *Array) Equal(other Object) bool {
|
||||
if obj, ok := other.(*Array); ok {
|
||||
if len(ao.Elements) != len(obj.Elements) {
|
||||
return false
|
||||
}
|
||||
for i, el := range ao.Elements {
|
||||
cmp, ok := el.(Comparable)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if !cmp.Equal(obj.Elements[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user