From fc115476216f364d947804cce2202b428d8afd6f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 20 Dec 2023 17:07:02 +0000 Subject: Rename PatioCodeGen to ShidToPatio --- .../compiler/backends/llvm/PatioToLlvmTests.java | 2 +- .../compiler/backends/wasm/PatioToWasmTests.java | 2 +- .../compiler/patio/PatioCodeGenCallTests.java | 44 ------ .../compiler/patio/PatioCodeGenContextTests.java | 96 ------------ .../patio/PatioCodeGenDefineEffectTests.java | 47 ------ .../patio/PatioCodeGenDefineFunctionTests.java | 173 --------------------- .../patio/PatioCodeGenDefineStructTests.java | 67 -------- .../compiler/patio/PatioCodeGenDiscardTests.java | 27 ---- .../patio/PatioCodeGenEffectHandlerBreakTests.java | 31 ---- .../compiler/patio/PatioCodeGenFieldLoadTests.java | 88 ----------- .../compiler/patio/PatioCodeGenIfTests.java | 84 ---------- .../compiler/patio/PatioCodeGenIntAddTests.java | 28 ---- .../compiler/patio/PatioCodeGenIntEqualsTests.java | 28 ---- .../patio/PatioCodeGenIntNotEqualTests.java | 28 ---- .../compiler/patio/PatioCodeGenIntSubTests.java | 28 ---- .../compiler/patio/PatioCodeGenLocalLoadTests.java | 25 --- .../patio/PatioCodeGenLocalStoreTests.java | 28 ---- .../compiler/patio/PatioCodeGenLoopBreakTests.java | 24 --- .../patio/PatioCodeGenLoopContinueTests.java | 24 --- .../compiler/patio/PatioCodeGenLoopTests.java | 30 ---- .../PatioCodeGenNamespaceFieldStoreTests.java | 43 ----- .../patio/PatioCodeGenNamespaceImportTests.java | 27 ---- .../compiler/patio/PatioCodeGenNamespaceTests.java | 56 ------- .../compiler/patio/PatioCodeGenPushBoolTests.java | 34 ---- .../compiler/patio/PatioCodeGenPushIntTests.java | 24 --- .../patio/PatioCodeGenPushStringTests.java | 34 ---- .../compiler/patio/PatioCodeGenPushUnitTests.java | 24 --- .../compiler/patio/PatioCodeGenResumeTests.java | 29 ---- .../compiler/patio/PatioCodeGenReturnTests.java | 28 ---- .../compiler/patio/PatioCodeGenStringAddTests.java | 28 ---- .../compiler/patio/PatioCodeGenSwitchTests.java | 57 ------- .../compiler/patio/PatioCodeGenTryTests.java | 80 ---------- .../patio/PatioCodeGenTupleCreateTests.java | 39 ----- .../compiler/patio/PatioCodeGenTupleGetTests.java | 27 ---- .../compiler/patio/PatioCodeGenVarLoadTests.java | 26 ---- .../compiler/patio/PatioCodeGenVarStoreTests.java | 43 ----- .../org/shedlang/compiler/patio/PatioTesting.java | 2 +- .../compiler/patio/ShidToPatioCallTests.java | 44 ++++++ .../compiler/patio/ShidToPatioContextTests.java | 96 ++++++++++++ .../patio/ShidToPatioDefineEffectTests.java | 47 ++++++ .../patio/ShidToPatioDefineFunctionTests.java | 173 +++++++++++++++++++++ .../patio/ShidToPatioDefineStructTests.java | 67 ++++++++ .../compiler/patio/ShidToPatioDiscardTests.java | 27 ++++ .../patio/ShidToPatioEffectHandlerBreakTests.java | 31 ++++ .../compiler/patio/ShidToPatioFieldLoadTests.java | 88 +++++++++++ .../compiler/patio/ShidToPatioIfTests.java | 84 ++++++++++ .../compiler/patio/ShidToPatioIntAddTests.java | 28 ++++ .../compiler/patio/ShidToPatioIntEqualsTests.java | 28 ++++ .../patio/ShidToPatioIntNotEqualTests.java | 28 ++++ .../compiler/patio/ShidToPatioIntSubTests.java | 28 ++++ .../compiler/patio/ShidToPatioLocalLoadTests.java | 25 +++ .../compiler/patio/ShidToPatioLocalStoreTests.java | 28 ++++ .../compiler/patio/ShidToPatioLoopBreakTests.java | 24 +++ .../patio/ShidToPatioLoopContinueTests.java | 24 +++ .../compiler/patio/ShidToPatioLoopTests.java | 30 ++++ .../patio/ShidToPatioNamespaceFieldStoreTests.java | 43 +++++ .../patio/ShidToPatioNamespaceImportTests.java | 27 ++++ .../compiler/patio/ShidToPatioNamespaceTests.java | 56 +++++++ .../compiler/patio/ShidToPatioPushBoolTests.java | 34 ++++ .../compiler/patio/ShidToPatioPushIntTests.java | 24 +++ .../compiler/patio/ShidToPatioPushStringTests.java | 34 ++++ .../compiler/patio/ShidToPatioPushUnitTests.java | 24 +++ .../compiler/patio/ShidToPatioResumeTests.java | 29 ++++ .../compiler/patio/ShidToPatioReturnTests.java | 28 ++++ .../compiler/patio/ShidToPatioStringAddTests.java | 28 ++++ .../compiler/patio/ShidToPatioSwitchTests.java | 57 +++++++ .../compiler/patio/ShidToPatioTryTests.java | 80 ++++++++++ .../patio/ShidToPatioTupleCreateTests.java | 39 +++++ .../compiler/patio/ShidToPatioTupleGetTests.java | 27 ++++ .../compiler/patio/ShidToPatioVarLoadTests.java | 26 ++++ .../compiler/patio/ShidToPatioVarStoreTests.java | 43 +++++ 71 files changed, 1502 insertions(+), 1502 deletions(-) delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenContextTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineEffectTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenEffectHandlerBreakTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenFieldLoadTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntAddTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntEqualsTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntSubTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalLoadTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceFieldStoreTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceImportTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushBoolTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushIntTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushStringTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushUnitTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenResumeTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenStringAddTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleGetTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioContextTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineEffectTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineFunctionTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineStructTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDiscardTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioEffectHandlerBreakTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioFieldLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIfTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntAddTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntEqualsTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntNotEqualTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntSubTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalStoreTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopBreakTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopContinueTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceFieldStoreTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceImportTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushBoolTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushIntTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushStringTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushUnitTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioResumeTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioReturnTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioStringAddTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioSwitchTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleCreateTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleGetTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioVarLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioVarStoreTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/PatioToLlvmTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/PatioToLlvmTests.java index 55583c6d..84083666 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/PatioToLlvmTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/PatioToLlvmTests.java @@ -58,7 +58,7 @@ public class PatioToLlvmTests extends PatioExampleTests { )); Files.createDirectories(Path.of("tests-tmp")); - var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "LlvmPatioCodeGen"); + var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "LlvmShidToPatio"); var objectFilePath = LlvmCompiler.compileLlvmModule( llvmModule, diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/PatioToWasmTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/PatioToWasmTests.java index a8687230..b5a10d11 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/PatioToWasmTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/PatioToWasmTests.java @@ -50,7 +50,7 @@ public class PatioToWasmTests extends PatioExampleTests { )); Files.createDirectories(Path.of("tests-tmp")); - var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmPatioCodeGen"); + var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "WasmShidToPatio"); var objectPath = workingDirectory.resolve("main.o"); try (var outputStream = new FileOutputStream(objectPath.toFile())) { diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java deleted file mode 100644 index a4ed029f..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenCallTests.java +++ /dev/null @@ -1,44 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenCallTests { - @Test - public void canCallFunctionWithNoArgs(Snapshotter snapshotter) throws IOException { - var instruction = Shid.call(0, List.of()); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("receiver")); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canCallFunctionWithArgs(Snapshotter snapshotter) throws IOException { - var instruction = Shid.call(2, List.of("b", "a")); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("receiver")) - .temporaryPush(new PatioLocalRef("pos1")) - .temporaryPush(new PatioLocalRef("pos2")) - .temporaryPush(new PatioLocalRef("namedB")) - .temporaryPush(new PatioLocalRef("namedA")); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenContextTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenContextTests.java deleted file mode 100644 index 7d46a284..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenContextTests.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.patio.ast.PatioDataElementImmutable; -import org.shedlang.compiler.patio.ast.types.PatioDataValues; -import org.shedlang.compiler.patio.errors.DuplicateCompilationUnitElementName; -import org.shedlang.compiler.sources.Sources; - -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class PatioCodeGenContextTests { - @Test - public void canAddCompilationUnitElementsWithDistinctNames() { - var context = PatioCodeGenContext.EMPTY; - - context = context.addCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(0), - Sources.NULL - )); - context = context.addCompilationUnitElement(new PatioDataElementImmutable( - "y", - PatioDataValues.int_(0), - Sources.NULL - )); - - assertThat(context.toCompilationUnit(Sources.NULL).elements(), isSequence( - instanceOf(PatioDataElementImmutable.class, has("name", x -> x.name(), equalTo("x"))), - instanceOf(PatioDataElementImmutable.class, has("name", x -> x.name(), equalTo("y"))) - )); - } - - @Test - public void addingCompilationUnitsWithSameNameThrowsError() { - var context = PatioCodeGenContext.EMPTY - .addCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(0), - Sources.NULL - )); - - var error = assertThrows( - DuplicateCompilationUnitElementName.class, - () -> context.addCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(0), - Sources.NULL - )) - ); - - assertThat(error.elementName(), equalTo("x")); - } - - @Test - public void canEnsureCompilationUnitElementsWithSameNameAndSameValue() { - var context = PatioCodeGenContext.EMPTY; - - context = context.ensureCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(0), - Sources.RUNTIME - )); - context = context.ensureCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(0), - Sources.RUNTIME - )); - - assertThat(context.toCompilationUnit(Sources.NULL).elements(), isSequence( - instanceOf(PatioDataElementImmutable.class, has("name", x -> x.name(), equalTo("x"))) - )); - } - - @Test - public void ensuringCompilationUnitsWithSameNameAndDifferentValueThrowsError() { - var context = PatioCodeGenContext.EMPTY - .ensureCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(0), - Sources.RUNTIME - )); - - var error = assertThrows( - DuplicateCompilationUnitElementName.class, - () -> context.ensureCompilationUnitElement(new PatioDataElementImmutable( - "x", - PatioDataValues.int_(1), - Sources.RUNTIME - )) - ); - - assertThat(error.elementName(), equalTo("x")); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineEffectTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineEffectTests.java deleted file mode 100644 index 7137affe..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineEffectTests.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.typechecker.TypeInfos; -import org.shedlang.compiler.types.*; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenDefineEffectTests { - @Test - public void canGenerateCodeForDefineEffect(Snapshotter snapshotter) throws IOException { - var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State"); - var instruction = Shid.defineEffect(effect); - var typeInfos = TypeInfos.EMPTY - .addUserDefinedEffectInfo( - effect, - UserDefinedEffectInfo.builder() - .addMethod( - "read", - FunctionType.builder() - .addNamedParam("fence", Types.BOOL) - .withReturnType(Types.INT) - .build() - ) - .addMethod( - "write", - FunctionType.builder() - .addPositionalParam(Types.INT) - .withReturnType(Types.UNIT) - .build() - ) - .build() - ); - var context = PatioCodeGenContext.forTypeInfos(typeInfos); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java deleted file mode 100644 index 9c52db77..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java +++ /dev/null @@ -1,173 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.ast.typed.FreeVariables; -import org.shedlang.compiler.patio.ast.types.PatioTypes; -import org.shedlang.compiler.patio.instructions.Patio; -import org.shedlang.compiler.sources.Sources; -import org.shedlang.compiler.shid.instructions.ShidDefineFunction; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenDefineFunctionTests { - @Test - public void canDefineMinimalFunction(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(), - List.of(), - FreeVariables.empty(), - Shid.block(List.of( - Shid.pushUnit(), - Shid.return_() - )), - ShidDefineFunction.Variant.FUNCTION, - Sources.NULL - ); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(), - List.of(), - FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(), - Shid.block(List.of( - Shid.localLoad("a"), - Shid.return_() - )), - ShidDefineFunction.Variant.FUNCTION, - Sources.NULL - ); - var context = PatioCodeGenContext.EMPTY - .defineShidParam("a") - .defineShidParam("b"); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canCaptureFreeVariablesThatAreSetAfterDefinition(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(), - List.of(), - FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(), - Shid.block(List.of( - Shid.localLoad("a"), - Shid.return_() - )), - ShidDefineFunction.Variant.FUNCTION, - Sources.NULL - ); - var context = PatioCodeGenContext.EMPTY - .declareShidVal("a", PatioCodeGenTypes.VALUE_TYPE).context() - .declareShidVal("b", PatioCodeGenTypes.VALUE_TYPE).context(); - - context = PatioCodeGen.generateInstruction(instruction, context); - - // We make sure there are some other instructions before setting the - // free variables so we can distinguish the free variable being copied - // into the closure upon closure creation (i.e. too soon) versus after - // the free variable is set. - context = context.addInstruction(Patio.localStore(Patio.localRef("a"), Patio.int_(42))); - context = context.initShidLocal("a"); - context = context.addInstruction(Patio.localStore(Patio.localRef("b"), Patio.int_(47))); - context = context.initShidLocal("b"); - - snapshotter.assertSnapshot(serialize(context)); - } - - @Test - public void pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(), - List.of(), - FreeVariables.builder() - .plus("a", Sources.NULL) - .plus("b", Sources.NULL) - .build(), - Shid.block(List.of( - Shid.localLoad("a"), - Shid.return_() - )), - ShidDefineFunction.Variant.FUNCTION, - Sources.NULL - ); - var context = PatioCodeGenContext.EMPTY - .declareShidVal("a", PatioTypes.DATA_POINTER).context() - .initShidLocal("a") - .declareShidVal("b", PatioTypes.DATA_POINTER).context() - .initShidLocal("b"); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void positionalAndNamedParamsAreDefined(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(Shid.param("param1"), Shid.param("param2")), - // Define named params in non-lexical order - List.of(Shid.param("paramB"), Shid.param("paramC"), Shid.param("paramA")), - FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(), - Shid.block(List.of( - Shid.localLoad("a"), - Shid.localLoad("param1"), - Shid.intAdd(), - Shid.localLoad("paramA"), - Shid.intAdd(), - Shid.return_() - )), - ShidDefineFunction.Variant.FUNCTION, - Sources.NULL - ); - var context = PatioCodeGenContext.EMPTY - .defineShidParam("a") - .defineShidParam("b"); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void effectHandlerMethodSwapsEffectHandlerInBody(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(), - List.of(), - FreeVariables.empty(), - Shid.block(List.of( - Shid.pushUnit(), - Shid.resume() - )), - ShidDefineFunction.Variant.EFFECT_HANDLER_METHOD, - Sources.NULL - ); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java deleted file mode 100644 index 450dcba2..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.typechecker.TypeInfos; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.StructTypeInfo; -import org.shedlang.compiler.types.Types; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.equalTo; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenDefineStructTests { - @Test - public void canGenerateCodeForDefineStruct(Snapshotter snapshotter) throws IOException { - var structType = Types.struct(NamespaceName.of("Example", "Project"), "User"); - var instruction = Shid.defineStruct( - structType, - List.of( - Shid.defineStructField("name"), - Shid.defineStructField("isActive") - ) - ); - var typeInfos = TypeInfos.EMPTY - .addStructTypeInfo( - structType, - StructTypeInfo.builder() - .addField("name", Types.STRING) - .addField("isActive", Types.BOOL) - .build() - ); - var context = PatioCodeGenContext.forTypeInfos(typeInfos); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void structsAreAssignedSequentialTags(Snapshotter snapshotter) throws IOException { - // TODO: we can probably optimise this to ensure that (for instance) - // variants of the same sum type have sequential tags, but source order - // will suffice for now - var structType1 = Types.struct(NamespaceName.of("Example", "Project"), "User"); - var structType2 = Types.struct(NamespaceName.of("Example", "Project"), "Id"); - var instruction1 = Shid.defineStruct(structType1, List.of()); - var instruction2 = Shid.defineStruct(structType1, List.of()); - var typeInfos = TypeInfos.EMPTY - .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) - .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); - var context = PatioCodeGenContext.forTypeInfos(typeInfos); - - context = PatioCodeGen.generateInstruction(instruction1, context); - context = PatioCodeGen.generateInstruction(instruction2, context); - - assertThat(context.structTag(structType1), equalTo(0)); - assertThat(context.structTag(structType2), equalTo(1)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java deleted file mode 100644 index ff66e6bb..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDiscardTests.java +++ /dev/null @@ -1,27 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenDiscardTests { - @Test - public void discardInstructionPopsFromTemporaryStack(Snapshotter snapshotter) throws IOException { - var shid = Shid.discard(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenEffectHandlerBreakTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenEffectHandlerBreakTests.java deleted file mode 100644 index 128f001a..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenEffectHandlerBreakTests.java +++ /dev/null @@ -1,31 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.types.Effects; -import org.shedlang.compiler.types.NamespaceName; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenEffectHandlerBreakTests { - @Test - public void effectHandlerBreakThrowsException(Snapshotter snapshotter) throws IOException { - var namespaceName = NamespaceName.of("Example", "Project"); - var effect = Effects.userDefined(namespaceName, "Effect"); - var shid = Shid.effectHandlerBreak(effect); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenFieldLoadTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenFieldLoadTests.java deleted file mode 100644 index 24ec9c18..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenFieldLoadTests.java +++ /dev/null @@ -1,88 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.typechecker.TypeCheckerContext; -import org.shedlang.compiler.typechecker.TypeInfos; -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 PatioCodeGenFieldLoadTests { - @Test - public void canGenerateCodeForNamespaceFieldLoad(Snapshotter snapshotter) throws IOException { - var namespaceName = new NamespaceName(List.of("Example", "Project")); - var shidInstruction = Shid.fieldLoad(namespaceName, "y"); - var typeInfos = TypeCheckerContext.EMPTY - .addNamespaceTypeInfo( - namespaceName, - NamespaceTypeInfo.builder() - .addField("x", Types.INT) - .addField("y", Types.INT) - .addField("z", Types.INT) - .build() - ) - .toTypeInfos(); - var context = PatioCodeGenContext.forTypeInfos(typeInfos) - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canGenerateCodeForStructFieldLoad(Snapshotter snapshotter) throws IOException { - var namespaceName = NamespaceName.of("Example", "Project"); - var structType = Types.struct(namespaceName, "User"); - var shidInstruction = Shid.fieldLoad(structType, "id"); - var typeInfos = TypeInfos.EMPTY - .addStructTypeInfo( - structType, - StructTypeInfo.builder() - .addField("name", Types.STRING) - .addField("id", Types.INT) - .addField("isActive", Types.BOOL) - .build() - ); - var context = PatioCodeGenContext.forTypeInfos(typeInfos) - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canGenerateCodeForUserDefinedEffectFieldLoad(Snapshotter snapshotter) throws IOException { - var namespaceName = NamespaceName.of("Example", "Project"); - var effect = Effects.userDefined(namespaceName, "State"); - var shidInstruction = Shid.fieldLoad(effect, "fence"); - var typeInfos = TypeInfos.EMPTY - .addUserDefinedEffectInfo( - effect, - UserDefinedEffectInfo.builder() - .addMethod("write", FunctionType.builder().build()) - .addMethod("fence", FunctionType.builder().build()) - .addMethod("read", FunctionType.builder().build()) - .build() - ); - var context = PatioCodeGenContext.forTypeInfos(typeInfos) - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java deleted file mode 100644 index 9a385a1f..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java +++ /dev/null @@ -1,84 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenIfTests { - @Test - public void canGenerateCodeForIfWithNoResults(Snapshotter snapshotter) throws IOException { - var instruction = Shid.if_( - 0, - Shid.block(List.of( - Shid.pushInt(1), - Shid.discard() - )), - Shid.block(List.of( - Shid.pushInt(101), - Shid.discard() - )) - ); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("condition")); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canGenerateCodeForIfWithSingleResult(Snapshotter snapshotter) throws IOException { - var instruction = Shid.if_( - 1, - Shid.block(List.of( - Shid.pushInt(1), - Shid.pushInt(2), - Shid.intAdd() - )), - Shid.block(List.of( - Shid.pushInt(101), - Shid.pushInt(102), - Shid.intAdd() - )) - ); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("condition")); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canGenerateCodeForIfWithMultipleResults(Snapshotter snapshotter) throws IOException { - var instruction = Shid.if_( - 2, - Shid.block(List.of( - Shid.pushInt(1), - Shid.pushInt(2) - )), - Shid.block(List.of( - Shid.pushInt(101), - Shid.pushInt(102) - )) - ); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("condition")); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntAddTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntAddTests.java deleted file mode 100644 index 53158d0d..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntAddTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenIntAddTests { - @Test - public void canGenerateCodeForIntAdd(Snapshotter snapshotter) throws IOException { - var shid = Shid.intAdd(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .temporaryPush(new PatioLocalRef("c")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntEqualsTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntEqualsTests.java deleted file mode 100644 index 8b9fe35f..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntEqualsTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenIntEqualsTests { - @Test - public void canGenerateCodeForIntEquals(Snapshotter snapshotter) throws IOException { - var shid = Shid.intEquals(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .temporaryPush(new PatioLocalRef("c")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java deleted file mode 100644 index 5715f0d4..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntNotEqualTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenIntNotEqualTests { - @Test - public void canGenerateCodeForIntNotEqual(Snapshotter snapshotter) throws IOException { - var shid = Shid.intNotEqual(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .temporaryPush(new PatioLocalRef("c")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntSubTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntSubTests.java deleted file mode 100644 index e2036764..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIntSubTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenIntSubTests { - @Test - public void canGenerateCodeForIntSub(Snapshotter snapshotter) throws IOException { - var shid = Shid.intSub(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .temporaryPush(new PatioLocalRef("c")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalLoadTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalLoadTests.java deleted file mode 100644 index f452583b..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalLoadTests.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenLocalLoadTests { - @Test - public void localLoadPushesLocalOntoStack(Snapshotter snapshotter) throws IOException { - var shid = Shid.localLoad("x"); - var context = PatioCodeGenContext.EMPTY - .defineShidParam("x"); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java deleted file mode 100644 index 00b50c44..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLocalStoreTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenLocalStoreTests { - @Test - public void localStorePopsTemporaryIntoLocal(Snapshotter snapshotter) throws IOException { - var shid = Shid.localStore("x"); - var context = PatioCodeGenContext.EMPTY - .declareShidVal("x", PatioCodeGenTypes.VALUE_TYPE).context() - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java deleted file mode 100644 index 16ab525d..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopBreakTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenLoopBreakTests { - @Test - public void canGenerateCodeForLoopBreak(Snapshotter snapshotter) throws IOException { - var instruction = Shid.loopBreak(); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java deleted file mode 100644 index 0e9372fc..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopContinueTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenLoopContinueTests { - @Test - public void canGenerateCodeForLoopContinue(Snapshotter snapshotter) throws IOException { - var instruction = Shid.loopContinue(); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java deleted file mode 100644 index 67ab77b5..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenLoopTests { - @Test - public void canGenerateCodeForLoop(Snapshotter snapshotter) throws IOException { - var instruction = Shid.loop( - Shid.block(List.of( - Shid.pushInt(1), - Shid.discard() - )) - ); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceFieldStoreTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceFieldStoreTests.java deleted file mode 100644 index 254249e0..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceFieldStoreTests.java +++ /dev/null @@ -1,43 +0,0 @@ -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.shid.instructions.Shid; -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.NamespaceName; -import org.shedlang.compiler.types.NamespaceTypeInfo; -import org.shedlang.compiler.types.Types; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenNamespaceFieldStoreTests { - @Test - public void canGenerateCodeForNamespaceFieldStore(Snapshotter snapshotter) throws IOException { - var namespaceName = new NamespaceName(List.of("Example", "Project")); - var shidInstruction = Shid.namespaceFieldStore(namespaceName, "y"); - var typeInfo = TypeCheckerContext.EMPTY - .addNamespaceTypeInfo( - namespaceName, - NamespaceTypeInfo.builder() - .addField("x", Types.INT) - .addField("y", Types.INT) - .addField("z", Types.INT) - .build() - ) - .toTypeInfos(); - var context = PatioCodeGenContext.forTypeInfos(typeInfo) - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceImportTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceImportTests.java deleted file mode 100644 index f86388cb..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceImportTests.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.types.NamespaceName; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenNamespaceImportTests { - @Test - public void canGenerateCodeForNamespaceImport(Snapshotter snapshotter) throws IOException { - var namespaceName = new NamespaceName(List.of("Example", "Project")); - var instruction = Shid.namespaceImport(namespaceName); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java deleted file mode 100644 index dc3b6adf..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java +++ /dev/null @@ -1,56 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.ast.typed.FreeVariables; -import org.shedlang.compiler.shid.ShidNamespace; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.shid.instructions.ShidDefineFunction; -import org.shedlang.compiler.sources.Sources; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.typechecker.TypeInfos; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.NamespaceTypeInfo; -import org.shedlang.compiler.types.Types; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serializeCompilationUnit; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenNamespaceTests { - @Test - public void canDefineNamespace(Snapshotter snapshotter) throws IOException { - var instruction = new ShidDefineFunction( - "f", - List.of(), - List.of(), - FreeVariables.empty(), - Shid.block(List.of( - Shid.pushUnit(), - Shid.return_() - )), - ShidDefineFunction.Variant.FUNCTION, - Sources.NULL - ); - var instructions = Shid.block(List.of(instruction, Shid.discard())); - var namespaceName = NamespaceName.of("An","Example"); - var typeInfos = TypeInfos.EMPTY - .addNamespaceTypeInfo( - namespaceName, - NamespaceTypeInfo.builder() - .addField("x", Types.INT) - .addField("y", Types.STRING) - .build() - ); - var context = PatioCodeGenContext.forTypeInfos(typeInfos); - var shidNamespace = new ShidNamespace(namespaceName, instructions); - - var result = PatioCodeGen.generateNamespace(shidNamespace, context); - - snapshotter.assertSnapshot(serializeCompilationUnit(result)); - } - -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushBoolTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushBoolTests.java deleted file mode 100644 index 4b3c869c..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushBoolTests.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenPushBoolTests { - @Test - public void pushBoolFalseInstructionGeneratesIntGetZeroInstruction(Snapshotter snapshotter) throws IOException { - var shid = Shid.pushBool(false); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void pushBoolTrueInstructionGeneratesIntGetOneInstruction(Snapshotter snapshotter) throws IOException { - var shid = Shid.pushBool(true); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushIntTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushIntTests.java deleted file mode 100644 index 0996f981..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushIntTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenPushIntTests { - @Test - public void pushIntInstructionGeneratesIntGetInstruction(Snapshotter snapshotter) throws IOException { - var shid = Shid.pushInt(42); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushStringTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushStringTests.java deleted file mode 100644 index 3a878afe..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushStringTests.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenPushStringTests { - @Test - public void pushStringIsCompiledToDataAndIndexToData(Snapshotter snapshotter) throws IOException { - var shidInstruction = Shid.pushString("hello"); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canHaveMultipleStrings(Snapshotter snapshotter) throws IOException { - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(Shid.pushString("hello"), context); - result = PatioCodeGen.generateInstruction(Shid.pushString("world"), result); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushUnitTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushUnitTests.java deleted file mode 100644 index d1aa4bd8..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenPushUnitTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenPushUnitTests { - @Test - public void pushUnitInstructionGeneratesIntGetInstruction(Snapshotter snapshotter) throws IOException { - var shid = Shid.pushUnit(); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenResumeTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenResumeTests.java deleted file mode 100644 index 6ac0e4f7..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenResumeTests.java +++ /dev/null @@ -1,29 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenResumeTests { - @Test - public void resumeInstructionGeneratesResumeInstruction(Snapshotter snapshotter) throws IOException { - var shid = Shid.resume(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .stashEffectHandler(new PatioLocalRef("effect_handler")) - .stashGcRootStackIndex(new PatioLocalRef("gc_root_stack_index")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java deleted file mode 100644 index f2bf807e..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenReturnTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenReturnTests { - @Test - public void returnInstructionGeneratesReturnInstruction(Snapshotter snapshotter) throws IOException { - var shid = Shid.return_(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .stashGcRootStackIndex(new PatioLocalRef("gc_root_stack_index"));; - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenStringAddTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenStringAddTests.java deleted file mode 100644 index 8eb09cb7..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenStringAddTests.java +++ /dev/null @@ -1,28 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenStringAddTests { - @Test - public void canGenerateCodeForStringAdd(Snapshotter snapshotter) throws IOException { - var shid = Shid.stringAdd(); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .temporaryPush(new PatioLocalRef("c")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java deleted file mode 100644 index e9838702..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java +++ /dev/null @@ -1,57 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; -import org.shedlang.compiler.typechecker.TypeInfos; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.StructTypeInfo; -import org.shedlang.compiler.types.Types; - -import java.io.IOException; -import java.util.List; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenSwitchTests { - @Test - public void canGenerateCodeForSwitchWithSingleResult(Snapshotter snapshotter) throws IOException { - var namespaceName = NamespaceName.of("Example", "Project"); - var structType1 = Types.struct(namespaceName, "A"); - var structType2 = Types.struct(namespaceName, "B"); - var instruction = Shid.switch_( - List.of( - Shid.switchCase( - structType1, - Shid.block(List.of( - Shid.pushInt(1), - Shid.pushInt(2), - Shid.intAdd() - )) - ), - Shid.switchCase( - structType2, - Shid.block(List.of( - Shid.pushInt(101), - Shid.pushInt(102), - Shid.intAdd() - )) - ) - ) - ); - var typeInfos = TypeInfos.EMPTY - .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) - .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); - var context = PatioCodeGenContext.forTypeInfos(typeInfos) - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("condition")); - - var result = PatioCodeGen.generateInstruction(instruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java deleted file mode 100644 index c64ed1d8..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java +++ /dev/null @@ -1,80 +0,0 @@ -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.shid.instructions.Shid; -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 shidInstruction = Shid.try_( - effect, - List.of("a", "c", "b"), - Shid.block(List.of( - Shid.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(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void effectHandlerIsPoppedBeforeReturn(Snapshotter snapshotter) throws IOException { - var namespaceName = new NamespaceName(List.of("Example", "Project")); - var effect = Effects.userDefined(namespaceName, "State"); - var shidInstruction = Shid.try_( - effect, - List.of("a"), - Shid.block(List.of( - Shid.pushInt(42), - Shid.return_() - )) - ); - var typeInfos = TypeCheckerContext.EMPTY - .addUserDefinedEffectInfo( - effect, - UserDefinedEffectInfo.builder() - .addMethod("a", FunctionType.builder().build()) - .build() - ) - .toTypeInfos(); - var context = PatioCodeGenContext.forTypeInfos(typeInfos) - .stashGcRootStackIndex(new PatioLocalRef("gcRootStackIndex")) - .temporaryPush(new PatioLocalRef("other")) - .temporaryPush(new PatioLocalRef("handlerA")); - - var result = PatioCodeGen.generateInstruction(shidInstruction, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java deleted file mode 100644 index d5cb6ef1..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleCreateTests.java +++ /dev/null @@ -1,39 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenTupleCreateTests { - @Test - public void canGenerateCodeForTupleCreate(Snapshotter snapshotter) throws IOException { - var shid = Shid.tupleCreate(3); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")) - .temporaryPush(new PatioLocalRef("c")) - .temporaryPush(new PatioLocalRef("d")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void zeroElementTupleIsCreatedWithoutAlloc(Snapshotter snapshotter) throws IOException { - var shid = Shid.tupleCreate(0); - var context = PatioCodeGenContext.EMPTY; - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleGetTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleGetTests.java deleted file mode 100644 index 41a037b0..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTupleGetTests.java +++ /dev/null @@ -1,27 +0,0 @@ -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.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenTupleGetTests { - @Test - public void canGenerateCodeForTupleGet(Snapshotter snapshotter) throws IOException { - var shid = Shid.tupleGet(2, 3); - var context = PatioCodeGenContext.EMPTY - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java deleted file mode 100644 index 3eae5cf9..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.shedlang.compiler.patio.ast.types.PatioTypes; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenVarLoadTests { - @Test - public void canGenerateCodeForVarLoad(Snapshotter snapshotter) throws IOException { - var shid = Shid.varLoad("x"); - var context = PatioCodeGenContext.EMPTY - .declareShidVal("x", PatioTypes.DATA_POINTER).context(); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java deleted file mode 100644 index 4f9d4430..00000000 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java +++ /dev/null @@ -1,43 +0,0 @@ -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.patio.ast.types.PatioTypes; -import org.shedlang.compiler.shid.instructions.Shid; -import org.shedlang.compiler.testing.snapshots.SnapshotResolver; -import org.shedlang.compiler.testing.snapshots.Snapshotter; - -import java.io.IOException; - -import static org.shedlang.compiler.patio.PatioTesting.serialize; - -@ExtendWith({SnapshotResolver.class}) -public class PatioCodeGenVarStoreTests { - @Test - public void canGenerateCodeForVarStore(Snapshotter snapshotter) throws IOException { - var shid = Shid.varStore("x"); - var context = PatioCodeGenContext.EMPTY - .declareShidVal("x", PatioTypes.DATA_POINTER).context() - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shid, context); - - snapshotter.assertSnapshot(serialize(result)); - } - - @Test - public void canStoreIntoVarMultipleTimes(Snapshotter snapshotter) throws IOException { - var shid = Shid.varStore("x"); - var context = PatioCodeGenContext.EMPTY - .declareShidVal("x", PatioTypes.DATA_POINTER).context() - .temporaryPush(new PatioLocalRef("a")) - .temporaryPush(new PatioLocalRef("b")); - - var result = PatioCodeGen.generateInstruction(shid, context); - result = PatioCodeGen.generateInstruction(shid, result); - - snapshotter.assertSnapshot(serialize(result)); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java index 6f655ccc..03344fb8 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java @@ -12,7 +12,7 @@ import org.shedlang.compiler.patio.ast.types.PatioType; class PatioTesting { private PatioTesting() {} - static String serialize(PatioCodeGenContext context) { + static String serialize(ShidToPatioContext context) { var builder = new StringBuilder(); builder.append("Compilation unit elements:\n\n"); diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java new file mode 100644 index 00000000..4f5d8056 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java @@ -0,0 +1,44 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioCallTests { + @Test + public void canCallFunctionWithNoArgs(Snapshotter snapshotter) throws IOException { + var instruction = Shid.call(0, List.of()); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("receiver")); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canCallFunctionWithArgs(Snapshotter snapshotter) throws IOException { + var instruction = Shid.call(2, List.of("b", "a")); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("receiver")) + .temporaryPush(new PatioLocalRef("pos1")) + .temporaryPush(new PatioLocalRef("pos2")) + .temporaryPush(new PatioLocalRef("namedB")) + .temporaryPush(new PatioLocalRef("namedA")); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioContextTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioContextTests.java new file mode 100644 index 00000000..1b2c0a75 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioContextTests.java @@ -0,0 +1,96 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.patio.ast.PatioDataElementImmutable; +import org.shedlang.compiler.patio.ast.types.PatioDataValues; +import org.shedlang.compiler.patio.errors.DuplicateCompilationUnitElementName; +import org.shedlang.compiler.sources.Sources; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ShidToPatioContextTests { + @Test + public void canAddCompilationUnitElementsWithDistinctNames() { + var context = ShidToPatioContext.EMPTY; + + context = context.addCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(0), + Sources.NULL + )); + context = context.addCompilationUnitElement(new PatioDataElementImmutable( + "y", + PatioDataValues.int_(0), + Sources.NULL + )); + + assertThat(context.toCompilationUnit(Sources.NULL).elements(), isSequence( + instanceOf(PatioDataElementImmutable.class, has("name", x -> x.name(), equalTo("x"))), + instanceOf(PatioDataElementImmutable.class, has("name", x -> x.name(), equalTo("y"))) + )); + } + + @Test + public void addingCompilationUnitsWithSameNameThrowsError() { + var context = ShidToPatioContext.EMPTY + .addCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(0), + Sources.NULL + )); + + var error = assertThrows( + DuplicateCompilationUnitElementName.class, + () -> context.addCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(0), + Sources.NULL + )) + ); + + assertThat(error.elementName(), equalTo("x")); + } + + @Test + public void canEnsureCompilationUnitElementsWithSameNameAndSameValue() { + var context = ShidToPatioContext.EMPTY; + + context = context.ensureCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(0), + Sources.RUNTIME + )); + context = context.ensureCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(0), + Sources.RUNTIME + )); + + assertThat(context.toCompilationUnit(Sources.NULL).elements(), isSequence( + instanceOf(PatioDataElementImmutable.class, has("name", x -> x.name(), equalTo("x"))) + )); + } + + @Test + public void ensuringCompilationUnitsWithSameNameAndDifferentValueThrowsError() { + var context = ShidToPatioContext.EMPTY + .ensureCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(0), + Sources.RUNTIME + )); + + var error = assertThrows( + DuplicateCompilationUnitElementName.class, + () -> context.ensureCompilationUnitElement(new PatioDataElementImmutable( + "x", + PatioDataValues.int_(1), + Sources.RUNTIME + )) + ); + + assertThat(error.elementName(), equalTo("x")); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineEffectTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineEffectTests.java new file mode 100644 index 00000000..a2e743ee --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineEffectTests.java @@ -0,0 +1,47 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.types.*; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioDefineEffectTests { + @Test + public void canGenerateCodeForDefineEffect(Snapshotter snapshotter) throws IOException { + var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State"); + var instruction = Shid.defineEffect(effect); + var typeInfos = TypeInfos.EMPTY + .addUserDefinedEffectInfo( + effect, + UserDefinedEffectInfo.builder() + .addMethod( + "read", + FunctionType.builder() + .addNamedParam("fence", Types.BOOL) + .withReturnType(Types.INT) + .build() + ) + .addMethod( + "write", + FunctionType.builder() + .addPositionalParam(Types.INT) + .withReturnType(Types.UNIT) + .build() + ) + .build() + ); + var context = ShidToPatioContext.forTypeInfos(typeInfos); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineFunctionTests.java new file mode 100644 index 00000000..7d156ff2 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineFunctionTests.java @@ -0,0 +1,173 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.ast.typed.FreeVariables; +import org.shedlang.compiler.patio.ast.types.PatioTypes; +import org.shedlang.compiler.patio.instructions.Patio; +import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.shid.instructions.ShidDefineFunction; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioDefineFunctionTests { + @Test + public void canDefineMinimalFunction(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(), + List.of(), + FreeVariables.empty(), + Shid.block(List.of( + Shid.pushUnit(), + Shid.return_() + )), + ShidDefineFunction.Variant.FUNCTION, + Sources.NULL + ); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void immutableFreeVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(), + List.of(), + FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(), + Shid.block(List.of( + Shid.localLoad("a"), + Shid.return_() + )), + ShidDefineFunction.Variant.FUNCTION, + Sources.NULL + ); + var context = ShidToPatioContext.EMPTY + .defineShidParam("a") + .defineShidParam("b"); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canCaptureFreeVariablesThatAreSetAfterDefinition(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(), + List.of(), + FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(), + Shid.block(List.of( + Shid.localLoad("a"), + Shid.return_() + )), + ShidDefineFunction.Variant.FUNCTION, + Sources.NULL + ); + var context = ShidToPatioContext.EMPTY + .declareShidVal("a", ShidToPatioTypes.VALUE_TYPE).context() + .declareShidVal("b", ShidToPatioTypes.VALUE_TYPE).context(); + + context = ShidToPatio.generateInstruction(instruction, context); + + // We make sure there are some other instructions before setting the + // free variables so we can distinguish the free variable being copied + // into the closure upon closure creation (i.e. too soon) versus after + // the free variable is set. + context = context.addInstruction(Patio.localStore(Patio.localRef("a"), Patio.int_(42))); + context = context.initShidLocal("a"); + context = context.addInstruction(Patio.localStore(Patio.localRef("b"), Patio.int_(47))); + context = context.initShidLocal("b"); + + snapshotter.assertSnapshot(serialize(context)); + } + + @Test + public void pointersToFreeMutableVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(), + List.of(), + FreeVariables.builder() + .plus("a", Sources.NULL) + .plus("b", Sources.NULL) + .build(), + Shid.block(List.of( + Shid.localLoad("a"), + Shid.return_() + )), + ShidDefineFunction.Variant.FUNCTION, + Sources.NULL + ); + var context = ShidToPatioContext.EMPTY + .declareShidVal("a", PatioTypes.DATA_POINTER).context() + .initShidLocal("a") + .declareShidVal("b", PatioTypes.DATA_POINTER).context() + .initShidLocal("b"); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void positionalAndNamedParamsAreDefined(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(Shid.param("param1"), Shid.param("param2")), + // Define named params in non-lexical order + List.of(Shid.param("paramB"), Shid.param("paramC"), Shid.param("paramA")), + FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(), + Shid.block(List.of( + Shid.localLoad("a"), + Shid.localLoad("param1"), + Shid.intAdd(), + Shid.localLoad("paramA"), + Shid.intAdd(), + Shid.return_() + )), + ShidDefineFunction.Variant.FUNCTION, + Sources.NULL + ); + var context = ShidToPatioContext.EMPTY + .defineShidParam("a") + .defineShidParam("b"); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void effectHandlerMethodSwapsEffectHandlerInBody(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(), + List.of(), + FreeVariables.empty(), + Shid.block(List.of( + Shid.pushUnit(), + Shid.resume() + )), + ShidDefineFunction.Variant.EFFECT_HANDLER_METHOD, + Sources.NULL + ); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineStructTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineStructTests.java new file mode 100644 index 00000000..18d45800 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineStructTests.java @@ -0,0 +1,67 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.StructTypeInfo; +import org.shedlang.compiler.types.Types; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioDefineStructTests { + @Test + public void canGenerateCodeForDefineStruct(Snapshotter snapshotter) throws IOException { + var structType = Types.struct(NamespaceName.of("Example", "Project"), "User"); + var instruction = Shid.defineStruct( + structType, + List.of( + Shid.defineStructField("name"), + Shid.defineStructField("isActive") + ) + ); + var typeInfos = TypeInfos.EMPTY + .addStructTypeInfo( + structType, + StructTypeInfo.builder() + .addField("name", Types.STRING) + .addField("isActive", Types.BOOL) + .build() + ); + var context = ShidToPatioContext.forTypeInfos(typeInfos); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void structsAreAssignedSequentialTags(Snapshotter snapshotter) throws IOException { + // TODO: we can probably optimise this to ensure that (for instance) + // variants of the same sum type have sequential tags, but source order + // will suffice for now + var structType1 = Types.struct(NamespaceName.of("Example", "Project"), "User"); + var structType2 = Types.struct(NamespaceName.of("Example", "Project"), "Id"); + var instruction1 = Shid.defineStruct(structType1, List.of()); + var instruction2 = Shid.defineStruct(structType1, List.of()); + var typeInfos = TypeInfos.EMPTY + .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) + .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); + var context = ShidToPatioContext.forTypeInfos(typeInfos); + + context = ShidToPatio.generateInstruction(instruction1, context); + context = ShidToPatio.generateInstruction(instruction2, context); + + assertThat(context.structTag(structType1), equalTo(0)); + assertThat(context.structTag(structType2), equalTo(1)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDiscardTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDiscardTests.java new file mode 100644 index 00000000..ca7a9bb5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioDiscardTests.java @@ -0,0 +1,27 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioDiscardTests { + @Test + public void discardInstructionPopsFromTemporaryStack(Snapshotter snapshotter) throws IOException { + var shid = Shid.discard(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioEffectHandlerBreakTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioEffectHandlerBreakTests.java new file mode 100644 index 00000000..16bb8adf --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioEffectHandlerBreakTests.java @@ -0,0 +1,31 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.types.Effects; +import org.shedlang.compiler.types.NamespaceName; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioEffectHandlerBreakTests { + @Test + public void effectHandlerBreakThrowsException(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var effect = Effects.userDefined(namespaceName, "Effect"); + var shid = Shid.effectHandlerBreak(effect); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioFieldLoadTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioFieldLoadTests.java new file mode 100644 index 00000000..b99bca39 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioFieldLoadTests.java @@ -0,0 +1,88 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeCheckerContext; +import org.shedlang.compiler.typechecker.TypeInfos; +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 ShidToPatioFieldLoadTests { + @Test + public void canGenerateCodeForNamespaceFieldLoad(Snapshotter snapshotter) throws IOException { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var shidInstruction = Shid.fieldLoad(namespaceName, "y"); + var typeInfos = TypeCheckerContext.EMPTY + .addNamespaceTypeInfo( + namespaceName, + NamespaceTypeInfo.builder() + .addField("x", Types.INT) + .addField("y", Types.INT) + .addField("z", Types.INT) + .build() + ) + .toTypeInfos(); + var context = ShidToPatioContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canGenerateCodeForStructFieldLoad(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var structType = Types.struct(namespaceName, "User"); + var shidInstruction = Shid.fieldLoad(structType, "id"); + var typeInfos = TypeInfos.EMPTY + .addStructTypeInfo( + structType, + StructTypeInfo.builder() + .addField("name", Types.STRING) + .addField("id", Types.INT) + .addField("isActive", Types.BOOL) + .build() + ); + var context = ShidToPatioContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canGenerateCodeForUserDefinedEffectFieldLoad(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var effect = Effects.userDefined(namespaceName, "State"); + var shidInstruction = Shid.fieldLoad(effect, "fence"); + var typeInfos = TypeInfos.EMPTY + .addUserDefinedEffectInfo( + effect, + UserDefinedEffectInfo.builder() + .addMethod("write", FunctionType.builder().build()) + .addMethod("fence", FunctionType.builder().build()) + .addMethod("read", FunctionType.builder().build()) + .build() + ); + var context = ShidToPatioContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIfTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIfTests.java new file mode 100644 index 00000000..5f65372b --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIfTests.java @@ -0,0 +1,84 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioIfTests { + @Test + public void canGenerateCodeForIfWithNoResults(Snapshotter snapshotter) throws IOException { + var instruction = Shid.if_( + 0, + Shid.block(List.of( + Shid.pushInt(1), + Shid.discard() + )), + Shid.block(List.of( + Shid.pushInt(101), + Shid.discard() + )) + ); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("condition")); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canGenerateCodeForIfWithSingleResult(Snapshotter snapshotter) throws IOException { + var instruction = Shid.if_( + 1, + Shid.block(List.of( + Shid.pushInt(1), + Shid.pushInt(2), + Shid.intAdd() + )), + Shid.block(List.of( + Shid.pushInt(101), + Shid.pushInt(102), + Shid.intAdd() + )) + ); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("condition")); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canGenerateCodeForIfWithMultipleResults(Snapshotter snapshotter) throws IOException { + var instruction = Shid.if_( + 2, + Shid.block(List.of( + Shid.pushInt(1), + Shid.pushInt(2) + )), + Shid.block(List.of( + Shid.pushInt(101), + Shid.pushInt(102) + )) + ); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("condition")); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntAddTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntAddTests.java new file mode 100644 index 00000000..2ef8587c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntAddTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioIntAddTests { + @Test + public void canGenerateCodeForIntAdd(Snapshotter snapshotter) throws IOException { + var shid = Shid.intAdd(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntEqualsTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntEqualsTests.java new file mode 100644 index 00000000..671e0678 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntEqualsTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioIntEqualsTests { + @Test + public void canGenerateCodeForIntEquals(Snapshotter snapshotter) throws IOException { + var shid = Shid.intEquals(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntNotEqualTests.java new file mode 100644 index 00000000..f662435d --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntNotEqualTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioIntNotEqualTests { + @Test + public void canGenerateCodeForIntNotEqual(Snapshotter snapshotter) throws IOException { + var shid = Shid.intNotEqual(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntSubTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntSubTests.java new file mode 100644 index 00000000..8fc103a3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntSubTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioIntSubTests { + @Test + public void canGenerateCodeForIntSub(Snapshotter snapshotter) throws IOException { + var shid = Shid.intSub(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalLoadTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalLoadTests.java new file mode 100644 index 00000000..c6eac482 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalLoadTests.java @@ -0,0 +1,25 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioLocalLoadTests { + @Test + public void localLoadPushesLocalOntoStack(Snapshotter snapshotter) throws IOException { + var shid = Shid.localLoad("x"); + var context = ShidToPatioContext.EMPTY + .defineShidParam("x"); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalStoreTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalStoreTests.java new file mode 100644 index 00000000..61c4cabd --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalStoreTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioLocalStoreTests { + @Test + public void localStorePopsTemporaryIntoLocal(Snapshotter snapshotter) throws IOException { + var shid = Shid.localStore("x"); + var context = ShidToPatioContext.EMPTY + .declareShidVal("x", ShidToPatioTypes.VALUE_TYPE).context() + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopBreakTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopBreakTests.java new file mode 100644 index 00000000..b31e37fe --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopBreakTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioLoopBreakTests { + @Test + public void canGenerateCodeForLoopBreak(Snapshotter snapshotter) throws IOException { + var instruction = Shid.loopBreak(); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopContinueTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopContinueTests.java new file mode 100644 index 00000000..6ad5ab09 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopContinueTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioLoopContinueTests { + @Test + public void canGenerateCodeForLoopContinue(Snapshotter snapshotter) throws IOException { + var instruction = Shid.loopContinue(); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopTests.java new file mode 100644 index 00000000..aee95b84 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopTests.java @@ -0,0 +1,30 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioLoopTests { + @Test + public void canGenerateCodeForLoop(Snapshotter snapshotter) throws IOException { + var instruction = Shid.loop( + Shid.block(List.of( + Shid.pushInt(1), + Shid.discard() + )) + ); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceFieldStoreTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceFieldStoreTests.java new file mode 100644 index 00000000..31159dbc --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceFieldStoreTests.java @@ -0,0 +1,43 @@ +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.shid.instructions.Shid; +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.NamespaceName; +import org.shedlang.compiler.types.NamespaceTypeInfo; +import org.shedlang.compiler.types.Types; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioNamespaceFieldStoreTests { + @Test + public void canGenerateCodeForNamespaceFieldStore(Snapshotter snapshotter) throws IOException { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var shidInstruction = Shid.namespaceFieldStore(namespaceName, "y"); + var typeInfo = TypeCheckerContext.EMPTY + .addNamespaceTypeInfo( + namespaceName, + NamespaceTypeInfo.builder() + .addField("x", Types.INT) + .addField("y", Types.INT) + .addField("z", Types.INT) + .build() + ) + .toTypeInfos(); + var context = ShidToPatioContext.forTypeInfos(typeInfo) + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceImportTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceImportTests.java new file mode 100644 index 00000000..e7d9e46f --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceImportTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.types.NamespaceName; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioNamespaceImportTests { + @Test + public void canGenerateCodeForNamespaceImport(Snapshotter snapshotter) throws IOException { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var instruction = Shid.namespaceImport(namespaceName); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceTests.java new file mode 100644 index 00000000..6b45c22e --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceTests.java @@ -0,0 +1,56 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.ast.typed.FreeVariables; +import org.shedlang.compiler.shid.ShidNamespace; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.shid.instructions.ShidDefineFunction; +import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.NamespaceTypeInfo; +import org.shedlang.compiler.types.Types; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serializeCompilationUnit; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioNamespaceTests { + @Test + public void canDefineNamespace(Snapshotter snapshotter) throws IOException { + var instruction = new ShidDefineFunction( + "f", + List.of(), + List.of(), + FreeVariables.empty(), + Shid.block(List.of( + Shid.pushUnit(), + Shid.return_() + )), + ShidDefineFunction.Variant.FUNCTION, + Sources.NULL + ); + var instructions = Shid.block(List.of(instruction, Shid.discard())); + var namespaceName = NamespaceName.of("An","Example"); + var typeInfos = TypeInfos.EMPTY + .addNamespaceTypeInfo( + namespaceName, + NamespaceTypeInfo.builder() + .addField("x", Types.INT) + .addField("y", Types.STRING) + .build() + ); + var context = ShidToPatioContext.forTypeInfos(typeInfos); + var shidNamespace = new ShidNamespace(namespaceName, instructions); + + var result = ShidToPatio.generateNamespace(shidNamespace, context); + + snapshotter.assertSnapshot(serializeCompilationUnit(result)); + } + +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushBoolTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushBoolTests.java new file mode 100644 index 00000000..b7864376 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushBoolTests.java @@ -0,0 +1,34 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioPushBoolTests { + @Test + public void pushBoolFalseInstructionGeneratesIntGetZeroInstruction(Snapshotter snapshotter) throws IOException { + var shid = Shid.pushBool(false); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void pushBoolTrueInstructionGeneratesIntGetOneInstruction(Snapshotter snapshotter) throws IOException { + var shid = Shid.pushBool(true); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushIntTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushIntTests.java new file mode 100644 index 00000000..ed78b991 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushIntTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioPushIntTests { + @Test + public void pushIntInstructionGeneratesIntGetInstruction(Snapshotter snapshotter) throws IOException { + var shid = Shid.pushInt(42); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushStringTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushStringTests.java new file mode 100644 index 00000000..7d538c4f --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushStringTests.java @@ -0,0 +1,34 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioPushStringTests { + @Test + public void pushStringIsCompiledToDataAndIndexToData(Snapshotter snapshotter) throws IOException { + var shidInstruction = Shid.pushString("hello"); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canHaveMultipleStrings(Snapshotter snapshotter) throws IOException { + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(Shid.pushString("hello"), context); + result = ShidToPatio.generateInstruction(Shid.pushString("world"), result); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushUnitTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushUnitTests.java new file mode 100644 index 00000000..fef89215 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushUnitTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioPushUnitTests { + @Test + public void pushUnitInstructionGeneratesIntGetInstruction(Snapshotter snapshotter) throws IOException { + var shid = Shid.pushUnit(); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioResumeTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioResumeTests.java new file mode 100644 index 00000000..57df08cc --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioResumeTests.java @@ -0,0 +1,29 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioResumeTests { + @Test + public void resumeInstructionGeneratesResumeInstruction(Snapshotter snapshotter) throws IOException { + var shid = Shid.resume(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .stashEffectHandler(new PatioLocalRef("effect_handler")) + .stashGcRootStackIndex(new PatioLocalRef("gc_root_stack_index")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioReturnTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioReturnTests.java new file mode 100644 index 00000000..c291a5e3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioReturnTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioReturnTests { + @Test + public void returnInstructionGeneratesReturnInstruction(Snapshotter snapshotter) throws IOException { + var shid = Shid.return_(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .stashGcRootStackIndex(new PatioLocalRef("gc_root_stack_index"));; + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioStringAddTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioStringAddTests.java new file mode 100644 index 00000000..fc087d2b --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioStringAddTests.java @@ -0,0 +1,28 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioStringAddTests { + @Test + public void canGenerateCodeForStringAdd(Snapshotter snapshotter) throws IOException { + var shid = Shid.stringAdd(); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioSwitchTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioSwitchTests.java new file mode 100644 index 00000000..d70e1723 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioSwitchTests.java @@ -0,0 +1,57 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.StructTypeInfo; +import org.shedlang.compiler.types.Types; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioSwitchTests { + @Test + public void canGenerateCodeForSwitchWithSingleResult(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var structType1 = Types.struct(namespaceName, "A"); + var structType2 = Types.struct(namespaceName, "B"); + var instruction = Shid.switch_( + List.of( + Shid.switchCase( + structType1, + Shid.block(List.of( + Shid.pushInt(1), + Shid.pushInt(2), + Shid.intAdd() + )) + ), + Shid.switchCase( + structType2, + Shid.block(List.of( + Shid.pushInt(101), + Shid.pushInt(102), + Shid.intAdd() + )) + ) + ) + ); + var typeInfos = TypeInfos.EMPTY + .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) + .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); + var context = ShidToPatioContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("condition")); + + var result = ShidToPatio.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java new file mode 100644 index 00000000..8ed1f6ff --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java @@ -0,0 +1,80 @@ +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.shid.instructions.Shid; +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 ShidToPatioTryTests { + @Test + public void canGenerateCodeForTry(Snapshotter snapshotter) throws IOException { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var effect = Effects.userDefined(namespaceName, "State"); + var shidInstruction = Shid.try_( + effect, + List.of("a", "c", "b"), + Shid.block(List.of( + Shid.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 = ShidToPatioContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("handlerA")) + .temporaryPush(new PatioLocalRef("handlerC")) + .temporaryPush(new PatioLocalRef("handlerB")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void effectHandlerIsPoppedBeforeReturn(Snapshotter snapshotter) throws IOException { + var namespaceName = new NamespaceName(List.of("Example", "Project")); + var effect = Effects.userDefined(namespaceName, "State"); + var shidInstruction = Shid.try_( + effect, + List.of("a"), + Shid.block(List.of( + Shid.pushInt(42), + Shid.return_() + )) + ); + var typeInfos = TypeCheckerContext.EMPTY + .addUserDefinedEffectInfo( + effect, + UserDefinedEffectInfo.builder() + .addMethod("a", FunctionType.builder().build()) + .build() + ) + .toTypeInfos(); + var context = ShidToPatioContext.forTypeInfos(typeInfos) + .stashGcRootStackIndex(new PatioLocalRef("gcRootStackIndex")) + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("handlerA")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleCreateTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleCreateTests.java new file mode 100644 index 00000000..8fc0e251 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleCreateTests.java @@ -0,0 +1,39 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioTupleCreateTests { + @Test + public void canGenerateCodeForTupleCreate(Snapshotter snapshotter) throws IOException { + var shid = Shid.tupleCreate(3); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")) + .temporaryPush(new PatioLocalRef("c")) + .temporaryPush(new PatioLocalRef("d")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void zeroElementTupleIsCreatedWithoutAlloc(Snapshotter snapshotter) throws IOException { + var shid = Shid.tupleCreate(0); + var context = ShidToPatioContext.EMPTY; + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleGetTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleGetTests.java new file mode 100644 index 00000000..14e43c45 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleGetTests.java @@ -0,0 +1,27 @@ +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.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioTupleGetTests { + @Test + public void canGenerateCodeForTupleGet(Snapshotter snapshotter) throws IOException { + var shid = Shid.tupleGet(2, 3); + var context = ShidToPatioContext.EMPTY + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarLoadTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarLoadTests.java new file mode 100644 index 00000000..76b74fd3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarLoadTests.java @@ -0,0 +1,26 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.ast.types.PatioTypes; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioVarLoadTests { + @Test + public void canGenerateCodeForVarLoad(Snapshotter snapshotter) throws IOException { + var shid = Shid.varLoad("x"); + var context = ShidToPatioContext.EMPTY + .declareShidVal("x", PatioTypes.DATA_POINTER).context(); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarStoreTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarStoreTests.java new file mode 100644 index 00000000..e0c868e0 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarStoreTests.java @@ -0,0 +1,43 @@ +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.patio.ast.types.PatioTypes; +import org.shedlang.compiler.shid.instructions.Shid; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class ShidToPatioVarStoreTests { + @Test + public void canGenerateCodeForVarStore(Snapshotter snapshotter) throws IOException { + var shid = Shid.varStore("x"); + var context = ShidToPatioContext.EMPTY + .declareShidVal("x", PatioTypes.DATA_POINTER).context() + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shid, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canStoreIntoVarMultipleTimes(Snapshotter snapshotter) throws IOException { + var shid = Shid.varStore("x"); + var context = ShidToPatioContext.EMPTY + .declareShidVal("x", PatioTypes.DATA_POINTER).context() + .temporaryPush(new PatioLocalRef("a")) + .temporaryPush(new PatioLocalRef("b")); + + var result = ShidToPatio.generateInstruction(shid, context); + result = ShidToPatio.generateInstruction(shid, result); + + snapshotter.assertSnapshot(serialize(result)); + } +} -- cgit v1.2.3