diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-10 15:59:25 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-10 15:59:25 +0100 |
| commit | 33cec419ce442aaaf8559abdd5920cd61699104e (patch) | |
| tree | a549e8774c0bd9aac1c09dd48469a29e0088ebad /src/test/java | |
| parent | 363cf48c00910afbe75107549372968ea1ba23b7 (diff) | |
Compile function definition into stack instructions
Diffstat (limited to 'src/test/java')
3 files changed, 62 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 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 <T extends TypeLevelValue> TypedTypeLevelExpressionNode<T> 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<DefineFunctionInstruction.Param> isDefineFunctionInstructionParam(String name) { + return has("name", x -> x.name(), equalTo(name)); + } + public static Matcher<Instruction> isDiscardInstruction() { return instanceOf( DiscardInstruction.class @@ -32,6 +36,13 @@ public class InstructionMatchers { ); } + public static Matcher<Instruction> isLocalStoreInstruction(String name) { + return instanceOf( + LocalStoreInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } + public static Matcher<Instruction> isPushBoolInstruction(boolean value) { return instanceOf( PushBoolInstruction.class, |
