object.go 3.6 KB

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