Skip to content

Commit

Permalink
ADT -> Union
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 11, 2024
1 parent c4bce3a commit d156026
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import treesittersmithy.NodeType
import treesittersmithy.TypeName

enum Type {
case ADT(name: TypeName, subtypes: NonEmptyList[Subtype])
case Union(name: TypeName, subtypes: NonEmptyList[Subtype])
case Product(name: TypeName, fields: List[Field], children: Option[Children])
}

Expand All @@ -18,11 +18,11 @@ case class Subtype(name: TypeName)
object IR {

def from(nt: NodeType): Type =
if nt.subtypes.nonEmpty then fromADT(nt)
if nt.subtypes.nonEmpty then fromUnion(nt)
else
fromProduct(nt)

private def fromADT(nt: NodeType): Type.ADT = Type.ADT(
private def fromUnion(nt: NodeType): Type.Union = Type.Union(
name = nt.tpe,
subtypes = NonEmptyList.fromListUnsafe(nt.subtypes.map(subtype => Subtype(name = subtype.tpe))),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ extension (tpe: NodeType) {

def render: String =
IR.from(tpe) match {
case adt: Type.ADT => renderAdt(adt)
case union: Type.Union => renderUnion(union)
case product: Type.Product => renderProduct(product)
}

}

private def renderAdt(adt: Type.ADT): String = {
val name = adt.name.render
private def renderUnion(u: Type.Union): String = {
val name = u.name.render

val projections = adt.subtypes.map { sub =>
val projections = u.subtypes.map { sub =>
// format: off
show"""def ${sub.name.renderProjection}: Option[${sub.name.render}] = ${sub.name.render}.unapply(node)"""
// format: on
}

val applyMethod = {
val cases = adt
val cases = u
.subtypes
.map(nodeType => show"""case ${nodeType.name.render}(node) => Right(node)""")

Expand All @@ -65,7 +65,7 @@ private def renderAdt(adt: Type.ADT): String = {
|
|import ${classOf[Node].getName()}
|
|opaque type $name <: Node = ${adt.subtypes.map(_.name.render).mkString_(" | ")}
|opaque type $name <: Node = ${u.subtypes.map(_.name.render).mkString_(" | ")}
|
|object $name {
|
Expand Down

0 comments on commit d156026

Please sign in to comment.