Skip to content

Commit

Permalink
getTypeMinusType refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Costea committed Feb 5, 2024
1 parent 49dad8a commit 2a9f6c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import wiles.shared.TokenLocation
import wiles.shared.constants.Predicates.IS_IDENTIFIER
import wiles.shared.constants.Tokens.ELSE_ID
import wiles.shared.constants.TypeUtils
import wiles.shared.constants.TypeUtils.getTypeMinusType
import wiles.shared.constants.TypeUtils.isFormerSuperTypeOfLatter
import wiles.shared.constants.Types.EITHER_ID

Expand All @@ -25,7 +26,7 @@ class InferFromWhen(details: InferrerDetails) : InferFromStatement(details) {
if (former.name == EITHER_ID && former.components.size == 0)
throw TypesExhaustedException(newLocation)
val result = former.copyRemovingLocation()
isFormerSuperTypeOfLatter(latter, result, getMinus = true)
getTypeMinusType(latter, result)
return TypeUtils.removeEmptyEither(result)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import wiles.shared.constants.Tokens.MUTABLE_ID
import wiles.shared.constants.Tokens.NOTHING_ID
import wiles.shared.constants.Tokens.TYPEDEF_ID
import wiles.shared.constants.TypeConstants.NOTHING_TYPE
import wiles.shared.constants.TypeUtils
import wiles.shared.constants.TypeUtils.isFormerSuperTypeOfLatter
import wiles.shared.constants.TypeUtils.makeEither
import wiles.shared.constants.TypeUtils.makeMutable
Expand Down Expand Up @@ -402,7 +403,7 @@ object InferrerUtils {
if(shouldGetType)
{
val typeToRemove = map[variable.key]!!.type.copy()
isFormerSuperTypeOfLatter(typeToRemove, type, getMinus = true)
TypeUtils.getTypeMinusType(typeToRemove, type)
type = removeEmptyEither(type)
}
continue
Expand Down
19 changes: 18 additions & 1 deletion Wiles Base/src/main/java/wiles/shared/constants/TypeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ object TypeUtils {
supertype : JSONStatement, subtype : JSONStatement,
unboxGenerics : Boolean = true, //should generics match?
genericTypes : GenericTypesMap? = null,
): Boolean
{
return isFormerSuperTypeOfLatter(supertype,subtype,unboxGenerics,genericTypes,getMinus = false)
}

fun getTypeMinusType(supertype : JSONStatement, subtype : JSONStatement,
unboxGenerics : Boolean = true, //should generics match?
genericTypes : GenericTypesMap? = null) : Boolean
{
return isFormerSuperTypeOfLatter(supertype,subtype,unboxGenerics,genericTypes,getMinus = true)
}

private fun isFormerSuperTypeOfLatter(
supertype : JSONStatement, subtype : JSONStatement,
unboxGenerics : Boolean = true, //should generics match?
genericTypes : GenericTypesMap? = null,
getMinus : Boolean = false,
): Boolean {
assert(supertype.syntaxType == SyntaxType.TYPE)
Expand Down Expand Up @@ -79,7 +95,8 @@ object TypeUtils {
return if(isFormerSuperTypeOfLatter(genericTypes[genName]!!.statement, subtype, unboxGenerics = false,
genericTypes = genericTypes) && !isDeclaration) {
genericTypes[genName] = GenericTypeValue(genericTypes[genName]!!.statement,
true, false)
occurredMultipleTimes = true, declarationReached = false
)
true
}
else if(genericTypes[genName]?.declarationReached != true && isFormerSuperTypeOfLatter(subtype,
Expand Down

0 comments on commit 2a9f6c2

Please sign in to comment.