From 616f475a4cf4c3296028c1fe89e17e0105898f3c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:36:27 +0100 Subject: Add typed int not equal node --- .../compiler/ast/typed/TypedBinaryOperator.java | 1 + .../shedlang/compiler/stackir/StackCompiler.java | 2 ++ .../org/shedlang/compiler/ast/typed/Typed.java | 12 ++++++++++ .../stackir/StackCompilerIntNotEqualTests.java | 26 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 4 ++++ 5 files changed, 45 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java index eac3cbce..38d628fa 100644 --- a/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java @@ -6,6 +6,7 @@ import org.shedlang.compiler.types.Types; public enum TypedBinaryOperator { INT_ADD(Types.INT), INT_EQUALS(Types.BOOL), + INT_NOT_EQUAL(Types.BOOL), INT_SUB(Types.INT); private final Type type; diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 4b2bdf5b..b6aec8dd 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -20,6 +20,8 @@ public class StackCompiler { new IntAddInstruction(node.source()); case INT_EQUALS -> new IntEqualsInstruction(node.source()); + case INT_NOT_EQUAL -> + new IntNotEqualInstruction(node.source()); case INT_SUB -> new IntSubInstruction(node.source()); }); diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java index 33685c06..09af73cc 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -63,6 +63,18 @@ public class Typed { return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL); } + public static TypedBinaryOperationNode intNotEqual( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_NOT_EQUAL, + left, + right, + Sources.NULL + ); + } + public static TypedBinaryOperationNode intSub( TypedExpressionNode left, TypedExpressionNode right diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java new file mode 100644 index 00000000..63543b11 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java @@ -0,0 +1,26 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerIntNotEqualTests { + @Test + public void intNotEqualsIsCompiledToIntNotEqualInstruction() { + var node = Typed.intNotEqual( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushIntInstruction(42), + isPushIntInstruction(47), + isIntNotEqualInstruction() + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java index 3768c6e1..a47e7c75 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -41,6 +41,10 @@ public class InstructionMatchers { return instanceOf(IntEqualsInstruction.class); } + public static Matcher isIntNotEqualInstruction() { + return instanceOf(IntNotEqualInstruction.class); + } + public static Matcher isIntSubInstruction() { return instanceOf(IntSubInstruction.class); } -- cgit v1.2.3