scope.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package muesli
  2. import "fmt"
  3. import "strings"
  4. // LookupFallback is a fallback function that can be set to be called when
  5. // lookup in a scope fails
  6. type LookupFallback func (name string) Value
  7. // Scope of symbols defined in the VM, hierarchical
  8. type Scope struct {
  9. parent *Scope
  10. children []*Scope
  11. symbols map[string]Value
  12. fallback LookupFallback
  13. }
  14. func NewScope(parent *Scope) *Scope {
  15. return &Scope{parent, make([]*Scope, 0), make(map[string]Value), nil}
  16. }
  17. // Sets a lookup fall back to be called before trying the parent scope.
  18. // Returns the previous lookup fallback, or nil if it was nil.
  19. func (scope * Scope) SetLookupFallback(fb LookupFallback) LookupFallback {
  20. res := scope.fallback
  21. scope.fallback = fb
  22. return res
  23. }
  24. // Lookup looks up a value of a name registered in the current scope,
  25. // and if not found there, recursively in the parent scope.
  26. // Returns NilValue if not found.
  27. func (scope *Scope) Lookup(name string) Value {
  28. value, ok := scope.symbols[name]
  29. if ok {
  30. return value
  31. }
  32. if scope.fallback != nil {
  33. return scope.fallback(name)
  34. }
  35. if scope.parent != nil {
  36. return scope.parent.Lookup(name)
  37. }
  38. return NilValue
  39. }
  40. func (scope *Scope) Register(name string, value Value) Value {
  41. scope.symbols[name] = value
  42. return value
  43. }
  44. func (scope * Scope) Known(filter func(string, Value) bool) map[string]Value {
  45. res := make(map[string]Value)
  46. if scope.parent != nil {
  47. res = scope.parent.Known(filter)
  48. }
  49. for k, v := range scope.symbols {
  50. if (filter == nil) || filter(k, v) {
  51. res[k] = v
  52. }
  53. }
  54. return res
  55. }
  56. func (scope * Scope) ForEachDefined(do func(string, Value) (bool, error)) (bool, error) {
  57. var res bool = true
  58. var err error
  59. if (do == nil) {
  60. return false, fmt.Errorf("do may not be nil")
  61. }
  62. if scope.parent != nil {
  63. res, err = scope.parent.ForEachDefined(do)
  64. }
  65. if res == false || err != nil {
  66. return res, err
  67. }
  68. for k, v := range scope.symbols {
  69. res, err = do(k, v)
  70. if res == false || err != nil {
  71. return res, err
  72. }
  73. }
  74. return res, err
  75. }
  76. func (scope* Scope) DefinedHelpers() []Helper {
  77. res := []Helper{}
  78. scope.ForEachDefined(func (k string, v Value) (bool, error) {
  79. helper, hok := v.(Helper)
  80. if hok {
  81. res = append(res, helper)
  82. }
  83. return true, nil
  84. })
  85. return res
  86. }
  87. func (scope* Scope) DefinedNamesLike(prefix string) []string {
  88. res := []string{}
  89. scope.ForEachDefined(func (k string, v Value) (bool, error) {
  90. if strings.HasPrefix(k, prefix) {
  91. res = append(res, k)
  92. }
  93. return true, nil
  94. })
  95. return res
  96. }