1
0

heap.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package std
  2. import (
  3. "fmt"
  4. "fw/cp"
  5. "fw/cp/node"
  6. "fw/cp/object"
  7. "fw/rt2/context"
  8. "fw/rt2/scope"
  9. "reflect"
  10. "ypk/assert"
  11. )
  12. type heap struct {
  13. d context.Domain
  14. data *area
  15. next int
  16. }
  17. type desc struct {
  18. scope.ID
  19. link object.Object
  20. }
  21. func (d *desc) Object() object.Object {
  22. return d.link
  23. }
  24. func nh() scope.Manager {
  25. return &heap{data: &area{ready: true, root: nil, x: make(map[scope.ID]interface{})}}
  26. }
  27. func (h *heap) String() (ret string) {
  28. ret = fmt.Sprintln("HEAP")
  29. ret = fmt.Sprintln(ret, h.data)
  30. return fmt.Sprint(ret, "END")
  31. }
  32. func (h *heap) Allocate(n node.Node, par ...interface{}) (ret scope.ValueFor) {
  33. var talloc func(t object.PointerType) (oid scope.ID)
  34. talloc = func(t object.PointerType) (oid scope.ID) {
  35. switch bt := t.Base().(type) {
  36. case object.RecordType:
  37. fake := object.New(object.VARIABLE, cp.SomeAdr())
  38. fake.SetComplex(bt)
  39. fake.SetType(object.COMPLEX)
  40. fake.SetName("@")
  41. r := &rec{link: fake}
  42. oid = scope.ID{Name: "@"}
  43. oid.Ref = new(int)
  44. *oid.Ref = h.next
  45. od := &desc{link: fake}
  46. od.ID = oid
  47. fake.SetRef(od)
  48. alloc(nil, h.data, oid, r)
  49. case object.DynArrayType:
  50. assert.For(len(par) > 0, 20)
  51. var p int64
  52. switch x := par[0].(type) {
  53. case int64:
  54. p = x
  55. case int32:
  56. p = int64(x)
  57. default:
  58. panic("mistyped parameter")
  59. }
  60. fake := object.New(object.VARIABLE, cp.SomeAdr())
  61. fake.SetComplex(bt)
  62. fake.SetType(object.COMPLEX)
  63. fake.SetName("@")
  64. r := &arr{link: fake, par: p}
  65. oid = scope.ID{Name: "@"}
  66. oid.Ref = new(int)
  67. *oid.Ref = h.next
  68. od := &desc{link: fake}
  69. od.ID = oid
  70. fake.SetRef(od)
  71. alloc(nil, h.data, oid, r)
  72. case object.ArrayType:
  73. panic(0)
  74. case object.PointerType:
  75. oid = talloc(bt)
  76. default:
  77. panic(fmt.Sprintln("cannot allocate", reflect.TypeOf(bt)))
  78. }
  79. return oid
  80. }
  81. switch v := n.(type) {
  82. case node.VariableNode:
  83. switch t := v.Object().Complex().(type) {
  84. case object.PointerType:
  85. h.next++
  86. oid := talloc(t)
  87. return func(interface{}) interface{} {
  88. return oid
  89. }
  90. default:
  91. panic(fmt.Sprintln("unsupported type", reflect.TypeOf(t)))
  92. }
  93. default:
  94. panic(fmt.Sprintln("unsupported node", reflect.TypeOf(v)))
  95. }
  96. }
  97. func (h *heap) Dispose(n node.Node) {
  98. }
  99. func (h *heap) Target(...scope.Allocator) scope.Allocator {
  100. return h
  101. }
  102. func (h *heap) Update(i scope.ID, val scope.ValueFor) {
  103. fmt.Println("update", i)
  104. var upd func(x interface{}) (ret interface{})
  105. upd = func(x interface{}) (ret interface{}) {
  106. fmt.Println(reflect.TypeOf(x))
  107. switch x := x.(type) {
  108. case value:
  109. old := x.get()
  110. tmp := val(old)
  111. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  112. //fmt.Println(tmp)
  113. x.set(tmp)
  114. ret = x
  115. case record:
  116. if i.Path == "" {
  117. //fmt.Println(i, depth)
  118. panic(0) //случай выбора всей записи целиком
  119. } else {
  120. z := x.getField(i.Path)
  121. ret = upd(z)
  122. }
  123. case array:
  124. if i.Index != nil {
  125. old := x.get(*i.Index)
  126. tmp := val(old)
  127. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  128. //fmt.Println(tmp)
  129. x.set(*i.Index, tmp)
  130. } else {
  131. old := x.sel()
  132. tmp := val(old)
  133. assert.For(tmp != nil, 40) //если устанавливают значение NIL, значит делают что-то неверно
  134. //fmt.Println(tmp)
  135. x.upd(arrConv(tmp))
  136. }
  137. ret = x
  138. default:
  139. panic(0)
  140. }
  141. return ret
  142. }
  143. upd(h.data.get(i))
  144. }
  145. func (h *heap) Select(i scope.ID) interface{} {
  146. fmt.Println("heap select", i)
  147. type result struct {
  148. x interface{}
  149. }
  150. var res *result
  151. var sel func(interface{}) *result
  152. sel = func(x interface{}) (ret *result) {
  153. fmt.Println(x)
  154. switch x := x.(type) {
  155. case record:
  156. if i.Path == "" {
  157. ret = &result{x: x.(*rec).link}
  158. } else {
  159. z := x.getField(i.Path)
  160. ret = sel(z)
  161. }
  162. case array:
  163. if i.Index != nil {
  164. ret = &result{x: x.get(*i.Index)}
  165. } else {
  166. ret = &result{x: x.sel()}
  167. }
  168. default:
  169. panic(0)
  170. }
  171. return ret
  172. }
  173. res = sel(h.data.get(i))
  174. assert.For(res != nil, 40)
  175. //fmt.Println(res.x)
  176. return res.x
  177. }
  178. func (h *heap) Init(d context.Domain) { h.d = d }
  179. func (h *heap) Domain() context.Domain { return h.d }
  180. func (h *heap) Handle(msg interface{}) {}