From 9688f65cec6e119659e23efd45d2b9aaa63461e0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Dec 2023 00:37:30 +0000 Subject: Generate Shid IR for breaks --- .../java/org/shedlang/compiler/patio/PatioCodeGen.java | 6 ++++++ src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java | 8 +++++++- .../java/org/shedlang/compiler/shid/ShidSerializer.java | 6 ++++++ .../compiler/shid/instructions/ShedEffectHandlerBreak.java | 14 ++++++++++++++ .../compiler/shid/instructions/ShidInstruction.java | 2 +- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShedEffectHandlerBreak.java (limited to 'src/main/java') 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(); } -- cgit v1.2.3