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 --- .../shedlang/compiler/stackir/StackCompiler.java | 13 ++++++++- .../org/shedlang/compiler/ast/typed/Typed.java | 6 +++++ .../StackCompilerEffectDefinitionTests.java | 31 ++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index bad4f72c..da20c770 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -114,6 +114,17 @@ public class StackCompiler { return context; } + private static Context compileEffectDefinition( + TypedEffectDefinitionNode node, + Context context + ) { + var localName = context.shedVariableToLocal(node.effect().name()); + + return context + .addInstruction(new DefineEffectInstruction(node.effect(), node.source())) + .addInstruction(new LocalStoreInstruction(localName, node.source())); + } + public static Context compileExpression(TypedExpressionNode node, Context context) { return switch (node) { case TypedBinaryOperationNode typedBinaryOperationNode -> @@ -450,7 +461,7 @@ public class StackCompiler { compileAssign(assign, context); case TypedEffectDefinitionNode effectDefinition -> - throw new UnsupportedOperationException("TODO"); + compileEffectDefinition(effectDefinition, context); case TypedFunctionDefinitionNode functionDefinition -> compileFunctionDefinition(functionDefinition, context); 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