treetab.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if not n.strVal.isNil:
  30. result = result !& hash(n.strVal)
  31. else:
  32. for i in countup(0, sonsLen(n) - 1):
  33. result = result !& hashTree(n.sons[i])
  34. proc treesEquivalent(a, b: PNode): bool =
  35. if a == b:
  36. result = true
  37. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  38. case a.kind
  39. of nkEmpty, nkNilLit, nkType: result = true
  40. of nkSym: result = a.sym.id == b.sym.id
  41. of nkIdent: result = a.ident.id == b.ident.id
  42. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  43. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  44. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  45. else:
  46. if sonsLen(a) == sonsLen(b):
  47. for i in countup(0, sonsLen(a) - 1):
  48. if not treesEquivalent(a.sons[i], b.sons[i]): return
  49. result = true
  50. if result: result = sameTypeOrNil(a.typ, b.typ)
  51. proc nodeTableRawGet(t: TNodeTable, k: Hash, key: PNode): int =
  52. var h: Hash = k and high(t.data)
  53. while t.data[h].key != nil:
  54. if (t.data[h].h == k) and treesEquivalent(t.data[h].key, key):
  55. return h
  56. h = nextTry(h, high(t.data))
  57. result = -1
  58. proc nodeTableGet*(t: TNodeTable, key: PNode): int =
  59. var index = nodeTableRawGet(t, hashTree(key), key)
  60. if index >= 0: result = t.data[index].val
  61. else: result = low(int)
  62. proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode,
  63. val: int) =
  64. var h: Hash = k and high(data)
  65. while data[h].key != nil: h = nextTry(h, high(data))
  66. assert(data[h].key == nil)
  67. data[h].h = k
  68. data[h].key = key
  69. data[h].val = val
  70. proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) =
  71. var n: TNodePairSeq
  72. var k: Hash = hashTree(key)
  73. var index = nodeTableRawGet(t, k, key)
  74. if index >= 0:
  75. assert(t.data[index].key != nil)
  76. t.data[index].val = val
  77. else:
  78. if mustRehash(len(t.data), t.counter):
  79. newSeq(n, len(t.data) * GrowthFactor)
  80. for i in countup(0, high(t.data)):
  81. if t.data[i].key != nil:
  82. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  83. swap(t.data, n)
  84. nodeTableRawInsert(t.data, k, key, val)
  85. inc(t.counter)
  86. proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int =
  87. var n: TNodePairSeq
  88. var k: Hash = hashTree(key)
  89. var index = nodeTableRawGet(t, k, key)
  90. if index >= 0:
  91. assert(t.data[index].key != nil)
  92. result = t.data[index].val
  93. else:
  94. if mustRehash(len(t.data), t.counter):
  95. newSeq(n, len(t.data) * GrowthFactor)
  96. for i in countup(0, high(t.data)):
  97. if t.data[i].key != nil:
  98. nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val)
  99. swap(t.data, n)
  100. nodeTableRawInsert(t.data, k, key, val)
  101. result = val
  102. inc(t.counter)