From 6820962c44cc7a27fe45d0a2ebe8140cd11f5811 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:29:08 +0100 Subject: Add typed int add node --- .../org/shedlang/compiler/ast/typed/Typed.java | 12 ++++++++++ .../compiler/stackir/StackCompilerIntAddTests.java | 27 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 4 ++++ 3 files changed, 43 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.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 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 isIntAddInstruction() { + return instanceOf(IntAddInstruction.class); + } + public static Matcher isLocalLoadInstruction(String name) { return instanceOf( LocalLoadInstruction.class, -- cgit v1.2.3