diff options
Diffstat (limited to 'compiler/treetab.nim')
-rw-r--r-- | compiler/treetab.nim | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/compiler/treetab.nim b/compiler/treetab.nim index e6eb8c666..6685c4a89 100644 --- a/compiler/treetab.nim +++ b/compiler/treetab.nim @@ -9,11 +9,16 @@ # Implements a table from trees to trees. Does structural equivalence checking. -import - hashes, ast, astalgo, types +import ast, astalgo, types -proc hashTree(n: PNode): Hash = - if n == nil: return +import std/hashes + +when defined(nimPreviewSlimSystem): + import std/assertions + +proc hashTree*(n: PNode): Hash = + if n.isNil: + return result = ord(n.kind) case n.kind of nkEmpty, nkNilLit, nkType: @@ -21,7 +26,7 @@ proc hashTree(n: PNode): Hash = of nkIdent: result = result !& n.ident.h of nkSym: - result = result !& n.sym.name.h + result = result !& n.sym.id of nkCharLit..nkUInt64Lit: if (n.intVal >= low(int)) and (n.intVal <= high(int)): result = result !& int(n.intVal) @@ -29,11 +34,13 @@ proc hashTree(n: PNode): Hash = if (n.floatVal >= - 1000000.0) and (n.floatVal <= 1000000.0): result = result !& toInt(n.floatVal) of nkStrLit..nkTripleStrLit: - if not n.strVal.isNil: - result = result !& hash(n.strVal) + result = result !& hash(n.strVal) else: - for i in countup(0, sonsLen(n) - 1): - result = result !& hashTree(n.sons[i]) + for i in 0..<n.len: + result = result !& hashTree(n[i]) + result = !$result + #echo "hashTree ", result + #echo n proc treesEquivalent(a, b: PNode): bool = if a == b: @@ -47,11 +54,15 @@ proc treesEquivalent(a, b: PNode): bool = of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal else: - if sonsLen(a) == sonsLen(b): - for i in countup(0, sonsLen(a) - 1): - if not treesEquivalent(a.sons[i], b.sons[i]): return + if a.len == b.len: + for i in 0..<a.len: + if not treesEquivalent(a[i], b[i]): return result = true + else: + result = false if result: result = sameTypeOrNil(a.typ, b.typ) + else: + result = false proc nodeTableRawGet(t: TNodeTable, k: Hash, key: PNode): int = var h: Hash = k and high(t.data) @@ -76,36 +87,34 @@ proc nodeTableRawInsert(data: var TNodePairSeq, k: Hash, key: PNode, data[h].val = val proc nodeTablePut*(t: var TNodeTable, key: PNode, val: int) = - var n: TNodePairSeq - var k: Hash = hashTree(key) - var index = nodeTableRawGet(t, k, key) + let k = hashTree(key) + let index = nodeTableRawGet(t, k, key) if index >= 0: assert(t.data[index].key != nil) t.data[index].val = val else: - if mustRehash(len(t.data), t.counter): - newSeq(n, len(t.data) * GrowthFactor) - for i in countup(0, high(t.data)): + if mustRehash(t.data.len, t.counter): + var n = newSeq[TNodePair](t.data.len * GrowthFactor) + for i in 0..high(t.data): if t.data[i].key != nil: nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val) - swap(t.data, n) + t.data = move n nodeTableRawInsert(t.data, k, key, val) inc(t.counter) proc nodeTableTestOrSet*(t: var TNodeTable, key: PNode, val: int): int = - var n: TNodePairSeq - var k: Hash = hashTree(key) - var index = nodeTableRawGet(t, k, key) + let k = hashTree(key) + let index = nodeTableRawGet(t, k, key) if index >= 0: assert(t.data[index].key != nil) result = t.data[index].val else: - if mustRehash(len(t.data), t.counter): - newSeq(n, len(t.data) * GrowthFactor) - for i in countup(0, high(t.data)): + if mustRehash(t.data.len, t.counter): + var n = newSeq[TNodePair](t.data.len * GrowthFactor) + for i in 0..high(t.data): if t.data[i].key != nil: nodeTableRawInsert(n, t.data[i].h, t.data[i].key, t.data[i].val) - swap(t.data, n) + t.data = move n nodeTableRawInsert(t.data, k, key, val) result = val inc(t.counter) |