reassign values
Some checks failed
Build / build (push) Failing after 1m46s
Test / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-15 15:24:48 -04:00
parent c818591f79
commit 2988719c9c
13 changed files with 327 additions and 17 deletions

View File

@@ -26,6 +26,12 @@ 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 {
Set(obj Object)
}
type Object interface {
Type() ObjectType
Inspect() string
@@ -47,6 +53,9 @@ 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
}
type Boolean struct {
Value bool
@@ -55,10 +64,12 @@ type Boolean struct {
func (b *Boolean) Type() ObjectType {
return BOOLEAN_OBJ
}
func (b *Boolean) Inspect() string {
return fmt.Sprintf("%t", b.Value)
}
func (i *Boolean) Set(obj Object) {
i.Value = obj.(*Boolean).Value
}
type Null struct{}
@@ -88,10 +99,12 @@ type Error struct {
func (e *Error) Type() ObjectType {
return ERROR_OBJ
}
func (e *Error) Inspect() string {
return "Error: " + e.Message
}
func (e *Error) Set(obj Object) {
e.Message = obj.(*Error).Message
}
type Function struct {
Parameters []*ast.Identifier
@@ -128,10 +141,12 @@ type String struct {
func (s *String) Type() ObjectType {
return STRING_OBJ
}
func (s *String) Inspect() string {
return s.Value
}
func (s *String) Set(obj Object) {
s.Value = obj.(*String).Value
}
type BuiltinFunction func(args ...Object) Object
@@ -150,11 +165,10 @@ type Array struct {
Elements []Object
}
func (ao Array) Type() ObjectType {
func (ao *Array) Type() ObjectType {
return ARRAY_OBJ
}
func (ao Array) Inspect() string {
func (ao *Array) Inspect() string {
var out bytes.Buffer
elements := []string{}
@@ -168,6 +182,9 @@ func (ao Array) Inspect() string {
return out.String()
}
func (ao *Array) Set(obj Object) {
ao.Elements = obj.(*Array).Elements
}
type HashKey struct {
Type ObjectType
@@ -213,7 +230,6 @@ type Hash struct {
func (h *Hash) Type() ObjectType {
return HASH_OBJ
}
func (h *Hash) Inspect() string {
var out bytes.Buffer
@@ -228,6 +244,9 @@ func (h *Hash) Inspect() string {
return out.String()
}
func (h *Hash) Set(obj Object) {
h.Pairs = obj.(*Hash).Pairs
}
func (cf *CompiledFunction) Type() ObjectType {
return COMPILED_FUNCTION_OBJ