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 --- .../org/shedlang/compiler/ast/typed/Typed.java | 8 ++++ .../StackCompilerFunctionDefinitionTests.java | 43 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 11 ++++++ 3 files changed, 62 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.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 80c02765..9d6cc3c3 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.types.Type; +import org.shedlang.compiler.types.TypeLevelValue; import org.shedlang.compiler.types.Types; import java.math.BigInteger; @@ -52,4 +53,11 @@ public class Typed { public static TypedReferenceNode reference(String name, Type type) { return new TypedReferenceNode(name, type, Sources.NULL); } + + public static TypedTypeLevelExpressionNode typeLevelReference( + String name, + T value + ) { + return new TypedTypeLevelReferenceNode<>(name, value, Sources.NULL); + } } 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