aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-13 22:25:10 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-13 22:25:10 +0000
commit778f8d3a31286ab3cf8016289a35757f9aef808d (patch)
tree6333168316b32d915fc069a4cba6c9ccd529977b /src/test/java
parente67385e2086ae14c430865e4577986dacba72256 (diff)
Introduce ShidBlock
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java24
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java24
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java4
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java11
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java8
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java4
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java8
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java8
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java36
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java12
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java5
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java20
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java8
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java32
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java39
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java17
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/Shid.java14
17 files changed, 156 insertions, 118 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java
index 3fd914be..9c52db77 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java
@@ -25,10 +25,10 @@ public class PatioCodeGenDefineFunctionTests {
List.of(),
List.of(),
FreeVariables.empty(),
- List.of(
+ Shid.block(List.of(
Shid.pushUnit(),
Shid.return_()
- ),
+ )),
ShidDefineFunction.Variant.FUNCTION,
Sources.NULL
);
@@ -46,10 +46,10 @@ public class PatioCodeGenDefineFunctionTests {
List.of(),
List.of(),
FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
- List.of(
+ Shid.block(List.of(
Shid.localLoad("a"),
Shid.return_()
- ),
+ )),
ShidDefineFunction.Variant.FUNCTION,
Sources.NULL
);
@@ -69,10 +69,10 @@ public class PatioCodeGenDefineFunctionTests {
List.of(),
List.of(),
FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
- List.of(
+ Shid.block(List.of(
Shid.localLoad("a"),
Shid.return_()
- ),
+ )),
ShidDefineFunction.Variant.FUNCTION,
Sources.NULL
);
@@ -104,10 +104,10 @@ public class PatioCodeGenDefineFunctionTests {
.plus("a", Sources.NULL)
.plus("b", Sources.NULL)
.build(),
- List.of(
+ Shid.block(List.of(
Shid.localLoad("a"),
Shid.return_()
- ),
+ )),
ShidDefineFunction.Variant.FUNCTION,
Sources.NULL
);
@@ -130,14 +130,14 @@ public class PatioCodeGenDefineFunctionTests {
// 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(),
- List.of(
+ Shid.block(List.of(
Shid.localLoad("a"),
Shid.localLoad("param1"),
Shid.intAdd(),
Shid.localLoad("paramA"),
Shid.intAdd(),
Shid.return_()
- ),
+ )),
ShidDefineFunction.Variant.FUNCTION,
Sources.NULL
);
@@ -157,10 +157,10 @@ public class PatioCodeGenDefineFunctionTests {
List.of(),
List.of(),
FreeVariables.empty(),
- List.of(
+ Shid.block(List.of(
Shid.pushUnit(),
Shid.resume()
- ),
+ )),
ShidDefineFunction.Variant.EFFECT_HANDLER_METHOD,
Sources.NULL
);
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java
index 34edd733..9a385a1f 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenIfTests.java
@@ -18,14 +18,14 @@ public class PatioCodeGenIfTests {
public void canGenerateCodeForIfWithNoResults(Snapshotter snapshotter) throws IOException {
var instruction = Shid.if_(
0,
- List.of(
+ Shid.block(List.of(
Shid.pushInt(1),
Shid.discard()
- ),
- List.of(
+ )),
+ Shid.block(List.of(
Shid.pushInt(101),
Shid.discard()
- )
+ ))
);
var context = PatioCodeGenContext.EMPTY
.temporaryPush(new PatioLocalRef("other"))
@@ -40,16 +40,16 @@ public class PatioCodeGenIfTests {
public void canGenerateCodeForIfWithSingleResult(Snapshotter snapshotter) throws IOException {
var instruction = Shid.if_(
1,
- List.of(
+ Shid.block(List.of(
Shid.pushInt(1),
Shid.pushInt(2),
Shid.intAdd()
- ),
- List.of(
+ )),
+ Shid.block(List.of(
Shid.pushInt(101),
Shid.pushInt(102),
Shid.intAdd()
- )
+ ))
);
var context = PatioCodeGenContext.EMPTY
.temporaryPush(new PatioLocalRef("other"))
@@ -64,14 +64,14 @@ public class PatioCodeGenIfTests {
public void canGenerateCodeForIfWithMultipleResults(Snapshotter snapshotter) throws IOException {
var instruction = Shid.if_(
2,
- List.of(
+ Shid.block(List.of(
Shid.pushInt(1),
Shid.pushInt(2)
- ),
- List.of(
+ )),
+ Shid.block(List.of(
Shid.pushInt(101),
Shid.pushInt(102)
- )
+ ))
);
var context = PatioCodeGenContext.EMPTY
.temporaryPush(new PatioLocalRef("other"))
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java
index 6a9648db..67ab77b5 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenLoopTests.java
@@ -16,10 +16,10 @@ public class PatioCodeGenLoopTests {
@Test
public void canGenerateCodeForLoop(Snapshotter snapshotter) throws IOException {
var instruction = Shid.loop(
- List.of(
+ Shid.block(List.of(
Shid.pushInt(1),
Shid.discard()
- )
+ ))
);
var context = PatioCodeGenContext.EMPTY;
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java
index c7dcf094..057f56e5 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenNamespaceTests.java
@@ -3,11 +3,10 @@ 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.sources.Sources;
import org.shedlang.compiler.shid.ShidNamespace;
-import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
-import org.shedlang.compiler.shid.instructions.ShidInstruction;
import org.shedlang.compiler.shid.instructions.Shid;
+import org.shedlang.compiler.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;
@@ -29,14 +28,14 @@ public class PatioCodeGenNamespaceTests {
List.of(),
List.of(),
FreeVariables.empty(),
- List.of(
+ Shid.block(List.of(
Shid.pushUnit(),
Shid.return_()
- ),
+ )),
ShidDefineFunction.Variant.FUNCTION,
Sources.NULL
);
- var instructions = List.<ShidInstruction>of(instruction);
+ var instructions = Shid.block(List.of(instruction));
var namespaceName = NamespaceName.of("An","Example");
var typeInfos = TypeInfos.EMPTY
.addNamespaceTypeInfo(
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java
index 58463cb4..e9838702 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java
@@ -27,19 +27,19 @@ public class PatioCodeGenSwitchTests {
List.of(
Shid.switchCase(
structType1,
- List.of(
+ Shid.block(List.of(
Shid.pushInt(1),
Shid.pushInt(2),
Shid.intAdd()
- )
+ ))
),
Shid.switchCase(
structType2,
- List.of(
+ Shid.block(List.of(
Shid.pushInt(101),
Shid.pushInt(102),
Shid.intAdd()
- )
+ ))
)
)
);
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java
index f534534c..a0f71ed4 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenTryTests.java
@@ -23,9 +23,9 @@ public class PatioCodeGenTryTests {
var shidInstruction = Shid.try_(
effect,
List.of("a", "c", "b"),
- List.of(
+ Shid.block(List.of(
Shid.pushInt(42)
- )
+ ))
);
var typeInfos = TypeCheckerContext.EMPTY
.addUserDefinedEffectInfo(
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java
index 0f6a3835..e0c63896 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java
@@ -39,12 +39,12 @@ public class ShidCodeGenFunctionDefinitionTests {
has("freeVariables", x -> x.freeVariables(), containsExactly(
has("name", x -> x.name(), equalTo("z__0"))
)),
- has("body", x -> x.body(), isSequence(
+ has("body", x -> x.body(), isBlockInstruction(isSequence(
isPushStringInstruction("hello"),
isDiscardInstruction(),
isPushUnitInstruction(),
isReturnInstruction()
- ))
+ )))
),
isLocalStoreInstruction("f__0")
));
@@ -64,10 +64,10 @@ public class ShidCodeGenFunctionDefinitionTests {
assertThat(result.instructions(), isSequence(
instanceOf(
ShidDefineFunction.class,
- has("body", x -> x.body(), isSequence(
+ has("body", x -> x.body(), isBlockInstruction(isSequence(
isPushStringInstruction("hello"),
isReturnInstruction()
- ))
+ )))
),
isLocalStoreInstruction("f__0")
));
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java
index c5f78340..2783a389 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java
@@ -38,12 +38,12 @@ public class ShidCodeGenFunctionExpressionTests {
has("freeVariables", x -> x.freeVariables(), containsExactly(
has("name", x -> x.name(), equalTo("z__0"))
)),
- has("body", x -> x.body(), isSequence(
+ has("body", x -> x.body(), isBlockInstruction(isSequence(
isPushStringInstruction("hello"),
isDiscardInstruction(),
isPushUnitInstruction(),
isReturnInstruction()
- ))
+ )))
)
));
}
@@ -61,10 +61,10 @@ public class ShidCodeGenFunctionExpressionTests {
assertThat(result.instructions(), isSequence(
instanceOf(
ShidDefineFunction.class,
- has("body", x -> x.body(), isSequence(
+ has("body", x -> x.body(), isBlockInstruction(isSequence(
isPushStringInstruction("hello"),
isReturnInstruction()
- ))
+ )))
)
));
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java
index f1f81e95..df07214d 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java
@@ -35,13 +35,15 @@ public class ShidCodeGenIfTests {
assertThat(result.instructions(), isSequence(
isLocalLoadInstruction("c__0"),
isIfInstruction(
- isSequence(
+ isBlockInstruction(isSequence(
isPushIntInstruction(42)
- ),
- isSequence(
- isPushStringInstruction("x"),
- isReturnInstruction()
- )
+ )),
+ isBlockInstruction(isSequence(
+ isBlockInstruction(isSequence(
+ isPushStringInstruction("x"),
+ isReturnInstruction()
+ ))
+ ))
)
));
}
@@ -77,21 +79,23 @@ public class ShidCodeGenIfTests {
assertThat(result.instructions(), isSequence(
isLocalLoadInstruction("c__0"),
isIfInstruction(
- isSequence(
+ isBlockInstruction(isSequence(
isPushIntInstruction(42)
- ),
- isSequence(
+ )),
+ isBlockInstruction(isSequence(
isLocalLoadInstruction("d__0"),
isIfInstruction(
- isSequence(
+ isBlockInstruction(isSequence(
isPushIntInstruction(1)
- ),
- isSequence(
- isPushStringInstruction("x"),
- isReturnInstruction()
- )
+ )),
+ isBlockInstruction(isSequence(
+ isBlockInstruction(isSequence(
+ isPushStringInstruction("x"),
+ isReturnInstruction()
+ ))
+ ))
)
- )
+ ))
)
));
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
index f56ba972..af0bae77 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
@@ -33,7 +33,7 @@ public class ShidCodeGenNamespaceTests {
var shidNamespace = ShidCodeGen.compileNamespace(node);
- assertThat(shidNamespace.instructions(), isSequence(
+ assertThat(shidNamespace.body(), isBlockInstruction(isSequence(
// TODO: test the builtin loading properly
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
@@ -46,7 +46,7 @@ public class ShidCodeGenNamespaceTests {
isLocalStoreInstruction("f__0"),
isLocalLoadInstruction("f__0"),
isNamespaceFieldStoreInstruction(namespaceName, "f")
- ));
+ )));
}
@Test
@@ -60,7 +60,7 @@ public class ShidCodeGenNamespaceTests {
var shidNamespace = ShidCodeGen.compileNamespace(node);
- assertThat(shidNamespace.instructions(), isSequence(
+ assertThat(shidNamespace.body(), isBlockInstruction(isSequence(
// TODO: test the builtin loading properly
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
@@ -70,7 +70,7 @@ public class ShidCodeGenNamespaceTests {
isLocalStoreInstruction("A__0"),
isNamespaceImportInstruction(NamespaceName.of("B")),
isLocalStoreInstruction("B__0")
- ));
+ )));
}
@Test
@@ -93,7 +93,7 @@ public class ShidCodeGenNamespaceTests {
var shidNamespace = ShidCodeGen.compileNamespace(node);
- assertThat(shidNamespace.instructions(), isSequence(
+ assertThat(shidNamespace.body(), isBlockInstruction(isSequence(
// TODO: test the builtin loading properly
instanceOf(ShidNamespaceImport.class),
instanceOf(ShidFieldLoad.class),
@@ -113,6 +113,6 @@ public class ShidCodeGenNamespaceTests {
isNamespaceFieldStoreInstruction(namespaceName, "f"),
isLocalLoadInstruction("None__0"),
isNamespaceFieldStoreInstruction(namespaceName, "None")
- ));
+ )));
}
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java
index daff19b0..ddcf1d08 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java
@@ -9,6 +9,7 @@ import org.shedlang.compiler.shid.instructions.ShidTry;
import org.shedlang.compiler.types.Effects;
import org.shedlang.compiler.types.NamespaceName;
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isBlockInstruction;
import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushIntInstruction;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -54,9 +55,9 @@ public class ShidCodeGenTryTests {
equalTo("read"),
equalTo("write")
)),
- has("body", x -> x.body(), isSequence(
+ has("body", x -> x.body(), isBlockInstruction(isSequence(
isPushIntInstruction(42)
- ))
+ )))
)
));
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java
index 5007e1e8..45f29904 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java
@@ -83,20 +83,22 @@ public class ShidCodeGenValTests {
isDiscardInstruction(),
isPushBoolInstruction(true),
isIfInstruction(
- isSequence(
+ isBlockInstruction(isSequence(
isPushIntInstruction(11),
isLocalStoreInstruction("x__1"),
isLocalLoadInstruction("x__1"),
isDiscardInstruction(),
isPushUnitInstruction()
- ),
- isSequence(
- isPushIntInstruction(12),
- isLocalStoreInstruction("x__2"),
- isLocalLoadInstruction("x__2"),
- isDiscardInstruction(),
- isPushUnitInstruction()
- )
+ )),
+ isBlockInstruction(isSequence(
+ isBlockInstruction(isSequence(
+ isPushIntInstruction(12),
+ isLocalStoreInstruction("x__2"),
+ isLocalLoadInstruction("x__2"),
+ isDiscardInstruction(),
+ isPushUnitInstruction()
+ ))
+ ))
),
isDiscardInstruction(),
isLocalLoadInstruction("x__0"),
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java
index f3e2880c..a7abfc69 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java
@@ -45,15 +45,15 @@ public class ShidCodeGenWhenTests {
isSequence(
isSwitchInstructionCase(
Types.struct(NAMESPACE_NAME, "A"),
- isSequence(
+ isBlockInstruction(isSequence(
isPushStringInstruction("a")
- )
+ ))
),
isSwitchInstructionCase(
Types.struct(NAMESPACE_NAME, "B"),
- isSequence(
+ isBlockInstruction(isSequence(
isPushStringInstruction("b")
- )
+ ))
)
)
)
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java
index 1ab5240a..3307db91 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java
@@ -25,18 +25,20 @@ public class ShidCodeGenWhileTests {
assertThat(result.instructions(), isSequence(
isLoopInstruction(
- isSequence(
+ isBlockInstruction(isSequence(
isLocalLoadInstruction("c__0"),
isIfInstruction(
- isSequence(),
- isSequence(
+ isBlockInstruction(isSequence()),
+ isBlockInstruction(isSequence(
isLoopBreakInstruction()
- )
+ ))
),
- isPushIntInstruction(42),
- isDiscardInstruction(),
+ isBlockInstruction(isSequence(
+ isPushIntInstruction(42),
+ isDiscardInstruction()
+ )),
isLoopContinueInstruction()
- )
+ ))
)
));
}
@@ -58,18 +60,20 @@ public class ShidCodeGenWhileTests {
assertThat(result.instructions(), isSequence(
isLoopInstruction(
- isSequence(
+ isBlockInstruction(isSequence(
isLocalLoadInstruction("c__0"),
isIfInstruction(
- isSequence(),
- isSequence(
+ isBlockInstruction(isSequence()),
+ isBlockInstruction(isSequence(
isLoopBreakInstruction()
- )
+ ))
),
- isPushIntInstruction(42),
- isDiscardInstruction(),
+ isBlockInstruction(isSequence(
+ isPushIntInstruction(42),
+ isDiscardInstruction()
+ )),
isLoopContinueInstruction()
- )
+ ))
)
));
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java
index 62346f3b..0076b982 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java
@@ -15,6 +15,21 @@ import static org.zwobble.precisely.Matchers.equalTo;
public class ShidSerializerTests {
@Test
+ public void canSerializeBlock() {
+ var instruction = Shid.block(List.of(Shid.pushInt(1)));
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ block
+ (
+ (push-int 1)
+ )
+ )"""));
+ }
+
+ @Test
public void canSerializeCall() {
var instruction = Shid.call(1, List.of("x", "y"));
@@ -156,14 +171,14 @@ public class ShidSerializerTests {
public void canSerializeIf() {
var instruction = Shid.if_(
1,
- List.of(
+ Shid.block(List.of(
Shid.pushUnit()
- ),
- List.of(
+ )),
+ Shid.block(List.of(
Shid.localLoad("x"),
Shid.discard(),
Shid.pushBool(true)
- )
+ ))
);
var string = serialize(instruction);
@@ -249,10 +264,10 @@ public class ShidSerializerTests {
@Test
public void canSerializeLoop() {
var instruction = Shid.loop(
- List.of(
+ Shid.block(List.of(
Shid.localLoad("x"),
Shid.discard()
- )
+ ))
);
var string = serialize(instruction);
@@ -385,15 +400,15 @@ public class ShidSerializerTests {
var instruction = Shid.switch_(List.of(
Shid.switchCase(
Types.struct(NamespaceName.of("Example", "Project"), "A"),
- List.of(
+ Shid.block(List.of(
Shid.return_()
- )
+ ))
),
Shid.switchCase(
Types.struct(NamespaceName.of("Example", "Project"), "B"),
- List.of(
+ Shid.block(List.of(
Shid.discard()
- )
+ ))
)
));
@@ -425,9 +440,9 @@ public class ShidSerializerTests {
var instruction = Shid.try_(
effect,
List.of("read", "write"),
- List.of(
+ Shid.block(List.of(
Shid.pushUnit()
- )
+ ))
);
var string = serialize(instruction);
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java
index ed7b1e83..6e57a4b2 100644
--- a/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java
@@ -14,6 +14,15 @@ public class InstructionMatchers {
private InstructionMatchers() {
}
+ public static Matcher<ShidInstruction> isBlockInstruction(
+ Matcher<Iterable<? extends ShidInstruction>> instructions
+ ) {
+ return instanceOf(
+ ShidBlock.class,
+ has("instructions", x -> x.instructions(), instructions)
+ );
+ }
+
public static Matcher<ShidInstruction> isBreakInstruction(UserDefinedEffect effect) {
return instanceOf(
ShedEffectHandlerBreak.class,
@@ -68,8 +77,8 @@ public class InstructionMatchers {
}
public static Matcher<ShidInstruction> isIfInstruction(
- Matcher<Iterable<? extends ShidInstruction>> trueBody,
- Matcher<Iterable<? extends ShidInstruction>> falseBody
+ Matcher<? super ShidBlock> trueBody,
+ Matcher<? super ShidBlock> falseBody
) {
return instanceOf(
ShidIf.class,
@@ -109,7 +118,7 @@ public class InstructionMatchers {
}
public static Matcher<ShidInstruction> isLoopInstruction(
- Matcher<Iterable<? extends ShidInstruction>> body
+ Matcher<? super ShidBlock> body
) {
return instanceOf(
ShidLoop.class,
@@ -199,7 +208,7 @@ public class InstructionMatchers {
public static Matcher<ShidSwitch.Case> isSwitchInstructionCase(
StructType caseType,
- Matcher<Iterable<? extends ShidInstruction>> body
+ Matcher<? super ShidBlock> body
) {
return allOf(
has("caseType", x -> x.caseType(), equalTo(caseType)),
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
index a30b806b..f3ba5614 100644
--- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
@@ -12,6 +12,10 @@ public class Shid {
private Shid() {
}
+ public static ShidBlock block(List<ShidInstruction> instructions) {
+ return new ShidBlock(instructions, Sources.NULL);
+ }
+
public static ShidInstruction call(int positionalArgCount, List<String> namedArgNames) {
return new ShidCall(positionalArgCount, namedArgNames, Sources.NULL);
}
@@ -73,8 +77,8 @@ public class Shid {
public static ShidInstruction if_(
int resultCount,
- List<ShidInstruction> trueBody,
- List<ShidInstruction> falseBody
+ ShidBlock trueBody,
+ ShidBlock falseBody
) {
return new ShidIf(resultCount, trueBody, falseBody, Sources.NULL);
}
@@ -107,7 +111,7 @@ public class Shid {
return new ShidLocalStore(name, Sources.NULL);
}
- public static ShidInstruction loop(List<ShidInstruction> body) {
+ public static ShidInstruction loop(ShidBlock body) {
return new ShidLoop(body, Sources.NULL);
}
@@ -165,7 +169,7 @@ public class Shid {
public static ShidSwitch.Case switchCase(
StructType type,
- List<ShidInstruction> body
+ ShidBlock body
) {
return new ShidSwitch.Case(type, body, Sources.NULL);
}
@@ -173,7 +177,7 @@ public class Shid {
public static ShidInstruction try_(
UserDefinedEffect handledEffect,
List<String> handlerMethodNames,
- List<ShidInstruction> body
+ ShidBlock body
) {
return new ShidTry(
handledEffect,