Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove sub-env and move locals rewrite to a separate pass #222

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions cps/transform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export Continuation, ContinuationProc, cpsCall, cpsMustJump
when CallNodes - {nnkHiddenCallConv} != nnkCallKinds:
{.error: "i'm afraid of what you may have become".}

proc annotate(parent: var Env; n: NormNode): NormNode
proc annotate(env: var Env; n: NormNode): NormNode

proc makeContProc(name: Name, cont: Name, source: NimNode): ProcDef =
## creates a continuation proc with `name` using continuation
Expand Down Expand Up @@ -625,21 +625,9 @@ proc shimAssign(env: var Env; store: NormNode, call: Call, tail: NormNode): Norm
shim.add env.annotate(body)
result = shim

proc annotate(parent: var Env; n: NormNode): NormNode =
proc annotate(env: var Env; n: NormNode): NormNode =
## annotate `input` or otherwise prepare it for conversion into a
## mutually-recursive cps convertible form; the `parent` environment
## may be mutated as a side-effect, otherwise a new environment will be
## created which points to this parent.

# the accumulated environment
var env =
if n.kind == nnkStmtList:
newEnv(parent)
else:
parent

# first, rewrite any symbols that have been moved to the env
var n = rewriteSymbolsIntoEnvDotField(parent, n)
## mutually-recursive cps convertible form

# the result is a copy of the current node
result = copyNimNode n
Expand Down Expand Up @@ -1114,18 +1102,18 @@ proc cpsTransformProc(T: NimNode, n: NimNode): NormNode =
Trace.hook env.first, n # hooking against the proc (minus cloned body)
body.add n.body # add in the cloned body of the original proc

# perform sym substitutions (or whatever)
n.body = env.rewriteSymbolsIntoEnvDotField body

# transform defers
n.body = rewriteDefer n.body
n.body = rewriteDefer body

# rewrite non-yielding cps calls
n.body = env.rewriteVoodoo n.body

# annotate the proc's body
n.body = env.annotate n.body

# perform sym substitutions (or whatever)
n.body = env.rewriteSymbolsIntoEnvDotField n.body

if n.body.firstReturn.isNil:
# fixes https://github.com/disruptek/cps/issues/145
# by ensuring that we always rewrite termination
Expand Down