object.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package object
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github/runnignwater/monkey/ast"
  6. "strings"
  7. )
  8. // ObjType type of object
  9. type ObjType string
  10. const (
  11. IntegerObj = "INTEGER"
  12. BooleanObj = "BOOLEAN"
  13. NullObj = "NULL"
  14. ReturnValueObj = "RETURN_VALUE"
  15. ErrorObj = "ERROR"
  16. FunctionObj = "FUNCTION"
  17. StringObj = "STRING"
  18. BuiltinObj = "BUILTIN"
  19. ArrayObj = "ARRAY"
  20. )
  21. // Object source code as an Object
  22. type Object interface {
  23. Type() ObjType
  24. Inspect() string
  25. }
  26. // BuiltinFunction 内建函数数据
  27. type BuiltinFunction func(args ...Object) Object
  28. type Integer struct {
  29. Value int64
  30. }
  31. func (i *Integer) Type() ObjType { return IntegerObj }
  32. func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
  33. type Boolean struct {
  34. Value bool
  35. }
  36. func (b *Boolean) Type() ObjType { return BooleanObj }
  37. func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
  38. type Null struct{}
  39. func (n *Null) Type() ObjType { return NullObj }
  40. func (n *Null) Inspect() string { return "null" }
  41. type ReturnValue struct {
  42. Value Object
  43. }
  44. func (rv *ReturnValue) Type() ObjType { return ReturnValueObj }
  45. func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
  46. type Error struct {
  47. Msg string
  48. }
  49. func (e *Error) Type() ObjType { return ErrorObj }
  50. func (e *Error) Inspect() string { return "ERROR: " + e.Msg }
  51. type Function struct {
  52. Parameters []*ast.Identifier
  53. Body *ast.BlockStatement
  54. Env *Environment
  55. }
  56. func (f *Function) Type() ObjType { return FunctionObj }
  57. func (f *Function) Inspect() string {
  58. var out bytes.Buffer
  59. params := []string{}
  60. for _, p := range f.Parameters {
  61. params = append(params, p.String())
  62. }
  63. out.WriteString("fn")
  64. out.WriteString("(")
  65. out.WriteString(strings.Join(params, ", "))
  66. out.WriteString(") {\n")
  67. out.WriteString(f.Body.String())
  68. out.WriteString("\n}")
  69. return out.String()
  70. }
  71. type String struct {
  72. Value string
  73. }
  74. func (s *String) Type() ObjType { return StringObj }
  75. func (s *String) Inspect() string { return s.Value }
  76. type Array struct {
  77. Elements []Object
  78. }
  79. func (ao *Array) Type() ObjType { return ArrayObj }
  80. func (ao *Array) Inspect() string {
  81. var out bytes.Buffer
  82. elements := []string{}
  83. for _, e := range ao.Elements {
  84. elements = append(elements, e.Inspect())
  85. }
  86. out.WriteString("[")
  87. out.WriteString(strings.Join(elements, ", "))
  88. out.WriteString("]")
  89. return out.String()
  90. }
  91. type Builtin struct {
  92. Fn BuiltinFunction
  93. }
  94. func (b Builtin) Type() ObjType { return BuiltinObj }
  95. func (b Builtin) Inspect() string { return "builtin function" }