diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-02 19:47:35 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-02 19:47:35 +0000 |
| commit | 07d62885c5150fe6eadfa9372a190dd73fd754cc (patch) | |
| tree | 17959fa67c7ab5d375f3966003358afc0715b210 /src/test/java/org/shedlang/compiler/stackir | |
| parent | 0ad5be30c1d301942f8cec567d0719fd9ff18f3f (diff) | |
Rename stack IR to Shid
Diffstat (limited to 'src/test/java/org/shedlang/compiler/stackir')
33 files changed, 0 insertions, 2288 deletions
diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java deleted file mode 100644 index c36977cf..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ /dev/null @@ -1,469 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; -import org.shedlang.compiler.stackir.instructions.Instruction; -import org.shedlang.compiler.stackir.instructions.Instructions; -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 = Instructions.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 = Instructions.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 = DefineFunctionInstruction.builder() - .withName("f") - .addPositionalParam("p1") - .addPositionalParam("p2") - .addNamedParam("n1") - .addNamedParam("n2") - .addFreeVariable("fv1") - .addFreeVariable("fv2") - .addBodyInstruction(Instructions.pushBool(true)) - .addBodyInstruction(Instructions.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 = Instructions.defineStruct( - Types.struct(NamespaceName.of("Example", "Project"), "User"), - List.of( - Instructions.defineStructField("name"), - Instructions.defineStructField("id") - ) - ); - - var string = serialize(instruction); - - assertThat(string, equalTo(""" - ( - define-struct - type="Example/Project.User" - fields=("name" "id") - )""")); - } - - @Test - public void canSerializeDiscard() { - var instruction = Instructions.discard(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(discard)")); - } - - @Test - public void canSerializeDuplicate() { - var instruction = Instructions.duplicate(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(duplicate)")); - } - - @Test - public void canSerializeNamespaceFieldLoad() { - var instruction = Instructions.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 = Instructions.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 = Instructions.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 = Instructions.if_( - 1, - List.of( - Instructions.pushUnit() - ), - List.of( - Instructions.localLoad("x"), - Instructions.discard(), - Instructions.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 = Instructions.intAdd(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(int-add)")); - } - - @Test - public void canSerializeIntEquals() { - var instruction = Instructions.intEquals(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(int-eq)")); - } - - @Test - public void canSerializeIntNotEqual() { - var instruction = Instructions.intNotEqual(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(int-ne)")); - } - - @Test - public void canSerializeIntSub() { - var instruction = Instructions.intSub(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(int-sub)")); - } - - @Test - public void canSerializeLocalLoad() { - var instruction = Instructions.localLoad("x"); - - var string = serialize(instruction); - - assertThat(string, equalTo("(local-load \"x\")")); - } - - @Test - public void canSerializeLocalStore() { - var instruction = Instructions.localStore("x"); - - var string = serialize(instruction); - - assertThat(string, equalTo("(local-store \"x\")")); - } - - @Test - public void canSerializeLoop() { - var instruction = Instructions.loop( - List.of( - Instructions.localLoad("x"), - Instructions.discard() - ) - ); - - var string = serialize(instruction); - - assertThat(string, equalTo(""" - ( - loop - ( - (local-load "x") - (discard) - ) - )""")); - } - - @Test - public void canSerializeLoopBreak() { - var instruction = Instructions.loopBreak(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(break)")); - } - - @Test - public void canSerializeLoopContinue() { - var instruction = Instructions.loopContinue(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(continue)")); - } - - @Test - public void canSerializeNamespaceFieldStore() { - var instruction = Instructions.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 = Instructions.namespaceImport( - new NamespaceName(List.of("Example", "Lib")) - ); - - var string = serialize(instruction); - - assertThat(string, equalTo("(namespace-import \"Example/Lib\")")); - } - - @Test - public void canSerializePushBool() { - var instruction = Instructions.pushBool(true); - - var string = serialize(instruction); - - assertThat(string, equalTo("(push-bool true)")); - } - - @Test - public void canSerializePushInt() { - var instruction = Instructions.pushInt(42); - - var string = serialize(instruction); - - assertThat(string, equalTo("(push-int 42)")); - } - - @Test - public void canSerializePushString() { - var instruction = Instructions.pushString("hello"); - - var string = serialize(instruction); - - assertThat(string, equalTo("(push-string \"hello\")")); - } - - @Test - public void specialCharactersInStringAreEscaped() { - var instruction = Instructions.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 = Instructions.pushUnit(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(push-unit)")); - } - - @Test - public void canSerializeResume() { - var instruction = Instructions.resume(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(resume)")); - } - - @Test - public void canSerializeReturn() { - var instruction = Instructions.return_(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(return)")); - } - - @Test - public void canSerializeStringAdd() { - var instruction = Instructions.stringAdd(); - - var string = serialize(instruction); - - assertThat(string, equalTo("(string-add)")); - } - - @Test - public void canSerializeSwitch() { - var instruction = Instructions.switch_(List.of( - Instructions.switchCase( - Types.struct(NamespaceName.of("Example", "Project"), "A"), - List.of( - Instructions.return_() - ) - ), - Instructions.switchCase( - Types.struct(NamespaceName.of("Example", "Project"), "B"), - List.of( - Instructions.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 = Instructions.try_( - effect, - List.of("read", "write"), - List.of( - Instructions.pushUnit() - ) - ); - - var string = serialize(instruction); - - assertThat(string, equalTo(""" - ( - try - "Example/Project.State" - ( - "read" - "write" - ) - ( - (push-unit) - ) - )""")); - } - - @Test - public void canSerializeTupleCreate() { - var instruction = Instructions.tupleCreate(2); - - var string = serialize(instruction); - - assertThat(string, equalTo("(tuple-create 2)")); - } - - @Test - public void canSerializeTupleGet() { - var instruction = Instructions.tupleGet(2, 3); - - var string = serialize(instruction); - - assertThat(string, equalTo("(tuple-get 2 3)")); - } - - @Test - public void canSerializeVarLoad() { - var instruction = Instructions.varLoad("x"); - - var string = serialize(instruction); - - assertThat(string, equalTo("(var-load \"x\")")); - } - - @Test - public void canSerializeVarStore() { - var instruction = Instructions.varStore("x"); - - var string = serialize(instruction); - - assertThat(string, equalTo("(var-store \"x\")")); - } - - private String serialize(Instruction instruction) { - var builder = new StringBuilder(); - InstructionSerializer.serializeInstruction(instruction, builder); - return builder.toString(); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java deleted file mode 100644 index 431556a0..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java +++ /dev/null @@ -1,94 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import java.util.List; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerAssignTests { - @Test - public void targetIgnoreIsCompiledToDiscard() { - var node = Typed.assign( - Typed.targetIgnore(), - Typed.reference("x") - ); - var context = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("x") - .addShedVal("a") - .addShedVal("b") - .addShedVal("c"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.compileFunctionStatement(node, context); - - assertThat(result.instructions(), isSequence( - isLocalLoadInstruction("x__0"), - isDiscardInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerBlockReturnTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerBlockReturnTests.java deleted file mode 100644 index df738405..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerBlockReturnTests.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushStringInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerBlockReturnTests { - @Test - public void expressionIsEvaluated() { - var node = Typed.blockReturn(Typed.stringLiteral("hello")); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileFunctionStatement(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("hello") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.java deleted file mode 100644 index ac96c48e..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushBoolInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerBoolLiteralTests { - @Test - public void boolLiteralIsCompiledToPushBoolInstruction() { - var node = Typed.boolLiteral(true); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushBoolInstruction(true) - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java deleted file mode 100644 index 2cf25bb1..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerCallTests.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import java.util.List; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerCallTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("f"); - - var result = StackCompiler.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/stackir/StackCompilerEffectDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java deleted file mode 100644 index 051c03e4..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerEffectDefinitionTests.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; -import org.shedlang.compiler.stackir.instructions.DefineEffectInstruction; -import org.shedlang.compiler.types.Effects; -import org.shedlang.compiler.types.NamespaceName; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalStoreInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class StackCompilerEffectDefinitionTests { - @Test - public void effectDefinitionIsCompiledToDefineEffectInstruction() { - var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State"); - var node = Typed.effectDefinition(effect); - var context = StackCompiler.Context.EMPTY - .addShedVal("State"); - - var result = StackCompiler.compileNamespaceStatement(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineEffectInstruction.class, - has("effect", x -> x.effect(), equalTo(effect)) - ), - isLocalStoreInstruction("State__0") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerExpressionStatementTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerExpressionStatementTests.java deleted file mode 100644 index f644408b..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerExpressionStatementTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isDiscardInstruction; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushStringInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerExpressionStatementTests { - @Test - public void expressionIsEvaluatedAndTheResultDiscarded() { - var node = Typed.expressionStatement(Typed.stringLiteral("hello")); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileFunctionStatement(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("hello"), - isDiscardInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java deleted file mode 100644 index 4c0821a6..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; -import org.shedlang.compiler.stackir.instructions.FieldLoadInstruction; -import org.shedlang.compiler.types.Effects; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.Types; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isFieldLoadInstruction; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalLoadInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerFieldAccessTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("Project"); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isLocalLoadInstruction("Project__0"), - isFieldLoadInstruction( - new FieldLoadInstruction.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 = StackCompiler.Context.EMPTY - .addShedVal("user"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("State"); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isLocalLoadInstruction("State__0"), - isFieldLoadInstruction( - new FieldLoadInstruction.Receiver.Effect(effect), - "read" - ) - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java deleted file mode 100644 index d28a3eb5..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionDefinitionTests.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; -import org.shedlang.compiler.ast.typed.TypedFunctionDefinitionNode; -import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; -import org.shedlang.compiler.types.Types; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class StackCompilerFunctionDefinitionTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("f") - .addShedVal("z"); - - var result = StackCompiler.compileNamespaceStatement(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineFunctionInstruction.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 = StackCompiler.Context.EMPTY - .addShedVal("f"); - - var result = StackCompiler.compileNamespaceStatement(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineFunctionInstruction.class, - has("body", x -> x.body(), isSequence( - isPushStringInstruction("hello"), - isReturnInstruction() - )) - ), - isLocalStoreInstruction("f__0") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionExpressionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionExpressionTests.java deleted file mode 100644 index d14356f7..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionExpressionTests.java +++ /dev/null @@ -1,71 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; -import org.shedlang.compiler.ast.typed.TypedFunctionExpressionNode; -import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction; -import org.shedlang.compiler.types.Types; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class StackCompilerFunctionExpressionTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("f") - .addShedVal("z"); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineFunctionInstruction.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 = StackCompiler.Context.EMPTY - .addShedVal("f"); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineFunctionInstruction.class, - has("body", x -> x.body(), isSequence( - isPushStringInstruction("hello"), - isReturnInstruction() - )) - ) - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionReturnTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionReturnTests.java deleted file mode 100644 index 8bd5c64d..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFunctionReturnTests.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerFunctionReturnTests { - @Test - public void expressionIsEvaluatedAndReturned() { - var node = Typed.functionReturn(Typed.stringLiteral("hello")); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileFunctionStatement(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("hello"), - isReturnInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java deleted file mode 100644 index dca0be02..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.shedlang.compiler.stackir; - -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.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerIfTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("c"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("c") - .addShedVal("d"); - - var result = StackCompiler.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/stackir/StackCompilerIntAddTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java deleted file mode 100644 index 4724b867..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isIntAddInstruction; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushIntInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerIntAddTests { - @Test - public void intAddIsCompiledToIntAddInstruction() { - var node = Typed.intAdd( - Typed.intLiteral(42), - Typed.intLiteral(47) - ); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushIntInstruction(42), - isPushIntInstruction(47), - isIntAddInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntEqualsTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntEqualsTests.java deleted file mode 100644 index 61eb93f9..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntEqualsTests.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerIntEqualsTests { - @Test - public void intEqualsIsCompiledToIntEqualsInstruction() { - var node = Typed.intEquals( - Typed.intLiteral(42), - Typed.intLiteral(47) - ); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushIntInstruction(42), - isPushIntInstruction(47), - isIntEqualsInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntLiteralTests.java deleted file mode 100644 index 402c6dac..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntLiteralTests.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushIntInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerIntLiteralTests { - @Test - public void intLiteralIsCompiledToPushIntInstruction() { - var node = Typed.intLiteral(42); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushIntInstruction(42) - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java deleted file mode 100644 index 8364bc81..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntNotEqualTests.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerIntNotEqualTests { - @Test - public void intNotEqualsIsCompiledToIntNotEqualInstruction() { - var node = Typed.intNotEqual( - Typed.intLiteral(42), - Typed.intLiteral(47) - ); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushIntInstruction(42), - isPushIntInstruction(47), - isIntNotEqualInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java deleted file mode 100644 index 110be63a..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerIntSubTests { - @Test - public void intSubIsCompiledToIntSubInstruction() { - var node = Typed.intSub( - Typed.intLiteral(42), - Typed.intLiteral(47) - ); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushIntInstruction(42), - isPushIntInstruction(47), - isIntSubInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java deleted file mode 100644 index 322bf49f..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerNamespaceTests.java +++ /dev/null @@ -1,118 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.*; -import org.shedlang.compiler.stackir.instructions.*; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.Types; - -import java.util.List; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class StackCompilerNamespaceTests { - @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 stackIrNamespace = StackCompiler.compileNamespace(node); - - assertThat(stackIrNamespace.instructions(), isSequence( - // TODO: test the builtin loading properly - instanceOf(NamespaceImportInstruction.class), - instanceOf(FieldLoadInstruction.class), - instanceOf(LocalStoreInstruction.class), - - instanceOf( - DefineFunctionInstruction.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 stackIrNamespace = StackCompiler.compileNamespace(node); - - assertThat(stackIrNamespace.instructions(), isSequence( - // TODO: test the builtin loading properly - instanceOf(NamespaceImportInstruction.class), - instanceOf(FieldLoadInstruction.class), - instanceOf(LocalStoreInstruction.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 stackIrNamespace = StackCompiler.compileNamespace(node); - - assertThat(stackIrNamespace.instructions(), isSequence( - // TODO: test the builtin loading properly - instanceOf(NamespaceImportInstruction.class), - instanceOf(FieldLoadInstruction.class), - instanceOf(LocalStoreInstruction.class), - - instanceOf( - DefineStructInstruction.class, - has("type", x -> x.type(), equalTo(structType)) - ), - isLocalStoreInstruction("None__0"), - instanceOf( - DefineFunctionInstruction.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/stackir/StackCompilerReferenceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java deleted file mode 100644 index 038351c4..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.shedlang.compiler.stackir; - -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.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerReferenceTests { - @Test - public void referenceIsCompiledToLocalLoad() { - var node = Typed.reference("x"); - var context = StackCompiler.Context.EMPTY - .addShedVariable(BoundVariable.immutable("x")); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isLocalLoadInstruction("x__0") - )); - } - - @Test - public void whenLocalIsVarThenReferenceIsCompiledToVarLoad() { - var node = Typed.reference("x"); - var context = StackCompiler.Context.EMPTY - .addShedVariable(BoundVariable.mutable("x")); - context = StackCompiler.compileFunctionStatement( - Typed.var("x", Typed.intLiteral(0)), - context - ); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushIntInstruction(0), - isVarStoreInstruction("x__0"), - isVarLoadInstruction("x__0") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerResumeTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerResumeTests.java deleted file mode 100644 index 8ee1f909..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerResumeTests.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerResumeTests { - @Test - public void expressionIsEvaluatedAndReturned() { - var node = Typed.resume(Typed.stringLiteral("hello")); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileFunctionStatement(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("hello"), - isResumeInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringAddTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringAddTests.java deleted file mode 100644 index 42557970..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringAddTests.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerStringAddTests { - @Test - public void stringAddIsCompiledToStringAddInstruction() { - var node = Typed.stringAdd( - Typed.stringLiteral("a"), - Typed.stringLiteral("b") - ); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("a"), - isPushStringInstruction("b"), - isStringAddInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.java deleted file mode 100644 index 44133713..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushStringInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerStringLiteralTests { - @Test - public void stringLiteralIsCompiledToPushStringInstruction() { - var node = Typed.stringLiteral("hello"); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("hello") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java deleted file mode 100644 index 6dc845bd..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; -import org.shedlang.compiler.ast.typed.TypedStructDefinitionNode; -import org.shedlang.compiler.stackir.instructions.DefineStructInstruction; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.Types; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalStoreInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class StackCompilerStructDefinitionTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("User"); - - var result = StackCompiler.compileNamespaceStatement(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineStructInstruction.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/stackir/StackCompilerSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerSumTypeDefinitionTests.java deleted file mode 100644 index 849e2df7..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerSumTypeDefinitionTests.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.shedlang.compiler.stackir; - -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.stackir.instructions.InstructionMatchers.isLocalStoreInstruction; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushUnitInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerSumTypeDefinitionTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("Option"); - - var result = StackCompiler.compileNamespaceStatement(node, context); - - assertThat(result.instructions(), isSequence( - isPushUnitInstruction(), - isLocalStoreInstruction("Option__0") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerTryTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTryTests.java deleted file mode 100644 index f4e87f8f..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerTryTests.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.shedlang.compiler.stackir; - -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.stackir.instructions.DefineFunctionInstruction; -import org.shedlang.compiler.stackir.instructions.TryInstruction; -import org.shedlang.compiler.types.Effects; -import org.shedlang.compiler.types.NamespaceName; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushIntInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.*; - -public class StackCompilerTryTests { - @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 = StackCompiler.Context.EMPTY - .addShedVal("c"); - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - instanceOf( - DefineFunctionInstruction.class, - has("name", x -> x.name(), equalTo("read")) - ), - instanceOf( - DefineFunctionInstruction.class, - has("name", x -> x.name(), equalTo("write")) - ), - instanceOf( - TryInstruction.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/stackir/StackCompilerTupleTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java deleted file mode 100644 index f05e9257..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import java.util.List; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerTupleTests { - @Test - public void stringAddIsCompiledToStringAddInstruction() { - var node = Typed.tuple(List.of( - Typed.stringLiteral("a"), - Typed.boolLiteral(true), - Typed.intLiteral(4) - )); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("a"), - isPushBoolInstruction(true), - isPushIntInstruction(4), - isTupleCreateInstruction(3) - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerUnitLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerUnitLiteralTests.java deleted file mode 100644 index 024b9561..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerUnitLiteralTests.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushUnitInstruction; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerUnitLiteralTests { - @Test - public void unitLiteralIsCompiledToPushUnitInstruction() { - var node = Typed.unitLiteral(); - var context = StackCompiler.Context.EMPTY; - - var result = StackCompiler.compileExpression(node, context); - - assertThat(result.instructions(), isSequence( - isPushUnitInstruction() - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java deleted file mode 100644 index 2e6194ca..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.shedlang.compiler.stackir; - -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.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerValTests { - @Test - public void whenValIsFunctionStatementThenExpressionIsEvaluatedAndStoredAsLocal() { - var node = Typed.val("x", Typed.stringLiteral("hello")); - var context = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("x"); - - for (var node : nodes) { - context = StackCompiler.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/stackir/StackCompilerVarTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerVarTests.java deleted file mode 100644 index 7df00442..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerVarTests.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerVarTests { - @Test - public void expressionIsEvaluatedAndStoredAsVar() { - var node = Typed.var("x", Typed.stringLiteral("hello")); - var context = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.compileFunctionStatement(node, context); - - assertThat(result.instructions(), isSequence( - isPushStringInstruction("hello"), - isVarStoreInstruction("x__0") - )); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhenTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhenTests.java deleted file mode 100644 index d19eb525..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhenTests.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.shedlang.compiler.stackir; - -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.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerWhenTests { - 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 = StackCompiler.Context.EMPTY - .addShedVal("x"); - - var result = StackCompiler.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/stackir/StackCompilerWhileTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java deleted file mode 100644 index 006c0b4c..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerWhileTests.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.shedlang.compiler.stackir; - -import org.junit.jupiter.api.Test; -import org.shedlang.compiler.ast.typed.Typed; - -import java.util.List; - -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; -import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; - -public class StackCompilerWhileTests { - @Test - public void whileIsCompiledToLoop() { - var node = Typed.while_( - Typed.reference("c"), - Typed.block(List.of( - Typed.expressionStatement(Typed.intLiteral(42)) - )) - ); - var context = StackCompiler.Context.EMPTY - .addShedVal("c"); - - var result = StackCompiler.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 = StackCompiler.Context.EMPTY - .addShedVal("c"); - - var result = StackCompiler.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/stackir/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java deleted file mode 100644 index 34c94f0e..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ /dev/null @@ -1,229 +0,0 @@ -package org.shedlang.compiler.stackir.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<Instruction> isCallInstruction( - int positionalArgCount, - List<String> namedArgNames - ) { - return instanceOf( - CallInstruction.class, - has("positionalArgCount", x -> x.positionalArgCount(), equalTo(positionalArgCount)), - has("namedArgNames", x -> x.namedArgNames(), equalTo(namedArgNames)) - ); - } - - public static Matcher<DefineFunctionInstruction.Param> isDefineFunctionInstructionParam(String name) { - return has("name", x -> x.name(), equalTo(name)); - } - - public static Matcher<Instruction> isDiscardInstruction() { - return instanceOf( - DiscardInstruction.class - ); - } - - public static Matcher<Instruction> isDuplicateInstruction() { - return instanceOf(DuplicateInstruction.class); - } - - public static Matcher<Instruction> isFieldLoadInstruction( - FieldLoadInstruction.Receiver receiver, - String fieldName - ) { - return instanceOf( - FieldLoadInstruction.class, - has("receiver", x -> x.receiver(), equalTo(receiver)), - has("fieldName", x -> x.fieldName(), equalTo(fieldName)) - ); - } - - public static Matcher<Instruction> isFieldLoadInstruction( - StructType receiverType, - String fieldName - ) { - return isFieldLoadInstruction( - new FieldLoadInstruction.Receiver.Struct(receiverType), - fieldName - ); - } - - public static Matcher<Instruction> isIfInstruction( - Matcher<Iterable<? extends Instruction>> trueBody, - Matcher<Iterable<? extends Instruction>> falseBody - ) { - return instanceOf( - IfInstruction.class, - has("trueBody", x -> x.trueBody(), trueBody), - has("falseBody", x -> x.falseBody(), falseBody) - ); - } - - public static Matcher<Instruction> isIntAddInstruction() { - return instanceOf(IntAddInstruction.class); - } - - public static Matcher<Instruction> isIntEqualsInstruction() { - return instanceOf(IntEqualsInstruction.class); - } - - public static Matcher<Instruction> isIntNotEqualInstruction() { - return instanceOf(IntNotEqualInstruction.class); - } - - public static Matcher<Instruction> isIntSubInstruction() { - return instanceOf(IntSubInstruction.class); - } - - public static Matcher<Instruction> isLocalLoadInstruction(String name) { - return instanceOf( - LocalLoadInstruction.class, - has("name", x -> x.name(), equalTo(name)) - ); - } - - public static Matcher<Instruction> isLocalStoreInstruction(String name) { - return instanceOf( - LocalStoreInstruction.class, - has("name", x -> x.name(), equalTo(name)) - ); - } - - public static Matcher<Instruction> isLoopInstruction( - Matcher<Iterable<? extends Instruction>> body - ) { - return instanceOf( - LoopInstruction.class, - has("body", x -> x.body(), body) - ); - } - - public static Matcher<Instruction> isLoopBreakInstruction() { - return instanceOf(LoopBreakInstruction.class); - } - - public static Matcher<Instruction> isLoopContinueInstruction() { - return instanceOf(LoopContinueInstruction.class); - } - - public static Matcher<Instruction> isNamespaceFieldStoreInstruction( - NamespaceName namespaceName, - String fieldName - ) { - return instanceOf( - NamespaceFieldStoreInstruction.class, - has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName)), - has("fieldName", x -> x.fieldName(), equalTo(fieldName)) - ); - } - - public static Matcher<Instruction> isNamespaceImportInstruction( - NamespaceName namespaceName - ) { - return instanceOf( - NamespaceImportInstruction.class, - has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName)) - ); - } - - public static Matcher<Instruction> isPushBoolInstruction(boolean value) { - return instanceOf( - PushBoolInstruction.class, - has("value", x -> x.value(), equalTo(value)) - ); - } - - public static Matcher<Instruction> isPushIntInstruction(int value) { - return instanceOf( - PushIntInstruction.class, - has("value", x -> x.value(), equalTo(BigInteger.valueOf(value))) - ); - } - - public static Matcher<Instruction> isPushStringInstruction(String value) { - return instanceOf( - PushStringInstruction.class, - has("value", x -> x.value(), equalTo(value)) - ); - } - - public static Matcher<Instruction> isPushUnitInstruction() { - return instanceOf( - PushUnitInstruction.class - ); - } - - public static Matcher<Instruction> isResumeInstruction() { - return instanceOf( - ResumeInstruction.class - ); - } - - public static Matcher<Instruction> isReturnInstruction() { - return instanceOf( - ReturnInstruction.class - ); - } - - public static Matcher<Instruction> isStringAddInstruction() { - return instanceOf(StringAddInstruction.class); - } - - public static Matcher<Instruction> isSwitchInstruction( - Matcher<Iterable<? extends SwitchInstruction.Case>> cases - ) { - return instanceOf( - SwitchInstruction.class, - has("cases", x -> x.cases(), cases) - ); - } - - public static Matcher<SwitchInstruction.Case> isSwitchInstructionCase( - StructType caseType, - Matcher<Iterable<? extends Instruction>> body - ) { - return allOf( - has("caseType", x -> x.caseType(), equalTo(caseType)), - has("body", x -> x.body(), body) - ); - } - - public static Matcher<Instruction> isTupleCreateInstruction(int elementCount) { - return instanceOf( - TupleCreateInstruction.class, - has("elementCount", x -> x.elementCount(), equalTo(elementCount)) - ); - } - - public static Matcher<Instruction> isTupleGetInstruction(int elementIndex) { - return instanceOf( - TupleGetInstruction.class, - has("elementIndex", x -> x.elementIndex(), equalTo(elementIndex)) - ); - } - - public static Matcher<Instruction> isVarLoadInstruction(String name) { - return instanceOf( - VarLoadInstruction.class, - has("name", x -> x.name(), equalTo(name)) - ); - } - - public static Matcher<Instruction> isVarStoreInstruction(String name) { - return instanceOf( - VarStoreInstruction.class, - has("name", x -> x.name(), equalTo(name)) - ); - } -} diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java deleted file mode 100644 index ce6ebf37..00000000 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ /dev/null @@ -1,193 +0,0 @@ -package org.shedlang.compiler.stackir.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 Instructions { - private Instructions() { - } - - public static Instruction call(int positionalArgCount, List<String> namedArgNames) { - return new CallInstruction(positionalArgCount, namedArgNames, Sources.NULL); - } - - public static Instruction defineEffect(UserDefinedEffect effect) { - return new DefineEffectInstruction(effect, Sources.NULL); - } - - public static Instruction defineStruct( - StructType type, - List<DefineStructInstruction.Field> fields - ) { - return new DefineStructInstruction( - type, - fields, - Sources.NULL - ); - } - - public static DefineStructInstruction.Field defineStructField(String name) { - return new DefineStructInstruction.Field(name, Sources.NULL); - } - - public static Instruction discard() { - return new DiscardInstruction(Sources.NULL); - } - - public static Instruction duplicate() { - return new DuplicateInstruction(Sources.NULL); - } - - public static Instruction fieldLoad(NamespaceName namespaceName, String fieldName) { - return new FieldLoadInstruction( - new FieldLoadInstruction.Receiver.Namespace(namespaceName), - fieldName, - Sources.NULL - ); - } - - public static Instruction fieldLoad(StructType type, String fieldName) { - return new FieldLoadInstruction( - new FieldLoadInstruction.Receiver.Struct(type), - fieldName, - Sources.NULL - ); - } - - public static Instruction fieldLoad(UserDefinedEffect effect, String fieldName) { - return new FieldLoadInstruction( - new FieldLoadInstruction.Receiver.Effect(effect), - fieldName, - Sources.NULL - ); - } - - public static Instruction if_( - int resultCount, - List<Instruction> trueBody, - List<Instruction> falseBody - ) { - return new IfInstruction(resultCount, trueBody, falseBody, Sources.NULL); - } - - public static Instruction intAdd() { - return new IntAddInstruction(Sources.NULL); - } - - public static Instruction intEquals() { - return new IntEqualsInstruction(Sources.NULL); - } - - public static Instruction intNotEqual() { - return new IntNotEqualInstruction(Sources.NULL); - } - - public static Instruction intSub() { - return new IntSubInstruction(Sources.NULL); - } - - public static Instruction localLoad(String name) { - return new LocalLoadInstruction(name, Sources.NULL); - } - - public static Instruction localStore(String name) { - return new LocalStoreInstruction(name, Sources.NULL); - } - - public static Instruction loop(List<Instruction> body) { - return new LoopInstruction(body, Sources.NULL); - } - - public static Instruction loopBreak() { - return new LoopBreakInstruction(Sources.NULL); - } - - public static Instruction loopContinue() { - return new LoopContinueInstruction(Sources.NULL); - } - - public static Instruction namespaceFieldStore(NamespaceName namespaceName, String fieldName) { - return new NamespaceFieldStoreInstruction(namespaceName, fieldName, Sources.NULL); - } - - public static Instruction namespaceImport(NamespaceName namespaceName) { - return new NamespaceImportInstruction(namespaceName, Sources.NULL); - } - - public static DefineFunctionInstruction.Param param(String name) { - return new DefineFunctionInstruction.Param(name, Sources.NULL); - } - - public static Instruction pushBool(boolean value) { - return new PushBoolInstruction(value, Sources.NULL); - } - - public static Instruction pushInt(int value) { - return new PushIntInstruction(BigInteger.valueOf(value), Sources.NULL); - } - - public static Instruction pushString(String value) { - return new PushStringInstruction(value, Sources.NULL); - } - - public static Instruction pushUnit() { - return new PushUnitInstruction(Sources.NULL); - } - - public static Instruction resume() { - return new ResumeInstruction(Sources.NULL); - } - - public static Instruction return_() { - return new ReturnInstruction(Sources.NULL); - } - - public static Instruction stringAdd() { - return new StringAddInstruction(Sources.NULL); - } - - public static Instruction switch_(List<SwitchInstruction.Case> cases) { - return new SwitchInstruction(cases, Sources.NULL); - } - - public static SwitchInstruction.Case switchCase( - StructType type, - List<Instruction> body - ) { - return new SwitchInstruction.Case(type, body, Sources.NULL); - } - - public static Instruction try_( - UserDefinedEffect handledEffect, - List<String> handlerMethodNames, - List<Instruction> body - ) { - return new TryInstruction( - handledEffect, - handlerMethodNames, - body, - Sources.NULL - ); - } - - public static Instruction tupleCreate(int elementCount) { - return new TupleCreateInstruction(elementCount, Sources.NULL); - } - - public static Instruction tupleGet(int elementIndex, int elementCount) { - return new TupleGetInstruction(elementIndex, elementCount, Sources.NULL); - } - - public static Instruction varLoad(String name) { - return new VarLoadInstruction(name, Sources.NULL); - } - - public static Instruction varStore(String name) { - return new VarStoreInstruction(name, Sources.NULL); - } -} |
