Skip to content

Commit

Permalink
Add specialized abort continuation
Browse files Browse the repository at this point in the history
  • Loading branch information
b-studios committed Jan 22, 2025
1 parent 84beb0b commit 07e2740
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
10 changes: 2 additions & 8 deletions effekt/shared/src/main/scala/effekt/cps/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object Transformer {
App(transform(callee), vargs.map(transform), bargs.map(transform), MetaCont(ks), k.reify)

case core.Stmt.Invoke(callee, method, tpe, targs, vargs, bargs) if stmt.tpe == core.Type.TBottom =>
Invoke(transform(callee), method, vargs.map(transform), bargs.map(transform), MetaCont(ks), Continuation.Empty)
Invoke(transform(callee), method, vargs.map(transform), bargs.map(transform), MetaCont(ks), Cont.Abort)

case core.Stmt.Invoke(callee, method, tpe, targs, vargs, bargs) =>
Invoke(transform(callee), method, vargs.map(transform), bargs.map(transform), MetaCont(ks), k.reify)
Expand Down Expand Up @@ -123,7 +123,7 @@ object Transformer {
transform(body, ks2, Continuation.Dynamic(k2)))

// if the answer type is Nothing, then the continuation is discarded anyways...
val continuation = if (stmt.tpe == core.Type.TBottom) Continuation.Empty else k.reify
val continuation = if (stmt.tpe == core.Type.TBottom) Cont.Abort else k.reify

Shift(prompt.id, translatedBody, MetaCont(ks), continuation)

Expand Down Expand Up @@ -239,10 +239,4 @@ enum Continuation {
}
object Continuation {
def Static(hint: Id)(k: (Pure, Id) => Stmt): Continuation.Static = Continuation.Static(hint, k)

val Empty: Cont = {
val a = Id("a")
val ks = Id("ks")
cps.Cont.ContLam(a, ks, cps.Hole())
}
}
3 changes: 3 additions & 0 deletions effekt/shared/src/main/scala/effekt/cps/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ case class Clause(vparams: List[Id], body: Stmt) extends Tree
enum Cont extends Tree {
case ContVar(id: Id)
case ContLam(result: Id, ks: Id, body: Stmt)
case Abort
}

case class MetaCont(id: Id) extends Tree
Expand Down Expand Up @@ -229,6 +230,7 @@ object Variables {
def free(ks: MetaCont): Variables = meta(ks.id)
def free(k: Cont): Variables = k match {
case Cont.ContVar(id) => cont(id)
case Cont.Abort => Set()
case Cont.ContLam(result, ks, body) => free(body) -- value(result) -- meta(ks)
}
}
Expand Down Expand Up @@ -385,6 +387,7 @@ object substitutions {
case Cont.ContVar(id) if subst.conts.isDefinedAt(id) => subst.conts(id)
case Cont.ContVar(id) => Cont.ContVar(id)
case lam @ Cont.ContLam(result, ks, body) => substitute(lam)
case Cont.Abort => Cont.Abort
}

def substitute(k: Cont.ContLam)(using subst: Substitution): Cont.ContLam = k match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ object TransformerCps extends Transformer {
nameRef(id)
case Cont.ContLam(result, ks, body) =>
js.Lambda(List(nameDef(result), nameDef(ks)), toJS(body)(using nonrecursive(ks)).stmts)
case Cont.Abort => js.Undefined
}

def toJS(e: cps.Expr)(using D: TransformerContext): js.Expr = e match {
Expand Down

0 comments on commit 07e2740

Please sign in to comment.