clone
Some checks failed
Build / build (push) Failing after 1m45s
Test / build (push) Failing after 12m0s

This commit is contained in:
Chuck Smith
2024-03-15 15:35:45 -04:00
parent 2988719c9c
commit 80f1a2d78c
6 changed files with 59 additions and 6 deletions

View File

@@ -29,6 +29,7 @@ const (
// 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 {
Clone() Object
Set(obj Object)
}
@@ -56,6 +57,9 @@ func (i *Integer) Inspect() string {
func (i *Integer) Set(obj Object) {
i.Value = obj.(*Integer).Value
}
func (i *Integer) Clone() Object {
return &Integer{Value: i.Value}
}
type Boolean struct {
Value bool
@@ -67,8 +71,11 @@ func (b *Boolean) Type() ObjectType {
func (b *Boolean) Inspect() string {
return fmt.Sprintf("%t", b.Value)
}
func (i *Boolean) Set(obj Object) {
i.Value = obj.(*Boolean).Value
func (b *Boolean) Set(obj Object) {
b.Value = obj.(*Boolean).Value
}
func (b *Boolean) Clone() Object {
return &Boolean{Value: b.Value}
}
type Null struct{}
@@ -105,6 +112,9 @@ func (e *Error) Inspect() string {
func (e *Error) Set(obj Object) {
e.Message = obj.(*Error).Message
}
func (e *Error) Clone() Object {
return &Error{Message: e.Message}
}
type Function struct {
Parameters []*ast.Identifier
@@ -147,6 +157,9 @@ func (s *String) Inspect() string {
func (s *String) Set(obj Object) {
s.Value = obj.(*String).Value
}
func (s *String) Clone() Object {
return &String{Value: s.Value}
}
type BuiltinFunction func(args ...Object) Object
@@ -185,6 +198,11 @@ func (ao *Array) Inspect() 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
@@ -247,6 +265,13 @@ func (h *Hash) Inspect() 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