object.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package object
  2. import "fmt"
  3. // ObjType type of object
  4. type ObjType string
  5. const (
  6. IntegerObj = "INTEGER"
  7. BooleanObj = "BOOLEAN"
  8. NullObj = "NULL"
  9. ReturnValueObj = "RETURN_VALUE"
  10. ErrorObj = "ERROR"
  11. )
  12. // Object source code as an Object
  13. type Object interface {
  14. Type() ObjType
  15. Inspect() string
  16. }
  17. type Integer struct {
  18. Value int64
  19. }
  20. func (i *Integer) Type() ObjType { return IntegerObj }
  21. func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
  22. type Boolean struct {
  23. Value bool
  24. }
  25. func (b *Boolean) Type() ObjType { return BooleanObj }
  26. func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
  27. type Null struct{}
  28. func (n *Null) Type() ObjType { return NullObj }
  29. func (n *Null) Inspect() string { return "null" }
  30. type ReturnValue struct {
  31. Value Object
  32. }
  33. func (rv *ReturnValue) Type() ObjType { return ReturnValueObj }
  34. func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
  35. type Error struct {
  36. Msg string
  37. }
  38. func (e *Error) Type() ObjType { return ErrorObj }
  39. func (e *Error) Inspect() string { return "ERROR: " + e.Msg }