aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-03 00:37:30 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-03 00:44:10 +0000
commit9688f65cec6e119659e23efd45d2b9aaa63461e0 (patch)
tree3a1cf81b23ba0a3c28fa49f37862b88c49ef1e76 /src/main/java
parent321a2ce998f27bfe575a38486e8b89fdf13a9cce (diff)
Generate Shid IR for breaks
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java6
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java8
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidSerializer.java6
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShedEffectHandlerBreak.java14
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java2
5 files changed, 34 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index 78c43e5c..733152b4 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -308,6 +308,10 @@ public class PatioCodeGen {
yield context.temporaryPush(value).temporaryPush(value);
}
+ case ShedEffectHandlerBreak effectHandlerBreak -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+
case ShidFieldLoad fieldLoad -> {
var receiverResult = context.temporaryPop();
var receiver = receiverResult.value();
@@ -948,6 +952,8 @@ public class PatioCodeGen {
}
case ShidDuplicate duplicateInstruction -> {
}
+ case ShedEffectHandlerBreak breakInstruction -> {
+ }
case ShidFieldLoad fieldLoadInstruction -> {
}
case ShidIf ifInstruction -> {
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
index 5ed3ffdb..2396f06b 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
@@ -90,6 +90,12 @@ public class ShidCodeGen {
return context.addInstruction(new ShidPushBool(node.value(), node.source()));
}
+ private static Context compileBreak(TypedBreakNode node, Context context) {
+ context = compileExpression(node.expression(), context);
+ context = context.addInstruction(new ShedEffectHandlerBreak(node.handledEffect(), node.source()));
+ return context;
+ }
+
private static Context compileCall(TypedCallNode node, Context context) {
context = compileExpression(node.receiver(), context);
@@ -337,7 +343,7 @@ public class ShidCodeGen {
compileBlockReturn(blockReturn, context);
case TypedBreakNode break_ ->
- throw new UnsupportedOperationException("TODO");
+ compileBreak(break_, context);
case TypedExpressionStatementNode expressionStatement ->
compileExpressionStatement(expressionStatement, context);
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
index 791eb64c..58f49ed0 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
@@ -104,6 +104,12 @@ public class ShidSerializer {
treeIdentifier("duplicate")
);
+ case ShedEffectHandlerBreak effectHandlerBreak ->
+ treeList(
+ treeIdentifier("effect-handler-break"),
+ userDefinedEffectToTree(effectHandlerBreak.effect())
+ );
+
case ShidFieldLoad fieldLoad ->
treeList(
treeIdentifier("field-load"),
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShedEffectHandlerBreak.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShedEffectHandlerBreak.java
new file mode 100644
index 00000000..fc27b8c1
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShedEffectHandlerBreak.java
@@ -0,0 +1,14 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+import org.shedlang.compiler.types.UserDefinedEffect;
+
+/**
+ * Pop a value from the temporary value stack and exit with that value from
+ * the closest try expression in the call stack that handles the given effect.
+ */
+public record ShedEffectHandlerBreak(
+ UserDefinedEffect effect,
+ Source source
+) implements ShidInstruction {
+}
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
index 308fe739..082c3de5 100644
--- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.shid.instructions;
import org.shedlang.compiler.sources.Source;
public sealed interface ShidInstruction
- permits ShidCall, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntNotEqual, ShidIntSub, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore {
+ permits ShidCall, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShedEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntNotEqual, ShidIntSub, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore {
Source source();
}