aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-11 18:03:47 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-11 18:03:47 +0000
commit041d35ae5b61d3dd94bcd164b72158de2049ff18 (patch)
tree47bb69c4d678c43a316dcfdad0ff33458a4a32d9 /src
parentaeb5658434d8090ec8e5cfc054ba6b13758c4da2 (diff)
Generate stack IR for effect definition
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java13
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java6
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java31
3 files changed, 49 insertions, 1 deletions
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")
+ ));
+ }
+}