From 4e34463f83f7ca08070565351df06eb06f67be94 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Sep 2023 10:13:17 +0100 Subject: Add basic type checking for struct definitions --- .../compiler/ast/typed/TypedNodeMatchers.java | 4 ++ .../typed/TypedStructDefinitionNodeMatcher.java | 43 ++++++++++++++++++++++ .../TypeCheckerStructDefinitionTests.java | 43 ++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java (limited to 'src/test/java') 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 52d91cdf..bc5da178 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java @@ -221,6 +221,10 @@ public class TypedNodeMatchers { ); } + public static TypedStructDefinitionNodeMatcher isTypedStructDefinitionNode() { + return TypedStructDefinitionNodeMatcher.empty(); + } + public static Matcher isTypedTargetIgnore() { return instanceOf(TypedTargetNode.Ignore.class); } diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java new file mode 100644 index 00000000..489fb558 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java @@ -0,0 +1,43 @@ +package org.shedlang.compiler.ast.typed; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.shedlang.compiler.util.P; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class TypedStructDefinitionNodeMatcher extends MatcherBuilder< + TypedNamespaceStatementNode, + TypedStructDefinitionNode, + TypedStructDefinitionNodeMatcher +> { + public static TypedStructDefinitionNodeMatcher empty() { + return new TypedStructDefinitionNodeMatcher(P.vector()); + } + + private TypedStructDefinitionNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return TypedStructDefinitionNode.class; + } + + @Override + protected TypedStructDefinitionNodeMatcher create(PVector> submatchers) { + return new TypedStructDefinitionNodeMatcher(submatchers); + } + + public TypedStructDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public TypedStructDefinitionNodeMatcher withFields( + Matcher> fields + ) { + return with(has("fields", x -> x.fields(), fields)); + } +} diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java new file mode 100644 index 00000000..ac361f79 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -0,0 +1,43 @@ +package org.shedlang.compiler.typechecker; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.TypedNamespaceStatementNode; +import org.shedlang.compiler.ast.untyped.UntypedNamespaceStatementNode; +import org.shedlang.compiler.ast.untyped.UntypedStructDefinitionNode; +import org.shedlang.compiler.util.P; + +import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.isTypedStructDefinitionNode; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class TypeCheckerStructDefinitionTests { + @Test + public void canTypeCheckStructDefinitionWithNoFields() { + var untyped = UntypedStructDefinitionNode.builder() + .withName("ExampleStruct") + .build(); + var context = TypeCheckerContext.EMPTY; + + var typed = typeCheckNamespaceStatement(untyped, context); + + assertThat( + typed.value(), + isTypedStructDefinitionNode() + .withName("ExampleStruct") + .withFields(isSequence()) + ); + } + + private static TypeCheckerResult typeCheckNamespaceStatement( + UntypedNamespaceStatementNode untyped, + TypeCheckerContext context + ) { + // TODO: remove duplication of this method + var declaredVariables = DeclaredVariables.forNamespaceStatement(untyped, P.vector()); + var environment = EnvironmentBuilder.EMPTY.addUndefined(declaredVariables); + + context = context.enterNamespace(environment.build()); + + return TypeChecker.typeCheckNamespaceStatement(untyped).typeCheckAllPhases(context); + } +} -- cgit v1.2.3