treetab.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Implements a table from trees to trees. Does structural equivalence checking.
  10. import
  11. hashes, ast, astalgo, types
  12. proc hashTree(n: PNode): Hash =
  13. if n == nil: return
  14. result = ord(n.kind)
  15. case n.kind
  16. of nkEmpty, nkNilLit, nkType:
  17. discard
  18. of nkIdent:
  19. result = result !& n.ident.h
  20. of nkSym:
  21. result = result !& n.sym.name.h
  22. of nkCharLit..nkUInt64Lit:
  23. if (n.intVal >= low(int)) and (n.intVal <= high(int)):
  24. result = result !& int(n.intVal)
  25. of nkFloatLit..nkFloat64Lit:
  26. if (n.floatVal >= - 1000000.0) and (n.floatVal <= 1000000.0):
  27. result = result !& toInt(n.floatVal)
  28. of nkStrLit..nkTripleStrLit:
  29. result = result !& hash(n.strVal)
  30. else:
  31. for i in 0..<n.len:
  32. result = result !& hashTree(n[i])
  33. proc treesEquivalent(a, b: PNode): bool =
  34. if a == b:
  35. result = true
  36. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  37. case a.kind
  38. of nkEmpty, nkNilLit, nkType: result = true
  39. of nkSym: result = a.sym.id == b.sym.id
  40. of nkIdent: result = a.ident.id == b.ident.id
  41. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  42. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  43. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  44. else:
  45. if a.len == b.len:
  46. for i in 0..<a.len:
  47. if not treesEquivalent(a[i], b[i]): return
  48. result = true
  49. if result: result = sameTypeOrNil(a.typ, b.typ)
  50. proc nodeTableRawGet(t: TNodeTable, k: Hash, key: PNode): int =
  51. var h: Hash = k and high(t.data)
  52. while t.data[h].key != nil:
  53. if (t.data[h].h == k) and treesEquivalent(t.data[h].key, key):
  54. return h
  55. h = nextTry(h, high(t.data))
  56. result = -1
  57. proc nodeTableGet*(t: TNodeTable, key: PNode): int =
  58. var index = nodeTableRawGet(t, hashTree(key), key)
  59. if index >= 0: result = t.data[index].val
  60. else: result = low(int)
  61. proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode,
  62. val: int) =
  63. var h: Hash = k and high(data)
  64. while data[h].key != nil: h = nextTry(h, high(data))
  65. assert(data[h].key == nil)
  66. data[h].h = k
  67. data[h].key = key
  68. data[h].val = val
  69. proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) =
  70. var n: TNodePairSeq
  71. var k: Hash = hashTree(key)
  72. var index = nodeTableRawGet(t, k, key)
  73. if index >= 0:
  74. assert(t.data[index].key != nil)
  75. t.data[index].val = val
  76. else:
  77. if mustRehash(t.data.len, t.counter):
  78. newSeq(n, t.data.len * GrowthFactor)
  79. for i in 0..high(t.data):
  80. if t.data[i].key != nil:
  81. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  82. swap(t.data, n)
  83. nodeTableRawInsert(t.data, k, key, val)
  84. inc(t.counter)
  85. proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int =
  86. var n: TNodePairSeq
  87. var k: Hash = hashTree(key)
  88. var index = nodeTableRawGet(t, k, key)
  89. if index >= 0:
  90. assert(t.data[index].key != nil)
  91. result = t.data[index].val
  92. else:
  93. if mustRehash(t.data.len, t.counter):
  94. newSeq(n, t.data.len * GrowthFactor)
  95. for i in 0..high(t.data):
  96. if t.data[i].key != nil:
  97. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  98. swap(t.data, n)
  99. nodeTableRawInsert(t.data, k, key, val)
  100. result = val
  101. inc(t.counter)