Skip to content

Commit

Permalink
support devel branch, reduce warning spam
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Sep 8, 2022
1 parent 15b4115 commit 6f8ce71
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
fail-fast: false
matrix:
os: ['macos-latest'] #, 'ubuntu-latest']
nim: ['version-1-6']
nim: ['devel', 'version-1-6']
name: '${{ matrix.os }} (${{ matrix.nim }})'
runs-on: ${{ matrix.os }}
steps:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![Test Matrix](https://github.com/nim-works/cps/workflows/CI/badge.svg)](https://github.com/nim-works/cps/actions?query=workflow%3ACI)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/nim-works/cps?style=flat)](https://github.com/nim-works/cps/releases/latest)
![Maximum supported Nim version](https://img.shields.io/badge/nim-1.6.7-informational?style=flat&logo=nim)
![Minimum supported Nim version](https://img.shields.io/badge/nim-1.5.1-informational?style=flat&logo=nim)
![Recommended Nim version](https://img.shields.io/badge/nim-1.6.7-informational?style=flat&logo=nim)
![Maximum supported Nim version](https://img.shields.io/badge/nim-1.7.1-informational?style=flat&logo=nim)
[![License](https://img.shields.io/github/license/nim-works/cps?style=flat)](#license)
[![Matrix](https://img.shields.io/matrix/cps:matrix.org?style=flat&logo=matrix)](https://matrix.to/#/#cps:matrix.org)
[![IRC](https://img.shields.io/badge/chat-%23cps%20on%20libera.chat-brightgreen?style=flat)](https://web.libera.chat/#cps)
Expand Down
12 changes: 6 additions & 6 deletions cps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template dismissed*(c: Continuation): bool =

{.pop.}

macro cpsTyped(T: typed, n: typed): untyped =
macro cpsTyped(tipe: typed, n: typed): untyped =
## This is the typed CPS transformation pass which follows the untyped pass below.
when defined(nimdoc):
n
Expand All @@ -59,25 +59,25 @@ macro cpsTyped(T: typed, n: typed): untyped =
{.warning: "compiler bug workaround, see: https://github.com/nim-lang/Nim/issues/18349".}
result =
# Add the main transform phase
newCall(bindSym"cpsTransform", T):
newCall(bindSym"cpsTransform", tipe):
# Add the flattening phase which will be run first
newCall(bindSym"cpsFlattenExpr"):
n
of nnkProcTy:
# converting a cps callback
result = cpsCallbackTypeDef(T, n)
result = cpsCallbackTypeDef(tipe, n)
else:
result = getAst(cpsTransform(T, n))
result = getAst(cpsTransform(tipe, n))

macro cps*(T: typed, n: untyped): untyped =
macro 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
when not defined(nimdoc):
# add the application of the typed transformation pass
n.addPragma:
nnkExprColonExpr.newTree(bindSym"cpsTyped", T)
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)
Expand Down
10 changes: 5 additions & 5 deletions cps/exprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func getMagic(n: NormNode): string =
else:
discard

macro cpsExprToTmp(T, n: typed): untyped =
macro cpsExprToTmp(tipe, n: typed): untyped =
## Create a temporary variable with type `T` and rewrite `n` so that the
## result is assigned to the temporary, then emit the temporary as the
## expression.
Expand All @@ -283,7 +283,7 @@ macro cpsExprToTmp(T, n: typed): untyped =
# Create a new statement list
newStmtList(
# Declare the temporary
newVarSection(tmp, T.TypeExpr),
newVarSection(tmp, tipe.TypeExpr),
# Add the rewritten expression
body
)
Expand Down Expand Up @@ -317,14 +317,14 @@ macro cpsAsgn(dst, src: typed): untyped =
# out
it[0]

macro cpsExprConv(T, n: typed): untyped =
macro cpsExprConv(tipe, n: typed): untyped =
## Apply the conversion to `T` directly into `n`'s trailing expressions.
# If we don't shadow this parameter, it will be nnkNilLit.
{.warning: "compiler workaround here, see: https://github.com/nim-lang/Nim/issues/18352".}
let T = normalizingRewrites T
let tipe = normalizingRewrites tipe
debugAnnotation cpsExprConv, n:
proc addConv(n: NormNode): NormNode =
newCall(T, copy n)
newCall(tipe, copy n)

it = filterExpr(it[0], addConv)

Expand Down
2 changes: 1 addition & 1 deletion cps/hooks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ proc hook*(hook: static[Hook]; n: NormNode): NormNode =
case hook
of Boot, Coop, Head:
# hook(continuation)
hook.entrace nil.NormNode, n:
hook.entrace NilNormNode, n:
newCall(hook.sym, n)
else:
# cast to `Call` avoids type mismatch as converters can't figure this out
Expand Down
22 changes: 13 additions & 9 deletions cps/normalizedast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ from std/typetraits import distinctBase
from cps/rewrites import NormNode, normalizingRewrites, replace,
desym, resym, resymCall

const
NilNormNode* = nil.NormNode
NilNimNode* = nil.NimNode

export NormNode

# # Structure of the Module
Expand Down Expand Up @@ -490,22 +494,22 @@ template withLineInfoPlease(body: typed): untyped =
copyLineInfo(sym, info)
sym

proc genSymType*(n: string; info: NormNode = nil): Name =
proc genSymType*(n: string; info: NormNode = NilNormNode): Name =
## `genSym` an `nskType`
withLineInfoPlease: genSym(nskType, n).Name
proc genSymVar*(n: string = ""; info: NormNode = nil): Name =
proc genSymVar*(n: string = ""; info: NormNode = NilNormNode): Name =
## `genSym` an `nskVar`
withLineInfoPlease: genSym(nskVar, n).Name
proc genSymLet*(n: string = ""; info: NormNode = nil): Name =
proc genSymLet*(n: string = ""; info: NormNode = NilNormNode): Name =
## `genSym` an `nskLet`
withLineInfoPlease: genSym(nskLet, n).Name
proc genSymProc*(n: string; info: NormNode = nil): Name =
proc genSymProc*(n: string; info: NormNode = NilNormNode): Name =
## `genSym` an `nskProc`
withLineInfoPlease: genSym(nskProc, n).Name
proc genSymField*(n: string; info: NormNode = nil): Name =
proc genSymField*(n: string; info: NormNode = NilNormNode): Name =
## `genSym` an `nskField`
withLineInfoPlease: genSym(nskField, n).Name
proc genSymUnknown*(n: string; info: NormNode = nil): Name =
proc genSymUnknown*(n: string; info: NormNode = NilNormNode): Name =
## `genSym` an `nskUnknown`
withLineInfoPlease: genSym(nskUnknown, n).Name

Expand Down Expand Up @@ -825,7 +829,7 @@ proc asVarLetIdentDef*(n: VarLet): VarLetIdentDef =
## return a VarLetIdentDef if the def is an IdentDef, otherwise error out
validateAndCoerce(n.NimNode, VarLetIdentDef)

func clone*(n: VarLet, value: NimNode = nil): VarLet =
func clone*(n: VarLet, value: NimNode = NilNimNode): VarLet =
## clone a `VarLet` but with `value` changed
let def = copyNimNode(n.def)
# copy all nodes in the original definition excluding the last node, which
Expand Down Expand Up @@ -1160,7 +1164,7 @@ iterator callingParams*(n: ProcDef): RoutineParam =
for a in n.formalParams[1..^1].items:
yield a.RoutineParam

proc clone*(n: ProcDef, body: NimNode = nil): ProcDef =
proc clone*(n: ProcDef, body: NimNode = NilNimNode): ProcDef =
## create a copy of a typed proc which satisfies the compiler
result = nnkProcDef.newTree(
ident(repr n.name), # repr to handle gensymbols
Expand All @@ -1169,7 +1173,7 @@ proc clone*(n: ProcDef, body: NimNode = nil): ProcDef =
copy n.NimNode.params, # parameter normalization will mutate these
newEmptyNode(),
newEmptyNode(),
if body == nil: macros.copy(n.body) else: body
if body.isNil: macros.copy(n.body) else: body
).ProcDef
result.copyLineInfo n

Expand Down
8 changes: 4 additions & 4 deletions cps/returns.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ proc firstReturn*(p: NormNode): NormNode =
elif p.isScopeExit:
result = p
else:
result = nil
result = NilNormNode

proc makeReturn*(contType: Name; n: NormNode): NormNode =
## generate a `return` of the node if it doesn't already contain a return
Expand Down Expand Up @@ -54,13 +54,13 @@ template pass*(source: Continuation; destination: Continuation): Continuation {.
## The return value specifies the destination continuation.
Continuation destination

proc terminator*(c: Name; contType: Name; T: NormNode): NormNode =
proc terminator*(c: Name; contType: Name; tipe: NormNode): NormNode =
## produce the terminating return statement of the continuation;
## this should return control to the mom and dealloc the continuation,
## or simply set the fn to nil and return the continuation.
let coop = NimNode hook(Coop, asName"result")
let pass = NimNode hook(Pass, newCall(contType, c), c.dot "mom")
let dealloc = NimNode hook(Dealloc, newCall(contType, c), T)
let dealloc = NimNode hook(Dealloc, newCall(contType, c), tipe)
let c = NimNode c
NormNode:
quote:
Expand All @@ -81,7 +81,7 @@ proc terminator*(c: Name; contType: Name; T: NormNode): NormNode =
# critically, terminate control-flow here!
return

proc tailCall*(cont, contType, to: Name; jump: NormNode = nil): NormNode =
proc tailCall*(cont, contType, to: Name; jump: NormNode = NilNormNode): NormNode =
## a tail call to `to` with `cont` as the continuation; if the `jump`
## is supplied, return that call instead of the continuation itself
result = newStmtList:
Expand Down
10 changes: 5 additions & 5 deletions cps/spec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,15 @@ proc createCallback*(sym: NimNode): NimNode =
NimNode:
nnkObjConstr.newTree(tipe, "fn".colon fn.NimNode, "rs".colon rs.NimNode)

proc cpsCallbackTypeDef*(T: NimNode, n: NimNode): NimNode =
proc cpsCallbackTypeDef*(tipe: 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 = copyOrVoid params[0]
params[0] = T
let P = nnkProcTy.newTree(params,
let r = copyOrVoid params[0]
params[0] = tipe
let p = nnkProcTy.newTree(params,
nnkPragma.newTree(ident"nimcall", bindSym"cpsCallback"))
result = nnkBracketExpr.newTree(bindSym"Callback", T, R, P)
result = nnkBracketExpr.newTree(bindSym"Callback", tipe, r, p)
result = workaroundRewrites result.NormNode

proc recover*[C, R, P](callback: Callback[C, R, P]; continuation: C): R =
Expand Down
18 changes: 9 additions & 9 deletions cps/transform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ proc restoreBreak(n: NormNode, label = newEmptyNormNode()): NormNode =
n.breakLabel
m
else:
nil
NilNormNode

filter(n, restorer)

Expand All @@ -141,7 +141,7 @@ proc restoreContinue(n: NormNode): NormNode =
newNimNode(nnkContinueStmt, n).add:
newEmptyNormNode()
else:
nil.NormNode
NilNormNode

filter(n, restorer)

Expand Down Expand Up @@ -911,7 +911,7 @@ proc annotate(parent: var Env; n: NormNode): NormNode =
result.add env.annotate(nc)
endAndReturn()

macro cpsResolver(T: typed; contType: typed; n: typed): untyped =
macro cpsResolver(tipe: typed; contType: typed; n: typed): untyped =
## resolve any left over cps control-flow annotations
##
## this is not needed, but it's here so we can change this to
Expand All @@ -934,7 +934,7 @@ macro cpsResolver(T: typed; contType: typed; n: typed): untyped =
# replace all `pending` and `terminate` with the end of continuation
it = replace(it, proc(x: NormNode): bool = x.isCpsPending or x.isCpsTerminate):
if n.firstReturn.isNil:
terminator(cont, contType.asName, T.NormNode)
terminator(cont, contType.asName, tipe.NormNode)
else:
doc"omitted a return in the resolver".NormNode

Expand Down Expand Up @@ -1106,7 +1106,7 @@ macro cpsHandleUnhandledException(contType: typed; n: typed): untyped =
debugAnnotation cpsHandleUnhandledException, n:
it = it.filter(handle)

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

# keep the original symbol of the proc
Expand All @@ -1119,7 +1119,7 @@ proc cpsTransformProc(T: NimNode, n: NimNode): NormNode =

# creating the env with the continuation type,
# and adding proc parameters to the env
var env = newEnv(types, T.asName, n.returnParam)
var env = newEnv(types, tipe.asName, n.returnParam)

# add parameters into the environment
for defs in n.callingParams:
Expand Down Expand Up @@ -1253,10 +1253,10 @@ proc cpsTransformProc(T: NimNode, n: NimNode): NormNode =

result = workaroundRewrites result

macro cpsTransform*(T, n: typed): untyped =
macro cpsTransform*(tipe, n: typed): untyped =
## This is the macro performing the main cps transformation
debug("cpsTransform", n, Original)
result = cpsTransformProc(T, n)
result = cpsTransformProc(tipe, n)
debug("cpsTransform", result, Transformed, n)

proc looksLikeCallback(n: NimNode): bool =
Expand Down Expand Up @@ -1323,7 +1323,7 @@ proc rewriteCalls*(n: NimNode): NimNode =
discard
result = filter(n, recall)

proc performUntypedPass*(T: NimNode; n: NimNode): NimNode =
proc performUntypedPass*(tipe: NimNode; n: NimNode): NimNode =
## Perform any rewrites needed prior to a `.cps: T.` transformation.
if n.kind != nnkProcDef: return n
result = n
Expand Down

0 comments on commit 6f8ce71

Please sign in to comment.