diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-02-02 18:59:42 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-02-02 19:07:53 +0000 |
| commit | 7001d0eef3e6139c3033570a463e02d845f3cbac (patch) | |
| tree | 1280f04b2bb5ce6b120c8fb3df56c7d77aa1ca10 /src/test/java | |
| parent | 72f814103f94f65f02c67d1ee0bd0f76768bddad (diff) | |
Compile typed relational int operations into Shid instructions
Diffstat (limited to 'src/test/java')
6 files changed, 172 insertions, 0 deletions
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 c1764f03..6c058686 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -137,6 +137,54 @@ public class Typed { ); } + public static TypedBinaryOperationNode intGreaterThan( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_GREATER_THAN, + left, + right, + Sources.NULL + ); + } + + public static TypedBinaryOperationNode intGreaterThanOrEqual( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_GREATER_THAN_OR_EQUAL, + left, + right, + Sources.NULL + ); + } + + public static TypedBinaryOperationNode intLessThan( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_LESS_THAN, + left, + right, + Sources.NULL + ); + } + + public static TypedBinaryOperationNode intLessThanOrEqual( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_LESS_THAN_OR_EQUAL, + left, + right, + Sources.NULL + ); + } + public static TypedIntLiteralNode intLiteral(int value) { return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanOrEqualTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanOrEqualTests.java new file mode 100644 index 00000000..b83292ba --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanOrEqualTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shid; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ShidCodeGenIntGreaterThanOrEqualTests { + @Test + public void intGreaterThanOrEqualIsCompiledToIntGreaterThanOrEqualInstruction() { + var node = Typed.intGreaterThanOrEqual( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + var context = ShidCodeGen.Context.EMPTY; + + var result = ShidCodeGen.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isShidPushInt(42), + isShidPushInt(47), + isShidIntGreaterThanOrEqual() + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanTests.java new file mode 100644 index 00000000..f89dd48d --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shid; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ShidCodeGenIntGreaterThanTests { + @Test + public void intGreaterThanIsCompiledToIntGreaterThanInstruction() { + var node = Typed.intGreaterThan( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + var context = ShidCodeGen.Context.EMPTY; + + var result = ShidCodeGen.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isShidPushInt(42), + isShidPushInt(47), + isShidIntGreaterThan() + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanOrEqualTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanOrEqualTests.java new file mode 100644 index 00000000..26d6e169 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanOrEqualTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shid; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ShidCodeGenIntLessThanOrEqualTests { + @Test + public void intLessThanOrEqualIsCompiledToIntLessThanOrEqualInstruction() { + var node = Typed.intLessThanOrEqual( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + var context = ShidCodeGen.Context.EMPTY; + + var result = ShidCodeGen.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isShidPushInt(42), + isShidPushInt(47), + isShidIntLessThanOrEqual() + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanTests.java new file mode 100644 index 00000000..c4b6b1e3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shid; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ShidCodeGenIntLessThanTests { + @Test + public void intLessThanIsCompiledToIntLessThanInstruction() { + var node = Typed.intLessThan( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + var context = ShidCodeGen.Context.EMPTY; + + var result = ShidCodeGen.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isShidPushInt(42), + isShidPushInt(47), + isShidIntLessThan() + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java index 2494a608..328d3311 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java @@ -95,6 +95,22 @@ public class ShidMatchers { return instanceOf(ShidIntEquals.class); } + public static Matcher<ShidInstruction> isShidIntGreaterThan() { + return instanceOf(ShidIntGreaterThan.class); + } + + public static Matcher<ShidInstruction> isShidIntGreaterThanOrEqual() { + return instanceOf(ShidIntGreaterThanOrEqual.class); + } + + public static Matcher<ShidInstruction> isShidIntLessThan() { + return instanceOf(ShidIntLessThan.class); + } + + public static Matcher<ShidInstruction> isShidIntLessThanOrEqual() { + return instanceOf(ShidIntLessThanOrEqual.class); + } + public static Matcher<ShidInstruction> isShidIntNotEqual() { return instanceOf(ShidIntNotEqual.class); } |
