refactor: make microWacc.ArrayElem recursive rather than flat

This commit is contained in:
Gleb Koval 2025-02-26 18:08:30 +00:00
parent 62df2c2244
commit 85190ce174
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
2 changed files with 10 additions and 9 deletions

View File

@ -1,7 +1,5 @@
package wacc
import cats.data.NonEmptyList
object microWacc {
import wacc.types._
@ -19,9 +17,7 @@ object microWacc {
extends Expr(identTy)
with CallTarget(identTy)
with LValue
case class ArrayElem(value: LValue, indices: NonEmptyList[Expr])(ty: SemType)
extends Expr(ty)
with LValue
case class ArrayElem(value: LValue, index: Expr)(ty: SemType) extends Expr(ty) with LValue
// Operators
case class UnaryOp(x: Expr, op: UnaryOperator)(ty: SemType) extends Expr(ty)

View File

@ -422,10 +422,15 @@ object typeChecker {
}
(next, idxTyped)
}
microWacc.ArrayElem(
val firstArrayElem = microWacc.ArrayElem(
microWacc.Ident(id.v, id.uid)(arrayTy),
indicesTyped
indicesTyped.head
)(elemTy.satisfies(constraint, value.pos))
val arrayElem = indicesTyped.tail.foldLeft(firstArrayElem) { (acc, idx) =>
microWacc.ArrayElem(acc, idx)(KnownType.Array(acc.ty))
}
// Need to type-check the final arrayElem with the constraint
microWacc.ArrayElem(arrayElem.value, arrayElem.index)(elemTy.satisfies(constraint, value.pos))
case ast.Fst(elem) =>
val elemTyped = checkLValue(
elem,
@ -433,7 +438,7 @@ object typeChecker {
)
microWacc.ArrayElem(
elemTyped,
NonEmptyList.of(microWacc.IntLiter(0))
microWacc.IntLiter(0)
)(elemTyped.ty match {
case KnownType.Pair(left, _) =>
left.satisfies(constraint, elem.pos)
@ -446,7 +451,7 @@ object typeChecker {
)
microWacc.ArrayElem(
elemTyped,
NonEmptyList.of(microWacc.IntLiter(1))
microWacc.IntLiter(1)
)(elemTyped.ty match {
case KnownType.Pair(_, right) =>
right.satisfies(constraint, elem.pos)