samlang.checker.ModuleTypeDefResolver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of samlang Show documentation
Show all versions of samlang Show documentation
Sam's Programming Language
package samlang.checker
import samlang.ast.checked.CheckedTypeExpr
import samlang.ast.checked.CheckedTypeExpr.*
import samlang.ast.checked.CheckedTypeExprVisitor
import samlang.errors.IllegalOtherClassMatch
import samlang.errors.TypeParamSizeMismatchError
import samlang.parser.Position
internal object ModuleTypeDefResolver {
fun applyGenericTypeParams(type: CheckedTypeExpr, context: Map): CheckedTypeExpr =
type.accept(visitor = Visitor, context = context)
fun getTypeDef(
identifierType: IdentifierType,
ctx: TypeCheckingContext,
errorPosition: Position,
isFromObject: Boolean
): Map {
val (id, typeArgs) = identifierType
if (id != ctx.currentModule) {
throw IllegalOtherClassMatch(position = errorPosition)
}
val (typeParams, varMap) = if (isFromObject) {
val (p, m) = ctx.getCurrentModuleObjectTypeDef(errorPosition = errorPosition)
p to m
} else {
val (p, m) = ctx.getCurrentModuleVariantTypeDef(errorPosition = errorPosition)
p to m
}
return if (typeArgs == null) {
if (typeParams != null) {
error(message = "BAD! TypeArgs: null, typeParams: $typeParams, identifierType: $identifierType")
}
varMap
} else {
if (typeParams == null) {
error(message = "BAD! TypeArgs: $typeArgs, typeParams: null, identifierType: $identifierType")
}
TypeParamSizeMismatchError.check(
expectedSize = typeParams.size,
actualSize = typeArgs.size,
position = errorPosition
)
varMap.mapValues { (_, v) -> applyGenericTypeParams(type = v, context = typeParams.zip(typeArgs).toMap()) }
}
}
private object Visitor : CheckedTypeExprVisitor