aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java25
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java40
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java18
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java8
4 files changed, 91 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java
new file mode 100644
index 00000000..ac51b8f1
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarLoadTests.java
@@ -0,0 +1,25 @@
+package org.shedlang.compiler.patio;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+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 static org.shedlang.compiler.patio.PatioTesting.serialize;
+
+@ExtendWith({SnapshotResolver.class})
+public class PatioCodeGenVarLoadTests {
+ @Test
+ public void canGenerateCodeForVarLoad(Snapshotter snapshotter) throws IOException {
+ var stackIr = Instructions.varLoad("x");
+ var context = PatioCodeGenContext.EMPTY
+ .addStackIrVarIfMissing("x");
+
+ var result = PatioCodeGen.generateInstruction(stackIr, context);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java
new file mode 100644
index 00000000..837be982
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenVarStoreTests.java
@@ -0,0 +1,40 @@
+package org.shedlang.compiler.patio;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
+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 static org.shedlang.compiler.patio.PatioTesting.serialize;
+
+@ExtendWith({SnapshotResolver.class})
+public class PatioCodeGenVarStoreTests {
+ @Test
+ public void canGenerateCodeForVarStore(Snapshotter snapshotter) throws IOException {
+ var stackIr = Instructions.varStore("x");
+ var context = PatioCodeGenContext.EMPTY
+ .temporaryPush(new PatioLocalRef("a"))
+ .temporaryPush(new PatioLocalRef("b"));
+
+ var result = PatioCodeGen.generateInstruction(stackIr, context);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
+
+ @Test
+ public void canStoreIntoVarMultipleTimes(Snapshotter snapshotter) throws IOException {
+ var stackIr = Instructions.varStore("x");
+ var context = PatioCodeGenContext.EMPTY
+ .temporaryPush(new PatioLocalRef("a"))
+ .temporaryPush(new PatioLocalRef("b"));
+
+ var result = PatioCodeGen.generateInstruction(stackIr, context);
+ result = PatioCodeGen.generateInstruction(stackIr, result);
+
+ snapshotter.assertSnapshot(serialize(result));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java
index e4893c31..aff45481 100644
--- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java
@@ -244,6 +244,24 @@ public class InstructionSerializerTests {
assertThat(string, equalTo("(string-add)"));
}
+ @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);
diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java
index 131207b8..18526f4a 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java
@@ -88,4 +88,12 @@ public class Instructions {
public static Instruction stringAdd() {
return new StringAddInstruction(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);
+ }
}