diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:29:08 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:29:08 +0100 |
| commit | 6820962c44cc7a27fe45d0a2ebe8140cd11f5811 (patch) | |
| tree | 19d8b4208605761f53eb8602a8d5b682e45902ef /src/test/java | |
| parent | bbd0ad7fad36761f55555347b01a32887fc4a5f9 (diff) | |
Add typed int add node
Diffstat (limited to 'src/test/java')
3 files changed, 43 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 d41842dc..cb4d1f02 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -35,6 +35,18 @@ public class Typed { return new TypedExpressionStatementNode(expression, Sources.NULL); } + public static TypedBinaryOperationNode intAdd( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_ADD, + 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/stackir/StackCompilerIntAddTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java new file mode 100644 index 00000000..55283682 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java @@ -0,0 +1,27 @@ +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.isIntAddInstruction; +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushIntInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerIntAddTests { + @Test + public void intAddIsCompiledToIntAddInstruction() { + var node = Typed.intAdd( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushIntInstruction(42), + isPushIntInstruction(47), + isIntAddInstruction() + )); + } +} 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 8e24e131..201a69c2 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -33,6 +33,10 @@ public class InstructionMatchers { ); } + public static Matcher<Instruction> isIntAddInstruction() { + return instanceOf(IntAddInstruction.class); + } + public static Matcher<Instruction> isLocalLoadInstruction(String name) { return instanceOf( LocalLoadInstruction.class, |
