Skip to content
Draft
Show file tree
Hide file tree
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
28 changes: 19 additions & 9 deletions cps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,29 @@ macro cpsTyped(tipe: typed, n: typed): untyped =
else:
result = getAst(cpsTransform(tipe, n))

macro cps*(tipe: typed, n: untyped): untyped =
macro cpsMarkExtract(n: untyped): untyped =
let cpsExtract = bindSym"cpsExtract"
result = nnkPragmaBlock.newTree(
nnkPragma.newTree(cpsExtract),
n
)

template cps*(tipe: typed, n: untyped): untyped =
## When applied to a procedure, rewrites the procedure into a
## continuation form. When applied to a procedure type definition,
## rewrites the type into a callback form.
result = n
bind cpsTyped
bind cpsScalpel
bind cpsMarkExtract

when not defined(nimdoc):
# add the application of the typed transformation pass
n.addPragma:
nnkExprColonExpr.newTree(bindSym"cpsTyped", tipe)
# let the untyped pass do what it will with this input
# XXX: currently disabled because it's a slipperly slope of regret
#result = performUntypedPass(T, n)
result = n
cpsScalpel:
const isInCps {.inject.} = true

cpsMarkExtract:
cpsTyped(tipe, n)
else:
n

proc adaptArguments(sym: NormNode; args: seq[NormNode]): seq[NormNode] =
## convert any arguments in the list as necessary to match those of
Expand Down
2 changes: 1 addition & 1 deletion cps/normalizedast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ defineToNimNodeConverter(
allowAutoDowngradeNormalizedNode(
Name, TypeExpr, Call, Conv, PragmaStmt, PragmaAtom, IdentDef, RoutineDef,
ProcDef, FormalParams, RoutineParam, VarSection, LetSection, VarLet,
VarLetIdentDef, VarLetTuple, DefVarLet, IdentDefLet, Sym
VarLetIdentDef, VarLetTuple, DefVarLet, IdentDefLet, Sym, PragmaBlock
)

# types that go from a specific type to a less specific type, "downgrade"
Expand Down
1 change: 1 addition & 0 deletions cps/spec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ template cpsTerminate*() {.pragma.} ## this is the end of this procedure
template cpsHasException*(cont, ex: typed) {.pragma.} ##
## the continuation has an exception stored in `ex`, with `cont` being the
## continuation symbol used.
template cpsExtract*() {.pragma.} ## extract this block from the scope

const
cpsStackFrames* {.booldefine, used.} = compileOption"stacktrace"
Expand Down
7 changes: 7 additions & 0 deletions cps/transform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,13 @@ macro cpsFloater(n: typed): untyped =
result = floater:
copyNimTree n

macro cpsScalpel*(n: typed): untyped =
## returns the first `{.cpsExtract.}` block from `n`
debugAnnotation cpsScalpel, n:
it = it.findChildRecursive() do (n: NormNode) -> bool:
n.kind == nnkPragmaBlock and n.asPragmaBlock().hasPragma("cpsExtract")
it = it.last

macro cpsManageException(name: static[string]; n: typed): untyped =
## rewrites all continuations in `n` containing an exception so that exception
## become the "current" exception of that continuation while preserving the
Expand Down