compiler-in-go/pkg/object/hash_key.go
jmug 0acd1d41e8 Add all the files from the interpreter repo
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
2025-01-13 19:51:49 -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()}
}