From af335486c659fb6f5d065eb61014074045e4e81d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Nov 2023 15:11:27 +0000 Subject: Generate Patio code for try statements --- .../compiler/patio/PatioCodeGenTryTests.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java new file mode 100644 index 00000000..1a9535d1 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java @@ -0,0 +1,50 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeCheckerContext; +import org.shedlang.compiler.types.*; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class PatioCodeGenTryTests { + @Test + public void canGenerateCodeForTry(Snapshotter snapshotter) throws IOException { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var effect = Effects.userDefined(namespaceName, "State"); + var stackIrInstruction = Instructions.try_( + effect, + List.of("a", "c", "b"), + List.of( + Instructions.pushInt(42) + ) + ); + var typeInfos = TypeCheckerContext.EMPTY + .addUserDefinedEffectInfo( + effect, + UserDefinedEffectInfo.builder() + .addMethod("c", FunctionType.builder().build()) + .addMethod("a", FunctionType.builder().build()) + .addMethod("b", FunctionType.builder().build()) + .build() + ) + .toTypeInfos(); + var context = PatioCodeGenContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("handlerA")) + .temporaryPush(new PatioLocalRef("handlerC")) + .temporaryPush(new PatioLocalRef("handlerB")); + + var result = PatioCodeGen.generateInstruction(stackIrInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} -- cgit v1.2.3