aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/stackir
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/shedlang/compiler/stackir')
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java39
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java9
2 files changed, 48 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java
new file mode 100644
index 00000000..f4a2f229
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java
@@ -0,0 +1,39 @@
+package org.shedlang.compiler.stackir;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import java.util.List;
+
+import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class StackCompilerCallTests {
+ @Test
+ public void callIsCompiledToInstructionsForOperandThenCall() {
+ var node = Typed.call(
+ Typed.reference("f"),
+ List.of(
+ Typed.stringLiteral("p1"),
+ Typed.stringLiteral("p2")
+
+ ),
+ List.of(
+ Typed.namedArg("x2", Typed.stringLiteral("n2")),
+ Typed.namedArg("x1", Typed.stringLiteral("n1"))
+ )
+ );
+
+ var instructions = StackCompiler.compileExpression(node);
+
+ assertThat(instructions, isSequence(
+ isLocalLoadInstruction("f"),
+ isPushStringInstruction("p1"),
+ isPushStringInstruction("p2"),
+ isPushStringInstruction("n2"),
+ isPushStringInstruction("n1"),
+ isCallInstruction(2, List.of("x2", "x1"))
+ ));
+ }
+}
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 2fc1aa21..c9cd8621 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
@@ -3,6 +3,7 @@ package org.shedlang.compiler.stackir.instructions;
import org.zwobble.precisely.Matcher;
import java.math.BigInteger;
+import java.util.List;
import static org.zwobble.precisely.Matchers.*;
@@ -10,6 +11,14 @@ public class InstructionMatchers {
private InstructionMatchers() {
}
+ public static Matcher<Instruction> isCallInstruction(int argCount, List<String> namedArgs) {
+ return instanceOf(
+ CallInstruction.class,
+ has("argCount", x -> x.argCount(), equalTo(argCount)),
+ has("namedArgs", x -> x.namedArgs(), equalTo(namedArgs))
+ );
+ }
+
public static Matcher<Instruction> isLocalLoadInstruction(String name) {
return instanceOf(
LocalLoadInstruction.class,