heap.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, par ...interface{}) (ret scope.ValueFor) {
  27. var talloc func(t object.PointerType) (oid scope.ID)
  28. talloc = func(t object.PointerType) (oid scope.ID) {
  29. switch bt := t.Base().(type) {
  30. case object.RecordType:
  31. fake := object.New(object.VARIABLE)
  32. fake.SetComplex(bt)
  33. fake.SetType(object.COMPLEX)
  34. fake.SetName("@")
  35. r := &rec{link: fake}
  36. oid = scope.ID{Name: "@"}
  37. oid.Ref = new(int)
  38. *oid.Ref = h.next
  39. od := &desc{link: fake}
  40. od.ID = oid
  41. fake.SetRef(od)
  42. alloc(nil, h.data, oid, r)
  43. case object.DynArrayType:
  44. assert.For(len(par) > 0, 20)
  45. p := int64(par[0].(int32))
  46. fake := object.New(object.VARIABLE)
  47. fake.SetComplex(bt)
  48. fake.SetType(object.COMPLEX)
  49. fake.SetName("@")
  50. r := &arr{link: fake, par: p}
  51. oid = scope.ID{Name: "@"}
  52. oid.Ref = new(int)
  53. *oid.Ref = h.next
  54. od := &desc{link: fake}
  55. od.ID = oid
  56. fake.SetRef(od)
  57. alloc(nil, h.data, oid, r)
  58. case object.ArrayType:
  59. panic(0)
  60. case object.PointerType:
  61. oid = talloc(bt)
  62. default:
  63. panic(fmt.Sprintln("cannot allocate", reflect.TypeOf(bt)))
  64. }
  65. return oid
  66. }
  67. switch v := n.(type) {
  68. case node.VariableNode:
  69. switch t := v.Object().Complex().(type) {
  70. case object.PointerType:
  71. h.next++
  72. oid := talloc(t)
  73. return func(interface{}) interface{} {
  74. return oid
  75. }
  76. default:
  77. panic(fmt.Sprintln("unsupported type", reflect.TypeOf(t)))
  78. }
  79. default:
  80. panic(fmt.Sprintln("unsupported node", reflect.TypeOf(v)))
  81. }
  82. }
  83. func (h *heap) Dispose(n node.Node) {
  84. }
  85. func (h *heap) Target(...scope.Allocator) scope.Allocator {
  86. return h
  87. }
  88. func (h *heap) Update(i scope.ID, val scope.ValueFor) {
  89. fmt.Println("update", i)
  90. var upd func(x interface{}) (ret interface{})
  91. upd = func(x interface{}) (ret interface{}) {
  92. fmt.Println(reflect.TypeOf(x))
  93. switch x := x.(type) {
  94. case value:
  95. old := x.get()
  96. tmp := val(old)
  97. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  98. //fmt.Println(tmp)
  99. x.set(tmp)
  100. ret = x
  101. case record:
  102. if i.Path == "" {
  103. //fmt.Println(i, depth)
  104. panic(0) //случай выбора всей записи целиком
  105. } else {
  106. z := x.getField(i.Path)
  107. ret = upd(z)
  108. }
  109. case array:
  110. if i.Index != nil {
  111. old := x.get(*i.Index)
  112. tmp := val(old)
  113. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  114. //fmt.Println(tmp)
  115. x.set(*i.Index, tmp)
  116. } else {
  117. old := x.sel()
  118. tmp := val(old)
  119. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  120. //fmt.Println(tmp)
  121. x.upd(arrConv(tmp))
  122. }
  123. ret = x
  124. default:
  125. panic(0)
  126. }
  127. return ret
  128. }
  129. upd(h.data.get(i))
  130. }
  131. func (h *heap) Select(i scope.ID) interface{} {
  132. fmt.Println("heap select", i)
  133. type result struct {
  134. x interface{}
  135. }
  136. var res *result
  137. var sel func(interface{}) *result
  138. sel = func(x interface{}) (ret *result) {
  139. fmt.Println(x)
  140. switch x := x.(type) {
  141. case record:
  142. if i.Path == "" {
  143. ret = &result{x: x.(*rec).link}
  144. } else {
  145. z := x.getField(i.Path)
  146. ret = sel(z)
  147. }
  148. default:
  149. panic(0)
  150. }
  151. return ret
  152. }
  153. res = sel(h.data.get(i))
  154. assert.For(res != nil, 40)
  155. //fmt.Println(res.x)
  156. return res.x
  157. }
  158. func (h *heap) Init(d context.Domain) { h.d = d }
  159. func (h *heap) Domain() context.Domain { return h.d }
  160. func (h *heap) Handle(msg interface{}) {}