diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:36:27 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:36:27 +0100 |
| commit | 616f475a4cf4c3296028c1fe89e17e0105898f3c (patch) | |
| tree | 4a9d98eb7b7b8d014c308c1f19272129d8c6f33c /src | |
| parent | 856895255a261a0d21a8b4a0271a75ed3a2cf7ba (diff) | |
Add typed int not equal node
Diffstat (limited to 'src')
5 files changed, 45 insertions, 0 deletions
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<Instruction> isIntNotEqualInstruction() { + return instanceOf(IntNotEqualInstruction.class); + } + public static Matcher<Instruction> isIntSubInstruction() { return instanceOf(IntSubInstruction.class); } |
