Skip to content

Commit

Permalink
Write adapters for backends that don't support splices yet
Browse files Browse the repository at this point in the history
  • Loading branch information
b-studios committed Dec 12, 2023
1 parent 219ad99 commit d241d4f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
6 changes: 4 additions & 2 deletions effekt/jvm/src/test/scala/effekt/ChezSchemeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class ChezSchemeLiftTests extends ChezSchemeTests {
def backendName = "chez-lift"

override def ignored: List[File] = super.ignored ++ List(
// global mutable state is not yet supported

// splices not yet supported
examplesDir / "pos" / "capture" / "resources.effekt" ,

// regions are not yet supported
examplesDir / "benchmarks" / "generator.effekt",
Expand All @@ -81,6 +83,6 @@ class ChezSchemeLiftTests extends ChezSchemeTests {

// known issues:
examplesDir / "pos" / "lambdas" / "simpleclosure.effekt", // doesn't work with lift inference, yet
examplesDir / "pos" / "capture" / "ffi_blocks.effekt" // ffi is passed evidence, which it does not need
examplesDir / "pos" / "capture" / "ffi_blocks.effekt", // ffi is passed evidence, which it does not need
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,17 @@ object TransformerLift {
chez.Lambda( params.flatMap {
case p: Param.EvidenceParam => None
case p => Some(ChezName(p.id.name.name)) },
chez.RawExpr(body)))
toChez(body)))

case Extern.Include(contents) =>
RawDef(contents)
}

def toChez(t: Template[lifted.Expr]): chez.Expr = t match {
case Template(List(string), Nil) => chez.RawExpr(string)
case _ => sys error "Splices not yet supported in the Chez Lift backend"
}

def toChez(defn: Definition): Either[chez.Def, Option[chez.Expr]] = defn match {
case Definition.Def(id, block) =>
Left(chez.Constant(nameDef(id), toChez(block)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,16 @@ object Transformer {

def toML(ext: Extern)(using TransformerContext): ml.Binding = ext match {
case Extern.Def(id, tparams, params, ret, body) =>
ml.FunBind(name(id), params map { p => ml.Param.Named(name(p.id.name)) }, RawExpr(body))
ml.FunBind(name(id), params map { p => ml.Param.Named(name(p.id.name)) }, toML(body))
case Extern.Include(contents) =>
RawBind(contents)
}

def toML(t: Template[lifted.Expr]): ml.Expr = t match {
case Template(List(string), Nil) => ml.RawExpr(string)
case _ => sys error "Splices not yet supported in the ML backend"
}

def toMLExpr(stmt: Stmt)(using C: TransformerContext): CPS = stmt match {
case lifted.Return(e) => CPS.pure(toML(e))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ object LiftInference extends Phase[CoreTransformed, CoreLifted] {
val eparams = bps map {
case core.BlockParam(id, tpe, capt) => Param.EvidenceParam(EvidenceSymbol())
}
Extern.Def(id, tps, vps.map(transform) ++ bps.map(transform), transform(ret), ???)
Extern.Def(id, tps, vps.map(transform) ++ bps.map(transform), transform(ret),
Template(body.strings, body.args.map(transform)))
case core.Extern.Include(contents) =>
Extern.Include(contents)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ object PrettyPrinter extends ParenPrettyPrinter {

def toDoc(e: Extern): Doc = e match {
case Extern.Def(id, tparams, params, ret, body) =>
"extern def" <+> toDoc(id.name) <> signature(tparams, params, ret) <+> "=" <+> "\"" <> body <> "\""
"extern def" <+> toDoc(id.name) <> signature(tparams, params, ret) <+> "=" <+> "\"" <> toDoc(body) <> "\""
case Extern.Include(contents) => emptyDoc // right now, do not print includes.
}

// TODO implement
def toDoc(t: Template[Expr]): Doc =
hsep(t.args.map(toDoc), comma)

def toDoc(b: Block): Doc = b match {
case BlockVar(v, _) => v.name.toString
case BlockLit(tps, ps, body) =>
Expand Down
2 changes: 1 addition & 1 deletion effekt/shared/src/main/scala/effekt/lifted/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ enum Extern {
// WARNING: builtins do not take evidence. If they are passed as function argument, they need to be eta-expanded.
// (however, if they _would_ take evidence, we could model mutable state with this)
// TODO revisit
case Def(id: Id, tparams: List[Id], params: List[Param], ret: ValueType, body: String)
case Def(id: Id, tparams: List[Id], params: List[Param], ret: ValueType, body: Template[Expr])
case Include(contents: String)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ object Transformer {
case lifted.EvidenceParam(id) => None // Variable(id.name.name, builtins.Evidence)
}
noteBlockParams(name, params map transform, List.empty)
Extern(transform(name), transformedParams, transform(ret), body)
Extern(transform(name), transformedParams, transform(ret), transform(body))

case lifted.Extern.Include(contents) =>
Include(contents)
}

def transform(t: Template[lifted.Expr]): String = t match {
case Template(List(string), Nil) => string
case _ => sys error "Splices not yet supported in backends based on Machine IR"
}

def transform(stmt: lifted.Stmt)(using BPC: BlocksParamsContext, DC: DeclarationContext, E: ErrorReporter): Statement =
stmt match {
case lifted.Scope(definitions, rest) =>
Expand Down

0 comments on commit d241d4f

Please sign in to comment.