|
|
|
@@ -1,5 +1,6 @@
|
|
|
|
|
package wacc
|
|
|
|
|
|
|
|
|
|
import cats.syntax.all._
|
|
|
|
|
import scala.collection.mutable
|
|
|
|
|
|
|
|
|
|
object typeChecker {
|
|
|
|
@@ -27,18 +28,47 @@ object typeChecker {
|
|
|
|
|
case Never(msg: String)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension (ty: SemType)
|
|
|
|
|
infix def satisfies(constraint: Constraint): SemType = (ty, constraint) match {
|
|
|
|
|
case (KnownType.Array(KnownType.Char), Constraint.Is(KnownType.String, _)) =>
|
|
|
|
|
KnownType.String
|
|
|
|
|
case (
|
|
|
|
|
KnownType.String,
|
|
|
|
|
Constraint.IsSymmetricCompatible(KnownType.Array(KnownType.Char), _)
|
|
|
|
|
) =>
|
|
|
|
|
KnownType.String
|
|
|
|
|
case (ty, Constraint.IsSymmetricCompatible(ty2, msg)) => ty satisfies Constraint.Is(ty2, msg)
|
|
|
|
|
case (ty, Constraint.Is(ty2, msg)) => ty satisfies Constraint.IsUnweakanable(ty2, msg)
|
|
|
|
|
}
|
|
|
|
|
extension (ty: SemType) {
|
|
|
|
|
def satisfies(constraint: Constraint, pos: Position)(using ctx: TypeCheckerCtx): SemType =
|
|
|
|
|
(ty, constraint) match {
|
|
|
|
|
case (KnownType.Array(KnownType.Char), Constraint.Is(KnownType.String, _)) =>
|
|
|
|
|
KnownType.String
|
|
|
|
|
case (
|
|
|
|
|
KnownType.String,
|
|
|
|
|
Constraint.IsSymmetricCompatible(KnownType.Array(KnownType.Char), _)
|
|
|
|
|
) =>
|
|
|
|
|
KnownType.String
|
|
|
|
|
case (ty, Constraint.IsSymmetricCompatible(ty2, msg)) =>
|
|
|
|
|
ty.satisfies(Constraint.Is(ty2, msg), pos)
|
|
|
|
|
case (ty, Constraint.Is(ty2, msg)) => ty.satisfies(Constraint.IsUnweakanable(ty2, msg), pos)
|
|
|
|
|
case (ty, Constraint.Unconstrained) => ty
|
|
|
|
|
case (KnownType.Func(_, _), Constraint.IsVar(msg)) =>
|
|
|
|
|
ctx.error(Error.SemanticError(pos, msg))
|
|
|
|
|
case (ty, Constraint.IsVar(msg)) => ty
|
|
|
|
|
case (ty, Constraint.Never(msg)) =>
|
|
|
|
|
ctx.error(Error.SemanticError(pos, msg))
|
|
|
|
|
case (ty, Constraint.IsEither(ty1, ty2, msg)) =>
|
|
|
|
|
(ty moreSpecific ty1).orElse(ty moreSpecific ty2).getOrElse {
|
|
|
|
|
ctx.error(Error.TypeMismatch(pos, ty1, ty, msg))
|
|
|
|
|
}
|
|
|
|
|
case (ty, Constraint.IsUnweakanable(ty2, msg)) =>
|
|
|
|
|
(ty moreSpecific ty2).getOrElse {
|
|
|
|
|
ctx.error(Error.TypeMismatch(pos, ty2, ty, msg))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
infix def moreSpecific(ty2: SemType): Option[SemType] =
|
|
|
|
|
(ty, ty2) match {
|
|
|
|
|
case (ty, ?) => Some(ty)
|
|
|
|
|
case (?, ty) => Some(ty)
|
|
|
|
|
case (ty1, ty2) if ty1 == ty2 => Some(ty1)
|
|
|
|
|
case (KnownType.Array(inn1), KnownType.Array(inn2)) =>
|
|
|
|
|
(inn1 moreSpecific inn2).map(KnownType.Array(_))
|
|
|
|
|
case (KnownType.Pair(fst1, snd1), KnownType.Pair(fst2, snd2)) =>
|
|
|
|
|
(fst1 moreSpecific fst2, snd1 moreSpecific snd2).mapN(KnownType.Pair(_, _))
|
|
|
|
|
case _ => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def check(prog: Program)(using
|
|
|
|
|
ctx: TypeCheckerCtx
|
|
|
|
@@ -103,13 +133,13 @@ object typeChecker {
|
|
|
|
|
private def checkValue(value: LValue | RValue | Expr, constraint: Constraint)(using
|
|
|
|
|
ctx: TypeCheckerCtx
|
|
|
|
|
): SemType = value match {
|
|
|
|
|
case IntLiter(_) => KnownType.Int satisfies constraint
|
|
|
|
|
case BoolLiter(_) => KnownType.Bool satisfies constraint
|
|
|
|
|
case CharLiter(_) => KnownType.Char satisfies constraint
|
|
|
|
|
case StrLiter(_) => KnownType.String satisfies constraint
|
|
|
|
|
case PairLiter() => KnownType.Pair(?, ?) satisfies constraint
|
|
|
|
|
case l @ IntLiter(_) => KnownType.Int.satisfies(constraint, l.pos)
|
|
|
|
|
case l @ BoolLiter(_) => KnownType.Bool.satisfies(constraint, l.pos)
|
|
|
|
|
case l @ CharLiter(_) => KnownType.Char.satisfies(constraint, l.pos)
|
|
|
|
|
case l @ StrLiter(_) => KnownType.String.satisfies(constraint, l.pos)
|
|
|
|
|
case l @ PairLiter() => KnownType.Pair(?, ?).satisfies(constraint, l.pos)
|
|
|
|
|
case id: Ident =>
|
|
|
|
|
ctx.typeOf(id) satisfies constraint
|
|
|
|
|
ctx.typeOf(id).satisfies(constraint, id.pos)
|
|
|
|
|
case ArrayElem(id, indices) =>
|
|
|
|
|
val arrayTy = ctx.typeOf(id)
|
|
|
|
|
val elemTy = indices.toList.foldRight(arrayTy) { (elem, acc) =>
|
|
|
|
@@ -122,20 +152,24 @@ object typeChecker {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elemTy satisfies constraint
|
|
|
|
|
elemTy.satisfies(constraint, id.pos)
|
|
|
|
|
case Parens(expr) => checkValue(expr, constraint)
|
|
|
|
|
case ArrayLiter(elems) =>
|
|
|
|
|
KnownType.Array(elems.foldRight[SemType](?) { case (elem, acc) =>
|
|
|
|
|
checkValue(
|
|
|
|
|
elem,
|
|
|
|
|
Constraint.IsSymmetricCompatible(acc, "array elements must have the same type")
|
|
|
|
|
case l @ ArrayLiter(elems) =>
|
|
|
|
|
KnownType
|
|
|
|
|
.Array(elems.foldRight[SemType](?) { case (elem, acc) =>
|
|
|
|
|
checkValue(
|
|
|
|
|
elem,
|
|
|
|
|
Constraint.IsSymmetricCompatible(acc, "array elements must have the same type")
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.satisfies(constraint, l.pos)
|
|
|
|
|
case l @ NewPair(fst, snd) =>
|
|
|
|
|
KnownType
|
|
|
|
|
.Pair(
|
|
|
|
|
checkValue(fst, Constraint.Unconstrained),
|
|
|
|
|
checkValue(snd, Constraint.Unconstrained)
|
|
|
|
|
)
|
|
|
|
|
}) satisfies constraint
|
|
|
|
|
case NewPair(fst, snd) =>
|
|
|
|
|
KnownType.Pair(
|
|
|
|
|
checkValue(fst, Constraint.Unconstrained),
|
|
|
|
|
checkValue(snd, Constraint.Unconstrained)
|
|
|
|
|
) satisfies constraint
|
|
|
|
|
.satisfies(constraint, l.pos)
|
|
|
|
|
case Call(id, args) =>
|
|
|
|
|
val funcTy = ctx.typeOf(id)
|
|
|
|
|
funcTy match {
|
|
|
|
@@ -143,7 +177,7 @@ object typeChecker {
|
|
|
|
|
args.zip(paramTys).foreach { case (arg, paramTy) =>
|
|
|
|
|
checkValue(arg, Constraint.Is(paramTy, s"argument type mismatch in function ${id.v}"))
|
|
|
|
|
}
|
|
|
|
|
retTy satisfies constraint
|
|
|
|
|
retTy.satisfies(constraint, id.pos)
|
|
|
|
|
// Should never happen, the scope-checker should have caught this already
|
|
|
|
|
// ctx error had it not
|
|
|
|
|
case _ => ctx.error(Error.InternalError(id.pos, "function call to non-function"))
|
|
|
|
@@ -153,8 +187,8 @@ object typeChecker {
|
|
|
|
|
elem,
|
|
|
|
|
Constraint.Is(KnownType.Pair(?, ?), "fst must be applied to a pair")
|
|
|
|
|
) match {
|
|
|
|
|
case KnownType.Pair(left, _) => left satisfies constraint
|
|
|
|
|
case ? => ? satisfies constraint
|
|
|
|
|
case KnownType.Pair(left, _) => left.satisfies(constraint, elem.pos)
|
|
|
|
|
case ? => ?.satisfies(constraint, elem.pos)
|
|
|
|
|
case _ => ctx.error(Error.InternalError(elem.pos, "fst must be applied to a pair"))
|
|
|
|
|
} // satisfies constraint
|
|
|
|
|
case Snd(elem) =>
|
|
|
|
@@ -162,38 +196,38 @@ object typeChecker {
|
|
|
|
|
elem,
|
|
|
|
|
Constraint.Is(KnownType.Pair(?, ?), "snd must be applied to a pair")
|
|
|
|
|
) match {
|
|
|
|
|
case KnownType.Pair(_, right) => right satisfies constraint
|
|
|
|
|
case ? => ? satisfies constraint
|
|
|
|
|
case KnownType.Pair(_, right) => right.satisfies(constraint, elem.pos)
|
|
|
|
|
case ? => ?.satisfies(constraint, elem.pos)
|
|
|
|
|
case _ => ctx.error(Error.InternalError(elem.pos, "snd must be applied to a pair"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unary operators
|
|
|
|
|
case Negate(x) =>
|
|
|
|
|
checkValue(x, Constraint.Is(KnownType.Int, "negation must be applied to an int"))
|
|
|
|
|
KnownType.Int satisfies constraint
|
|
|
|
|
KnownType.Int.satisfies(constraint, x.pos)
|
|
|
|
|
case Not(x) =>
|
|
|
|
|
checkValue(x, Constraint.Is(KnownType.Bool, "logical not must be applied to a bool"))
|
|
|
|
|
KnownType.Bool satisfies constraint
|
|
|
|
|
KnownType.Bool.satisfies(constraint, x.pos)
|
|
|
|
|
case Len(x) =>
|
|
|
|
|
checkValue(x, Constraint.Is(KnownType.Array(?), "len must be applied to an array"))
|
|
|
|
|
KnownType.Int satisfies constraint
|
|
|
|
|
KnownType.Int.satisfies(constraint, x.pos)
|
|
|
|
|
case Ord(x) =>
|
|
|
|
|
checkValue(x, Constraint.Is(KnownType.Char, "ord must be applied to a char"))
|
|
|
|
|
KnownType.Int satisfies constraint
|
|
|
|
|
KnownType.Int.satisfies(constraint, x.pos)
|
|
|
|
|
case Chr(x) =>
|
|
|
|
|
checkValue(x, Constraint.Is(KnownType.Int, "chr must be applied to an int"))
|
|
|
|
|
KnownType.Char satisfies constraint
|
|
|
|
|
KnownType.Char.satisfies(constraint, x.pos)
|
|
|
|
|
|
|
|
|
|
// Binary operators
|
|
|
|
|
case op: (Add | Sub | Mul | Div | Mod) =>
|
|
|
|
|
val operand = Constraint.Is(KnownType.Int, "binary operator must be applied to an int")
|
|
|
|
|
checkValue(op.x, operand)
|
|
|
|
|
checkValue(op.y, operand)
|
|
|
|
|
KnownType.Int satisfies constraint
|
|
|
|
|
KnownType.Int.satisfies(constraint, op.pos)
|
|
|
|
|
case op: (Eq | Neq) =>
|
|
|
|
|
val xTy = checkValue(op.x, Constraint.Unconstrained)
|
|
|
|
|
checkValue(op.y, Constraint.Is(xTy, "equality must be applied to values of the same type"))
|
|
|
|
|
KnownType.Bool satisfies constraint
|
|
|
|
|
KnownType.Bool.satisfies(constraint, op.pos)
|
|
|
|
|
case op: (Less | LessEq | Greater | GreaterEq) =>
|
|
|
|
|
val xTy = checkValue(
|
|
|
|
|
op.x,
|
|
|
|
@@ -204,11 +238,11 @@ object typeChecker {
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
checkValue(op.y, Constraint.Is(xTy, "comparison must be applied to values of the same type"))
|
|
|
|
|
KnownType.Bool satisfies constraint
|
|
|
|
|
KnownType.Bool.satisfies(constraint, op.pos)
|
|
|
|
|
case op: (And | Or) =>
|
|
|
|
|
val operand = Constraint.Is(KnownType.Bool, "logical operator must be applied to a bool")
|
|
|
|
|
checkValue(op.x, operand)
|
|
|
|
|
checkValue(op.y, operand)
|
|
|
|
|
KnownType.Bool satisfies constraint
|
|
|
|
|
KnownType.Bool.satisfies(constraint, op.pos)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|