diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-08-29 11:53:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-29 11:53:00 +0200 |
commit | 12cd8fe9e27256e2fb565ca3445ef94328339a6d (patch) | |
tree | e07732a2adf374a438c7ec24c63f843b4d16603a /compiler | |
parent | 10ee254a502042965beaa4af3000008f4f45299b (diff) | |
parent | defc7bbded2b38a45c0602f97e44fe702f8c5f0b (diff) | |
download | Nim-12cd8fe9e27256e2fb565ca3445ef94328339a6d.tar.gz |
Merge pull request #4660 from mbaulch/tree_helper_polish
Tree helpers: Polish, optimise, remove those not used
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/trees.nim | 68 |
1 files changed, 12 insertions, 56 deletions
diff --git a/compiler/trees.nim b/compiler/trees.nim index fdd88c348..a629b3834 100644 --- a/compiler/trees.nim +++ b/compiler/trees.nim @@ -12,32 +12,21 @@ import ast, astalgo, lexer, msgs, strutils, wordrecg -proc hasSon(father, son: PNode): bool = - for i in countup(0, sonsLen(father) - 1): - if father.sons[i] == son: - return true - result = false - -proc cyclicTreeAux(n, s: PNode): bool = - if n == nil: - return false - if hasSon(s, n): - return true - var m = sonsLen(s) - addSon(s, n) +proc cyclicTreeAux(n: PNode, visited: var seq[PNode]): bool = + if n == nil: return + for v in visited: + if v == n: return true if not (n.kind in {nkEmpty..nkNilLit}): - for i in countup(0, sonsLen(n) - 1): - if cyclicTreeAux(n.sons[i], s): - return true - result = false - delSon(s, m) + visited.add(n) + for nSon in n.sons: + if cyclicTreeAux(nSon, visited): return true + discard visited.pop() proc cyclicTree*(n: PNode): bool = - var s = newNodeI(nkEmpty, n.info) - result = cyclicTreeAux(n, s) + var visited: seq[PNode] = @[] + cyclicTreeAux(n, visited) proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool = - result = false if a == b: result = true elif (a != nil) and (b != nil) and (a.kind == b.kind): @@ -61,7 +50,6 @@ proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool = result = true proc sameTree*(a, b: PNode): bool = - result = false if a == b: result = true elif a != nil and b != nil and a.kind == b.kind: @@ -84,17 +72,6 @@ proc sameTree*(a, b: PNode): bool = if not sameTree(a.sons[i], b.sons[i]): return result = true -proc getProcSym*(call: PNode): PSym = - result = call.sons[0].sym - -proc getOpSym*(op: PNode): PSym = - if op.kind notin {nkCall, nkHiddenCallConv, nkCommand, nkCallStrLit}: - result = nil - else: - if sonsLen(op) <= 0: internalError(op.info, "getOpSym") - elif op.sons[0].kind == nkSym: result = op.sons[0].sym - else: result = nil - proc getMagic*(op: PNode): TMagic = case op.kind of nkCallKinds: @@ -104,9 +81,8 @@ proc getMagic*(op: PNode): TMagic = else: result = mNone proc isConstExpr*(n: PNode): bool = - result = (n.kind in - {nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit, - nkFloatLit..nkFloat64Lit, nkNilLit}) or (nfAllConst in n.flags) + const atomKinds = {nkCharLit..nkNilLit} # Char, Int, UInt, Str, Float and Nil literals + n.kind in atomKinds or nfAllConst in n.flags proc isCaseObj*(n: PNode): bool = if n.kind == nkRecCase: return true @@ -131,25 +107,6 @@ proc isDeepConstExpr*(n: PNode): bool = result = true else: discard -proc flattenTreeAux(d, a: PNode, op: TMagic) = - if (getMagic(a) == op): # a is a "leaf", so add it: - for i in countup(1, sonsLen(a) - 1): # BUGFIX - flattenTreeAux(d, a.sons[i], op) - else: - addSon(d, copyTree(a)) - -proc flattenTree*(root: PNode, op: TMagic): PNode = - result = copyNode(root) - if getMagic(root) == op: - # BUGFIX: forget to copy prc - addSon(result, copyNode(root.sons[0])) - flattenTreeAux(result, root, op) - -proc swapOperands*(op: PNode) = - var tmp = op.sons[1] - op.sons[1] = op.sons[2] - op.sons[2] = tmp - proc isRange*(n: PNode): bool {.inline.} = if n.kind in nkCallKinds: if n[0].kind == nkIdent and n[0].ident.id == ord(wDotDot) or @@ -168,7 +125,6 @@ proc unnestStmts(n, result: PNode) = result.add(n) proc flattenStmts*(n: PNode): PNode = - ## flattens a nested statement list; used for pattern matching result = newNodeI(nkStmtList, n.info) unnestStmts(n, result) if result.len == 1: |