Small ast.nim cleanup (#12156)

* Remove sonsLen
* Use Indexable
This commit is contained in:
Clyybber 2019-09-09 11:54:15 +02:00 • committed by Andreas Rumpf
commit ed1d41c51e
54 changed files with 559 additions and 573 deletions

View file

@ -395,7 +395,7 @@ proc atom(g: TSrcGen; n: PNode): string =
proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
assert(theEnd < 0)
result = 0
for i in start .. sonsLen(n) + theEnd:
for i in start .. len(n) + theEnd:
let param = n.sons[i]
if nfDefaultParam notin param.flags:
inc(result, lsub(g, param))
@ -406,7 +406,7 @@ proc lcomma(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
proc lsons(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
assert(theEnd < 0)
result = 0
for i in start .. sonsLen(n) + theEnd: inc(result, lsub(g, n.sons[i]))
for i in start .. len(n) + theEnd: inc(result, lsub(g, n.sons[i]))
proc lsub(g: TSrcGen; n: PNode): int =
# computes the length of a tree
@ -436,7 +436,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkTableConstr:
result = if n.len > 0: lcomma(g, n) + 2 else: len("{:}")
of nkClosedSymChoice, nkOpenSymChoice:
result = lsons(g, n) + len("()") + sonsLen(n) - 1
result = lsons(g, n) + len("()") + len(n) - 1
of nkTupleTy: result = lcomma(g, n) + len("tuple[]")
of nkTupleClassTy: result = len("tuple")
of nkDotExpr: result = lsons(g, n) + 1
@ -448,7 +448,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkDo: result = lsons(g, n) + len("do__:_")
of nkConstDef, nkIdentDefs:
result = lcomma(g, n, 0, - 3)
var L = sonsLen(n)
var L = len(n)
if n.sons[L - 2].kind != nkEmpty: result = result + lsub(g, n.sons[L - 2]) + 2
if n.sons[L - 1].kind != nkEmpty: result = result + lsub(g, n.sons[L - 1]) + 3
of nkVarTuple: result = lcomma(g, n, 0, - 3) + len("() = ") + lsub(g, lastSon(n))
@ -457,7 +457,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkChckRange: result = len("chckRange") + 2 + lcomma(g, n)
of nkObjDownConv, nkObjUpConv:
result = 2
if sonsLen(n) >= 1: result = result + lsub(g, n.sons[0])
if len(n) >= 1: result = result + lsub(g, n.sons[0])
result = result + lcomma(g, n, 1)
of nkExprColonExpr: result = lsons(g, n) + 2
of nkInfix: result = lsons(g, n) + 2
@ -491,16 +491,16 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkIteratorTy: result = lsons(g, n) + len("iterator_")
of nkSharedTy: result = lsons(g, n) + len("shared_")
of nkEnumTy:
if sonsLen(n) > 0:
if len(n) > 0:
result = lsub(g, n.sons[0]) + lcomma(g, n, 1) + len("enum_")
else:
result = len("enum")
of nkEnumFieldDef: result = lsons(g, n) + 3
of nkVarSection, nkLetSection:
if sonsLen(n) > 1: result = MaxLineLen + 1
if len(n) > 1: result = MaxLineLen + 1
else: result = lsons(g, n) + len("var_")
of nkUsingStmt:
if sonsLen(n) > 1: result = MaxLineLen + 1
if len(n) > 1: result = MaxLineLen + 1
else: result = lsons(g, n) + len("using_")
of nkReturnStmt:
if n.len > 0 and n[0].kind == nkAsgn:
@ -556,7 +556,7 @@ proc hasCom(n: PNode): bool =
case n.kind
of nkEmpty..nkNilLit: discard
else:
for i in 0 ..< sonsLen(n):
for i in 0 ..< len(n):
if hasCom(n.sons[i]): return true
proc putWithSpace(g: var TSrcGen, kind: TTokType, s: string) =
@ -565,8 +565,8 @@ proc putWithSpace(g: var TSrcGen, kind: TTokType, s: string) =
proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0,
theEnd: int = - 1, separator = tkComma) =
for i in start .. sonsLen(n) + theEnd:
var c = i < sonsLen(n) + theEnd
for i in start .. len(n) + theEnd:
var c = i < len(n) + theEnd
var sublen = lsub(g, n.sons[i]) + ord(c)
if not fits(g, sublen) and (ind + sublen < MaxLineLen): optNL(g, ind)
let oldLen = g.tokens.len
@ -600,15 +600,15 @@ proc gsemicolon(g: var TSrcGen, n: PNode, start: int = 0, theEnd: int = - 1) =
proc gsons(g: var TSrcGen, n: PNode, c: TContext, start: int = 0,
theEnd: int = - 1) =
for i in start .. sonsLen(n) + theEnd: gsub(g, n.sons[i], c)
for i in start .. len(n) + theEnd: gsub(g, n.sons[i], c)
proc gsection(g: var TSrcGen, n: PNode, c: TContext, kind: TTokType,
k: string) =
if sonsLen(n) == 0: return # empty var sections are possible
if len(n) == 0: return # empty var sections are possible
putWithSpace(g, kind, k)
gcoms(g)
indentNL(g)
for i in 0 ..< sonsLen(n):
for i in 0 ..< len(n):
optNL(g)
gsub(g, n.sons[i], c)
gcoms(g)
@ -618,7 +618,7 @@ proc longMode(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): bool =
result = n.comment.len > 0
if not result:
# check further
for i in start .. sonsLen(n) + theEnd:
for i in start .. len(n) + theEnd:
if (lsub(g, n.sons[i]) > MaxLineLen):
result = true
break
@ -663,7 +663,7 @@ proc gif(g: var TSrcGen, n: PNode) =
incl(c.flags, rfLongMode)
gcoms(g) # a good place for comments
gstmts(g, n.sons[0].sons[1], c)
var length = sonsLen(n)
var length = len(n)
for i in 1 ..< length:
optNL(g)
gsub(g, n.sons[i], c)
@ -712,7 +712,7 @@ proc gtry(g: var TSrcGen, n: PNode) =
proc gfor(g: var TSrcGen, n: PNode) =
var c: TContext
var length = sonsLen(n)
var length = len(n)
putWithSpace(g, tkFor, "for")
initContext(c)
if longMode(g, n) or
@ -730,7 +730,7 @@ proc gfor(g: var TSrcGen, n: PNode) =
proc gcase(g: var TSrcGen, n: PNode) =
var c: TContext
initContext(c)
var length = sonsLen(n)
var length = len(n)
if length == 0: return
var last = if n.sons[length-1].kind == nkElse: -2 else: -1
if longMode(g, n, 0, last): incl(c.flags, rfLongMode)
@ -966,7 +966,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkColon, ":")
gsub(g, n, n.len-1)
else:
if sonsLen(n) >= 1: accentedName(g, n[0])
if len(n) >= 1: accentedName(g, n[0])
put(g, tkParLe, "(")
gcomma(g, n, 1)
put(g, tkParRi, ")")
@ -1046,14 +1046,14 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
gcomma(g, n)
put(g, tkParRi, ")")
of nkObjDownConv, nkObjUpConv:
if sonsLen(n) >= 1: gsub(g, n.sons[0])
if len(n) >= 1: gsub(g, n.sons[0])
put(g, tkParLe, "(")
gcomma(g, n, 1)
put(g, tkParRi, ")")
of nkClosedSymChoice, nkOpenSymChoice:
if renderIds in g.flags:
put(g, tkParLe, "(")
for i in 0 ..< sonsLen(n):
for i in 0 ..< len(n):
if i > 0: put(g, tkOpr, "|")
if n.sons[i].kind == nkSym:
let s = n[i].sym
@ -1115,7 +1115,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
gsub(g, n, bodyPos)
of nkConstDef, nkIdentDefs:
gcomma(g, n, 0, -3)
var L = sonsLen(n)
var L = len(n)
if L >= 2 and n.sons[L - 2].kind != nkEmpty:
putWithSpace(g, tkColon, ":")
gsub(g, n, L - 2)
@ -1200,19 +1200,19 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
if n.len > 0: gsub(g, n.sons[0])
put(g, tkParRi, ")")
of nkRefTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkRef, "ref")
gsub(g, n.sons[0])
else:
put(g, tkRef, "ref")
of nkPtrTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkPtr, "ptr")
gsub(g, n.sons[0])
else:
put(g, tkPtr, "ptr")
of nkVarTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkVar, "var")
gsub(g, n.sons[0])
else:
@ -1243,7 +1243,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
putWithSpace(g, tkEquals, "=")
gsub(g, n.sons[2])
of nkObjectTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkObject, "object")
gsub(g, n.sons[0])
gsub(g, n.sons[1])
@ -1253,7 +1253,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
put(g, tkObject, "object")
of nkRecList:
indentNL(g)
for i in 0 ..< sonsLen(n):
for i in 0 ..< len(n):
optNL(g)
gsub(g, n.sons[i], c)
gcoms(g)
@ -1263,14 +1263,14 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
putWithSpace(g, tkOf, "of")
gsub(g, n, 0)
of nkProcTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkProc, "proc")
gsub(g, n, 0)
gsub(g, n, 1)
else:
put(g, tkProc, "proc")
of nkIteratorTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkIterator, "iterator")
gsub(g, n, 0)
gsub(g, n, 1)
@ -1283,7 +1283,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
gsub(g, n.sons[0])
put(g, tkBracketRi, "]")
of nkEnumTy:
if sonsLen(n) > 0:
if len(n) > 0:
putWithSpace(g, tkEnum, "enum")
gsub(g, n.sons[0])
gcoms(g)
@ -1341,7 +1341,7 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
incl(a.flags, rfInConstExpr)
gsection(g, n, a, tkConst, "const")
of nkVarSection, nkLetSection, nkUsingStmt:
var L = sonsLen(n)
var L = len(n)
if L == 0: return
if n.kind == nkVarSection: putWithSpace(g, tkVar, "var")
elif n.kind == nkLetSection: putWithSpace(g, tkLet, "let")
@ -1552,7 +1552,7 @@ proc renderModule*(n: PNode, infile, outfile: string,
g: TSrcGen
initSrcGen(g, renderFlags, conf)
g.fid = fid
for i in 0 ..< sonsLen(n):
for i in 0 ..< len(n):
gsub(g, n.sons[i])
optNL(g)
case n.sons[i].kind