aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shid
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-02 19:47:35 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-02 19:47:35 +0000
commit07d62885c5150fe6eadfa9372a190dd73fd754cc (patch)
tree17959fa67c7ab5d375f3966003358afc0715b210 /src/test/java/org/shedlang/compiler/shid
parent0ad5be30c1d301942f8cec567d0719fd9ff18f3f (diff)
Rename stack IR to Shid
Diffstat (limited to 'src/test/java/org/shedlang/compiler/shid')
-rw-r--r--src/test/java/org/shedlang/compiler/shid/InstructionSerializerTests.java469
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenAssignTests.java94
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenBlockReturnTests.java22
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenBoolLiteralTests.java22
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java41
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenEffectDefinitionTests.java31
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenExpressionStatementTests.java24
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java78
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java75
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java71
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionReturnTests.java23
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java98
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntAddTests.java28
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntEqualsTests.java27
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLiteralTests.java22
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntNotEqualTests.java27
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntSubTests.java27
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java118
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenReferenceTests.java43
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenResumeTests.java23
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringAddTests.java27
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringLiteralTests.java22
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java40
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenSumTypeDefinitionTests.java30
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java63
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenTupleTests.java31
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenUnitLiteralTests.java22
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java106
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenVarTests.java24
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java62
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java76
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java229
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/Shid.java193
33 files changed, 2288 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/shid/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/shid/InstructionSerializerTests.java
new file mode 100644
index 00000000..9f6bdcf1
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/InstructionSerializerTests.java
@@ -0,0 +1,469 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+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.types.Effects;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import java.util.List;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class InstructionSerializerTests {
+ @Test
+ public void canSerializeCall() {
+ var instruction = Shid.call(1, List.of("x", "y"));
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(call positional-arg-count=1 named-arg-names=(\"x\" \"y\"))"));
+ }
+
+ @Test
+ public void canSerializeDefineEffect() {
+ var instruction = Shid.defineEffect(
+ Effects.userDefined(NamespaceName.of("Example", "Project"), "State")
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (define-effect "Example/Project.State")"""
+ ));
+ }
+
+ @Test
+ public void canSerializeDefineFunction() {
+ var instruction = ShidDefineFunction.builder()
+ .withName("f")
+ .addPositionalParam("p1")
+ .addPositionalParam("p2")
+ .addNamedParam("n1")
+ .addNamedParam("n2")
+ .addFreeVariable("fv1")
+ .addFreeVariable("fv2")
+ .addBodyInstruction(Shid.pushBool(true))
+ .addBodyInstruction(Shid.pushBool(false))
+ .build();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ define-function
+ name="f"
+ positional-params=("p1" "p2")
+ named-params=("n1" "n2")
+ free-variables=("fv1" "fv2")
+ body=(
+ (push-bool true)
+ (push-bool false)
+ )
+ )"""));
+ }
+
+ @Test
+ public void canSerializeDefineStruct() {
+ var instruction = Shid.defineStruct(
+ Types.struct(NamespaceName.of("Example", "Project"), "User"),
+ List.of(
+ Shid.defineStructField("name"),
+ Shid.defineStructField("id")
+ )
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ define-struct
+ type="Example/Project.User"
+ fields=("name" "id")
+ )"""));
+ }
+
+ @Test
+ public void canSerializeDiscard() {
+ var instruction = Shid.discard();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(discard)"));
+ }
+
+ @Test
+ public void canSerializeDuplicate() {
+ var instruction = Shid.duplicate();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(duplicate)"));
+ }
+
+ @Test
+ public void canSerializeNamespaceFieldLoad() {
+ var instruction = Shid.fieldLoad(
+ new NamespaceName(List.of("Example", "Lib")),
+ "x"
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(field-load (namespace \"Example/Lib\") \"x\")"));
+ }
+
+ @Test
+ public void canSerializeStructFieldLoad() {
+ var instruction = Shid.fieldLoad(
+ Types.struct(NamespaceName.of("Example", "Project"), "User"),
+ "name"
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(field-load (struct \"Example/Project.User\") \"name\")"));
+ }
+
+ @Test
+ public void canSerializeUserDefinedEffectFieldLoad() {
+ var instruction = Shid.fieldLoad(
+ Effects.userDefined(NamespaceName.of("Example", "Project"), "State"),
+ "read"
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(field-load (effect \"Example/Project.State\") \"read\")"));
+ }
+
+ @Test
+ public void canSerializeIf() {
+ var instruction = Shid.if_(
+ 1,
+ List.of(
+ Shid.pushUnit()
+ ),
+ List.of(
+ Shid.localLoad("x"),
+ Shid.discard(),
+ Shid.pushBool(true)
+ )
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ if
+ result-count=1
+ (
+ (push-unit)
+ )
+ (
+ (local-load "x")
+ (discard)
+ (push-bool true)
+ )
+ )"""));
+ }
+
+ @Test
+ public void canSerializeIntAdd() {
+ var instruction = Shid.intAdd();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-add)"));
+ }
+
+ @Test
+ public void canSerializeIntEquals() {
+ var instruction = Shid.intEquals();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-eq)"));
+ }
+
+ @Test
+ public void canSerializeIntNotEqual() {
+ var instruction = Shid.intNotEqual();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-ne)"));
+ }
+
+ @Test
+ public void canSerializeIntSub() {
+ var instruction = Shid.intSub();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(int-sub)"));
+ }
+
+ @Test
+ public void canSerializeLocalLoad() {
+ var instruction = Shid.localLoad("x");
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(local-load \"x\")"));
+ }
+
+ @Test
+ public void canSerializeLocalStore() {
+ var instruction = Shid.localStore("x");
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(local-store \"x\")"));
+ }
+
+ @Test
+ public void canSerializeLoop() {
+ var instruction = Shid.loop(
+ List.of(
+ Shid.localLoad("x"),
+ Shid.discard()
+ )
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ loop
+ (
+ (local-load "x")
+ (discard)
+ )
+ )"""));
+ }
+
+ @Test
+ public void canSerializeLoopBreak() {
+ var instruction = Shid.loopBreak();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(break)"));
+ }
+
+ @Test
+ public void canSerializeLoopContinue() {
+ var instruction = Shid.loopContinue();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(continue)"));
+ }
+
+ @Test
+ public void canSerializeNamespaceFieldStore() {
+ var instruction = Shid.namespaceFieldStore(
+ new NamespaceName(List.of("Example", "Lib")),
+ "x"
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(namespace-field-store \"Example/Lib\" \"x\")"));
+ }
+
+ @Test
+ public void canSerializeNamespaceImport() {
+ var instruction = Shid.namespaceImport(
+ new NamespaceName(List.of("Example", "Lib"))
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(namespace-import \"Example/Lib\")"));
+ }
+
+ @Test
+ public void canSerializePushBool() {
+ var instruction = Shid.pushBool(true);
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(push-bool true)"));
+ }
+
+ @Test
+ public void canSerializePushInt() {
+ var instruction = Shid.pushInt(42);
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(push-int 42)"));
+ }
+
+ @Test
+ public void canSerializePushString() {
+ var instruction = Shid.pushString("hello");
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(push-string \"hello\")"));
+ }
+
+ @Test
+ public void specialCharactersInStringAreEscaped() {
+ var instruction = Shid.pushString("\b\t\n\f\r\"\\");
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(push-string \"\\b\\t\\n\\f\\r\\\"\\\\\")"));
+ }
+
+ @Test
+ public void canSerializePushUnit() {
+ var instruction = Shid.pushUnit();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(push-unit)"));
+ }
+
+ @Test
+ public void canSerializeResume() {
+ var instruction = Shid.resume();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(resume)"));
+ }
+
+ @Test
+ public void canSerializeReturn() {
+ var instruction = Shid.return_();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(return)"));
+ }
+
+ @Test
+ public void canSerializeStringAdd() {
+ var instruction = Shid.stringAdd();
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(string-add)"));
+ }
+
+ @Test
+ public void canSerializeSwitch() {
+ var instruction = Shid.switch_(List.of(
+ Shid.switchCase(
+ Types.struct(NamespaceName.of("Example", "Project"), "A"),
+ List.of(
+ Shid.return_()
+ )
+ ),
+ Shid.switchCase(
+ Types.struct(NamespaceName.of("Example", "Project"), "B"),
+ List.of(
+ Shid.discard()
+ )
+ )
+ ));
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ switch
+ (
+ (
+ "Example/Project.A"
+ (
+ (return)
+ )
+ )
+ (
+ "Example/Project.B"
+ (
+ (discard)
+ )
+ )
+ )
+ )"""
+ ));
+ }
+ @Test
+ public void canSerializeTry() {
+ var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State");
+ var instruction = Shid.try_(
+ effect,
+ List.of("read", "write"),
+ List.of(
+ Shid.pushUnit()
+ )
+ );
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ try
+ "Example/Project.State"
+ (
+ "read"
+ "write"
+ )
+ (
+ (push-unit)
+ )
+ )"""));
+ }
+
+ @Test
+ public void canSerializeTupleCreate() {
+ var instruction = Shid.tupleCreate(2);
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(tuple-create 2)"));
+ }
+
+ @Test
+ public void canSerializeTupleGet() {
+ var instruction = Shid.tupleGet(2, 3);
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(tuple-get 2 3)"));
+ }
+
+ @Test
+ public void canSerializeVarLoad() {
+ var instruction = Shid.varLoad("x");
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(var-load \"x\")"));
+ }
+
+ @Test
+ public void canSerializeVarStore() {
+ var instruction = Shid.varStore("x");
+
+ var string = serialize(instruction);
+
+ assertThat(string, equalTo("(var-store \"x\")"));
+ }
+
+ private String serialize(ShidInstruction instruction) {
+ var builder = new StringBuilder();
+ InstructionSerializer.serializeInstruction(instruction, builder);
+ return builder.toString();
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenAssignTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenAssignTests.java
new file mode 100644
index 00000000..65e30e5a
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenAssignTests.java
@@ -0,0 +1,94 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenAssignTests {
+ @Test
+ public void targetIgnoreIsCompiledToDiscard() {
+ var node = Typed.assign(
+ Typed.targetIgnore(),
+ Typed.reference("x")
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("x__0"),
+ isDiscardInstruction()
+ ));
+ }
+
+ @Test
+ public void whenTargetIsReassignThenExpressionIsEvaluatedAndStoredAsVar() {
+ var node = Typed.assign(
+ Typed.targetReassign("x"),
+ Typed.stringLiteral("hello")
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello"),
+ isVarStoreInstruction("x__0")
+ ));
+ }
+
+ @Test
+ public void whenTargetIsTupleThenExpressionIsDeconstructed() {
+ var node = Typed.assign(
+ Typed.targetTuple(List.of(
+ Typed.targetVal("a"),
+ Typed.targetVal("b"),
+ Typed.targetVal("c")
+ )),
+ Typed.reference("x")
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x")
+ .addShedVal("a")
+ .addShedVal("b")
+ .addShedVal("c");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("x__0"),
+ isDuplicateInstruction(),
+ isTupleGetInstruction(0),
+ isLocalStoreInstruction("a__0"),
+ isDuplicateInstruction(),
+ isTupleGetInstruction(1),
+ isLocalStoreInstruction("b__0"),
+ isTupleGetInstruction(2),
+ isLocalStoreInstruction("c__0")
+ ));
+ }
+
+ @Test
+ public void deconstructingEmptyTupleDiscardsExpression() {
+ var node = Typed.assign(
+ Typed.targetTuple(List.of()),
+ Typed.reference("x")
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("x__0"),
+ isDiscardInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBlockReturnTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBlockReturnTests.java
new file mode 100644
index 00000000..082560b7
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBlockReturnTests.java
@@ -0,0 +1,22 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushStringInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenBlockReturnTests {
+ @Test
+ public void expressionIsEvaluated() {
+ var node = Typed.blockReturn(Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBoolLiteralTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBoolLiteralTests.java
new file mode 100644
index 00000000..965f9b3c
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenBoolLiteralTests.java
@@ -0,0 +1,22 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushBoolInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenBoolLiteralTests {
+ @Test
+ public void boolLiteralIsCompiledToPushBoolInstruction() {
+ var node = Typed.boolLiteral(true);
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushBoolInstruction(true)
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java
new file mode 100644
index 00000000..5d14cc31
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenCallTests.java
@@ -0,0 +1,41 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenCallTests {
+ @Test
+ public void callIsCompiledToInstructionsForOperandThenCall() {
+ var node = Typed.call(
+ Typed.reference("f"),
+ List.of(
+ Typed.stringLiteral("p1"),
+ Typed.stringLiteral("p2")
+
+ ),
+ List.of(
+ Typed.namedArg("x2", Typed.stringLiteral("n2")),
+ Typed.namedArg("x1", Typed.stringLiteral("n1"))
+ )
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("f");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("f__0"),
+ isPushStringInstruction("p1"),
+ isPushStringInstruction("p2"),
+ isPushStringInstruction("n2"),
+ isPushStringInstruction("n1"),
+ isCallInstruction(2, List.of("x2", "x1"))
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenEffectDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenEffectDefinitionTests.java
new file mode 100644
index 00000000..4777f5a7
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenEffectDefinitionTests.java
@@ -0,0 +1,31 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.shid.instructions.ShidDefineEffect;
+import org.shedlang.compiler.types.Effects;
+import org.shedlang.compiler.types.NamespaceName;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isLocalStoreInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ShidCodeGenEffectDefinitionTests {
+ @Test
+ public void effectDefinitionIsCompiledToDefineEffectInstruction() {
+ var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State");
+ var node = Typed.effectDefinition(effect);
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("State");
+
+ var result = ShidCodeGen.compileNamespaceStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineEffect.class,
+ has("effect", x -> x.effect(), equalTo(effect))
+ ),
+ isLocalStoreInstruction("State__0")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenExpressionStatementTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenExpressionStatementTests.java
new file mode 100644
index 00000000..05a47d39
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenExpressionStatementTests.java
@@ -0,0 +1,24 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isDiscardInstruction;
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushStringInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenExpressionStatementTests {
+ @Test
+ public void expressionIsEvaluatedAndTheResultDiscarded() {
+ var node = Typed.expressionStatement(Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello"),
+ isDiscardInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java
new file mode 100644
index 00000000..0e869445
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java
@@ -0,0 +1,78 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.shid.instructions.ShidFieldLoad;
+import org.shedlang.compiler.types.Effects;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isFieldLoadInstruction;
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isLocalLoadInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenFieldAccessTests {
+ @Test
+ public void namespaceFieldAccessIsCompiledToFieldLoad() {
+ var namespaceType = Types.namespace(NamespaceName.of("Example", "Project"));
+ var node = Typed.fieldAccess(
+ Typed.reference("Project", namespaceType),
+ "name",
+ Types.STRING
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("Project");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("Project__0"),
+ isFieldLoadInstruction(
+ new ShidFieldLoad.Receiver.Namespace(namespaceType.namespaceName()),
+ "name"
+ )
+ ));
+ }
+
+ @Test
+ public void structFieldAccessIsCompiledToFieldLoad() {
+ var structType = Types.struct(NamespaceName.of("Example", "Project"), "User");
+ var node = Typed.fieldAccess(
+ Typed.reference("user", structType),
+ "name",
+ Types.STRING
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("user");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("user__0"),
+ isFieldLoadInstruction(structType, "name")
+ ));
+ }
+
+ @Test
+ public void userDefinedEffectFieldAccessIsCompiledToFieldLoad() {
+ var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State");
+ var node = Typed.fieldAccess(
+ Typed.reference("State", Types.effectType(effect)),
+ "read",
+ Types.STRING
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("State");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("State__0"),
+ isFieldLoadInstruction(
+ new ShidFieldLoad.Receiver.Effect(effect),
+ "read"
+ )
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java
new file mode 100644
index 00000000..0f6a3835
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionDefinitionTests.java
@@ -0,0 +1,75 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode;
+import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
+import org.shedlang.compiler.types.Types;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ShidCodeGenFunctionDefinitionTests {
+ @Test
+ public void functionIsCompiledToDefineFunctionInstruction() {
+ var node = TypedFunctionDefinitionNode.builder()
+ .withName("f")
+ .addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING))
+ .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT))
+ .addFreeVariable("z")
+ .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello")))
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("f")
+ .addShedVal("z");
+
+ var result = ShidCodeGen.compileNamespaceStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("f")),
+ has("positionalParams", x -> x.positionalParams(), isSequence(
+ isDefineFunctionInstructionParam("x__0")
+ )),
+ has("namedParams", x -> x.namedParams(), isSequence(
+ isDefineFunctionInstructionParam("y__0")
+ )),
+ has("freeVariables", x -> x.freeVariables(), containsExactly(
+ has("name", x -> x.name(), equalTo("z__0"))
+ )),
+ has("body", x -> x.body(), isSequence(
+ isPushStringInstruction("hello"),
+ isDiscardInstruction(),
+ isPushUnitInstruction(),
+ isReturnInstruction()
+ ))
+ ),
+ isLocalStoreInstruction("f__0")
+ ));
+ }
+
+ @Test
+ public void whenLastStatementIsBlockReturnThenThatValueIsReturned() {
+ var node = TypedFunctionDefinitionNode.builder()
+ .withName("f")
+ .addStatement(Typed.blockReturn(Typed.stringLiteral("hello")))
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("f");
+
+ var result = ShidCodeGen.compileNamespaceStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineFunction.class,
+ has("body", x -> x.body(), 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
new file mode 100644
index 00000000..c5f78340
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionExpressionTests.java
@@ -0,0 +1,71 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.ast.typed.TypedFunctionExpressionNode;
+import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
+import org.shedlang.compiler.types.Types;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ShidCodeGenFunctionExpressionTests {
+ @Test
+ public void functionIsCompiledToDefineFunctionInstruction() {
+ var node = TypedFunctionExpressionNode.builder()
+ .addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING))
+ .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT))
+ .addFreeVariable("z")
+ .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello")))
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("f")
+ .addShedVal("z");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("anonymous")),
+ has("positionalParams", x -> x.positionalParams(), isSequence(
+ isDefineFunctionInstructionParam("x__0")
+ )),
+ has("namedParams", x -> x.namedParams(), isSequence(
+ isDefineFunctionInstructionParam("y__0")
+ )),
+ has("freeVariables", x -> x.freeVariables(), containsExactly(
+ has("name", x -> x.name(), equalTo("z__0"))
+ )),
+ has("body", x -> x.body(), isSequence(
+ isPushStringInstruction("hello"),
+ isDiscardInstruction(),
+ isPushUnitInstruction(),
+ isReturnInstruction()
+ ))
+ )
+ ));
+ }
+
+ @Test
+ public void whenLastStatementIsBlockReturnThenThatValueIsReturned() {
+ var node = TypedFunctionExpressionNode.builder()
+ .addStatement(Typed.blockReturn(Typed.stringLiteral("hello")))
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("f");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineFunction.class,
+ has("body", x -> x.body(), isSequence(
+ isPushStringInstruction("hello"),
+ isReturnInstruction()
+ ))
+ )
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionReturnTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionReturnTests.java
new file mode 100644
index 00000000..d6a63c1f
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFunctionReturnTests.java
@@ -0,0 +1,23 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenFunctionReturnTests {
+ @Test
+ public void expressionIsEvaluatedAndReturned() {
+ var node = Typed.functionReturn(Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), 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
new file mode 100644
index 00000000..f1f81e95
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIfTests.java
@@ -0,0 +1,98 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.types.Types;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenIfTests {
+ @Test
+ public void ifWithSingleConditionalBranchIsCompiledToSingleIfInstruction() {
+ var node = Typed.if_(
+ List.of(
+ Typed.conditionalBranch(
+ Typed.reference("c"),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.intLiteral(42))
+ ))
+ )
+ ),
+ Typed.block(List.of(
+ Typed.functionReturn(Typed.stringLiteral("x"))
+ )),
+ Types.INT
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("c");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("c__0"),
+ isIfInstruction(
+ isSequence(
+ isPushIntInstruction(42)
+ ),
+ isSequence(
+ isPushStringInstruction("x"),
+ isReturnInstruction()
+ )
+ )
+ ));
+ }
+
+ @Test
+ public void ifWithMultipleConditionalBranchesIsCompiledToNestedIfInstructions() {
+ var node = Typed.if_(
+ List.of(
+ Typed.conditionalBranch(
+ Typed.reference("c"),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.intLiteral(42))
+ ))
+ ),
+ Typed.conditionalBranch(
+ Typed.reference("d"),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.intLiteral(1))
+ ))
+ )
+ ),
+ Typed.block(List.of(
+ Typed.functionReturn(Typed.stringLiteral("x"))
+ )),
+ Types.INT
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("c")
+ .addShedVal("d");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("c__0"),
+ isIfInstruction(
+ isSequence(
+ isPushIntInstruction(42)
+ ),
+ isSequence(
+ isLocalLoadInstruction("d__0"),
+ isIfInstruction(
+ isSequence(
+ isPushIntInstruction(1)
+ ),
+ isSequence(
+ isPushStringInstruction("x"),
+ isReturnInstruction()
+ )
+ )
+ )
+ )
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntAddTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntAddTests.java
new file mode 100644
index 00000000..4ff0e6eb
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntAddTests.java
@@ -0,0 +1,28 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isIntAddInstruction;
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushIntInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenIntAddTests {
+ @Test
+ public void intAddIsCompiledToIntAddInstruction() {
+ var node = Typed.intAdd(
+ Typed.intLiteral(42),
+ Typed.intLiteral(47)
+ );
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushIntInstruction(42),
+ isPushIntInstruction(47),
+ isIntAddInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntEqualsTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntEqualsTests.java
new file mode 100644
index 00000000..464e73f4
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntEqualsTests.java
@@ -0,0 +1,27 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenIntEqualsTests {
+ @Test
+ public void intEqualsIsCompiledToIntEqualsInstruction() {
+ var node = Typed.intEquals(
+ Typed.intLiteral(42),
+ Typed.intLiteral(47)
+ );
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushIntInstruction(42),
+ isPushIntInstruction(47),
+ isIntEqualsInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLiteralTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLiteralTests.java
new file mode 100644
index 00000000..ed95e765
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntLiteralTests.java
@@ -0,0 +1,22 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushIntInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenIntLiteralTests {
+ @Test
+ public void intLiteralIsCompiledToPushIntInstruction() {
+ var node = Typed.intLiteral(42);
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushIntInstruction(42)
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntNotEqualTests.java
new file mode 100644
index 00000000..9c55f222
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntNotEqualTests.java
@@ -0,0 +1,27 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenIntNotEqualTests {
+ @Test
+ public void intNotEqualsIsCompiledToIntNotEqualInstruction() {
+ var node = Typed.intNotEqual(
+ Typed.intLiteral(42),
+ Typed.intLiteral(47)
+ );
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushIntInstruction(42),
+ isPushIntInstruction(47),
+ isIntNotEqualInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntSubTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntSubTests.java
new file mode 100644
index 00000000..df3e9791
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenIntSubTests.java
@@ -0,0 +1,27 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenIntSubTests {
+ @Test
+ public void intSubIsCompiledToIntSubInstruction() {
+ var node = Typed.intSub(
+ Typed.intLiteral(42),
+ Typed.intLiteral(47)
+ );
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushIntInstruction(42),
+ isPushIntInstruction(47),
+ isIntSubInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
new file mode 100644
index 00000000..f56ba972
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java
@@ -0,0 +1,118 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.*;
+import org.shedlang.compiler.shid.instructions.*;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ShidCodeGenNamespaceTests {
+ @Test
+ public void bodyOfNamespaceIsCompiledWithNamesBeingExported() {
+ var namespaceName = new NamespaceName(List.of("Example"));
+
+ var node = Typed.namespace(
+ namespaceName,
+ List.of(
+ TypedFunctionDefinitionNode.builder()
+ .withName("f")
+ .addPositionalParam("x", Typed.typeLevelReference("String", Types.STRING))
+ .addNamedParam("y", Typed.typeLevelReference("Int", Types.INT))
+ .addStatement(Typed.expressionStatement(Typed.stringLiteral("hello")))
+ .build()
+ ),
+ List.of(BoundVariable.immutable("f")),
+ List.of("f")
+ );
+
+ var shidNamespace = ShidCodeGen.compileNamespace(node);
+
+ assertThat(shidNamespace.instructions(), isSequence(
+ // TODO: test the builtin loading properly
+ instanceOf(ShidNamespaceImport.class),
+ instanceOf(ShidFieldLoad.class),
+ instanceOf(ShidLocalStore.class),
+
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("f"))
+ ),
+ isLocalStoreInstruction("f__0"),
+ isLocalLoadInstruction("f__0"),
+ isNamespaceFieldStoreInstruction(namespaceName, "f")
+ ));
+ }
+
+ @Test
+ public void importsAreLoaded() {
+ var node = TypedNamespaceNode.builder()
+ .addImport(Typed.import_(NamespaceName.of("A")))
+ .addBoundVariable(BoundVariable.immutable("A"))
+ .addImport(Typed.import_(NamespaceName.of("B")))
+ .addBoundVariable(BoundVariable.immutable("B"))
+ .build();
+
+ var shidNamespace = ShidCodeGen.compileNamespace(node);
+
+ assertThat(shidNamespace.instructions(), isSequence(
+ // TODO: test the builtin loading properly
+ instanceOf(ShidNamespaceImport.class),
+ instanceOf(ShidFieldLoad.class),
+ instanceOf(ShidLocalStore.class),
+
+ isNamespaceImportInstruction(NamespaceName.of("A")),
+ isLocalStoreInstruction("A__0"),
+ isNamespaceImportInstruction(NamespaceName.of("B")),
+ isLocalStoreInstruction("B__0")
+ ));
+ }
+
+ @Test
+ public void structsAreHoisted() {
+ var namespaceName = new NamespaceName(List.of("Example"));
+ var structType = Types.struct(namespaceName, "None");
+ var node = Typed.namespace(
+ namespaceName,
+ List.of(
+ TypedFunctionDefinitionNode.builder()
+ .withName("f")
+ .build(),
+ TypedStructDefinitionNode.builder()
+ .withType(structType)
+ .build()
+ ),
+ List.of(BoundVariable.immutable("f"), BoundVariable.immutable("None")),
+ List.of("f", "None")
+ );
+
+ var shidNamespace = ShidCodeGen.compileNamespace(node);
+
+ assertThat(shidNamespace.instructions(), isSequence(
+ // TODO: test the builtin loading properly
+ instanceOf(ShidNamespaceImport.class),
+ instanceOf(ShidFieldLoad.class),
+ instanceOf(ShidLocalStore.class),
+
+ instanceOf(
+ ShidDefineStruct.class,
+ has("type", x -> x.type(), equalTo(structType))
+ ),
+ isLocalStoreInstruction("None__0"),
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("f"))
+ ),
+ isLocalStoreInstruction("f__0"),
+ isLocalLoadInstruction("f__0"),
+ isNamespaceFieldStoreInstruction(namespaceName, "f"),
+ isLocalLoadInstruction("None__0"),
+ isNamespaceFieldStoreInstruction(namespaceName, "None")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenReferenceTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenReferenceTests.java
new file mode 100644
index 00000000..2a30266b
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenReferenceTests.java
@@ -0,0 +1,43 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.BoundVariable;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenReferenceTests {
+ @Test
+ public void referenceIsCompiledToLocalLoad() {
+ var node = Typed.reference("x");
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVariable(BoundVariable.immutable("x"));
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("x__0")
+ ));
+ }
+
+ @Test
+ public void whenLocalIsVarThenReferenceIsCompiledToVarLoad() {
+ var node = Typed.reference("x");
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVariable(BoundVariable.mutable("x"));
+ context = ShidCodeGen.compileFunctionStatement(
+ Typed.var("x", Typed.intLiteral(0)),
+ context
+ );
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushIntInstruction(0),
+ isVarStoreInstruction("x__0"),
+ isVarLoadInstruction("x__0")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenResumeTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenResumeTests.java
new file mode 100644
index 00000000..6992783d
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenResumeTests.java
@@ -0,0 +1,23 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenResumeTests {
+ @Test
+ public void expressionIsEvaluatedAndReturned() {
+ var node = Typed.resume(Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello"),
+ isResumeInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringAddTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringAddTests.java
new file mode 100644
index 00000000..56b2750e
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringAddTests.java
@@ -0,0 +1,27 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenStringAddTests {
+ @Test
+ public void stringAddIsCompiledToStringAddInstruction() {
+ var node = Typed.stringAdd(
+ Typed.stringLiteral("a"),
+ Typed.stringLiteral("b")
+ );
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("a"),
+ isPushStringInstruction("b"),
+ isStringAddInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringLiteralTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringLiteralTests.java
new file mode 100644
index 00000000..b308c300
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStringLiteralTests.java
@@ -0,0 +1,22 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushStringInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenStringLiteralTests {
+ @Test
+ public void stringLiteralIsCompiledToPushStringInstruction() {
+ var node = Typed.stringLiteral("hello");
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java
new file mode 100644
index 00000000..4e989e9b
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java
@@ -0,0 +1,40 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.ast.typed.TypedStructDefinitionNode;
+import org.shedlang.compiler.shid.instructions.ShidDefineStruct;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isLocalStoreInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ShidCodeGenStructDefinitionTests {
+ @Test
+ public void structDefinitionIsCompiledToDefineStructInstruction() {
+ var structType = Types.struct(NamespaceName.of("Example", "Project"), "User");
+ var node = TypedStructDefinitionNode.builder()
+ .withType(structType)
+ .addField(Typed.structFieldDefinition("name", Typed.typeLevelReference("String", Types.STRING)))
+ .addField(Typed.structFieldDefinition("isActive", Typed.typeLevelReference("Bool", Types.BOOL)))
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("User");
+
+ var result = ShidCodeGen.compileNamespaceStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineStruct.class,
+ has("type", x -> x.type(), equalTo(structType)),
+ has("fields", x -> x.fields(), isSequence(
+ has("name", x -> x.name(), equalTo("name")),
+ has("name", x -> x.name(), equalTo("isActive"))
+ ))
+ ),
+ isLocalStoreInstruction("User__0")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenSumTypeDefinitionTests.java
new file mode 100644
index 00000000..74521d29
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenSumTypeDefinitionTests.java
@@ -0,0 +1,30 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isLocalStoreInstruction;
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushUnitInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenSumTypeDefinitionTests {
+ @Test
+ public void sumTypeDefinitionIsCompiledToUnit() {
+ // TODO: this is a bit of a hack, we probably want to be able to have
+ // sum types in some potential backends (e.g. the JVM)
+ var sumType = Types.sum(NamespaceName.of("Example", "Project"), "Option");
+ var node = Typed.sumTypeDefinition(sumType);
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("Option");
+
+ var result = ShidCodeGen.compileNamespaceStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushUnitInstruction(),
+ isLocalStoreInstruction("Option__0")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java
new file mode 100644
index 00000000..daff19b0
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTryTests.java
@@ -0,0 +1,63 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.ast.typed.TypedTryHandlerMethodNode;
+import org.shedlang.compiler.ast.typed.TypedTryNode;
+import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
+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.isPushIntInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ShidCodeGenTryTests {
+ @Test
+ public void tryNodeIsCompiledToTryInstruction() {
+ var effect = Effects.userDefined(NamespaceName.of("Example"), "State");
+ var node = TypedTryNode.builder()
+ .addBodyStatement(
+ Typed.blockReturn(Typed.intLiteral(42))
+ )
+ .withHandledEffect(Typed.typeLevelReference("State", effect))
+ .addHandlerMethod(
+ TypedTryHandlerMethodNode.builder()
+ .withName("read")
+ .build()
+ )
+ .addHandlerMethod(
+ TypedTryHandlerMethodNode.builder()
+ .withName("write")
+ .build()
+ )
+ .build();
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("c");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("read"))
+ ),
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("write"))
+ ),
+ instanceOf(
+ ShidTry.class,
+ has("handledEffect", x -> x.handledEffect(), equalTo(effect)),
+ has("handlerMethodNames", x -> x.handlerMethodNames(), isSequence(
+ equalTo("read"),
+ equalTo("write")
+ )),
+ has("body", x -> x.body(), isSequence(
+ isPushIntInstruction(42)
+ ))
+ )
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTupleTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTupleTests.java
new file mode 100644
index 00000000..c3194182
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenTupleTests.java
@@ -0,0 +1,31 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenTupleTests {
+ @Test
+ public void stringAddIsCompiledToStringAddInstruction() {
+ var node = Typed.tuple(List.of(
+ Typed.stringLiteral("a"),
+ Typed.boolLiteral(true),
+ Typed.intLiteral(4)
+ ));
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("a"),
+ isPushBoolInstruction(true),
+ isPushIntInstruction(4),
+ isTupleCreateInstruction(3)
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenUnitLiteralTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenUnitLiteralTests.java
new file mode 100644
index 00000000..27b7ed6c
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenUnitLiteralTests.java
@@ -0,0 +1,22 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.isPushUnitInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenUnitLiteralTests {
+ @Test
+ public void unitLiteralIsCompiledToPushUnitInstruction() {
+ var node = Typed.unitLiteral();
+ var context = ShidCodeGen.Context.EMPTY;
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushUnitInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java
new file mode 100644
index 00000000..5007e1e8
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenValTests.java
@@ -0,0 +1,106 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.BoundVariable;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.types.Types;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenValTests {
+ @Test
+ public void whenValIsFunctionStatementThenExpressionIsEvaluatedAndStoredAsLocal() {
+ var node = Typed.val("x", Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello"),
+ isLocalStoreInstruction("x__0")
+ ));
+ }
+
+ @Test
+ public void whenValIsNamespaceStatementThenExpressionIsEvaluatedAndStoredAsLocal() {
+ var node = Typed.val("x", Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileNamespaceStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello"),
+ isLocalStoreInstruction("x__0")
+ ));
+ }
+
+ @Test
+ public void valsInDifferentBlocksAssignToDifferentLocals() {
+ var nodes = List.of(
+ Typed.val("x", Typed.intLiteral(10)),
+ Typed.expressionStatement(Typed.reference("x")),
+ Typed.expressionStatement(Typed.if_(
+ List.of(
+ Typed.conditionalBranch(
+ Typed.boolLiteral(true),
+ Typed.block(
+ List.of(
+ Typed.val("x", Typed.intLiteral(11)),
+ Typed.expressionStatement(Typed.reference("x"))
+ ),
+ List.of(BoundVariable.immutable("x"))
+ )
+ )
+ ),
+ Typed.block(
+ List.of(
+ Typed.val("x", Typed.intLiteral(12)),
+ Typed.expressionStatement(Typed.reference("x"))
+ ),
+ List.of(BoundVariable.immutable("x"))
+ ),
+ Types.INT
+ )),
+ Typed.expressionStatement(Typed.reference("x"))
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ for (var node : nodes) {
+ context = ShidCodeGen.compileFunctionStatement(node, context);
+ }
+
+ assertThat(context.instructions(), isSequence(
+ isPushIntInstruction(10),
+ isLocalStoreInstruction("x__0"),
+ isLocalLoadInstruction("x__0"),
+ isDiscardInstruction(),
+ isPushBoolInstruction(true),
+ isIfInstruction(
+ isSequence(
+ isPushIntInstruction(11),
+ isLocalStoreInstruction("x__1"),
+ isLocalLoadInstruction("x__1"),
+ isDiscardInstruction(),
+ isPushUnitInstruction()
+ ),
+ isSequence(
+ isPushIntInstruction(12),
+ isLocalStoreInstruction("x__2"),
+ isLocalLoadInstruction("x__2"),
+ isDiscardInstruction(),
+ isPushUnitInstruction()
+ )
+ ),
+ isDiscardInstruction(),
+ isLocalLoadInstruction("x__0"),
+ isDiscardInstruction()
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenVarTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenVarTests.java
new file mode 100644
index 00000000..a1e264ad
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenVarTests.java
@@ -0,0 +1,24 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenVarTests {
+ @Test
+ public void expressionIsEvaluatedAndStoredAsVar() {
+ var node = Typed.var("x", Typed.stringLiteral("hello"));
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("hello"),
+ isVarStoreInstruction("x__0")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java
new file mode 100644
index 00000000..f3e2880c
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java
@@ -0,0 +1,62 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenWhenTests {
+ private final static NamespaceName NAMESPACE_NAME = NamespaceName.of("Example", "Project");
+
+ @Test
+ public void whenIsCompiledToSwitchInstruction() {
+ var node = Typed.when(
+ Typed.reference("x"),
+ List.of(
+ Typed.whenCase(
+ Typed.typeLevelReference("A", Types.struct(NAMESPACE_NAME, "A")),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.stringLiteral("a"))
+ ))
+ ),
+ Typed.whenCase(
+ Typed.typeLevelReference("B", Types.struct(NAMESPACE_NAME, "B")),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.stringLiteral("b"))
+ ))
+ )
+ ),
+ Types.STRING
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("x");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("x__0"),
+ isSwitchInstruction(
+ isSequence(
+ isSwitchInstructionCase(
+ Types.struct(NAMESPACE_NAME, "A"),
+ isSequence(
+ isPushStringInstruction("a")
+ )
+ ),
+ isSwitchInstructionCase(
+ Types.struct(NAMESPACE_NAME, "B"),
+ 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
new file mode 100644
index 00000000..1ab5240a
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhileTests.java
@@ -0,0 +1,76 @@
+package org.shedlang.compiler.shid;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import java.util.List;
+
+import static org.shedlang.compiler.shid.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class ShidCodeGenWhileTests {
+ @Test
+ public void whileIsCompiledToLoop() {
+ var node = Typed.while_(
+ Typed.reference("c"),
+ Typed.block(List.of(
+ Typed.expressionStatement(Typed.intLiteral(42))
+ ))
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("c");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLoopInstruction(
+ isSequence(
+ isLocalLoadInstruction("c__0"),
+ isIfInstruction(
+ isSequence(),
+ isSequence(
+ isLoopBreakInstruction()
+ )
+ ),
+ isPushIntInstruction(42),
+ isDiscardInstruction(),
+ isLoopContinueInstruction()
+ )
+ )
+ ));
+ }
+
+ @Test
+ public void whenBodyYieldsValueThenValueIsDiscarded() {
+ // In principle, the type checker should prevent the body from yielding
+ // a value, but we check we handle this case anyway.
+ var node = Typed.while_(
+ Typed.reference("c"),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.intLiteral(42))
+ ))
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("c");
+
+ var result = ShidCodeGen.compileFunctionStatement(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLoopInstruction(
+ isSequence(
+ isLocalLoadInstruction("c__0"),
+ isIfInstruction(
+ isSequence(),
+ isSequence(
+ isLoopBreakInstruction()
+ )
+ ),
+ isPushIntInstruction(42),
+ isDiscardInstruction(),
+ isLoopContinueInstruction()
+ )
+ )
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java
new file mode 100644
index 00000000..c7c89825
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/InstructionMatchers.java
@@ -0,0 +1,229 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.StructType;
+import org.zwobble.precisely.Matcher;
+
+import java.math.BigInteger;
+import java.util.List;
+
+import static org.zwobble.precisely.Matchers.*;
+
+public class InstructionMatchers {
+ private InstructionMatchers() {
+ }
+
+ public static Matcher<ShidInstruction> isCallInstruction(
+ int positionalArgCount,
+ List<String> namedArgNames
+ ) {
+ return instanceOf(
+ ShidCall.class,
+ has("positionalArgCount", x -> x.positionalArgCount(), equalTo(positionalArgCount)),
+ has("namedArgNames", x -> x.namedArgNames(), equalTo(namedArgNames))
+ );
+ }
+
+ public static Matcher<ShidDefineFunction.Param> isDefineFunctionInstructionParam(String name) {
+ return has("name", x -> x.name(), equalTo(name));
+ }
+
+ public static Matcher<ShidInstruction> isDiscardInstruction() {
+ return instanceOf(
+ ShidDiscard.class
+ );
+ }
+
+ public static Matcher<ShidInstruction> isDuplicateInstruction() {
+ return instanceOf(ShidDuplicate.class);
+ }
+
+ public static Matcher<ShidInstruction> isFieldLoadInstruction(
+ ShidFieldLoad.Receiver receiver,
+ String fieldName
+ ) {
+ return instanceOf(
+ ShidFieldLoad.class,
+ has("receiver", x -> x.receiver(), equalTo(receiver)),
+ has("fieldName", x -> x.fieldName(), equalTo(fieldName))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isFieldLoadInstruction(
+ StructType receiverType,
+ String fieldName
+ ) {
+ return isFieldLoadInstruction(
+ new ShidFieldLoad.Receiver.Struct(receiverType),
+ fieldName
+ );
+ }
+
+ public static Matcher<ShidInstruction> isIfInstruction(
+ Matcher<Iterable<? extends ShidInstruction>> trueBody,
+ Matcher<Iterable<? extends ShidInstruction>> falseBody
+ ) {
+ return instanceOf(
+ ShidIf.class,
+ has("trueBody", x -> x.trueBody(), trueBody),
+ has("falseBody", x -> x.falseBody(), falseBody)
+ );
+ }
+
+ public static Matcher<ShidInstruction> isIntAddInstruction() {
+ return instanceOf(ShidIntAdd.class);
+ }
+
+ public static Matcher<ShidInstruction> isIntEqualsInstruction() {
+ return instanceOf(ShidIntEquals.class);
+ }
+
+ public static Matcher<ShidInstruction> isIntNotEqualInstruction() {
+ return instanceOf(ShidIntNotEqual.class);
+ }
+
+ public static Matcher<ShidInstruction> isIntSubInstruction() {
+ return instanceOf(ShidIntSub.class);
+ }
+
+ public static Matcher<ShidInstruction> isLocalLoadInstruction(String name) {
+ return instanceOf(
+ ShidLocalLoad.class,
+ has("name", x -> x.name(), equalTo(name))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isLocalStoreInstruction(String name) {
+ return instanceOf(
+ ShidLocalStore.class,
+ has("name", x -> x.name(), equalTo(name))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isLoopInstruction(
+ Matcher<Iterable<? extends ShidInstruction>> body
+ ) {
+ return instanceOf(
+ ShidLoop.class,
+ has("body", x -> x.body(), body)
+ );
+ }
+
+ public static Matcher<ShidInstruction> isLoopBreakInstruction() {
+ return instanceOf(ShidLoopBreak.class);
+ }
+
+ public static Matcher<ShidInstruction> isLoopContinueInstruction() {
+ return instanceOf(ShidLoopContinue.class);
+ }
+
+ public static Matcher<ShidInstruction> isNamespaceFieldStoreInstruction(
+ NamespaceName namespaceName,
+ String fieldName
+ ) {
+ return instanceOf(
+ ShidNamespaceFieldStore.class,
+ has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName)),
+ has("fieldName", x -> x.fieldName(), equalTo(fieldName))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isNamespaceImportInstruction(
+ NamespaceName namespaceName
+ ) {
+ return instanceOf(
+ ShidNamespaceImport.class,
+ has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isPushBoolInstruction(boolean value) {
+ return instanceOf(
+ ShidPushBool.class,
+ has("value", x -> x.value(), equalTo(value))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isPushIntInstruction(int value) {
+ return instanceOf(
+ ShidPushInt.class,
+ has("value", x -> x.value(), equalTo(BigInteger.valueOf(value)))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isPushStringInstruction(String value) {
+ return instanceOf(
+ ShidPushString.class,
+ has("value", x -> x.value(), equalTo(value))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isPushUnitInstruction() {
+ return instanceOf(
+ ShidPushUnit.class
+ );
+ }
+
+ public static Matcher<ShidInstruction> isResumeInstruction() {
+ return instanceOf(
+ ShidResume.class
+ );
+ }
+
+ public static Matcher<ShidInstruction> isReturnInstruction() {
+ return instanceOf(
+ ShidReturn.class
+ );
+ }
+
+ public static Matcher<ShidInstruction> isStringAddInstruction() {
+ return instanceOf(ShidStringAdd.class);
+ }
+
+ public static Matcher<ShidInstruction> isSwitchInstruction(
+ Matcher<Iterable<? extends ShidSwitch.Case>> cases
+ ) {
+ return instanceOf(
+ ShidSwitch.class,
+ has("cases", x -> x.cases(), cases)
+ );
+ }
+
+ public static Matcher<ShidSwitch.Case> isSwitchInstructionCase(
+ StructType caseType,
+ Matcher<Iterable<? extends ShidInstruction>> body
+ ) {
+ return allOf(
+ has("caseType", x -> x.caseType(), equalTo(caseType)),
+ has("body", x -> x.body(), body)
+ );
+ }
+
+ public static Matcher<ShidInstruction> isTupleCreateInstruction(int elementCount) {
+ return instanceOf(
+ ShidTupleCreate.class,
+ has("elementCount", x -> x.elementCount(), equalTo(elementCount))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isTupleGetInstruction(int elementIndex) {
+ return instanceOf(
+ ShidTupleGet.class,
+ has("elementIndex", x -> x.elementIndex(), equalTo(elementIndex))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isVarLoadInstruction(String name) {
+ return instanceOf(
+ ShidVarLoad.class,
+ has("name", x -> x.name(), equalTo(name))
+ );
+ }
+
+ public static Matcher<ShidInstruction> isVarStoreInstruction(String name) {
+ return instanceOf(
+ ShidVarStore.class,
+ has("name", x -> x.name(), equalTo(name))
+ );
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
new file mode 100644
index 00000000..cd8e41fd
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java
@@ -0,0 +1,193 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Sources;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.StructType;
+import org.shedlang.compiler.types.UserDefinedEffect;
+
+import java.math.BigInteger;
+import java.util.List;
+
+public class Shid {
+ private Shid() {
+ }
+
+ public static ShidInstruction call(int positionalArgCount, List<String> namedArgNames) {
+ return new ShidCall(positionalArgCount, namedArgNames, Sources.NULL);
+ }
+
+ public static ShidInstruction defineEffect(UserDefinedEffect effect) {
+ return new ShidDefineEffect(effect, Sources.NULL);
+ }
+
+ public static ShidInstruction defineStruct(
+ StructType type,
+ List<ShidDefineStruct.Field> fields
+ ) {
+ return new ShidDefineStruct(
+ type,
+ fields,
+ Sources.NULL
+ );
+ }
+
+ public static ShidDefineStruct.Field defineStructField(String name) {
+ return new ShidDefineStruct.Field(name, Sources.NULL);
+ }
+
+ public static ShidInstruction discard() {
+ return new ShidDiscard(Sources.NULL);
+ }
+
+ public static ShidInstruction duplicate() {
+ return new ShidDuplicate(Sources.NULL);
+ }
+
+ public static ShidInstruction fieldLoad(NamespaceName namespaceName, String fieldName) {
+ return new ShidFieldLoad(
+ new ShidFieldLoad.Receiver.Namespace(namespaceName),
+ fieldName,
+ Sources.NULL
+ );
+ }
+
+ public static ShidInstruction fieldLoad(StructType type, String fieldName) {
+ return new ShidFieldLoad(
+ new ShidFieldLoad.Receiver.Struct(type),
+ fieldName,
+ Sources.NULL
+ );
+ }
+
+ public static ShidInstruction fieldLoad(UserDefinedEffect effect, String fieldName) {
+ return new ShidFieldLoad(
+ new ShidFieldLoad.Receiver.Effect(effect),
+ fieldName,
+ Sources.NULL
+ );
+ }
+
+ public static ShidInstruction if_(
+ int resultCount,
+ List<ShidInstruction> trueBody,
+ List<ShidInstruction> falseBody
+ ) {
+ return new ShidIf(resultCount, trueBody, falseBody, Sources.NULL);
+ }
+
+ public static ShidInstruction intAdd() {
+ return new ShidIntAdd(Sources.NULL);
+ }
+
+ public static ShidInstruction intEquals() {
+ return new ShidIntEquals(Sources.NULL);
+ }
+
+ public static ShidInstruction intNotEqual() {
+ return new ShidIntNotEqual(Sources.NULL);
+ }
+
+ public static ShidInstruction intSub() {
+ return new ShidIntSub(Sources.NULL);
+ }
+
+ public static ShidInstruction localLoad(String name) {
+ return new ShidLocalLoad(name, Sources.NULL);
+ }
+
+ public static ShidInstruction localStore(String name) {
+ return new ShidLocalStore(name, Sources.NULL);
+ }
+
+ public static ShidInstruction loop(List<ShidInstruction> body) {
+ return new ShidLoop(body, Sources.NULL);
+ }
+
+ public static ShidInstruction loopBreak() {
+ return new ShidLoopBreak(Sources.NULL);
+ }
+
+ public static ShidInstruction loopContinue() {
+ return new ShidLoopContinue(Sources.NULL);
+ }
+
+ public static ShidInstruction namespaceFieldStore(NamespaceName namespaceName, String fieldName) {
+ return new ShidNamespaceFieldStore(namespaceName, fieldName, Sources.NULL);
+ }
+
+ public static ShidInstruction namespaceImport(NamespaceName namespaceName) {
+ return new ShidNamespaceImport(namespaceName, Sources.NULL);
+ }
+
+ public static ShidDefineFunction.Param param(String name) {
+ return new ShidDefineFunction.Param(name, Sources.NULL);
+ }
+
+ public static ShidInstruction pushBool(boolean value) {
+ return new ShidPushBool(value, Sources.NULL);
+ }
+
+ public static ShidInstruction pushInt(int value) {
+ return new ShidPushInt(BigInteger.valueOf(value), Sources.NULL);
+ }
+
+ public static ShidInstruction pushString(String value) {
+ return new ShidPushString(value, Sources.NULL);
+ }
+
+ public static ShidInstruction pushUnit() {
+ return new ShidPushUnit(Sources.NULL);
+ }
+
+ public static ShidInstruction resume() {
+ return new ShidResume(Sources.NULL);
+ }
+
+ public static ShidInstruction return_() {
+ return new ShidReturn(Sources.NULL);
+ }
+
+ public static ShidInstruction stringAdd() {
+ return new ShidStringAdd(Sources.NULL);
+ }
+
+ public static ShidInstruction switch_(List<ShidSwitch.Case> cases) {
+ return new ShidSwitch(cases, Sources.NULL);
+ }
+
+ public static ShidSwitch.Case switchCase(
+ StructType type,
+ List<ShidInstruction> body
+ ) {
+ return new ShidSwitch.Case(type, body, Sources.NULL);
+ }
+
+ public static ShidInstruction try_(
+ UserDefinedEffect handledEffect,
+ List<String> handlerMethodNames,
+ List<ShidInstruction> body
+ ) {
+ return new ShidTry(
+ handledEffect,
+ handlerMethodNames,
+ body,
+ Sources.NULL
+ );
+ }
+
+ public static ShidInstruction tupleCreate(int elementCount) {
+ return new ShidTupleCreate(elementCount, Sources.NULL);
+ }
+
+ public static ShidInstruction tupleGet(int elementIndex, int elementCount) {
+ return new ShidTupleGet(elementIndex, elementCount, Sources.NULL);
+ }
+
+ public static ShidInstruction varLoad(String name) {
+ return new ShidVarLoad(name, Sources.NULL);
+ }
+
+ public static ShidInstruction varStore(String name) {
+ return new ShidVarStore(name, Sources.NULL);
+ }
+}