From 7001d0eef3e6139c3033570a463e02d845f3cbac Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 2 Feb 2025 18:59:42 +0000 Subject: Compile typed relational int operations into Shid instructions --- .../org/shedlang/compiler/ast/typed/Typed.java | 48 ++++++++++++++++++++++ .../ShidCodeGenIntGreaterThanOrEqualTests.java | 27 ++++++++++++ .../shid/ShidCodeGenIntGreaterThanTests.java | 27 ++++++++++++ .../shid/ShidCodeGenIntLessThanOrEqualTests.java | 27 ++++++++++++ .../compiler/shid/ShidCodeGenIntLessThanTests.java | 27 ++++++++++++ .../compiler/shid/instructions/ShidMatchers.java | 16 ++++++++ 6 files changed, 172 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanOrEqualTests.java create mode 100644 src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntGreaterThanTests.java create mode 100644 src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanOrEqualTests.java create mode 100644 src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLessThanTests.java (limited to 'src/test/java') 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 isShidIntGreaterThan() { + return instanceOf(ShidIntGreaterThan.class); + } + + public static Matcher isShidIntGreaterThanOrEqual() { + return instanceOf(ShidIntGreaterThanOrEqual.class); + } + + public static Matcher isShidIntLessThan() { + return instanceOf(ShidIntLessThan.class); + } + + public static Matcher isShidIntLessThanOrEqual() { + return instanceOf(ShidIntLessThanOrEqual.class); + } + public static Matcher isShidIntNotEqual() { return instanceOf(ShidIntNotEqual.class); } -- cgit v1.2.3