package object import ( "bytes" "fmt" "github/runnignwater/monkey/ast" "hash/fnv" "strings" ) // ObjType type of object type ObjType string const ( IntegerObj = "INTEGER" BooleanObj = "BOOLEAN" NullObj = "NULL" ReturnValueObj = "RETURN_VALUE" ErrorObj = "ERROR" FunctionObj = "FUNCTION" StringObj = "STRING" BuiltinObj = "BUILTIN" ArrayObj = "ARRAY" HashObj = "HASH" ) // Object source code as an Object type Object interface { Type() ObjType Inspect() string } // BuiltinFunction 内建函数数据 type BuiltinFunction func(args ...Object) Object type HashKey struct { Type ObjType Value uint64 } type Integer struct { Value int64 } func (i *Integer) Type() ObjType { return IntegerObj } 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() ObjType { return BooleanObj } 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() ObjType { return NullObj } func (n *Null) Inspect() string { return "null" } type ReturnValue struct { Value Object } func (rv *ReturnValue) Type() ObjType { return ReturnValueObj } func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() } type Error struct { Msg string } func (e *Error) Type() ObjType { return ErrorObj } func (e *Error) Inspect() string { return "ERROR: " + e.Msg } type Function struct { Parameters []*ast.Identifier Body *ast.BlockStatement Env *Environment } func (f *Function) Type() ObjType { return FunctionObj } 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() ObjType { return StringObj } func (s *String) Inspect() string { return s.Value } func (s *String) HashKey() HashKey { h := fnv.New64a() _, err := h.Write([]byte(s.Value)) if err != nil { return HashKey{Type: NullObj} } return HashKey{Type: s.Type(), Value: h.Sum64()} } type Array struct { Elements []Object } func (ao *Array) Type() ObjType { return ArrayObj } 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 Builtin struct { Fn BuiltinFunction } func (b *Builtin) Type() ObjType { return BuiltinObj } func (b *Builtin) Inspect() string { return "builtin function" } type HashPair struct { Key Object Value Object } type Hash struct { Pairs map[HashKey]HashPair } func (h *Hash) Type() ObjType { return HashObj } 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 Hashtable interface { HashKey() HashKey }