Skip to content

Commit

Permalink
refactor(tyck): Refactor ElaboraterBlock to use capability-based appr…
Browse files Browse the repository at this point in the history
…oach - Move DeclarationInfo to companion object, update trait relationships, make ProvideElaboraterBlock extend ElaboraterBlock
  • Loading branch information
mio-19 committed Feb 18, 2025
1 parent b98eec9 commit bc29d5c
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions semantic/shared/src/main/scala/chester/tyck/ElaboraterBlock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import chester.syntax.concrete.*
import chester.syntax.core.*
import chester.tyck.api.SemanticCollector
import chester.uniqid.*
import chester.utils.propagator.*

import scala.language.implicitConversions

trait ElaboraterBlock extends Elaborater {
object ElaboraterBlock {
// Sealed trait for declaration information, for forwarding references
sealed trait DeclarationInfo extends Product with Serializable {
def expr: Expr
Expand Down Expand Up @@ -54,6 +55,10 @@ trait ElaboraterBlock extends Elaborater {
uniqId: UniqidOf[ObjectStmtTerm],
name: Name
) extends DeclarationInfo
}

trait ElaboraterBlock { this: ElaboraterBase & ElaboraterCommon =>
import ElaboraterBlock.*

def elabBlock(expr: Block, ty0: CellIdOr[Term], effects: CIdOf[EffectsCell])(using
localCtx: Context,
Expand All @@ -63,7 +68,9 @@ trait ElaboraterBlock extends Elaborater {
): BlockTerm
}

trait ProvideElaboraterBlock extends ElaboraterBlock {
trait ProvideElaboraterBlock extends ElaboraterBlock { this: Elaborater & ElaboraterBase & ElaboraterCommon =>
import ElaboraterBlock.*

def elabBlock(expr: Block, ty0: CellIdOr[Term], effects: CIdOf[EffectsCell])(using
localCtx: Context,
parameter: SemanticCollector,
Expand Down Expand Up @@ -91,13 +98,11 @@ trait ProvideElaboraterBlock extends ElaboraterBlock {
ctx = newCtx
stmtTerms

// Process trait statements
case expr: TraitStmt =>
val (stmtTerms, newCtx) = processTraitStmt(expr, ctx, declarationsMap, effects)
ctx = newCtx
stmtTerms

// Process interface statements
case expr: InterfaceStmt =>
val (stmtTerms, newCtx) = processInterfaceStmt(expr, ctx, declarationsMap, effects)
ctx = newCtx
Expand All @@ -112,7 +117,6 @@ trait ProvideElaboraterBlock extends ElaboraterBlock {
ck.reporter.apply(NotImplemented(importStmt))
Vector.empty

// Process object statements
case expr: ObjectStmt =>
val (stmtTerms, newCtx) = processObjectStmt(expr, ctx, declarationsMap, effects)
ctx = newCtx
Expand Down Expand Up @@ -142,10 +146,8 @@ trait ProvideElaboraterBlock extends ElaboraterBlock {
ck: Tyck,
state: StateAbility[Tyck]
): (Seq[DeclarationInfo], Seq[Name], Context) = {

// Collect all declarations in a single pass
val declarations = heads.collect {
// Collect 'def' declarations
case expr: LetDefStmt if expr.kind == LetDefType.Def =>
val name = expr.defined match {
// TODO: support other defined patterns
Expand All @@ -163,40 +165,33 @@ trait ProvideElaboraterBlock extends ElaboraterBlock {
ContextItem(name, id, localv, tyAndVal.ty, Some(r))
)

// Collect 'record' declarations
case expr: RecordStmt =>
val name = expr.name.name
val id = Uniqid.generate[RecordStmtTerm]
RecordDeclaration(expr, id, name)

// Collect 'trait' declarations
case expr: TraitStmt =>
val name = expr.name.name
val id = Uniqid.generate[TraitStmtTerm]
TraitDeclaration(expr, id, name)

// Collect 'interface' declarations
case expr: InterfaceStmt =>
val name = expr.name.name
val id = Uniqid.generate[InterfaceStmtTerm]
InterfaceDeclaration(expr, id, name)

// Collect 'object' declarations
case expr: ObjectStmt =>
val name = expr.name.name
val id = Uniqid.generate[ObjectStmtTerm]
ObjectDeclaration(expr, id, name)
}

val names = declarations.map(_.name)

// Collect context items from 'def' declarations
val defContextItems = declarations.collect { case defDecl: DefDeclaration =>
defDecl.item
}
val initialCtx = localCtx.add(defContextItems)

// Return all declarations, names, and the initial context
(declarations, names, initialCtx)
}

Expand All @@ -206,6 +201,7 @@ trait ProvideElaboraterBlock extends ElaboraterBlock {
ck.reporter.apply(problem)
}
}

def processDefLetDefStmt(
expr: LetDefStmt,
ctx: Context,
Expand Down

0 comments on commit bc29d5c

Please sign in to comment.