Merge pull request #4660 from mbaulch/tree_helper_polish

Tree helpers: Polish, optimise, remove those not used
This commit is contained in:
Andreas Rumpf 2016-08-29 11:53:00 +02:00 • committed by GitHub
commit 12cd8fe9e2

View file

@ -12,32 +12,21 @@
import import
ast, astalgo, lexer, msgs, strutils, wordrecg ast, astalgo, lexer, msgs, strutils, wordrecg
proc hasSon(father, son: PNode): bool = proc cyclicTreeAux(n: PNode, visited: var seq[PNode]): bool =
for i in countup(0, sonsLen(father) - 1): if n == nil: return
if father.sons[i] == son: for v in visited:
return true if v == n: 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)
if not (n.kind in {nkEmpty..nkNilLit}): if not (n.kind in {nkEmpty..nkNilLit}):
for i in countup(0, sonsLen(n) - 1): visited.add(n)
if cyclicTreeAux(n.sons[i], s): for nSon in n.sons:
return true if cyclicTreeAux(nSon, visited): return true
result = false discard visited.pop()
delSon(s, m)
proc cyclicTree*(n: PNode): bool = proc cyclicTree*(n: PNode): bool =
var s = newNodeI(nkEmpty, n.info) var visited: seq[PNode] = @[]
result = cyclicTreeAux(n, s) cyclicTreeAux(n, visited)
proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool = proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
result = false
if a == b: if a == b:
result = true result = true
elif (a != nil) and (b != nil) and (a.kind == b.kind): 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 result = true
proc sameTree*(a, b: PNode): bool = proc sameTree*(a, b: PNode): bool =
result = false
if a == b: if a == b:
result = true result = true
elif a != nil and b != nil and a.kind == b.kind: 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 if not sameTree(a.sons[i], b.sons[i]): return
result = true 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 = proc getMagic*(op: PNode): TMagic =
case op.kind case op.kind
of nkCallKinds: of nkCallKinds:
@ -104,9 +81,8 @@ proc getMagic*(op: PNode): TMagic =
else: result = mNone else: result = mNone
proc isConstExpr*(n: PNode): bool = proc isConstExpr*(n: PNode): bool =
result = (n.kind in const atomKinds = {nkCharLit..nkNilLit} # Char, Int, UInt, Str, Float and Nil literals
{nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit, n.kind in atomKinds or nfAllConst in n.flags
nkFloatLit..nkFloat64Lit, nkNilLit}) or (nfAllConst in n.flags)
proc isCaseObj*(n: PNode): bool = proc isCaseObj*(n: PNode): bool =
if n.kind == nkRecCase: return true if n.kind == nkRecCase: return true
@ -131,25 +107,6 @@ proc isDeepConstExpr*(n: PNode): bool =
result = true result = true
else: discard 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.} = proc isRange*(n: PNode): bool {.inline.} =
if n.kind in nkCallKinds: if n.kind in nkCallKinds:
if n[0].kind == nkIdent and n[0].ident.id == ord(wDotDot) or 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) result.add(n)
proc flattenStmts*(n: PNode): PNode = proc flattenStmts*(n: PNode): PNode =
## flattens a nested statement list; used for pattern matching
result = newNodeI(nkStmtList, n.info) result = newNodeI(nkStmtList, n.info)
unnestStmts(n, result) unnestStmts(n, result)
if result.len == 1: if result.len == 1: