aboutsummaryrefslogtreecommitdiff
path: root/object/object.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2024-04-10 20:42:41 +0000
committerBobby <[email protected]>2024-04-10 20:42:41 +0000
commitca46690f9166681e4b32af90e28fb215c12f76c0 (patch)
tree1b8ac7bf26616bcc0bdc3038221997d30d66d751 /object/object.go
parente7b7baba5e6485ae6e241eee4c3f30557afa0fa8 (diff)
downloadmana-ca46690f9166681e4b32af90e28fb215c12f76c0.tar.xz
mana-ca46690f9166681e4b32af90e28fb215c12f76c0.zip
hashes
Diffstat (limited to 'object/object.go')
-rw-r--r--object/object.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/object/object.go b/object/object.go
index 9fe896e..22d0441 100644
--- a/object/object.go
+++ b/object/object.go
@@ -3,6 +3,7 @@ package object
import (
"bytes"
"fmt"
+ "hash/fnv"
"mana/ast"
"strings"
)
@@ -19,6 +20,7 @@ const (
ERROR_OBJ = "ERROR"
BUILTIN_OBJ = "BUILTIN"
ARRAY_OBJ = "ARRAY"
+ HASH_OBJ = "HASH"
)
type Object interface {
@@ -144,3 +146,61 @@ func (ao *Array) Inspect() string {
return out.String()
}
+
+type HashKey struct {
+ Type ObjectType
+ Value uint64
+}
+
+func (b *Boolean) HashKey() HashKey {
+ var value uint64
+
+ if b.Value {
+ value = 1
+ } else {
+ value = 0
+ }
+
+ return HashKey{Type: b.Type(), Value: value}
+}
+
+func (i *Integer) HashKey() HashKey {
+ return HashKey{Type: i.Type(), Value: uint64(i.Value)}
+}
+
+func (s *String) HashKey() HashKey {
+ h := fnv.New64a()
+ h.Write([]byte(s.Value))
+
+ return HashKey{Type: s.Type(), Value: h.Sum64()}
+}
+
+type HashPair struct {
+ Key Object
+ Value Object
+}
+
+type Hash struct {
+ Pairs map[HashKey]HashPair
+}
+
+func (h *Hash) Type() ObjectType { return HASH_OBJ }
+
+func (h *Hash) Inspect() string {
+ var out bytes.Buffer
+
+ pairs := []string{}
+ for _, pair := range h.Pairs {
+ pairs = append(pairs, fmt.Sprintf("%s: %s", pair.Key.Inspect(), pair.Value.Inspect()))
+ }
+
+ out.WriteString("{")
+ out.WriteString(strings.Join(pairs, ", "))
+ out.WriteString("}")
+
+ return out.String()
+}
+
+type Hashable interface {
+ HashKey() HashKey
+}