fixed mutability
Some checks failed
Build / build (push) Failing after 1m30s
Test / build (push) Failing after 12m1s

This commit is contained in:
Chuck Smith
2024-03-15 15:50:11 -04:00
parent 80f1a2d78c
commit 7463b3af39
7 changed files with 37 additions and 118 deletions

View File

@@ -146,7 +146,11 @@ var Builtins = []struct {
newElements := make([]Object, length+1, length+1)
copy(newElements, arr.Elements)
newElements[length] = args[1]
if immutable, ok := args[1].(Immutable); ok {
newElements[length] = immutable.Clone()
} else {
newElements[length] = args[1]
}
return &Array{Elements: newElements}
},

View File

@@ -26,11 +26,10 @@ const (
CLOSURE_OBJ = "CLOSURE"
)
// Mutable is the interface for all mutable objects which must implement
// the Set() method which rebinds its internal value for assignment statements
type Mutable interface {
// Mutable is the interface for all immutable objects which must implement
// the Clone() method used by binding names to values.
type Immutable interface {
Clone() Object
Set(obj Object)
}
type Object interface {
@@ -54,9 +53,6 @@ func (i *Integer) Type() ObjectType {
func (i *Integer) Inspect() string {
return fmt.Sprintf("%d", i.Value)
}
func (i *Integer) Set(obj Object) {
i.Value = obj.(*Integer).Value
}
func (i *Integer) Clone() Object {
return &Integer{Value: i.Value}
}
@@ -71,9 +67,6 @@ func (b *Boolean) Type() ObjectType {
func (b *Boolean) Inspect() string {
return fmt.Sprintf("%t", b.Value)
}
func (b *Boolean) Set(obj Object) {
b.Value = obj.(*Boolean).Value
}
func (b *Boolean) Clone() Object {
return &Boolean{Value: b.Value}
}
@@ -109,9 +102,6 @@ func (e *Error) Type() ObjectType {
func (e *Error) Inspect() string {
return "Error: " + e.Message
}
func (e *Error) Set(obj Object) {
e.Message = obj.(*Error).Message
}
func (e *Error) Clone() Object {
return &Error{Message: e.Message}
}
@@ -154,9 +144,6 @@ func (s *String) Type() ObjectType {
func (s *String) Inspect() string {
return s.Value
}
func (s *String) Set(obj Object) {
s.Value = obj.(*String).Value
}
func (s *String) Clone() Object {
return &String{Value: s.Value}
}
@@ -195,14 +182,6 @@ func (ao *Array) Inspect() string {
return out.String()
}
func (ao *Array) Set(obj Object) {
ao.Elements = obj.(*Array).Elements
}
func (ao *Array) Clone() Object {
elements := make([]Object, len(ao.Elements))
copy(elements, ao.Elements)
return &Array{Elements: elements}
}
type HashKey struct {
Type ObjectType
@@ -262,21 +241,10 @@ func (h *Hash) Inspect() string {
return out.String()
}
func (h *Hash) Set(obj Object) {
h.Pairs = obj.(*Hash).Pairs
}
func (h *Hash) Clone() Object {
pairs := make(map[HashKey]HashPair)
for k, v := range h.Pairs {
pairs[k] = v
}
return &Hash{Pairs: pairs}
}
func (cf *CompiledFunction) Type() ObjectType {
return COMPILED_FUNCTION_OBJ
}
func (cf *CompiledFunction) Inspect() string {
return fmt.Sprintf("CompiledFunction[%p]", cf)
}
@@ -289,7 +257,6 @@ type Closure struct {
func (c *Closure) Type() ObjectType {
return CLOSURE_OBJ
}
func (c *Closure) Inspect() string {
return fmt.Sprintf("Closure[%p]", c)
}