Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed May 18, 2022
1 parent 6cee9ba commit 2f98a20
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cps.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std/[genasts, deques]
import cps/[spec, transform, rewrites, hooks, exprs, normalizedast]
import std/macros except newStmtList, newTree
export Continuation, ContinuationProc, State
export Continuation, ContinuationProc, State, Whelp
export cpsCall, cpsMagicCall, cpsVoodooCall, cpsMustJump
export cpsMagic, cpsVoodoo, trampoline, trampolineIt
export writeStackFrames, writeTraceDeque
Expand Down Expand Up @@ -63,6 +63,12 @@ macro cps*(T: typed, n: typed): untyped =
# Add the flattening phase which will be run first
newCall(bindSym"cpsFlattenExpr"):
n
of nnkProcTy:
# converting a cps callback
result = cpsCallbackTypeDef(T, n)
result = workaroundRewrites result.NormNode
echo treeRepr(result)
echo repr(result)
else:
result = getAst(cpsTransform(T, n))

Expand Down
4 changes: 4 additions & 0 deletions cps/spec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type

ContinuationProc*[T] = proc(c: T): T {.nimcall.}

Whelp*[C: Continuation; R; P: proc] = object
fn: P ## a pointer to a specific `whelp` function
rs: proc (cont: C): R ## the type signature of the `()` functions

TraceFrame* = object ## a record of where the continuation has been
hook*: Hook ## the hook that provoked the trace entry
fun*: string ## a short label for the notable symbol
Expand Down
10 changes: 10 additions & 0 deletions cps/transform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,16 @@ macro cpsHandleUnhandledException(contType: typed; n: typed): untyped =
debugAnnotation cpsHandleUnhandledException, n:
it = it.filter(handle)

proc cpsCallbackTypeDef*(T: NimNode, n: NimNode): NimNode =
## looks like cpsTransformProc but applies to proc typedefs;
## this is where we create our calling convention concept
let params = copyNimTree n[0]
let R = copyNimTree params[0]
params[0] = T
let P = nnkProcTy.newTree(params, newEmptyNode())
result = nnkBracketExpr.newTree(bindSym"Whelp", T, R, P)
result = workaroundRewrites result.NormNode

proc cpsTransformProc(T: NimNode, n: NimNode): NormNode =
## rewrite the target procedure in Continuation-Passing Style

Expand Down

0 comments on commit 2f98a20

Please sign in to comment.