From bed3635d310d243a7946566e4eea0ed5e4833a49 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 20 Dec 2023 17:11:37 +0000 Subject: Move ShidToPatio code to separate package --- .../org/shedlang/compiler/patio/PatioTesting.java | 4 +- .../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/ShidToPatioTesting.java | 41 ----- .../compiler/patio/ShidToPatioTryTests.java | 80 ---------- .../patio/ShidToPatioTupleCreateTests.java | 39 ----- .../compiler/patio/ShidToPatioTupleGetTests.java | 27 ---- .../compiler/patio/ShidToPatioVarLoadTests.java | 26 ---- .../compiler/patio/ShidToPatioVarStoreTests.java | 43 ----- .../compiler/shidtopatio/ShidToPatioCallTests.java | 44 ++++++ .../shidtopatio/ShidToPatioContextTests.java | 96 ++++++++++++ .../shidtopatio/ShidToPatioDefineEffectTests.java | 47 ++++++ .../ShidToPatioDefineFunctionTests.java | 173 +++++++++++++++++++++ .../shidtopatio/ShidToPatioDefineStructTests.java | 67 ++++++++ .../shidtopatio/ShidToPatioDiscardTests.java | 27 ++++ .../ShidToPatioEffectHandlerBreakTests.java | 31 ++++ .../shidtopatio/ShidToPatioFieldLoadTests.java | 88 +++++++++++ .../compiler/shidtopatio/ShidToPatioIfTests.java | 84 ++++++++++ .../shidtopatio/ShidToPatioIntAddTests.java | 28 ++++ .../shidtopatio/ShidToPatioIntEqualsTests.java | 28 ++++ .../shidtopatio/ShidToPatioIntNotEqualTests.java | 28 ++++ .../shidtopatio/ShidToPatioIntSubTests.java | 28 ++++ .../shidtopatio/ShidToPatioLocalLoadTests.java | 25 +++ .../shidtopatio/ShidToPatioLocalStoreTests.java | 28 ++++ .../shidtopatio/ShidToPatioLoopBreakTests.java | 24 +++ .../shidtopatio/ShidToPatioLoopContinueTests.java | 24 +++ .../compiler/shidtopatio/ShidToPatioLoopTests.java | 30 ++++ .../ShidToPatioNamespaceFieldStoreTests.java | 43 +++++ .../ShidToPatioNamespaceImportTests.java | 27 ++++ .../shidtopatio/ShidToPatioNamespaceTests.java | 56 +++++++ .../shidtopatio/ShidToPatioPushBoolTests.java | 34 ++++ .../shidtopatio/ShidToPatioPushIntTests.java | 24 +++ .../shidtopatio/ShidToPatioPushStringTests.java | 34 ++++ .../shidtopatio/ShidToPatioPushUnitTests.java | 24 +++ .../shidtopatio/ShidToPatioResumeTests.java | 29 ++++ .../shidtopatio/ShidToPatioReturnTests.java | 28 ++++ .../shidtopatio/ShidToPatioStringAddTests.java | 28 ++++ .../shidtopatio/ShidToPatioSwitchTests.java | 57 +++++++ .../compiler/shidtopatio/ShidToPatioTesting.java | 41 +++++ .../compiler/shidtopatio/ShidToPatioTryTests.java | 80 ++++++++++ .../shidtopatio/ShidToPatioTupleCreateTests.java | 39 +++++ .../shidtopatio/ShidToPatioTupleGetTests.java | 27 ++++ .../shidtopatio/ShidToPatioVarLoadTests.java | 26 ++++ .../shidtopatio/ShidToPatioVarStoreTests.java | 43 +++++ 71 files changed, 1542 insertions(+), 1542 deletions(-) delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioContextTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineEffectTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineFunctionTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineStructTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioDiscardTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioEffectHandlerBreakTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioFieldLoadTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIfTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntAddTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntEqualsTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntNotEqualTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioIntSubTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalLoadTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalStoreTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopBreakTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopContinueTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceFieldStoreTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceImportTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushBoolTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushIntTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushStringTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioPushUnitTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioResumeTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioReturnTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioStringAddTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioSwitchTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTesting.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleCreateTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleGetTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioVarLoadTests.java delete mode 100644 src/test/java/org/shedlang/compiler/patio/ShidToPatioVarStoreTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioCallTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioContextTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineFunctionTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDiscardTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffectHandlerBreakTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIfTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntAddTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntEqualsTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntNotEqualTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntSubTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalStoreTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopBreakTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopContinueTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceImportTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushBoolTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushIntTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushStringTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushUnitTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioResumeTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioReturnTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioStringAddTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTupleCreateTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTupleGetTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioVarLoadTests.java create mode 100644 src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioVarStoreTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java index 2bbc1f02..ddebca23 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java @@ -9,10 +9,10 @@ import org.shedlang.compiler.patio.ast.types.PatioDataLayout; import org.shedlang.compiler.patio.ast.types.PatioDataValue; import org.shedlang.compiler.patio.ast.types.PatioType; -class PatioTesting { +public class PatioTesting { private PatioTesting() {} - static String serializeCompilationUnit(PatioCompilationUnit compilationUnit) { + public static String serializeCompilationUnit(PatioCompilationUnit compilationUnit) { var builder = new StringBuilder(); PatioSerializer.serializeCompilationUnit(compilationUnit, builder); return builder.toString(); diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.java deleted file mode 100644 index 135cdc28..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioCallTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 1b2c0a75..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioContextTests.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 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 deleted file mode 100644 index 8d29b212..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineEffectTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 769069a4..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineFunctionTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 3db3c28b..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDefineStructTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 58cbbe6d..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioDiscardTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 5a6a8bb1..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioEffectHandlerBreakTests.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.ShidToPatioTesting.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 deleted file mode 100644 index be7c5ab6..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioFieldLoadTests.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.ShidToPatioTesting.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 deleted file mode 100644 index c0ed1d9a..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIfTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 309b1860..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntAddTests.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.ShidToPatioTesting.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 deleted file mode 100644 index e17f4097..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntEqualsTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 7e6c69ad..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntNotEqualTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 76da989f..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioIntSubTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 99d4377e..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalLoadTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 7ee3e31a..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLocalStoreTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 849778f5..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopBreakTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 3c669c54..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopContinueTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 41ffdc24..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioLoopTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 6265a472..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceFieldStoreTests.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.ShidToPatioTesting.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 deleted file mode 100644 index cea552e6..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceImportTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 6b45c22e..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioNamespaceTests.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 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 deleted file mode 100644 index d0d6b0ed..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushBoolTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 91d1bd3d..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushIntTests.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.ShidToPatioTesting.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 deleted file mode 100644 index e61c342a..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushStringTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 586417b6..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioPushUnitTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 8c8abacf..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioResumeTests.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.ShidToPatioTesting.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 deleted file mode 100644 index de19cfbd..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioReturnTests.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.ShidToPatioTesting.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 deleted file mode 100644 index a0f80322..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioStringAddTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 487287e6..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioSwitchTests.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.ShidToPatioTesting.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/ShidToPatioTesting.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTesting.java deleted file mode 100644 index c57084fe..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTesting.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.shedlang.compiler.patio; - -import org.shedlang.compiler.patio.ast.PatioSerializer; - -class ShidToPatioTesting { - private ShidToPatioTesting() {} - - static String serialize(ShidToPatioContext context) { - var builder = new StringBuilder(); - - builder.append("Compilation unit elements:\n\n"); - for (var element : context.compilationUnitContext().elementsByName().values()) { - PatioSerializer.serializeCompilationUnitElement(element, builder); - builder.append("\n"); - } - builder.append("\n"); - - builder.append("Locals:\n"); - for (var local : context.functionContext().localsByName().values()) { - builder.append(" * "); - PatioSerializer.serializeLocal(local, builder); - builder.append("\n"); - } - builder.append("\n"); - - builder.append("Instructions:\n\n"); - PatioSerializer.serializeInstructions( - context.functionContext().instructions(), - builder - ); - - builder.append("\n\nTemporary stack:\n"); - for (var value : context.functionContext().temporaryStack()) { - builder.append(" * "); - builder.append(value.toString()); - builder.append("\n"); - } - - return builder.toString(); - } -} diff --git a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java b/src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.java deleted file mode 100644 index d334afd5..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTryTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 5fdd44a0..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleCreateTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 317f265c..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioTupleGetTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 009af9fa..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarLoadTests.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.ShidToPatioTesting.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 deleted file mode 100644 index 79ee8f75..00000000 --- a/src/test/java/org/shedlang/compiler/patio/ShidToPatioVarStoreTests.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.ShidToPatioTesting.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)); - } -} diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioCallTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioCallTests.java new file mode 100644 index 00000000..3ce22459 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioCallTests.java @@ -0,0 +1,44 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioContextTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioContextTests.java new file mode 100644 index 00000000..b8bf1da5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioContextTests.java @@ -0,0 +1,96 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.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/shidtopatio/ShidToPatioDefineEffectTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java new file mode 100644 index 00000000..686f708c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java @@ -0,0 +1,47 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineFunctionTests.java new file mode 100644 index 00000000..12db80c6 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineFunctionTests.java @@ -0,0 +1,173 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioDefineStructTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java new file mode 100644 index 00000000..c9dd3cf1 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java @@ -0,0 +1,67 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioDiscardTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDiscardTests.java new file mode 100644 index 00000000..a8d1a7fe --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDiscardTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioEffectHandlerBreakTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffectHandlerBreakTests.java new file mode 100644 index 00000000..5becfc12 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioEffectHandlerBreakTests.java @@ -0,0 +1,31 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioFieldLoadTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java new file mode 100644 index 00000000..8b5db2b7 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java @@ -0,0 +1,88 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioIfTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIfTests.java new file mode 100644 index 00000000..8c409f93 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIfTests.java @@ -0,0 +1,84 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioIntAddTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntAddTests.java new file mode 100644 index 00000000..e8d2f255 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntAddTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioIntEqualsTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntEqualsTests.java new file mode 100644 index 00000000..25c315a2 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntEqualsTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntNotEqualTests.java new file mode 100644 index 00000000..9a02fda1 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntNotEqualTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioIntSubTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntSubTests.java new file mode 100644 index 00000000..5d958883 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioIntSubTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioLocalLoadTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalLoadTests.java new file mode 100644 index 00000000..56760ffa --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalLoadTests.java @@ -0,0 +1,25 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioLocalStoreTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalStoreTests.java new file mode 100644 index 00000000..5b070142 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLocalStoreTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioLoopBreakTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopBreakTests.java new file mode 100644 index 00000000..839d24d9 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopBreakTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioLoopContinueTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopContinueTests.java new file mode 100644 index 00000000..a12c1cc8 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopContinueTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioLoopTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopTests.java new file mode 100644 index 00000000..cd489cd3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioLoopTests.java @@ -0,0 +1,30 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java new file mode 100644 index 00000000..3d9d4691 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java @@ -0,0 +1,43 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioNamespaceImportTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceImportTests.java new file mode 100644 index 00000000..6ffc20c7 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceImportTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioNamespaceTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java new file mode 100644 index 00000000..6ceea279 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java @@ -0,0 +1,56 @@ +package org.shedlang.compiler.shidtopatio; + +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/shidtopatio/ShidToPatioPushBoolTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushBoolTests.java new file mode 100644 index 00000000..c1310e31 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushBoolTests.java @@ -0,0 +1,34 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioPushIntTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushIntTests.java new file mode 100644 index 00000000..17a6716c --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushIntTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioPushStringTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushStringTests.java new file mode 100644 index 00000000..65952ea0 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushStringTests.java @@ -0,0 +1,34 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioPushUnitTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushUnitTests.java new file mode 100644 index 00000000..27702e50 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioPushUnitTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioResumeTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioResumeTests.java new file mode 100644 index 00000000..87a4ca86 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioResumeTests.java @@ -0,0 +1,29 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioReturnTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioReturnTests.java new file mode 100644 index 00000000..ae8194ee --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioReturnTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioStringAddTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioStringAddTests.java new file mode 100644 index 00000000..0fe97285 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioStringAddTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioSwitchTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java new file mode 100644 index 00000000..f98e7ef6 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java @@ -0,0 +1,57 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioTesting.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java new file mode 100644 index 00000000..3f3df6d2 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java @@ -0,0 +1,41 @@ +package org.shedlang.compiler.shidtopatio; + +import org.shedlang.compiler.patio.ast.PatioSerializer; + +class ShidToPatioTesting { + private ShidToPatioTesting() {} + + static String serialize(ShidToPatioContext context) { + var builder = new StringBuilder(); + + builder.append("Compilation unit elements:\n\n"); + for (var element : context.compilationUnitContext().elementsByName().values()) { + PatioSerializer.serializeCompilationUnitElement(element, builder); + builder.append("\n"); + } + builder.append("\n"); + + builder.append("Locals:\n"); + for (var local : context.functionContext().localsByName().values()) { + builder.append(" * "); + PatioSerializer.serializeLocal(local, builder); + builder.append("\n"); + } + builder.append("\n"); + + builder.append("Instructions:\n\n"); + PatioSerializer.serializeInstructions( + context.functionContext().instructions(), + builder + ); + + builder.append("\n\nTemporary stack:\n"); + for (var value : context.functionContext().temporaryStack()) { + builder.append(" * "); + builder.append(value.toString()); + builder.append("\n"); + } + + return builder.toString(); + } +} diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java new file mode 100644 index 00000000..a89c5725 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java @@ -0,0 +1,80 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioTupleCreateTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTupleCreateTests.java new file mode 100644 index 00000000..0187d719 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTupleCreateTests.java @@ -0,0 +1,39 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioTupleGetTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTupleGetTests.java new file mode 100644 index 00000000..a19ac6e9 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTupleGetTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioVarLoadTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioVarLoadTests.java new file mode 100644 index 00000000..6aa7526a --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioVarLoadTests.java @@ -0,0 +1,26 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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/shidtopatio/ShidToPatioVarStoreTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioVarStoreTests.java new file mode 100644 index 00000000..03ab43ac --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioVarStoreTests.java @@ -0,0 +1,43 @@ +package org.shedlang.compiler.shidtopatio; + +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.shidtopatio.ShidToPatioTesting.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