From 978bf81a82c61e9f2a9aa88d7e2b5882d1a86c45 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Nov 2026 10:26:30 +0000 Subject: Type check binary operations --- .../ast/typed/TypedBinaryOperationNodeMatcher.java | 65 ++++++++++++++++++++++ .../TypeCheckerBinaryOperationTests.java | 65 ++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java new file mode 100644 index 0000000..4d533fa --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java @@ -0,0 +1,65 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.ast.typed; + +// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher imports + +public final class TypedBinaryOperationNodeMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher isTypedBinaryOperationNode() { + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypedBinaryOperationNodeMatcher( + java.util.List> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withOperator(org.zwobble.precisely.Matcher operator) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("operator", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::operator, (org.zwobble.precisely.Matcher) operator)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withLeft(org.zwobble.precisely.Matcher left) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("left", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::left, (org.zwobble.precisely.Matcher) left)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withRight(org.zwobble.precisely.Matcher right) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("right", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::right, (org.zwobble.precisely.Matcher) right)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withType(org.zwobble.precisely.Matcher type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withSource(org.zwobble.precisely.Matcher source) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::source, (org.zwobble.precisely.Matcher) source)); + return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java new file mode 100644 index 0000000..22584d8 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java @@ -0,0 +1,65 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperator; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode; +import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; +import org.zwobble.hobgoblin.compiler.sources.NullSource; +import org.zwobble.hobgoblin.compiler.typechecker.errors.UnsupportedBinaryOperationError; +import org.zwobble.hobgoblin.compiler.types.NamespaceName; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher.isTypedBinaryOperationNode; +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class TypeCheckerBinaryOperationTests { + @Test + public void whenLeftAndRightOperandsAreNumbersThenEqualityOperationTypeChecks() { + var untyped = UntypedBinaryOperationNode.arbitrary() + .withOperator(UntypedBinaryOperator.EQUALS) + .withLeft(UntypedReferenceNode.arbitrary().withName("a")) + .withRight(UntypedReferenceNode.arbitrary().withName("b")) + .build(); + var globalContext = TypeCheckerContextArb.globalContext(); + var namespaceName = NamespaceName.of("App"); + var namespaceContext = globalContext.enterNamespace(namespaceName); + namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); + namespaceContext.declare("b", NativeTypes.INT_32, NullSource.INSTANCE); + + var typed = TypeChecker.typeCheckExpression(untyped, namespaceContext); + + assertThat(typed, isTypedBinaryOperationNode() + .withOperator(equalTo(TypedBinaryOperator.EQUALS)) + .withLeft(isTypedReferenceNode().withName(equalTo("a"))) + .withRight(isTypedReferenceNode().withName(equalTo("b"))) + .withType(equalTo(NativeTypes.INT_32)) + ); + } + + @Test + public void whenLeftAndRightOperandsAreDifferentTypesThenEqualityOperationFailsToTypeCheck() { + var untyped = UntypedBinaryOperationNode.arbitrary() + .withOperator(UntypedBinaryOperator.EQUALS) + .withLeft(UntypedReferenceNode.arbitrary().withName("a")) + .withRight(UntypedReferenceNode.arbitrary().withName("b")) + .build(); + var globalContext = TypeCheckerContextArb.globalContext(); + var namespaceName = NamespaceName.of("App"); + var namespaceContext = globalContext.enterNamespace(namespaceName); + namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); + namespaceContext.declare("b", NativeTypes.STRING, NullSource.INSTANCE); + + var error = assertThrows( + UnsupportedBinaryOperationError.class, + () -> TypeChecker.typeCheckExpression(untyped, namespaceContext) + ); + + assertThat(error.operator(), equalTo(UntypedBinaryOperator.EQUALS)); + assertThat(error.left(), equalTo(NativeTypes.INT_32)); + assertThat(error.right(), equalTo(NativeTypes.STRING)); + } +} -- cgit v1.2.3