| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package object
- import (
- "bytes"
- "fmt"
- "github/runnignwater/monkey/ast"
- "github/runnignwater/monkey/code"
- "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"
- CompiledFunctionObj = "COMPILED_FUNCTION_OBJ"
- )
- // 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
- var 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
- }
- type CompileFunction struct {
- Instructions code.Instructions
- }
- func (cf *CompileFunction) Type() ObjType { return CompiledFunctionObj }
- func (cf *CompileFunction) Inspect() string {
- return fmt.Sprintf("CompileFunction[%p]", cf)
- }
|