Cosmetics
This commit is contained in:
parent
7d38db284b
commit
13167c85f6
1 changed files with 23 additions and 22 deletions
|
|
@ -65,18 +65,19 @@ import
|
||||||
idents, renderer, types, magicsys, rodread, lowerings, tables, sequtils,
|
idents, renderer, types, magicsys, rodread, lowerings, tables, sequtils,
|
||||||
lambdalifting
|
lambdalifting
|
||||||
|
|
||||||
type ClosureIteratorTransformationContext = object
|
type
|
||||||
fn: PSym
|
Ctx = object
|
||||||
stateVarSym: PSym # :state variable. nil if env already introduced by lambdalifting
|
fn: PSym
|
||||||
states: seq[PNode] # The resulting states. Every state is an nkState node.
|
stateVarSym: PSym # :state variable. nil if env already introduced by lambdalifting
|
||||||
blockLevel: int # Temp used to transform break and continue stmts
|
states: seq[PNode] # The resulting states. Every state is an nkState node.
|
||||||
stateLoopLabel: PSym # Label to break on, when jumping between states.
|
blockLevel: int # Temp used to transform break and continue stmts
|
||||||
exitStateIdx: int # index of the last state
|
stateLoopLabel: PSym # Label to break on, when jumping between states.
|
||||||
tempVarId: int # unique name counter
|
exitStateIdx: int # index of the last state
|
||||||
tempVars: PNode # Temp var decls, nkVarSection
|
tempVarId: int # unique name counter
|
||||||
loweredStmtListExpr: PNode # Temporary used for nkStmtListExpr lowering
|
tempVars: PNode # Temp var decls, nkVarSection
|
||||||
|
loweredStmtListExpr: PNode # Temporary used for nkStmtListExpr lowering
|
||||||
|
|
||||||
proc newStateAssgn(ctx: var ClosureIteratorTransformationContext, stateNo: int = -2): PNode =
|
proc newStateAssgn(ctx: var Ctx, stateNo: int = -2): PNode =
|
||||||
# Creates state assignmen:
|
# Creates state assignmen:
|
||||||
# :state = stateNo
|
# :state = stateNo
|
||||||
|
|
||||||
|
|
@ -95,7 +96,7 @@ proc setStateInAssgn(stateAssgn: PNode, stateNo: int) =
|
||||||
assert stateAssgn[1].kind == nkIntLit
|
assert stateAssgn[1].kind == nkIntLit
|
||||||
stateAssgn[1].intVal = stateNo
|
stateAssgn[1].intVal = stateNo
|
||||||
|
|
||||||
proc newState(ctx: var ClosureIteratorTransformationContext, n, gotoOut: PNode): int =
|
proc newState(ctx: var Ctx, n, gotoOut: PNode): int =
|
||||||
# Creates a new state, adds it to the context fills out `gotoOut` so that it
|
# Creates a new state, adds it to the context fills out `gotoOut` so that it
|
||||||
# will goto this state.
|
# will goto this state.
|
||||||
# Returns index of the newly created state
|
# Returns index of the newly created state
|
||||||
|
|
@ -123,7 +124,7 @@ proc addGotoOut(n: PNode, gotoOut: PNode): PNode =
|
||||||
if result.len != 0 and result.sons[^1].kind != nkGotoState:
|
if result.len != 0 and result.sons[^1].kind != nkGotoState:
|
||||||
result.add(gotoOut)
|
result.add(gotoOut)
|
||||||
|
|
||||||
proc newTempVarAccess(ctx: var ClosureIteratorTransformationContext, typ: PType, i: TLineInfo): PNode =
|
proc newTempVarAccess(ctx: var Ctx, typ: PType, i: TLineInfo): PNode =
|
||||||
if not ctx.stateVarSym.isNil:
|
if not ctx.stateVarSym.isNil:
|
||||||
# We haven't gone through labmda lifting yet, so just create a local var,
|
# We haven't gone through labmda lifting yet, so just create a local var,
|
||||||
# it will be lifted later
|
# it will be lifted later
|
||||||
|
|
@ -157,7 +158,7 @@ proc hasYields(n: PNode): bool =
|
||||||
result = true
|
result = true
|
||||||
break
|
break
|
||||||
|
|
||||||
proc transformBreaksAndContinuesInWhile(ctx: var ClosureIteratorTransformationContext, n: PNode, before, after: PNode): PNode =
|
proc transformBreaksAndContinuesInWhile(ctx: var Ctx, n: PNode, before, after: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
||||||
|
|
@ -177,7 +178,7 @@ proc transformBreaksAndContinuesInWhile(ctx: var ClosureIteratorTransformationCo
|
||||||
for i in 0 ..< n.len:
|
for i in 0 ..< n.len:
|
||||||
n[i] = ctx.transformBreaksAndContinuesInWhile(n[i], before, after)
|
n[i] = ctx.transformBreaksAndContinuesInWhile(n[i], before, after)
|
||||||
|
|
||||||
proc transformBreaksInBlock(ctx: var ClosureIteratorTransformationContext, n: PNode, label, after: PNode): PNode =
|
proc transformBreaksInBlock(ctx: var Ctx, n: PNode, label, after: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
||||||
|
|
@ -252,7 +253,7 @@ proc hasYieldsInExpressions(n: PNode): bool =
|
||||||
if c.hasYieldsInExpressions:
|
if c.hasYieldsInExpressions:
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc lowerStmtListExpr(ctx: var ClosureIteratorTransformationContext, n: PNode): PNode =
|
proc lowerStmtListExpr(ctx: var Ctx, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
||||||
|
|
@ -274,7 +275,7 @@ proc lowerStmtListExpr(ctx: var ClosureIteratorTransformationContext, n: PNode):
|
||||||
for i in 0 ..< n.len:
|
for i in 0 ..< n.len:
|
||||||
n[i] = ctx.lowerStmtListExpr(n[i])
|
n[i] = ctx.lowerStmtListExpr(n[i])
|
||||||
|
|
||||||
proc transformClosureIteratorBody(ctx: var ClosureIteratorTransformationContext, 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 nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
||||||
|
|
@ -412,7 +413,7 @@ proc stateFromGotoState(n: PNode): int =
|
||||||
assert(n.kind == nkGotoState)
|
assert(n.kind == nkGotoState)
|
||||||
result = n[0].intVal.int
|
result = n[0].intVal.int
|
||||||
|
|
||||||
proc tranformStateAssignments(ctx: var ClosureIteratorTransformationContext, n: PNode): PNode =
|
proc tranformStateAssignments(ctx: var Ctx, n: PNode): PNode =
|
||||||
# This transforms 3 patterns:
|
# This transforms 3 patterns:
|
||||||
########################## 1
|
########################## 1
|
||||||
# yield e
|
# yield e
|
||||||
|
|
@ -484,7 +485,7 @@ proc skipStmtList(n: PNode): PNode =
|
||||||
if result.len == 0: return emptyNode
|
if result.len == 0: return emptyNode
|
||||||
result = result[0]
|
result = result[0]
|
||||||
|
|
||||||
proc skipThroughEmptyStates(ctx: var ClosureIteratorTransformationContext, n: PNode): PNode =
|
proc skipThroughEmptyStates(ctx: var Ctx, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
of nkCharLit..nkUInt64Lit, nkFloatLit..nkFloat128Lit, nkStrLit..nkTripleStrLit,
|
||||||
|
|
@ -515,7 +516,7 @@ proc skipThroughEmptyStates(ctx: var ClosureIteratorTransformationContext, n: PN
|
||||||
for i in 0 ..< n.len:
|
for i in 0 ..< n.len:
|
||||||
n[i] = ctx.skipThroughEmptyStates(n[i])
|
n[i] = ctx.skipThroughEmptyStates(n[i])
|
||||||
|
|
||||||
proc wrapIntoStateLoop(ctx: var ClosureIteratorTransformationContext, n: PNode): PNode =
|
proc wrapIntoStateLoop(ctx: var Ctx, n: PNode): PNode =
|
||||||
result = newNode(nkWhileStmt)
|
result = newNode(nkWhileStmt)
|
||||||
result.add(newSymNode(getSysSym("true")))
|
result.add(newSymNode(getSysSym("true")))
|
||||||
|
|
||||||
|
|
@ -549,7 +550,7 @@ proc wrapIntoStateLoop(ctx: var ClosureIteratorTransformationContext, n: PNode):
|
||||||
|
|
||||||
loopBody.add(blockStmt)
|
loopBody.add(blockStmt)
|
||||||
|
|
||||||
proc deleteEmptyStates(ctx: var ClosureIteratorTransformationContext) =
|
proc deleteEmptyStates(ctx: var Ctx) =
|
||||||
let goOut = newNode(nkGotoState)
|
let goOut = newNode(nkGotoState)
|
||||||
goOut.add(newIntLit(-1))
|
goOut.add(newIntLit(-1))
|
||||||
|
|
||||||
|
|
@ -580,7 +581,7 @@ proc deleteEmptyStates(ctx: var ClosureIteratorTransformationContext) =
|
||||||
inc i
|
inc i
|
||||||
|
|
||||||
proc transformClosureIterator*(fn: PSym, n: PNode): PNode =
|
proc transformClosureIterator*(fn: PSym, n: PNode): PNode =
|
||||||
var ctx: ClosureIteratorTransformationContext
|
var ctx: Ctx
|
||||||
ctx.fn = fn
|
ctx.fn = fn
|
||||||
|
|
||||||
if getEnvParam(fn).isNil:
|
if getEnvParam(fn).isNil:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue