diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-03 00:37:30 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-03 00:44:10 +0000 |
| commit | 9688f65cec6e119659e23efd45d2b9aaa63461e0 (patch) | |
| tree | 3a1cf81b23ba0a3c28fa49f37862b88c49ef1e76 /src/test/java | |
| parent | 321a2ce998f27bfe575a38486e8b89fdf13a9cce (diff) | |
Generate Shid IR for breaks
Diffstat (limited to 'src/test/java')
5 files changed, 60 insertions, 1 deletions
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 6ec6c0fb..58b66557 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -36,6 +36,13 @@ public class Typed { return new TypedBoolLiteralNode(value, Sources.NULL); } + public static TypedBreakNode break_( + UserDefinedEffect handledEffect, + TypedExpressionNode expression + ) { + return new TypedBreakNode(handledEffect, expression, Sources.NULL); + } + public static TypedCallNode call( TypedExpressionNode receiver, List<TypedExpressionNode> positionalArgs, diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBreakTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBreakTests.java new file mode 100644 index 00000000..90823945 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBreakTests.java @@ -0,0 +1,29 @@ +package org.shedlang.compiler.shid; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.types.Effects; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.UserDefinedEffect; + +import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ShidCodeGenBreakTests { + private static final NamespaceName NAMESPACE_NAME = NamespaceName.of("Example"); + private static final UserDefinedEffect EFFECT = Effects.userDefined(NAMESPACE_NAME, "Effect"); + + @Test + public void expressionIsEvaluatedAndReturned() { + var node = Typed.break_(EFFECT, Typed.stringLiteral("hello")); + var context = ShidCodeGen.Context.EMPTY; + + var result = ShidCodeGen.compileFunctionStatement(node, context); + + assertThat(result.instructions(), isSequence( + isPushStringInstruction("hello"), + isBreakInstruction(EFFECT) + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java index b73ecdd6..b1810131 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java @@ -1,9 +1,9 @@ package org.shedlang.compiler.shid; import org.junit.jupiter.api.Test; +import org.shedlang.compiler.shid.instructions.Shid; import org.shedlang.compiler.shid.instructions.ShidDefineFunction; import org.shedlang.compiler.shid.instructions.ShidInstruction; -import org.shedlang.compiler.shid.instructions.Shid; import org.shedlang.compiler.types.Effects; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; @@ -106,6 +106,17 @@ public class ShidSerializerTests { } @Test + public void canSerializeEffectHandlerBreak() { + var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State"); + var instruction = Shid.effectHandlerBreak(effect); + + var string = serialize(instruction); + + assertThat(string, equalTo(""" + (effect-handler-break "Example/Project.State")""")); + } + + @Test public void canSerializeNamespaceFieldLoad() { var instruction = Shid.fieldLoad( new NamespaceName(List.of("Example", "Lib")), diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java index c7c89825..ed7b1e83 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.StructType; +import org.shedlang.compiler.types.UserDefinedEffect; import org.zwobble.precisely.Matcher; import java.math.BigInteger; @@ -13,6 +14,13 @@ public class InstructionMatchers { private InstructionMatchers() { } + public static Matcher<ShidInstruction> isBreakInstruction(UserDefinedEffect effect) { + return instanceOf( + ShedEffectHandlerBreak.class, + has("effect", x -> x.effect(), equalTo(effect)) + ); + } + public static Matcher<ShidInstruction> isCallInstruction( int positionalArgCount, List<String> namedArgNames diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java index cd8e41fd..96b36025 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java @@ -43,6 +43,10 @@ public class Shid { return new ShidDuplicate(Sources.NULL); } + public static ShidInstruction effectHandlerBreak(UserDefinedEffect effect) { + return new ShedEffectHandlerBreak(effect, Sources.NULL); + } + public static ShidInstruction fieldLoad(NamespaceName namespaceName, String fieldName) { return new ShidFieldLoad( new ShidFieldLoad.Receiver.Namespace(namespaceName), |
