interpreter-in-go/pkg/object/hash_key.go
jmug fb25a86b91 Done with the book! Added hash maps and the puts builtin.
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
2025-01-10 21:09:44 -08:00

30 lines
502 B
Go

package object
import "hash/fnv"
type Hashable interface {
HashKey() HashKey
}
type HashKey struct {
Type ObjectType
Value uint64
}
func (b *Boolean) HashKey() HashKey {
var val uint64 = 0
if b.Value {
val = 1
}
return HashKey{Type: b.Type(), Value: val}
}
func (i *Integer) HashKey() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
}
func (s *String) HashKey() HashKey {
h := fnv.New64()
h.Write([]byte(s.Value))
return HashKey{Type: s.Type(), Value: h.Sum64()}
}