From 765d7579b96f558ffc6232b707d471850664a11a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 5 Feb 2025 18:48:01 +0000 Subject: Add native types --- .../shedlang/compiler/types/ConstructedNativeType.java | 13 +++++++++++++ .../org/shedlang/compiler/types/ConstructedType.java | 2 +- .../org/shedlang/compiler/types/ConstructibleType.java | 2 +- .../java/org/shedlang/compiler/types/NativeType.java | 5 +++++ src/main/java/org/shedlang/compiler/types/Scope.java | 1 + .../org/shedlang/compiler/types/SimpleNativeType.java | 13 +++++++++++++ src/main/java/org/shedlang/compiler/types/Type.java | 2 +- .../java/org/shedlang/compiler/types/TypeParam.java | 4 ++++ .../org/shedlang/compiler/types/TypeSubstitution.java | 17 +++++++++++++++++ src/main/java/org/shedlang/compiler/types/Types.java | 13 +++++++++++++ 10 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java create mode 100644 src/main/java/org/shedlang/compiler/types/NativeType.java create mode 100644 src/main/java/org/shedlang/compiler/types/SimpleNativeType.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java b/src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java new file mode 100644 index 00000000..24653b3d --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/ConstructedNativeType.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.types; + +import java.util.List; + +public record ConstructedNativeType( + TypeConstructor constructor, + List args +) implements ConstructedType, NativeType { + @Override + public SimpleNativeType rawType() { + return genericType(); + } +} diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedType.java b/src/main/java/org/shedlang/compiler/types/ConstructedType.java index e49b3520..8a51d6b0 100644 --- a/src/main/java/org/shedlang/compiler/types/ConstructedType.java +++ b/src/main/java/org/shedlang/compiler/types/ConstructedType.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.types; import java.util.List; import java.util.stream.Collectors; -public sealed interface ConstructedType extends Type permits ConstructedStructType, ConstructedSumType { +public sealed interface ConstructedType extends Type permits ConstructedNativeType, ConstructedStructType, ConstructedSumType { TypeConstructor constructor(); List args(); diff --git a/src/main/java/org/shedlang/compiler/types/ConstructibleType.java b/src/main/java/org/shedlang/compiler/types/ConstructibleType.java index 164b2b8d..c9283467 100644 --- a/src/main/java/org/shedlang/compiler/types/ConstructibleType.java +++ b/src/main/java/org/shedlang/compiler/types/ConstructibleType.java @@ -1,5 +1,5 @@ package org.shedlang.compiler.types; -public sealed interface ConstructibleType extends Type permits SimpleStructType, SimpleSumType { +public sealed interface ConstructibleType extends Type permits SimpleNativeType, SimpleStructType, SimpleSumType { String name(); } diff --git a/src/main/java/org/shedlang/compiler/types/NativeType.java b/src/main/java/org/shedlang/compiler/types/NativeType.java new file mode 100644 index 00000000..815b8274 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/NativeType.java @@ -0,0 +1,5 @@ +package org.shedlang.compiler.types; + +public sealed interface NativeType extends Type permits ConstructedNativeType, SimpleNativeType { + SimpleNativeType rawType(); +} diff --git a/src/main/java/org/shedlang/compiler/types/Scope.java b/src/main/java/org/shedlang/compiler/types/Scope.java index b7fb0ecd..b0141916 100644 --- a/src/main/java/org/shedlang/compiler/types/Scope.java +++ b/src/main/java/org/shedlang/compiler/types/Scope.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.types; public sealed interface Scope { record Function(NamespaceName namespaceName, String name) implements Scope {} + record Native(NativeType type) implements Scope {} record Struct(SimpleStructType type) implements Scope {} record Sum(SimpleSumType type) implements Scope {} } diff --git a/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java b/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java new file mode 100644 index 00000000..a2acde62 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.types; + +public record SimpleNativeType(NamespaceName namespaceName, String name) implements ConstructibleType, NativeType { + @Override + public String describe() { + return namespaceName.toString() + "." + name; + } + + @Override + public SimpleNativeType rawType() { + return this; + } +} diff --git a/src/main/java/org/shedlang/compiler/types/Type.java b/src/main/java/org/shedlang/compiler/types/Type.java index d2607906..a05304c8 100644 --- a/src/main/java/org/shedlang/compiler/types/Type.java +++ b/src/main/java/org/shedlang/compiler/types/Type.java @@ -6,5 +6,5 @@ package org.shedlang.compiler.types; * found through auxiliary data. */ public sealed interface Type extends TypeLevelValue - permits BoolType, BottomType, ConstructedType, ConstructibleType, FunctionType, IntType, NamespaceType, StringType, StructType, SumType, TopType, TupleType, TypeArg, TypeLevelValueType, TypeParam, UnitType { + permits BoolType, BottomType, ConstructedType, ConstructibleType, FunctionType, IntType, NamespaceType, NativeType, StringType, StructType, SumType, TopType, TupleType, TypeArg, TypeLevelValueType, TypeParam, UnitType { } diff --git a/src/main/java/org/shedlang/compiler/types/TypeParam.java b/src/main/java/org/shedlang/compiler/types/TypeParam.java index 45262cc4..6d311940 100644 --- a/src/main/java/org/shedlang/compiler/types/TypeParam.java +++ b/src/main/java/org/shedlang/compiler/types/TypeParam.java @@ -9,6 +9,10 @@ public record TypeParam(Scope scope, String name) implements Type, TypeLevelPara return new TypeParam(new Scope.Function(namespaceName, functionName), name); } + public static TypeParam forNativeType(SimpleNativeType nativeType, String name) { + return new TypeParam(new Scope.Native(nativeType), name); + } + public static TypeParam forStructType(SimpleStructType structType, String name) { return new TypeParam(new Scope.Struct(structType), name); } diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java index 1aca81c7..cefe3d0f 100644 --- a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java +++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java @@ -39,6 +39,9 @@ public class TypeSubstitution { case BottomType bottomType -> type; + case ConstructedNativeType constructedNativeType -> + substituteConstructedNativeType(constructedNativeType, map); + case ConstructedStructType constructedType -> substituteConstructedStructType(constructedType, map); @@ -54,6 +57,9 @@ public class TypeSubstitution { case NamespaceType namespaceType -> type; + case NativeType nativeType -> + type; + case StringType stringType -> type; @@ -89,6 +95,17 @@ public class TypeSubstitution { }; } + private static ConstructedNativeType substituteConstructedNativeType( + ConstructedNativeType constructedType, + TypeSubstitutionMap map + ) { + return substituteConstructedType( + ConstructedNativeType::new, + constructedType, + map + ); + } + private static ConstructedStructType substituteConstructedStructType( ConstructedStructType constructedType, TypeSubstitutionMap map diff --git a/src/main/java/org/shedlang/compiler/types/Types.java b/src/main/java/org/shedlang/compiler/types/Types.java index a8c3aef5..ea4e7053 100644 --- a/src/main/java/org/shedlang/compiler/types/Types.java +++ b/src/main/java/org/shedlang/compiler/types/Types.java @@ -21,6 +21,10 @@ public class Types { return new NamespaceType(namespaceName); } + public static SimpleNativeType nativeType(NamespaceName namespaceName, String name) { + return new SimpleNativeType(namespaceName, name); + } + public static SimpleStructType struct(NamespaceName namespaceName, String name) { return new SimpleStructType(namespaceName, name); } @@ -62,6 +66,8 @@ public class Types { List args ) { return switch (constructor.genericType()) { + case SimpleNativeType nativeType -> + constructedNativeType((TypeConstructor) constructor, args); case SimpleStructType structType -> constructedStructType((TypeConstructor) constructor, args); case SimpleSumType sumType -> @@ -69,6 +75,13 @@ public class Types { }; } + public static ConstructedNativeType constructedNativeType( + TypeConstructor constructor, + List args + ) { + return new ConstructedNativeType(constructor, args); + } + public static ConstructedStructType constructedStructType( TypeConstructor constructor, List args -- cgit v1.2.3