From a1806b32dc21709d81e2378923910b100145cf90 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Sep 2023 17:41:04 +0100 Subject: Construct typed node for sum type definitions --- .../ast/typed/TypedSumTypeDefinitionNode.java | 3 ++- .../ast/untyped/UntypedSumTypeDefinitionNode.java | 15 ++++++++++--- .../shedlang/compiler/typechecker/TypeChecker.java | 7 +++++- .../compiler/ast/typed/TypedNodeMatchers.java | 10 +++++++++ .../TypeCheckerSumTypeDefinitionTests.java | 26 ++++++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java index 851e64e8..cb6b6418 100644 --- a/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedSumTypeDefinitionNode.java @@ -1,9 +1,10 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.SumType; public record TypedSumTypeDefinitionNode( - String name, + SumType type, Source source ) implements TypedNamespaceStatementNode { } diff --git a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java index 84f46711..e6900457 100644 --- a/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNode.java @@ -1,7 +1,9 @@ package org.shedlang.compiler.ast.untyped; +import org.pcollections.PVector; import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.util.P; import java.util.List; @@ -11,10 +13,13 @@ public record UntypedSumTypeDefinitionNode( Source source ) implements UntypedNamespaceStatementNode { public static Builder builder() { - return new Builder("DefaultSumTypeName"); + return new Builder("DefaultSumTypeName", P.vector()); } - public record Builder(String name) { + public record Builder( + String name, + PVector variants + ) { public UntypedSumTypeDefinitionNode build() { return new UntypedSumTypeDefinitionNode( name, @@ -24,7 +29,11 @@ public record UntypedSumTypeDefinitionNode( } public Builder withName(String name) { - return new Builder(name); + return new Builder(name, this.variants); + } + + public Builder addVariant(UntypedTypeLevelExpressionNode variant) { + return new Builder(this.name, this.variants.plus(variant)); } } } diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index f754d0d0..e184e26b 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -926,7 +926,12 @@ public class TypeChecker { @Override public TypeCheckerResult finish(TypeCheckerContext context) { - return context.toResult(null); + var type = context.getLexicalState(SumType.class, untyped.name()); + + return context.toResult(new TypedSumTypeDefinitionNode( + type, + untyped.source() + )); } }; } diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java index 7bd1402d..1f64f631 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java @@ -1,5 +1,6 @@ package org.shedlang.compiler.ast.typed; +import org.shedlang.compiler.types.SumType; import org.shedlang.compiler.types.TupleType; import org.shedlang.compiler.types.Type; import org.shedlang.compiler.types.TypeLevelValue; @@ -251,6 +252,15 @@ public class TypedNodeMatchers { return TypedStructFieldDefinitionNodeMatcher.empty(); } + public static Matcher isTypedSumTypeDefinitionNode( + SumType type + ) { + return instanceOf( + TypedSumTypeDefinitionNode.class, + has("type", x -> x.type(), equalTo(type)) + ); + } + public static Matcher isTypedTargetIgnore() { return instanceOf(TypedTargetNode.Ignore.class); } diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerSumTypeDefinitionTests.java index 167a2477..0a551c95 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerSumTypeDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerSumTypeDefinitionTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.typechecker; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.TypedNamespaceStatementNode; +import org.shedlang.compiler.ast.untyped.Untyped; import org.shedlang.compiler.ast.untyped.UntypedNamespaceStatementNode; import org.shedlang.compiler.ast.untyped.UntypedSumTypeDefinitionNode; import org.shedlang.compiler.typechecker.errors.CannotDefineSumTypeOutsideOfNamespace; @@ -9,7 +10,10 @@ import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; +import java.util.Map; + import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.isTypedSumTypeDefinitionNode; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; import static org.zwobble.precisely.Matchers.isOptionalOf; @@ -48,6 +52,28 @@ public class TypeCheckerSumTypeDefinitionTests { ); } + @Test + public void canTypeCheckSumTypeWithVariantsInSameNamespace() { + var untyped = UntypedSumTypeDefinitionNode.builder() + .withName("ExampleSumType") + .addVariant(Untyped.typeLevelReference("A")) + .addVariant(Untyped.typeLevelReference("A")) + .build(); + var variantType1 = Types.struct(NAMESPACE_NAME, "A"); + var variantType2 = Types.struct(NAMESPACE_NAME, "B"); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map( + Map.entry("A", Types.metaType(variantType1)), + Map.entry("B", Types.metaType(variantType2)) + )); + + var typed = typeCheckNamespaceStatement(untyped, context); + + assertThat(typed.value(), isTypedSumTypeDefinitionNode( + Types.sum(NAMESPACE_NAME, "ExampleSumType") + )); + } + private static TypeCheckerResult typeCheckNamespaceStatement( UntypedNamespaceStatementNode untyped, TypeCheckerContext context -- cgit v1.2.3