From d4c714a248602ec48b4ad4cb531d2e335f7e51d0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 25 Dec 2023 11:39:53 +0000 Subject: Add basic type substitution --- .../shedlang/compiler/types/TypeSubstitution.java | 49 ++++++++++++++++++++++ .../compiler/types/TypeSubstitutionMap.java | 19 +++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/main/java/org/shedlang/compiler/types/TypeSubstitution.java create mode 100644 src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java (limited to 'src/main/java') 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... map) { + return new TypeSubstitutionMap(Map.ofEntries(map)); + } + + private final Map map; + + public TypeSubstitutionMap(Map map) { + this.map = map; + } + + public Type substitute(TypeParam typeParam) { + return this.map.getOrDefault(typeParam, typeParam); + } +} -- cgit v1.2.3