diff options
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/types/TypeSubstitution.java | 49 | ||||
| -rw-r--r-- | src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java | 19 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java new file mode 100644 index 00000000..bd679292 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java @@ -0,0 +1,49 @@ +package org.shedlang.compiler.types; + +public class TypeSubstitution { + private TypeSubstitution() {} + + public static Type substitute(Type type, TypeSubstitutionMap map) { + return switch (type) { + case BoolType boolType -> type; + + case BottomType bottomType -> type; + + case ConstructedType constructedType -> { + throw new UnsupportedOperationException("TODO"); + } + + case FunctionType functionType -> { + throw new UnsupportedOperationException("TODO"); + } + + case IntType intType -> type; + + case NamespaceType namespaceType -> type; + + case StringType stringType -> type; + + case StructuredType structuredType -> { + throw new UnsupportedOperationException("TODO"); + } + + case SumType sumType -> { + throw new UnsupportedOperationException("TODO"); + } + + case TopType topType -> type; + + case TupleType tupleType -> { + throw new UnsupportedOperationException("TODO"); + } + + case TypeLevelValueType typeLevelValueType -> { + throw new UnsupportedOperationException("TODO"); + } + + case TypeParam typeParam -> map.substitute(typeParam); + + case UnitType unitType -> type; + }; + } +} diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java new file mode 100644 index 00000000..2709fc02 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java @@ -0,0 +1,19 @@ +package org.shedlang.compiler.types; + +import java.util.Map; + +public class TypeSubstitutionMap { + public static TypeSubstitutionMap of(Map.Entry<TypeParam, Type>... map) { + return new TypeSubstitutionMap(Map.ofEntries(map)); + } + + private final Map<TypeParam, Type> map; + + public TypeSubstitutionMap(Map<TypeParam, Type> map) { + this.map = map; + } + + public Type substitute(TypeParam typeParam) { + return this.map.getOrDefault(typeParam, typeParam); + } +} |
