From 2cefa82d059dfd16d224b16f92fd69104beb24a2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 11:52:04 +0100 Subject: Compile calls to stack instructions --- .../compiler/stackir/StackCompilerCallTests.java | 39 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 9 +++++ 2 files changed, 48 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java (limited to 'src/test/java/org/shedlang/compiler/stackir') 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 isCallInstruction(int argCount, List namedArgs) { + return instanceOf( + CallInstruction.class, + has("argCount", x -> x.argCount(), equalTo(argCount)), + has("namedArgs", x -> x.namedArgs(), equalTo(namedArgs)) + ); + } + public static Matcher isLocalLoadInstruction(String name) { return instanceOf( LocalLoadInstruction.class, -- cgit v1.2.3