diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-25 11:39:53 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-25 12:07:37 +0000 |
| commit | d4c714a248602ec48b4ad4cb531d2e335f7e51d0 (patch) | |
| tree | 21e8c847c4eb7fa12b76be319dac20d583165da2 /src/main/java | |
| parent | eb90e991d9c28cdced85d3fb0bfe2e24ca8b091a (diff) | |
Add basic type substitution
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); + } +} |
