feat: improve semantic errors #15

Merged
gk1623 merged 5 commits from improve-semantic-errors into master 2025-02-07 15:07:36 +00:00
4 changed files with 46 additions and 27 deletions
Showing only changes of commit ba1b7d67c7 - Show all commits

View File

@@ -159,17 +159,18 @@ object typeChecker {
ctx.typeOf(id).satisfies(constraint, id.pos) ctx.typeOf(id).satisfies(constraint, id.pos)
case ArrayElem(id, indices) => case ArrayElem(id, indices) =>
val arrayTy = ctx.typeOf(id) val arrayTy = ctx.typeOf(id)
val elemTy = indices.foldLeft(arrayTy) { (acc, elem) => val elemTy = indices.foldLeftM(arrayTy) { (acc, elem) =>
checkValue(elem, Constraint.Is(KnownType.Int, "array index must be an int")) checkValue(elem, Constraint.Is(KnownType.Int, "array index must be an int"))
acc match { acc match {
case KnownType.Array(innerTy) => innerTy case KnownType.Array(innerTy) => Some(innerTy)
case _ => case nonArrayTy =>
ctx.error( ctx.error(
Error.TypeMismatch(elem.pos, KnownType.Array(?), acc, "cannot index into a non-array") Error.TypeMismatch(elem.pos, KnownType.Array(?), acc, "cannot index into a non-array")
) )
None
} }
} }
elemTy.satisfies(constraint, id.pos) elemTy.getOrElse(?).satisfies(constraint, id.pos)
case Parens(expr) => checkValue(expr, constraint) case Parens(expr) => checkValue(expr, constraint)
case l @ ArrayLiter(elems) => case l @ ArrayLiter(elems) =>
KnownType KnownType