Skip to content

Instantly share code, notes, and snippets.

@mandubian
Last active November 13, 2017 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/bba50d55f26fcd14e82e9c0c29854a57 to your computer and use it in GitHub Desktop.
Save mandubian/bba50d55f26fcd14e82e9c0c29854a57 to your computer and use it in GitHub Desktop.
// Fake Scalafix rule to rewrite Scala to CCC
// Just so that you catch the idea
final case class CCCRule(index: SemanticdbIndex) extends SemanticRule(index, "CCCRule") {
def convertSubTree(ctx: RuleCtx, v: Tree, tree: Tree): Tree = {
tree match {
// the identity morphism
case t@q"$x => ${y: Term.Name}" if (x.name.isEqual(y)) =>
q"""K.id[${x.decltpe.get}]"""
// the plus function completely hardcoded and supposing we have CCC & CCCNumExt in the context
case t@q"$px => $py => $x + $y" =>
q"""○(addC)(⨂(exl[${x.decltpe.get}, ${x.decltpe.get}])(exr[${px.decltpe.get}, ${py.decltpe.get}]))"""
}
override def fix(ctx: RuleCtx): Patch = {
val trees = convertTree(ctx, ctx.tree)
trees.map { case (tree, newTree) => ctx.replaceTree(tree, newTree.toString) }.asPatch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment