aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-09-28 14:30:42 +0100
committerMichael Williamson <mike@zwobble.org>2023-09-28 14:30:42 +0100
commit04fa68550d0826bdfd8a2cf56fdb0ad1df8c15cb (patch)
tree694e4e3e73ddfedd9f1619155499e464d241084f /src/test/java
parentab91c6113f1b4cbcb3a6adf61fe5c5f19e710a50 (diff)
Generate stack IR for struct definition
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java7
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java65
2 files changed, 72 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
index 110696a7..24ee99a3 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -190,6 +190,13 @@ public class Typed {
);
}
+ public static TypedStructFieldDefinitionNode structFieldDefinition(
+ String name,
+ TypedTypeLevelExpressionNode<Type> type
+ ) {
+ return new TypedStructFieldDefinitionNode(name, type, Sources.NULL);
+ }
+
public static TypedTargetNode targetIgnore() {
return new TypedTargetNode.Ignore(Sources.NULL);
}
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java
new file mode 100644
index 00000000..78a14e2f
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStructDefinitionTests.java
@@ -0,0 +1,65 @@
+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.ast.typed.TypedStructDefinitionNode;
+import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction;
+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.*;
+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
+ .addShedVariable("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")
+ ));
+ }
+
+ @Test
+ public void whenLastStatementIsBlockReturnThenThatValueIsReturned() {
+ var node = TypedFunctionDefinitionNode.builder()
+ .withName("f")
+ .addStatement(Typed.blockReturn(Typed.stringLiteral("hello")))
+ .build();
+ var context = StackCompiler.Context.EMPTY
+ .addShedVariable("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")
+ ));
+ }
+}