diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-01 20:34:41 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-01 20:34:41 +0100 |
| commit | af50498623bd37db5f5b60f2828e81b7e50d2ec4 (patch) | |
| tree | a8aea12ef93e0977a09bf8d300fd8385b2787c6a /src/test/java | |
| parent | c2cdddeb6abc41ed9bf4209aeeb5bbb44eb9b5e2 (diff) | |
Compile sum type definition to stack IR
Diffstat (limited to 'src/test/java')
3 files changed, 35 insertions, 3 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 24ee99a3..06c481ba 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -197,6 +197,10 @@ public class Typed { return new TypedStructFieldDefinitionNode(name, type, Sources.NULL); } + public static TypedSumTypeDefinitionNode sumTypeDefinition(SumType sumType) { + return new TypedSumTypeDefinitionNode(sumType, Sources.NULL); + } + public static TypedTargetNode targetIgnore() { return new TypedTargetNode.Ignore(Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java index 3be1b1a5..f2f07ed5 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java @@ -2,14 +2,12 @@ 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.ast.typed.TypedStructDefinitionNode; -import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; import org.shedlang.compiler.stackir.instructions.DefineStructInstruction; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalStoreInstruction; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerSumTypeDefinitionTests.java new file mode 100644 index 00000000..488ac261 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerSumTypeDefinitionTests.java @@ -0,0 +1,30 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.Types; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalStoreInstruction; +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushUnitInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerSumTypeDefinitionTests { + @Test + public void sumTypeDefinitionIsCompiledToUnit() { + // TODO: this is a bit of a hack, we probably want to be able to have + // sum types in some potential backends (e.g. the JVM) + var sumType = Types.sum(NamespaceName.of("Example", "Project"), "Option"); + var node = Typed.sumTypeDefinition(sumType); + var context = StackCompiler.Context.EMPTY + .addShedVariable("Option"); + + var result = StackCompiler.compileNamespaceStatement(node, context); + + assertThat(result.instructions(), isSequence( + isPushUnitInstruction(), + isLocalStoreInstruction("Option__0") + )); + } +} |
