package object // import "monkey/object" import ( "bytes" "fmt" "hash/fnv" "strings" "monkey/ast" ) type BuiltinFunction func(args ...Object) Object //go:generate stringer -type=ObjectType type ObjectType int const ( NULL ObjectType = iota ERROR INTEGER BOOLEAN STRING RETURN_VALUE FUNCTION BUILTIN ARRAY HASH QUOTE MACRO ) type HashKey struct { Type ObjectType Value uint64 } type Hashable interface { HashKey() HashKey } type Object interface { Type() ObjectType Inspect() string } type Integer struct { Value int64 } func (i *Integer) Type() ObjectType { return INTEGER } func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) } func (i *Integer) HashKey() HashKey { return HashKey{Type: i.Type(), Value: uint64(i.Value)} } type Boolean struct { Value bool } func (b *Boolean) Type() ObjectType { return BOOLEAN } func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) } func (b *Boolean) HashKey() HashKey { var value uint64 if b.Value { value = 1 } else { value = 0 } return HashKey{Type: b.Type(), Value: value} } type Null struct{} func (n *Null) Type() ObjectType { return NULL } func (n *Null) Inspect() string { return "null" } type ReturnValue struct { Value Object } func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE } func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() } type Error struct { Message string } func (e *Error) Type() ObjectType { return ERROR } func (e *Error) Inspect() string { return "ERROR: " + e.Message } type Function struct { Parameters []*ast.Identifier Body *ast.BlockStatement Env *Environment } func (f *Function) Type() ObjectType { return FUNCTION } func (f *Function) Inspect() string { var out bytes.Buffer params := []string{} for _, p := range f.Parameters { params = append(params, p.String()) } out.WriteString("fn") out.WriteString("(") out.WriteString(strings.Join(params, ", ")) out.WriteString(") {\n") out.WriteString(f.Body.String()) out.WriteString("\n}") return out.String() } type String struct { Value string } func (s *String) Type() ObjectType { return STRING } func (s *String) Inspect() string { return s.Value } func (s *String) HashKey() HashKey { h := fnv.New64a() h.Write([]byte(s.Value)) return HashKey{Type: s.Type(), Value: h.Sum64()} } type Builtin struct { Fn BuiltinFunction } func (b *Builtin) Type() ObjectType { return BUILTIN } func (b *Builtin) Inspect() string { return "builtin function" } type Array struct { Elements []Object } func (ao *Array) Type() ObjectType { return ARRAY } func (ao *Array) Inspect() string { var out bytes.Buffer elements := []string{} for _, e := range ao.Elements { elements = append(elements, e.Inspect()) } out.WriteString("[") out.WriteString(strings.Join(elements, ", ")) out.WriteString("]") return out.String() } type HashPair struct { Key Object Value Object } type Hash struct { Pairs map[HashKey]HashPair } func (h *Hash) Type() ObjectType { return HASH } 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 Quote struct { Node ast.Node } func (q *Quote) Type() ObjectType { return QUOTE } func (q *Quote) Inspect() string { return "QUOTE(" + q.Node.String() + ")" } type Macro struct { Parameters []*ast.Identifier Body *ast.BlockStatement Env *Environment } func (m *Macro) Type() ObjectType { return MACRO } func (m *Macro) Inspect() string { var out bytes.Buffer params := []string{} for _, p := range m.Parameters { params = append(params, p.String()) } out.WriteString("macro") out.WriteString("(") out.WriteString(strings.Join(params, ", ")) out.WriteString(") {\n") out.WriteString(m.Body.String()) out.WriteString("\n}") return out.String() }