Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Dec 19, 2024
1 parent 002302b commit 1b7ed02
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
34 changes: 22 additions & 12 deletions syntax/shared/src/main/scala/chester/syntax/core/Term.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: More correctly implement toDoc
package chester.syntax.core
package chester.syntax.core.spec

import cats.data.*
import chester.doc.*
Expand Down Expand Up @@ -36,8 +36,8 @@ import scala.language.implicitConversions
* Example hierarchy:
* - TermT[Term] - Base trait for all terms
* - LocalVC[Term] - Structure for local variables
* - LocalV - Concrete implementation
* - LocalVF[Term, ThisTree] - Factory for creating LocalV instances
* - LocalVC[Term] - Concrete implementation
* - LocalVF[Term, ThisTree] - Factory for creating LocalVC[Term] instances
*/
object spec {

Expand Down Expand Up @@ -291,6 +291,8 @@ object spec {
override def descent(f: Term => Term, g: TreeMap[Term]): Term = this
}

implicit def metaVec[Term <: TermT[Term], ThisTree <: MetaTermC[Term]](x: Vector[MetaTermC[Term]]): Vector[ThisTree] = x.asInstanceOf[Vector[ThisTree]]

@FunctionalInterface
trait ListTermF[Term <: TermT[Term], ThisTree <: ListTermC[Term]] {
def newListTerm(terms: Vector[Term], meta: OptionTermMeta): ThisTree
Expand Down Expand Up @@ -1059,13 +1061,13 @@ object spec {

@FunctionalInterface
trait EffectsF[Term <: TermT[Term], ThisTree <: EffectsC[Term]] {
def newEffects(effects: Map[LocalV, Term], meta: OptionTermMeta): ThisTree
def newEffects(effects: Map[LocalVC[Term], Term], meta: OptionTermMeta): ThisTree
}

trait EffectsC[Term <: TermT[Term]] extends EffectT[Term] {
override type ThisTree <: EffectsC[Term]

def effects: Map[LocalV, Term]
def effects: Map[LocalVC[Term], Term]

def cons: EffectsF[Term, ThisTree]

Expand All @@ -1081,14 +1083,17 @@ object spec {
override def collectMeta: Vector[MetaTermC[Term]] =
effects.flatMap((a, b) => a.collectMeta ++ b.collectMeta).toVector

override def replaceMeta(f: MetaTermC[Term] => Term): Effects = cpy(effects = effects.map { case (a, b) =>
(a.replaceMeta(f).asInstanceOf[LocalV], b.replaceMeta(f))
override def replaceMeta(f: MetaTermC[Term] => Term): ThisTree = cpy(effects = effects.map { case (a, b) =>
(a.replaceMeta(f).asInstanceOf[LocalVC[Term]], b.replaceMeta(f))
})
def cpy(effects: Map[LocalV, Term] = effects, meta: OptionTermMeta = meta): ThisTree = cons.newEffects(effects, meta)
def cpy(effects: Map[LocalVC[Term], Term] = effects, meta: OptionTermMeta = meta): ThisTree = cons.newEffects(effects, meta)

override def descent(f: Term => Term, g: TreeMap[Term]): Term = thisOr(cpy(effects = effects.map { case (k, v) => (k, f(v)) }))
}

implicit def convertF[Term <: TermT[Term], MetaTerm <: MetaTermC[Term]](x: MetaTerm): MetaTermC[Term] = x.asInstanceOf[MetaTermC[Term]]

implicit def convertF[Term <: TermT[Term], MetaTerm <: MetaTermC[Term]](x: (MetaTerm=>Term)): (MetaTermC[Term]=>Term) = x.asInstanceOf[(MetaTermC[Term]=>Term) ]
@FunctionalInterface
trait ExceptionEffectF[Term <: TermT[Term], ThisTree <: ExceptionEffectC[Term]] {
def newExceptionEffect(meta: OptionTermMeta): ThisTree
Expand Down Expand Up @@ -1464,14 +1469,16 @@ object spec {
def extendsClause: Option[Term]

def body: Option[BlockTermC[Term]]
override def switchUniqId(r: UReplacer): ObjectStmtTerm = copy(uniqId = r(uniqId))
override def switchUniqId(r: UReplacer): ObjectStmtTerm = cpy(uniqId = r(uniqId))

override def toDoc(using options: PrettierOptions): Doc = {
val extendsDoc = extendsClause.map(_.toDoc).getOrElse(Doc.empty)
val bodyDoc = body.map(_.toDoc).getOrElse(Doc.empty)
Doc.text("object") <+> Doc.text(name) <+> extendsDoc <+> bodyDoc
}

def cons: ObjectStmtTermF[Term, ThisTree]

def cpy(name: Name = name, uniqId: UniqidOf[ObjectStmtTermC[Term]] = uniqId, extendsClause: Option[Term] = extendsClause, body: Option[BlockTermC[Term]] = body, meta: OptionTermMeta = meta): ThisTree =
cons.newObjectStmt(name, uniqId, extendsClause, body, meta)

Expand All @@ -1496,7 +1503,7 @@ object spec {
def body: Option[BlockTermC[Term]]


override def switchUniqId(r: UReplacer): InterfaceStmtTerm = cpy(uniqId = r(uniqId))
override def switchUniqId(r: UReplacer): ThisTree = cpy(uniqId = r(uniqId))


override def toDoc(using options: PrettierOptions): Doc = {
Expand All @@ -1507,6 +1514,8 @@ object spec {
)
}

def cons: InterfaceStmtTermF[Term, ThisTree]

def cpy(name: Name = name, uniqId: UniqidOf[InterfaceStmtTermC[Term]] = uniqId, extendsClause: Option[Term] = extendsClause, body: Option[BlockTermC[Term]] = body, meta: OptionTermMeta = meta): ThisTree =
cons.newInterfaceStmt(name, uniqId, extendsClause, body, meta)

Expand All @@ -1531,7 +1540,7 @@ object spec {
def extendsClause: Option[Term]
def body: Option[BlockTermC[Term]]

override def switchUniqId(r: UReplacer): TraitStmtTerm = cpy(uniqId = r(uniqId))
override def switchUniqId(r: UReplacer): ThisTree = cpy(uniqId = r(uniqId))


override def toDoc(using options: PrettierOptions): Doc = {
Expand All @@ -1542,6 +1551,7 @@ object spec {
)
}

def cons: TraitStmtTermF[Term, ThisTree]
def cpy(name: Name = name, uniqId: UniqidOf[TraitStmtTermC[Term]] = uniqId, extendsClause: Option[Term] = extendsClause, body: Option[BlockTermC[Term]] = body, meta: OptionTermMeta = meta): ThisTree =
cons.newTraitStmt(name, uniqId, extendsClause, body, meta)

Expand Down Expand Up @@ -1574,7 +1584,7 @@ object spec {

def cons: RecordStmtTermF[Term, ThisTree]

override def switchUniqId(r: UReplacer): RecordStmtTerm = cpy(uniqId = r(uniqId))
override def switchUniqId(r: UReplacer): ThisTree = cpy(uniqId = r(uniqId))

override def toDoc(using options: PrettierOptions): Doc = {
val fieldsDoc = fields.map(_.toDoc).reduceOption(_ <> Doc.text(", ") <> _).getOrElse(Doc.empty)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package chester.syntax.core

export chester.syntax.core.spec.*
export chester.syntax.core.spec.spec.*
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import cats.data.*
import chester.doc.const.*
import chester.error.Problem
import chester.syntax.*
import chester.syntax.core.spec.*
import chester.syntax.core.spec.spec.*
import chester.uniqid.*
import chester.utils.*
import chester.utils.doc.*
Expand Down

0 comments on commit 1b7ed02

Please sign in to comment.