aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-07 18:31:11 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-07 18:31:17 +0100
commitd42333cb45df71c033d8b542f6df5b3b4fcdf8b4 (patch)
tree93dc91db7f96876f832fd8b50451ee4f1da6e171 /src/test/java
parent6820962c44cc7a27fe45d0a2ebe8140cd11f5811 (diff)
Add typed sub node
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java12
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java26
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java4
3 files changed, 42 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 cb4d1f02..18fe79cf 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -51,6 +51,18 @@ public class Typed {
return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL);
}
+ public static TypedBinaryOperationNode intSub(
+ TypedExpressionNode left,
+ TypedExpressionNode right
+ ) {
+ return new TypedBinaryOperationNode(
+ TypedBinaryOperator.INT_SUB,
+ left,
+ right,
+ Sources.NULL
+ );
+ }
+
public static TypedNamedArgNode namedArg(String name, TypedExpressionNode expression) {
return new TypedNamedArgNode(name, expression, Sources.NULL);
}
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java
new file mode 100644
index 00000000..b11863c5
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java
@@ -0,0 +1,26 @@
+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.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class StackCompilerIntSubTests {
+ @Test
+ public void intSubIsCompiledToIntSubInstruction() {
+ var node = Typed.intSub(
+ Typed.intLiteral(42),
+ Typed.intLiteral(47)
+ );
+
+ var instructions = StackCompiler.compileExpression(node);
+
+ assertThat(instructions, isSequence(
+ isPushIntInstruction(42),
+ isPushIntInstruction(47),
+ isIntSubInstruction()
+ ));
+ }
+}
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 201a69c2..08f02227 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
@@ -37,6 +37,10 @@ public class InstructionMatchers {
return instanceOf(IntAddInstruction.class);
}
+ public static Matcher<Instruction> isIntSubInstruction() {
+ return instanceOf(IntSubInstruction.class);
+ }
+
public static Matcher<Instruction> isLocalLoadInstruction(String name) {
return instanceOf(
LocalLoadInstruction.class,