[refactoring] closureiters.nim: style change
This commit is contained in:
parent
50afafdb38
commit
c83914b435
1 changed files with 129 additions and 131 deletions
|
|
@ -854,156 +854,155 @@ proc transformReturnsInTry(ctx: var Ctx, n: PNode): PNode =
|
||||||
|
|
||||||
proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode =
|
proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
case n.kind:
|
case n.kind
|
||||||
of nkSkip:
|
of nkSkip: discard
|
||||||
discard
|
|
||||||
|
|
||||||
of nkStmtList, nkStmtListExpr:
|
of nkStmtList, nkStmtListExpr:
|
||||||
result = addGotoOut(result, gotoOut)
|
result = addGotoOut(result, gotoOut)
|
||||||
for i in 0 ..< n.len:
|
for i in 0 ..< n.len:
|
||||||
if n[i].hasYields:
|
if n[i].hasYields:
|
||||||
# Create a new split
|
# Create a new split
|
||||||
let go = newNodeI(nkGotoState, n[i].info)
|
let go = newNodeI(nkGotoState, n[i].info)
|
||||||
n[i] = ctx.transformClosureIteratorBody(n[i], go)
|
n[i] = ctx.transformClosureIteratorBody(n[i], go)
|
||||||
|
|
||||||
let s = newNodeI(nkStmtList, n[i + 1].info)
|
let s = newNodeI(nkStmtList, n[i + 1].info)
|
||||||
for j in i + 1 ..< n.len:
|
for j in i + 1 ..< n.len:
|
||||||
s.add(n[j])
|
s.add(n[j])
|
||||||
|
|
||||||
n.sons.setLen(i + 1)
|
n.sons.setLen(i + 1)
|
||||||
discard ctx.newState(s, go)
|
discard ctx.newState(s, go)
|
||||||
if ctx.transformClosureIteratorBody(s, gotoOut) != s:
|
if ctx.transformClosureIteratorBody(s, gotoOut) != s:
|
||||||
internalError(ctx.g.config, "transformClosureIteratorBody != s")
|
internalError(ctx.g.config, "transformClosureIteratorBody != s")
|
||||||
break
|
break
|
||||||
|
|
||||||
of nkYieldStmt:
|
of nkYieldStmt:
|
||||||
result = newNodeI(nkStmtList, n.info)
|
result = newNodeI(nkStmtList, n.info)
|
||||||
result.add(n)
|
result.add(n)
|
||||||
result.add(gotoOut)
|
result.add(gotoOut)
|
||||||
|
|
||||||
of nkElse, nkElseExpr:
|
of nkElse, nkElseExpr:
|
||||||
result[0] = addGotoOut(result[0], gotoOut)
|
result[0] = addGotoOut(result[0], gotoOut)
|
||||||
result[0] = ctx.transformClosureIteratorBody(result[0], gotoOut)
|
result[0] = ctx.transformClosureIteratorBody(result[0], gotoOut)
|
||||||
|
|
||||||
of nkElifBranch, nkElifExpr, nkOfBranch:
|
of nkElifBranch, nkElifExpr, nkOfBranch:
|
||||||
result[^1] = addGotoOut(result[^1], gotoOut)
|
result[^1] = addGotoOut(result[^1], gotoOut)
|
||||||
result[^1] = ctx.transformClosureIteratorBody(result[^1], gotoOut)
|
result[^1] = ctx.transformClosureIteratorBody(result[^1], gotoOut)
|
||||||
|
|
||||||
of nkIfStmt, nkCaseStmt:
|
|
||||||
for i in 0 ..< n.len:
|
|
||||||
n[i] = ctx.transformClosureIteratorBody(n[i], gotoOut)
|
|
||||||
if n[^1].kind != nkElse:
|
|
||||||
# We don't have an else branch, but every possible branch has to end with
|
|
||||||
# gotoOut, so add else here.
|
|
||||||
let elseBranch = newTree(nkElse, gotoOut)
|
|
||||||
n.add(elseBranch)
|
|
||||||
|
|
||||||
of nkWhileStmt:
|
|
||||||
# while e:
|
|
||||||
# s
|
|
||||||
# ->
|
|
||||||
# BEGIN_STATE:
|
|
||||||
# if e:
|
|
||||||
# s
|
|
||||||
# goto BEGIN_STATE
|
|
||||||
# else:
|
|
||||||
# goto OUT
|
|
||||||
|
|
||||||
result = newNodeI(nkGotoState, n.info)
|
|
||||||
|
|
||||||
let s = newNodeI(nkStmtList, n.info)
|
|
||||||
discard ctx.newState(s, result)
|
|
||||||
let ifNode = newNodeI(nkIfStmt, n.info)
|
|
||||||
let elifBranch = newNodeI(nkElifBranch, n.info)
|
|
||||||
elifBranch.add(n[0])
|
|
||||||
|
|
||||||
var body = addGotoOut(n[1], result)
|
|
||||||
|
|
||||||
body = ctx.transformBreaksAndContinuesInWhile(body, result, gotoOut)
|
|
||||||
body = ctx.transformClosureIteratorBody(body, result)
|
|
||||||
|
|
||||||
elifBranch.add(body)
|
|
||||||
ifNode.add(elifBranch)
|
|
||||||
|
|
||||||
|
of nkIfStmt, nkCaseStmt:
|
||||||
|
for i in 0 ..< n.len:
|
||||||
|
n[i] = ctx.transformClosureIteratorBody(n[i], gotoOut)
|
||||||
|
if n[^1].kind != nkElse:
|
||||||
|
# We don't have an else branch, but every possible branch has to end with
|
||||||
|
# gotoOut, so add else here.
|
||||||
let elseBranch = newTree(nkElse, gotoOut)
|
let elseBranch = newTree(nkElse, gotoOut)
|
||||||
ifNode.add(elseBranch)
|
n.add(elseBranch)
|
||||||
s.add(ifNode)
|
|
||||||
|
|
||||||
of nkBlockStmt:
|
of nkWhileStmt:
|
||||||
result[1] = addGotoOut(result[1], gotoOut)
|
# while e:
|
||||||
result[1] = ctx.transformBreaksInBlock(result[1], result[0], gotoOut)
|
# s
|
||||||
result[1] = ctx.transformClosureIteratorBody(result[1], gotoOut)
|
# ->
|
||||||
|
# BEGIN_STATE:
|
||||||
|
# if e:
|
||||||
|
# s
|
||||||
|
# goto BEGIN_STATE
|
||||||
|
# else:
|
||||||
|
# goto OUT
|
||||||
|
|
||||||
of nkTryStmt, nkHiddenTryStmt:
|
result = newNodeI(nkGotoState, n.info)
|
||||||
# See explanation above about how this works
|
|
||||||
ctx.hasExceptions = true
|
|
||||||
|
|
||||||
result = newNodeI(nkGotoState, n.info)
|
let s = newNodeI(nkStmtList, n.info)
|
||||||
var tryBody = toStmtList(n[0])
|
discard ctx.newState(s, result)
|
||||||
var exceptBody = ctx.collectExceptState(n)
|
let ifNode = newNodeI(nkIfStmt, n.info)
|
||||||
var finallyBody = newTree(nkStmtList, getFinallyNode(ctx, n))
|
let elifBranch = newNodeI(nkElifBranch, n.info)
|
||||||
finallyBody = ctx.transformReturnsInTry(finallyBody)
|
elifBranch.add(n[0])
|
||||||
finallyBody.add(ctx.newEndFinallyNode(finallyBody.info))
|
|
||||||
|
|
||||||
# The following index calculation is based on the knowledge how state
|
var body = addGotoOut(n[1], result)
|
||||||
# indexes are assigned
|
|
||||||
let tryIdx = ctx.states.len
|
|
||||||
var exceptIdx, finallyIdx: int
|
|
||||||
if exceptBody.kind != nkEmpty:
|
|
||||||
exceptIdx = -(tryIdx + 1)
|
|
||||||
finallyIdx = tryIdx + 2
|
|
||||||
else:
|
|
||||||
exceptIdx = tryIdx + 1
|
|
||||||
finallyIdx = tryIdx + 1
|
|
||||||
|
|
||||||
let outToFinally = newNodeI(nkGotoState, finallyBody.info)
|
body = ctx.transformBreaksAndContinuesInWhile(body, result, gotoOut)
|
||||||
|
body = ctx.transformClosureIteratorBody(body, result)
|
||||||
|
|
||||||
block: # Create initial states.
|
elifBranch.add(body)
|
||||||
let oldExcHandlingState = ctx.curExcHandlingState
|
ifNode.add(elifBranch)
|
||||||
ctx.curExcHandlingState = exceptIdx
|
|
||||||
let realTryIdx = ctx.newState(tryBody, result)
|
|
||||||
assert(realTryIdx == tryIdx)
|
|
||||||
|
|
||||||
if exceptBody.kind != nkEmpty:
|
let elseBranch = newTree(nkElse, gotoOut)
|
||||||
ctx.curExcHandlingState = finallyIdx
|
ifNode.add(elseBranch)
|
||||||
let realExceptIdx = ctx.newState(exceptBody, nil)
|
s.add(ifNode)
|
||||||
assert(realExceptIdx == -exceptIdx)
|
|
||||||
|
|
||||||
ctx.curExcHandlingState = oldExcHandlingState
|
of nkBlockStmt:
|
||||||
let realFinallyIdx = ctx.newState(finallyBody, outToFinally)
|
result[1] = addGotoOut(result[1], gotoOut)
|
||||||
assert(realFinallyIdx == finallyIdx)
|
result[1] = ctx.transformBreaksInBlock(result[1], result[0], gotoOut)
|
||||||
|
result[1] = ctx.transformClosureIteratorBody(result[1], gotoOut)
|
||||||
|
|
||||||
block: # Subdivide the states
|
of nkTryStmt, nkHiddenTryStmt:
|
||||||
let oldNearestFinally = ctx.nearestFinally
|
# See explanation above about how this works
|
||||||
ctx.nearestFinally = finallyIdx
|
ctx.hasExceptions = true
|
||||||
|
|
||||||
let oldExcHandlingState = ctx.curExcHandlingState
|
result = newNodeI(nkGotoState, n.info)
|
||||||
|
var tryBody = toStmtList(n[0])
|
||||||
ctx.curExcHandlingState = exceptIdx
|
var exceptBody = ctx.collectExceptState(n)
|
||||||
|
var finallyBody = newTree(nkStmtList, getFinallyNode(ctx, n))
|
||||||
if ctx.transformReturnsInTry(tryBody) != tryBody:
|
finallyBody = ctx.transformReturnsInTry(finallyBody)
|
||||||
internalError(ctx.g.config, "transformReturnsInTry != tryBody")
|
finallyBody.add(ctx.newEndFinallyNode(finallyBody.info))
|
||||||
if ctx.transformClosureIteratorBody(tryBody, outToFinally) != tryBody:
|
|
||||||
internalError(ctx.g.config, "transformClosureIteratorBody != tryBody")
|
|
||||||
|
|
||||||
ctx.curExcHandlingState = finallyIdx
|
|
||||||
ctx.addElseToExcept(exceptBody)
|
|
||||||
if ctx.transformReturnsInTry(exceptBody) != exceptBody:
|
|
||||||
internalError(ctx.g.config, "transformReturnsInTry != exceptBody")
|
|
||||||
if ctx.transformClosureIteratorBody(exceptBody, outToFinally) != exceptBody:
|
|
||||||
internalError(ctx.g.config, "transformClosureIteratorBody != exceptBody")
|
|
||||||
|
|
||||||
ctx.curExcHandlingState = oldExcHandlingState
|
|
||||||
ctx.nearestFinally = oldNearestFinally
|
|
||||||
if ctx.transformClosureIteratorBody(finallyBody, gotoOut) != finallyBody:
|
|
||||||
internalError(ctx.g.config, "transformClosureIteratorBody != finallyBody")
|
|
||||||
|
|
||||||
of nkGotoState, nkForStmt:
|
|
||||||
internalError(ctx.g.config, "closure iter " & $n.kind)
|
|
||||||
|
|
||||||
|
# The following index calculation is based on the knowledge how state
|
||||||
|
# indexes are assigned
|
||||||
|
let tryIdx = ctx.states.len
|
||||||
|
var exceptIdx, finallyIdx: int
|
||||||
|
if exceptBody.kind != nkEmpty:
|
||||||
|
exceptIdx = -(tryIdx + 1)
|
||||||
|
finallyIdx = tryIdx + 2
|
||||||
else:
|
else:
|
||||||
for i in 0 ..< n.len:
|
exceptIdx = tryIdx + 1
|
||||||
n[i] = ctx.transformClosureIteratorBody(n[i], gotoOut)
|
finallyIdx = tryIdx + 1
|
||||||
|
|
||||||
|
let outToFinally = newNodeI(nkGotoState, finallyBody.info)
|
||||||
|
|
||||||
|
block: # Create initial states.
|
||||||
|
let oldExcHandlingState = ctx.curExcHandlingState
|
||||||
|
ctx.curExcHandlingState = exceptIdx
|
||||||
|
let realTryIdx = ctx.newState(tryBody, result)
|
||||||
|
assert(realTryIdx == tryIdx)
|
||||||
|
|
||||||
|
if exceptBody.kind != nkEmpty:
|
||||||
|
ctx.curExcHandlingState = finallyIdx
|
||||||
|
let realExceptIdx = ctx.newState(exceptBody, nil)
|
||||||
|
assert(realExceptIdx == -exceptIdx)
|
||||||
|
|
||||||
|
ctx.curExcHandlingState = oldExcHandlingState
|
||||||
|
let realFinallyIdx = ctx.newState(finallyBody, outToFinally)
|
||||||
|
assert(realFinallyIdx == finallyIdx)
|
||||||
|
|
||||||
|
block: # Subdivide the states
|
||||||
|
let oldNearestFinally = ctx.nearestFinally
|
||||||
|
ctx.nearestFinally = finallyIdx
|
||||||
|
|
||||||
|
let oldExcHandlingState = ctx.curExcHandlingState
|
||||||
|
|
||||||
|
ctx.curExcHandlingState = exceptIdx
|
||||||
|
|
||||||
|
if ctx.transformReturnsInTry(tryBody) != tryBody:
|
||||||
|
internalError(ctx.g.config, "transformReturnsInTry != tryBody")
|
||||||
|
if ctx.transformClosureIteratorBody(tryBody, outToFinally) != tryBody:
|
||||||
|
internalError(ctx.g.config, "transformClosureIteratorBody != tryBody")
|
||||||
|
|
||||||
|
ctx.curExcHandlingState = finallyIdx
|
||||||
|
ctx.addElseToExcept(exceptBody)
|
||||||
|
if ctx.transformReturnsInTry(exceptBody) != exceptBody:
|
||||||
|
internalError(ctx.g.config, "transformReturnsInTry != exceptBody")
|
||||||
|
if ctx.transformClosureIteratorBody(exceptBody, outToFinally) != exceptBody:
|
||||||
|
internalError(ctx.g.config, "transformClosureIteratorBody != exceptBody")
|
||||||
|
|
||||||
|
ctx.curExcHandlingState = oldExcHandlingState
|
||||||
|
ctx.nearestFinally = oldNearestFinally
|
||||||
|
if ctx.transformClosureIteratorBody(finallyBody, gotoOut) != finallyBody:
|
||||||
|
internalError(ctx.g.config, "transformClosureIteratorBody != finallyBody")
|
||||||
|
|
||||||
|
of nkGotoState, nkForStmt:
|
||||||
|
internalError(ctx.g.config, "closure iter " & $n.kind)
|
||||||
|
|
||||||
|
else:
|
||||||
|
for i in 0 ..< n.len:
|
||||||
|
n[i] = ctx.transformClosureIteratorBody(n[i], gotoOut)
|
||||||
|
|
||||||
proc stateFromGotoState(n: PNode): int =
|
proc stateFromGotoState(n: PNode): int =
|
||||||
assert(n.kind == nkGotoState)
|
assert(n.kind == nkGotoState)
|
||||||
|
|
@ -1149,7 +1148,6 @@ proc newCatchBody(ctx: var Ctx, info: TLineInfo): PNode {.inline.} =
|
||||||
|
|
||||||
# :state = exceptionTable[:state]
|
# :state = exceptionTable[:state]
|
||||||
block:
|
block:
|
||||||
|
|
||||||
# exceptionTable[:state]
|
# exceptionTable[:state]
|
||||||
let getNextState = newTree(nkBracketExpr,
|
let getNextState = newTree(nkBracketExpr,
|
||||||
ctx.createExceptionTable(),
|
ctx.createExceptionTable(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue