aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-20 10:59:42 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-20 11:00:00 +0100
commit400e05ae86949488a49d9dcbb49463b2a17b8fc4 (patch)
tree34e9fefd29f9f33dbcd64de4434b36a63ee3bcf7 /src/test/java
parent492a56b70d5956a956052f37fd77081554cdb837 (diff)
Add stubbed function definitions for Patio
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java82
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java34
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioTesting.java16
3 files changed, 122 insertions, 10 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java
new file mode 100644
index 00000000..ac510295
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineFunctionTests.java
@@ -0,0 +1,82 @@
+package org.shedlang.compiler.patio;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.shedlang.compiler.ast.typed.FreeVariables;
+import org.shedlang.compiler.sources.Sources;
+import org.shedlang.compiler.stackir.instructions.DefineFunctionInstruction;
+import org.shedlang.compiler.stackir.instructions.Instructions;
+import org.shedlang.compiler.testing.snapshots.SnapshotResolver;
+import org.shedlang.compiler.testing.snapshots.Snapshotter;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.shedlang.compiler.patio.PatioTesting.serialize;
+
+@ExtendWith({SnapshotResolver.class})
+public class PatioCodeGenDefineFunctionTests {
+ @Test
+ public void canDefineMinimalFunction(Snapshotter snapshotter) throws IOException {
+ var instruction = new DefineFunctionInstruction(
+ "f",
+ List.of(),
+ List.of(),
+ FreeVariables.empty(),
+ List.of(
+ Instructions.pushUnit(),
+ Instructions.return_()
+ ),
+ Sources.NULL
+ );
+ var context = PatioCodeGenContext.EMPTY;
+
+ var result = PatioCodeGen.generateInstruction(instruction, context);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
+
+ @Test
+ public void freeVariablesAreStoredInAndLoadedFromEnvironment(Snapshotter snapshotter) throws IOException {
+ var instruction = new DefineFunctionInstruction(
+ "f",
+ List.of(),
+ List.of(),
+ FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
+ List.of(
+ Instructions.localLoad("a"),
+ Instructions.return_()
+ ),
+ Sources.NULL
+ );
+ var context = PatioCodeGenContext.EMPTY;
+
+ var result = PatioCodeGen.generateInstruction(instruction, context);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
+
+ @Test
+ public void positionalAndNamedParamsAreDefined(Snapshotter snapshotter) throws IOException {
+ var instruction = new DefineFunctionInstruction(
+ "f",
+ List.of(Instructions.param("param1"), Instructions.param("param2")),
+ List.of(Instructions.param("paramA"), Instructions.param("paramB")),
+ FreeVariables.builder().plus("a", Sources.NULL).plus("b", Sources.NULL).build(),
+ List.of(
+ Instructions.localLoad("a"),
+ Instructions.localLoad("param1"),
+ Instructions.intAdd(),
+ Instructions.localLoad("paramA"),
+ Instructions.intAdd(),
+ Instructions.return_()
+ ),
+ Sources.NULL
+ );
+ var context = PatioCodeGenContext.EMPTY;
+
+ var result = PatioCodeGen.generateInstruction(instruction, context);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java
index 03e00a92..c92bb08b 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioInstructionSerializerTests.java
@@ -2,19 +2,35 @@ package org.shedlang.compiler.patio;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.patio.instructions.Patio;
+import org.shedlang.compiler.patio.instructions.PatioFunctionDefinition;
import java.util.List;
-import static org.shedlang.compiler.patio.PatioTesting.serialize;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
public class PatioInstructionSerializerTests {
@Test
+ public void canSerializeFunctionDefinition() {
+ var element = PatioFunctionDefinition.builder()
+ .withName("f")
+ .build();
+
+ var string = PatioTesting.serializeCompilationUnitElement(element);
+
+ assertThat(string, equalTo("""
+ (
+ function
+ \"f\"
+ )"""
+ ));
+ }
+
+ @Test
public void canSerializeAlloc() {
var instruction = Patio.alloc("x", 42);
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(alloc \"x\" 42)"));
}
@@ -27,7 +43,7 @@ public class PatioInstructionSerializerTests {
List.of("a", "b", "c")
);
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(call-pointer \"target\" \"f\" (\"a\" \"b\" \"c\"))"));
}
@@ -36,7 +52,7 @@ public class PatioInstructionSerializerTests {
public void canSerializeFunctionPointerGet() {
var instruction = Patio.functionPointerGet("target", "f");
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(function-pointer-get \"target\" \"f\")"));
}
@@ -45,7 +61,7 @@ public class PatioInstructionSerializerTests {
public void canSerializeIntGet() {
var instruction = Patio.intGet("target", 42);
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(int-get \"target\" 42)"));
}
@@ -54,7 +70,7 @@ public class PatioInstructionSerializerTests {
public void canSerializeLocalCopy() {
var instruction = Patio.localCopy("target", "x");
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(local-copy \"target\" \"x\")"));
}
@@ -63,7 +79,7 @@ public class PatioInstructionSerializerTests {
public void canSerializeMemoryLoad() {
var instruction = Patio.memoryLoad("target", "pointer", 4);
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(memory-load \"target\" pointer=\"pointer\" offset=4)"));
}
@@ -72,7 +88,7 @@ public class PatioInstructionSerializerTests {
public void canSerializeMemoryStore() {
var instruction = Patio.memoryStore("pointer", 4, "value");
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(memory-store pointer=\"pointer\" offset=4 value=\"value\")"));
}
@@ -81,7 +97,7 @@ public class PatioInstructionSerializerTests {
public void canSerializeReturn() {
var instruction = Patio.return_("value");
- var string = serialize(instruction);
+ var string = PatioTesting.serializeInstruction(instruction);
assertThat(string, equalTo("(return \"value\")"));
}
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java
index 5a0a4670..e571d49a 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioTesting.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioTesting.java
@@ -1,5 +1,6 @@
package org.shedlang.compiler.patio;
+import org.shedlang.compiler.patio.instructions.PatioCompilationUnitElement;
import org.shedlang.compiler.patio.instructions.PatioInstruction;
class PatioTesting {
@@ -8,6 +9,13 @@ class PatioTesting {
static String serialize(PatioCodeGenContext context) {
var builder = new StringBuilder();
+ builder.append("Compilation unit elements:\n\n");
+ for (var element : context.compilationUnitContext().elementsByName().values()) {
+ PatioInstructionSerializer.serializeCompilationUnitElement(element, builder);
+ builder.append("\n");
+ }
+ builder.append("\n");
+
builder.append("Instructions:\n\n");
PatioInstructionSerializer.serializeInstructions(
context.functionContext().instructions(),
@@ -24,7 +32,13 @@ class PatioTesting {
return builder.toString();
}
- static String serialize(PatioInstruction instruction) {
+ static String serializeCompilationUnitElement(PatioCompilationUnitElement element) {
+ var builder = new StringBuilder();
+ PatioInstructionSerializer.serializeCompilationUnitElement(element, builder);
+ return builder.toString();
+ }
+
+ static String serializeInstruction(PatioInstruction instruction) {
var builder = new StringBuilder();
PatioInstructionSerializer.serializeInstruction(instruction, builder);
return builder.toString();