From 04204c8c4fb943b98409678bc47b924629bc6b1b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 23 Dec 2023 22:40:47 +0000 Subject: Add ConstructedType --- .../org/shedlang/compiler/types/ConstructedType.java | 17 +++++++++++++++++ src/main/java/org/shedlang/compiler/types/Type.java | 2 +- .../java/org/shedlang/compiler/types/TypeParam.java | 4 ++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/types/ConstructedType.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedType.java b/src/main/java/org/shedlang/compiler/types/ConstructedType.java new file mode 100644 index 00000000..558b227b --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/ConstructedType.java @@ -0,0 +1,17 @@ +package org.shedlang.compiler.types; + +import java.util.List; +import java.util.stream.Collectors; + +public record ConstructedType( + TypeConstructor constructor, + List args +) implements Type { + @Override + public String describe() { + var argsString = args.stream() + .map(arg -> arg.describe()) + .collect(Collectors.joining(", ")); + return constructor.describe() + "[" + argsString + "]"; + } +} diff --git a/src/main/java/org/shedlang/compiler/types/Type.java b/src/main/java/org/shedlang/compiler/types/Type.java index 8ec577bf..9a062156 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, FunctionType, IntType, NamespaceType, StringType, StructuredType, SumType, TopType, TupleType, TypeLevelValueType, TypeParam, UnitType { + permits BoolType, BottomType, ConstructedType, FunctionType, IntType, NamespaceType, StringType, StructuredType, SumType, TopType, TupleType, 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 240d701d..702d6a5f 100644 --- a/src/main/java/org/shedlang/compiler/types/TypeParam.java +++ b/src/main/java/org/shedlang/compiler/types/TypeParam.java @@ -1,6 +1,10 @@ package org.shedlang.compiler.types; public record TypeParam(Scope scope, String name) implements Type { + public static TypeParam forStructType(StructType structType, String name) { + return new TypeParam(new Scope.Struct(structType), name); + } + @Override public String describe() { return name; -- cgit v1.2.3