From 041d35ae5b61d3dd94bcd164b72158de2049ff18 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 11 Nov 2023 18:03:47 +0000 Subject: Generate stack IR for effect definition --- .../org/shedlang/compiler/ast/typed/Typed.java | 6 +++++ .../StackCompilerEffectDefinitionTests.java | 31 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.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 acb407c8..717e94d0 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -61,6 +61,12 @@ public class Typed { ); } + public static TypedEffectDefinitionNode effectDefinition( + UserDefinedEffect effect + ) { + return new TypedEffectDefinitionNode(effect, Sources.NULL); + } + public static TypedExpressionStatementNode expressionStatement(TypedExpressionNode expression) { return new TypedExpressionStatementNode(expression, Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java new file mode 100644 index 00000000..e167816c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java @@ -0,0 +1,31 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.stackir.instructions.DefineEffectInstruction; +import org.shedlang.compiler.types.Effects; +import org.shedlang.compiler.types.NamespaceName; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalStoreInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class StackCompilerEffectDefinitionTests { + @Test + public void effectDefinitionIsCompiledToDefineEffectInstruction() { + var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State"); + var node = Typed.effectDefinition(effect); + var context = StackCompiler.Context.EMPTY + .addShedVariable("State"); + + var result = StackCompiler.compileNamespaceStatement(node, context); + + assertThat(result.instructions(), isSequence( + instanceOf( + DefineEffectInstruction.class, + has("effect", x -> x.effect(), equalTo(effect)) + ), + isLocalStoreInstruction("State__0") + )); + } +} -- cgit v1.2.3