From 33cec419ce442aaaf8559abdd5920cd61699104e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 15:59:25 +0100 Subject: Compile function definition into stack instructions --- .../StackCompilerFunctionDefinitionTests.java | 43 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 11 ++++++ 2 files changed, 54 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java (limited to 'src/test/java/org/shedlang/compiler/stackir') diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java new file mode 100644 index 00000000..4e0be235 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java @@ -0,0 +1,43 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode; +import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; +import org.shedlang.compiler.types.Types; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class StackCompilerFunctionDefinitionTests { + @Test + public void functionIsCompiledToDefineFunctionInstruction() { + var node = TypedFunctionDefinitionNode.builder() + .withName("f") + .addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING)) + .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT)) + .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello"))) + .build(); + + var instructions = StackCompiler.compileNamespaceStatement(node); + + assertThat(instructions, isSequence( + instanceOf( + DefineFunctionInstruction.class, + has("name", x -> x.name(), equalTo("f")), + has("positionalParams", x -> x.positionalParams(), isSequence( + isDefineFunctionInstructionParam("x") + )), + has("namedParams", x -> x.namedParams(), isSequence( + isDefineFunctionInstructionParam("y") + )), + has("body", x -> x.body(), isSequence( + isPushStringInstruction("hello"), + isDiscardInstruction() + )) + ), + isLocalStoreInstruction("f") + )); + } +} 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 3863c2f9..4ae1e793 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -19,6 +19,10 @@ public class InstructionMatchers { ); } + public static Matcher isDefineFunctionInstructionParam(String name) { + return has("name", x -> x.name(), equalTo(name)); + } + public static Matcher isDiscardInstruction() { return instanceOf( DiscardInstruction.class @@ -32,6 +36,13 @@ public class InstructionMatchers { ); } + public static Matcher isLocalStoreInstruction(String name) { + return instanceOf( + LocalStoreInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } + public static Matcher isPushBoolInstruction(boolean value) { return instanceOf( PushBoolInstruction.class, -- cgit v1.2.3