heap.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package std
  2. import (
  3. "fmt"
  4. "fw/cp/node"
  5. "fw/cp/object"
  6. "fw/rt2/context"
  7. "fw/rt2/scope"
  8. "reflect"
  9. "ypk/assert"
  10. )
  11. type heap struct {
  12. d context.Domain
  13. data *area
  14. next int
  15. }
  16. type desc struct {
  17. scope.ID
  18. link object.Object
  19. }
  20. func (d *desc) Object() object.Object {
  21. return d.link
  22. }
  23. func nh() scope.Manager {
  24. return &heap{data: &area{ready: true, root: nil, x: make(map[scope.ID]interface{})}}
  25. }
  26. func (h *heap) Allocate(n node.Node) scope.ValueFor {
  27. switch v := n.(type) {
  28. case node.VariableNode:
  29. switch t := v.Object().Complex().(type) {
  30. case object.PointerType:
  31. h.next++
  32. switch bt := t.Base().(type) {
  33. case object.RecordType:
  34. fake := object.New(object.VARIABLE)
  35. fake.SetComplex(bt)
  36. fake.SetType(object.COMPLEX)
  37. fake.SetName("@")
  38. r := &rec{link: fake}
  39. oid := scope.ID{Name: "@"}
  40. oid.Ref = new(int)
  41. *oid.Ref = h.next
  42. od := &desc{link: fake}
  43. od.ID = oid
  44. fake.SetRef(od)
  45. alloc(nil, h.data, oid, r)
  46. return func(interface{}) interface{} {
  47. return oid
  48. }
  49. default:
  50. panic(fmt.Sprintln("cannot allocate", reflect.TypeOf(t)))
  51. }
  52. default:
  53. panic(fmt.Sprintln("unsupported type", reflect.TypeOf(t)))
  54. }
  55. default:
  56. panic(fmt.Sprintln("unsupported node", reflect.TypeOf(v)))
  57. }
  58. }
  59. func (h *heap) Dispose(n node.Node) {
  60. }
  61. func (h *heap) Target(...scope.Allocator) scope.Allocator {
  62. return h
  63. }
  64. func (h *heap) Update(i scope.ID, val scope.ValueFor) {
  65. fmt.Println("update", i)
  66. var upd func(x interface{}) (ret interface{})
  67. upd = func(x interface{}) (ret interface{}) {
  68. fmt.Println(reflect.TypeOf(x))
  69. switch x := x.(type) {
  70. case value:
  71. old := x.get()
  72. tmp := val(old)
  73. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  74. //fmt.Println(tmp)
  75. x.set(tmp)
  76. ret = x
  77. case record:
  78. if i.Path == "" {
  79. //fmt.Println(i, depth)
  80. panic(0) //случай выбора всей записи целиком
  81. } else {
  82. z := x.getField(i.Path)
  83. ret = upd(z)
  84. }
  85. default:
  86. panic(0)
  87. }
  88. return ret
  89. }
  90. upd(h.data.get(i))
  91. }
  92. func (h *heap) Select(i scope.ID) interface{} {
  93. fmt.Println("heap select", i)
  94. type result struct {
  95. x interface{}
  96. }
  97. var res *result
  98. var sel func(interface{}) *result
  99. sel = func(x interface{}) (ret *result) {
  100. fmt.Println(x)
  101. switch x := x.(type) {
  102. case record:
  103. if i.Path == "" {
  104. ret = &result{x: x.(*rec).link}
  105. } else {
  106. z := x.getField(i.Path)
  107. ret = sel(z)
  108. }
  109. default:
  110. panic(0)
  111. }
  112. return ret
  113. }
  114. res = sel(h.data.get(i))
  115. assert.For(res != nil, 40)
  116. //fmt.Println(res.x)
  117. return res.x
  118. }
  119. func (h *heap) Init(d context.Domain) { h.d = d }
  120. func (h *heap) Domain() context.Domain { return h.d }
  121. func (h *heap) Handle(msg interface{}) {}