object.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package object
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github/runnignwater/monkey/ast"
  6. "github/runnignwater/monkey/code"
  7. "hash/fnv"
  8. "strings"
  9. )
  10. // ObjType type of object
  11. type ObjType string
  12. const (
  13. IntegerObj = "INTEGER"
  14. BooleanObj = "BOOLEAN"
  15. NullObj = "NULL"
  16. ReturnValueObj = "RETURN_VALUE"
  17. ErrorObj = "ERROR"
  18. FunctionObj = "FUNCTION"
  19. StringObj = "STRING"
  20. BuiltinObj = "BUILTIN"
  21. ArrayObj = "ARRAY"
  22. HashObj = "HASH"
  23. CompiledFunctionObj = "COMPILED_FUNCTION_OBJ"
  24. )
  25. // Object source code as an Object
  26. type Object interface {
  27. Type() ObjType
  28. Inspect() string
  29. }
  30. // BuiltinFunction 内建函数数据
  31. type BuiltinFunction func(args ...Object) Object
  32. type HashKey struct {
  33. Type ObjType
  34. Value uint64
  35. }
  36. type Integer struct {
  37. Value int64
  38. }
  39. func (i *Integer) Type() ObjType { return IntegerObj }
  40. func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
  41. func (i *Integer) HashKey() HashKey {
  42. return HashKey{Type: i.Type(), Value: uint64(i.Value)}
  43. }
  44. type Boolean struct {
  45. Value bool
  46. }
  47. func (b *Boolean) Type() ObjType { return BooleanObj }
  48. func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
  49. func (b *Boolean) HashKey() HashKey {
  50. var value uint64
  51. if b.Value {
  52. value = 1
  53. } else {
  54. value = 0
  55. }
  56. return HashKey{Type: b.Type(), Value: value}
  57. }
  58. type Null struct{}
  59. func (n *Null) Type() ObjType { return NullObj }
  60. func (n *Null) Inspect() string { return "null" }
  61. type ReturnValue struct {
  62. Value Object
  63. }
  64. func (rv *ReturnValue) Type() ObjType { return ReturnValueObj }
  65. func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
  66. type Error struct {
  67. Msg string
  68. }
  69. func (e *Error) Type() ObjType { return ErrorObj }
  70. func (e *Error) Inspect() string { return "ERROR: " + e.Msg }
  71. type Function struct {
  72. Parameters []*ast.Identifier
  73. Body *ast.BlockStatement
  74. Env *Environment
  75. }
  76. func (f *Function) Type() ObjType { return FunctionObj }
  77. func (f *Function) Inspect() string {
  78. var out bytes.Buffer
  79. var params []string
  80. for _, p := range f.Parameters {
  81. params = append(params, p.String())
  82. }
  83. out.WriteString("fn")
  84. out.WriteString("(")
  85. out.WriteString(strings.Join(params, ", "))
  86. out.WriteString(") {\n")
  87. out.WriteString(f.Body.String())
  88. out.WriteString("\n}")
  89. return out.String()
  90. }
  91. type String struct {
  92. Value string
  93. }
  94. func (s *String) Type() ObjType { return StringObj }
  95. func (s *String) Inspect() string { return s.Value }
  96. func (s *String) HashKey() HashKey {
  97. h := fnv.New64a()
  98. _, err := h.Write([]byte(s.Value))
  99. if err != nil {
  100. return HashKey{Type: NullObj}
  101. }
  102. return HashKey{Type: s.Type(), Value: h.Sum64()}
  103. }
  104. type Array struct {
  105. Elements []Object
  106. }
  107. func (ao *Array) Type() ObjType { return ArrayObj }
  108. func (ao *Array) Inspect() string {
  109. var out bytes.Buffer
  110. elements := []string{}
  111. for _, e := range ao.Elements {
  112. elements = append(elements, e.Inspect())
  113. }
  114. out.WriteString("[")
  115. out.WriteString(strings.Join(elements, ", "))
  116. out.WriteString("]")
  117. return out.String()
  118. }
  119. type Builtin struct {
  120. Fn BuiltinFunction
  121. }
  122. func (b *Builtin) Type() ObjType { return BuiltinObj }
  123. func (b *Builtin) Inspect() string { return "builtin function" }
  124. type HashPair struct {
  125. Key Object
  126. Value Object
  127. }
  128. type Hash struct {
  129. Pairs map[HashKey]HashPair
  130. }
  131. func (h *Hash) Type() ObjType { return HashObj }
  132. func (h *Hash) Inspect() string {
  133. var out bytes.Buffer
  134. pairs := []string{}
  135. for _, pair := range h.Pairs {
  136. pairs = append(pairs, fmt.Sprintf("%s %s",
  137. pair.Key.Inspect(), pair.Value.Inspect()))
  138. }
  139. out.WriteString("{")
  140. out.WriteString(strings.Join(pairs, ", "))
  141. out.WriteString("}")
  142. return out.String()
  143. }
  144. type Hashtable interface {
  145. HashKey() HashKey
  146. }
  147. type CompileFunction struct {
  148. Instructions code.Instructions
  149. }
  150. func (cf *CompileFunction) Type() ObjType { return CompiledFunctionObj }
  151. func (cf *CompileFunction) Inspect() string {
  152. return fmt.Sprintf("CompileFunction[%p]", cf)
  153. }