Skip to content

Commit

Permalink
more code
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Jan 18, 2025
1 parent f236d05 commit 4535fd1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tests/tyck/wip.chester
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
record A(a: Integer);
let aT = A;
//def getA(x: A): Integer = x.a;
def getA(x: A): Integer = x.a;
30 changes: 5 additions & 25 deletions tyck/src/main/scala/chester/tyck/Elaborater.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,33 +178,13 @@ trait ProvideElaborater extends ProvideCtx with Elaborater with ElaboraterFuncti
ck.reporter.apply(problem)
ErrorTerm(problem, convertMeta(expr.meta))
} else {
// Elaborate the record expression
val recordTy = newType
val recordTerm = elab(recordExpr, recordTy, effects)

// Get the field name
fieldExpr match {
case Identifier(fieldName, _) =>
// Read the record type
readMetaVar(toTerm(recordTy)) match {
case RecordCallTerm(recordDef, _, _) =>
// Find the field in the record definition
recordDef.fields.find(_.name == fieldName) match {
case Some(fieldTerm) =>
// Unify the field type with the expected type
unify(ty, fieldTerm.ty, expr)
// Create field access term
FieldAccessTerm(recordTerm, fieldName, fieldTerm.ty, convertMeta(meta))
case None =>
val problem = FieldNotFound(fieldName, recordDef.name, expr)
ck.reporter.apply(problem)
ErrorTerm(problem, convertMeta(expr.meta))
}
case other =>
val problem = NotARecordType(other, expr)
ck.reporter.apply(problem)
ErrorTerm(problem, convertMeta(expr.meta))
}
val recordTy = newType
val recordTerm = elab(recordExpr, recordTy, effects)
val resultTerm = FieldAccessTerm(recordTerm, fieldName, toTerm(ty), convertMeta(meta))
state.addPropagator(RecordFieldPropagator(recordTy, fieldName, ty, expr))
resultTerm
case _ =>
val problem = InvalidFieldName(fieldExpr)
ck.reporter.apply(problem)
Expand Down
41 changes: 41 additions & 0 deletions tyck/src/main/scala/chester/tyck/ElaboraterCommon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,47 @@ trait ElaboraterCommon extends ProvideCtx with ElaboraterBase with CommonPropaga
toTerm(argTerm)
}

case class RecordFieldPropagator(
recordTy: CellId[Term],
fieldName: Name,
expectedTy: CellId[Term],
cause: Expr
)(using localCtx: Context) extends Propagator[Tyck] {
override val readingCells: Set[CIdOf[Cell[?]]] = Set(recordTy)
override val writingCells: Set[CIdOf[Cell[?]]] = Set(expectedTy)
override val zonkingCells: Set[CIdOf[Cell[?]]] = Set(recordTy, expectedTy)

override def run(using state: StateAbility[Tyck], more: Tyck): Boolean = {
state.readStable(recordTy) match {
case Some(Meta(id)) =>
state.addPropagator(RecordFieldPropagator(id, fieldName, expectedTy, cause))
true
case Some(RecordCallTerm(recordDef, _, _)) =>
recordDef.fields.find(_.name == fieldName) match {
case Some(fieldTerm) =>
unify(expectedTy, fieldTerm.ty, cause)
true
case None =>
val problem = FieldNotFound(fieldName, recordDef.name, cause)
more.reporter.apply(problem)
true
}
case Some(other) =>
val problem = NotARecordType(other, cause)
more.reporter.apply(problem)
true
case None => false
}
}

override def naiveZonk(needed: Vector[CellIdAny])(using state: StateAbility[Tyck], more: Tyck): ZonkResult = {
state.readStable(recordTy) match {
case None => ZonkResult.Require(Vector(recordTy))
case _ => ZonkResult.Done
}
}
}

}

trait ElaboraterBase extends CommonPropagator[Tyck] {
Expand Down

0 comments on commit 4535fd1

Please sign in to comment.