From 77d0ea46b3f1de99357c7706c4c05eb44c237412 Mon Sep 17 00:00:00 2001 From: Bobby Date: Thu, 4 Apr 2024 16:25:56 +0000 Subject: arrays and array indexes --- object/object.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'object') diff --git a/object/object.go b/object/object.go index 24de9be..9fe896e 100644 --- a/object/object.go +++ b/object/object.go @@ -18,6 +18,7 @@ const ( STRING_OBJ = "STRING" ERROR_OBJ = "ERROR" BUILTIN_OBJ = "BUILTIN" + ARRAY_OBJ = "ARRAY" ) type Object interface { @@ -59,6 +60,10 @@ type Builtin struct { Fn BuiltinFunction } +type Array struct { + Elements []Object +} + func (i *Integer) Type() ObjectType { return INTEGER_OBJ } @@ -122,3 +127,20 @@ func (s *String) Inspect() string { return s.Value } func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ } func (b *Builtin) Inspect() string { return "builtin function" } + +func (ao *Array) Type() ObjectType { return ARRAY_OBJ } +func (ao *Array) Inspect() string { + var out bytes.Buffer + + elements := []string{} + + for _, el := range ao.Elements { + elements = append(elements, el.Inspect()) + } + + out.WriteString("[") + out.WriteString(strings.Join(elements, ", ")) + out.WriteString("]") + + return out.String() +} -- cgit v1.2.3