heap.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. func nh() scope.Manager {
  17. return &heap{data: &area{ready: true, root: nil, x: make(map[scope.ID]interface{})}}
  18. }
  19. func (h *heap) Allocate(n node.Node) scope.ValueFor {
  20. switch v := n.(type) {
  21. case node.VariableNode:
  22. switch t := v.Object().Complex().(type) {
  23. case object.PointerType:
  24. h.next++
  25. switch bt := t.Base().(type) {
  26. case object.RecordType:
  27. fake := object.New(object.VARIABLE)
  28. fake.SetComplex(bt)
  29. fake.SetType(object.COMPLEX)
  30. r := &rec{link: fake}
  31. id := scope.ID{Name: "@"}
  32. id.Ref = new(int)
  33. *id.Ref = h.next
  34. alloc(nil, h.data, id, r)
  35. return func(interface{}) interface{} {
  36. return id
  37. }
  38. default:
  39. panic(fmt.Sprintln("cannot allocate", reflect.TypeOf(t)))
  40. }
  41. default:
  42. panic(fmt.Sprintln("unsupported type", reflect.TypeOf(t)))
  43. }
  44. default:
  45. panic(fmt.Sprintln("unsupported node", reflect.TypeOf(v)))
  46. }
  47. }
  48. func (h *heap) Dispose(n node.Node) {
  49. }
  50. func (h *heap) Target(...scope.Allocator) scope.Allocator {
  51. return h
  52. }
  53. func (h *heap) Update(i scope.ID, val scope.ValueFor) {}
  54. func (h *heap) Select(i scope.ID) interface{} {
  55. fmt.Println("heap select", i)
  56. type result struct {
  57. x interface{}
  58. }
  59. var res *result
  60. var sel func(interface{}) *result
  61. sel = func(x interface{}) (ret *result) {
  62. fmt.Println(x)
  63. switch x := x.(type) {
  64. case record:
  65. if i.Path == "" {
  66. ret = &result{x: x.(*rec).link}
  67. } else {
  68. z := x.getField(i.Path)
  69. ret = sel(z)
  70. }
  71. default:
  72. panic(0)
  73. }
  74. return ret
  75. }
  76. res = sel(h.data.get(i))
  77. assert.For(res != nil, 40)
  78. //fmt.Println(res.x)
  79. return res.x
  80. }
  81. func (h *heap) Init(d context.Domain) { h.d = d }
  82. func (h *heap) Domain() context.Domain { return h.d }
  83. func (h *heap) Handle(msg interface{}) {}