diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-12 19:29:46 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-12 19:29:46 +0000 |
| commit | f3caf574b02e54d459374b9750e5791df5549b9f (patch) | |
| tree | 015547416c3ff3a0a9b3963f6b47309fd6a7a0ff /src/test/java | |
| parent | 04511855a22f07d595789520aa8beabaa642a0e6 (diff) | |
Compile try node into stack IR
Diffstat (limited to 'src/test/java')
3 files changed, 71 insertions, 29 deletions
diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java index 7c974d94..1ba7fb6a 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -381,14 +381,9 @@ public class InstructionSerializerTests { public void canSerializeTry() { var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State"); var instruction = Instructions.try_( - effect, List.of( - DefineFunctionInstruction.builder() - .withName("read") - .build(), - DefineFunctionInstruction.builder() - .withName("write") - .build() - ), List.of( + effect, + List.of("read", "write"), + List.of( Instructions.pushUnit() ) ); @@ -400,27 +395,11 @@ public class InstructionSerializerTests { try "Example/Project.State" ( - (push-unit) + "read" + "write" ) ( - ( - define-function - name="read" - positional-params=() - named-params=() - free-variables=() - body=( - ) - ) - ( - define-function - name="write" - positional-params=() - named-params=() - free-variables=() - body=( - ) - ) + (push-unit) ) )""")); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerTryTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTryTests.java new file mode 100644 index 00000000..26bf46a8 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTryTests.java @@ -0,0 +1,63 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.ast.typed.TypedTryHandlerMethodNode; +import org.shedlang.compiler.ast.typed.TypedTryNode; +import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; +import org.shedlang.compiler.stackir.instructions.TryInstruction; +import org.shedlang.compiler.types.Effects; +import org.shedlang.compiler.types.NamespaceName; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushIntInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class StackCompilerTryTests { + @Test + public void tryNodeIsCompiledToTryInstruction() { + var effect = Effects.userDefined(NamespaceName.of("Example"), "State"); + var node = TypedTryNode.builder() + .addBodyStatement( + Typed.blockReturn(Typed.intLiteral(42)) + ) + .withHandledEffect(Typed.typeLevelReference("State", effect)) + .addHandlerMethod( + TypedTryHandlerMethodNode.builder() + .withName("read") + .build() + ) + .addHandlerMethod( + TypedTryHandlerMethodNode.builder() + .withName("write") + .build() + ) + .build(); + var context = StackCompiler.Context.EMPTY + .addShedVariable("c"); + + var result = StackCompiler.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + instanceOf( + DefineFunctionInstruction.class, + has("name", x -> x.name(), equalTo("read")) + ), + instanceOf( + DefineFunctionInstruction.class, + has("name", x -> x.name(), equalTo("write")) + ), + instanceOf( + TryInstruction.class, + has("handledEffect", x -> x.handledEffect(), equalTo(effect)), + has("handlerMethodNames", x -> x.handlerMethodNames(), isSequence( + equalTo("read"), + equalTo("write") + )), + has("body", x -> x.body(), isSequence( + isPushIntInstruction(42) + )) + ) + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java index 8133750d..fe820b55 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java @@ -152,12 +152,12 @@ public class Instructions { public static Instruction try_( UserDefinedEffect handledEffect, - List<DefineFunctionInstruction> handlerMethods, + List<String> handlerMethodNames, List<Instruction> body ) { return new TryInstruction( handledEffect, - handlerMethods, + handlerMethodNames, body, Sources.NULL ); |
