From 67cfee13acaea27b5d4daf5b58ade49d9a14363b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 2 Feb 2025 19:57:08 +0000 Subject: Generate Shid instructions for int multiply --- .../org/shedlang/compiler/ast/typed/Typed.java | 12 ++++++++++ .../compiler/shid/ShidCodeGenIntMultiplyTests.java | 27 ++++++++++++++++++++++ .../compiler/shid/instructions/ShidMatchers.java | 4 ++++ 3 files changed, 43 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntMultiplyTests.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 6c058686..cd65a2c9 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -189,6 +189,18 @@ public class Typed { return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL); } + public static TypedBinaryOperationNode intMultiply( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_MULTIPLY, + left, + right, + Sources.NULL + ); + } + public static TypedBinaryOperationNode intNotEqual( TypedExpressionNode left, TypedExpressionNode right diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntMultiplyTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntMultiplyTests.java new file mode 100644 index 00000000..41b3ed01 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntMultiplyTests.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 ShidCodeGenIntMultiplyTests { + @Test + public void intMultiplyIsCompiledToIntMultiplyInstruction() { + var node = Typed.intMultiply( + 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), + isShidIntMultiply() + )); + } +} 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 328d3311..715b0b42 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java @@ -111,6 +111,10 @@ public class ShidMatchers { return instanceOf(ShidIntLessThanOrEqual.class); } + public static Matcher isShidIntMultiply() { + return instanceOf(ShidIntMultiply.class); + } + public static Matcher isShidIntNotEqual() { return instanceOf(ShidIntNotEqual.class); } -- cgit v1.2.3