diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-30 16:47:10 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-30 16:47:10 +0100 |
| commit | 3227422c3f62cba3c414b5ce0bf71137553cdf6b (patch) | |
| tree | c63797e887bdf5cfe2cfc2567ff81746cd12e44e /src/test/java | |
| parent | cc21a3479f84cf7c829303132a68fb802676814f (diff) | |
Add vars to Patio
Diffstat (limited to 'src/test/java')
3 files changed, 87 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index c4a40645..abb0807f 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -48,5 +48,48 @@ public abstract class PatioExampleTests { assertThat(result.exitCode(), equalTo(47)); } + @Test + public void vars() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.INT) + .addLocal(Patio.var("x", PatioTypes.INT)) + .addLocal(Patio.val("y", PatioTypes.INT)) + .addLocal(Patio.val("z", PatioTypes.INT)) + .addLocal(Patio.val("r", PatioTypes.INT)) + .addBodyInstruction(Patio.varStore( + Patio.localRef("x"), + Patio.int_(42) + )) + .addBodyInstruction(Patio.varLoad( + Patio.localRef("y"), + Patio.localRef("x") + )) + .addBodyInstruction(Patio.intAdd( + Patio.localRef("z"), + Patio.localRef("y"), + Patio.int_(5) + )) + .addBodyInstruction(Patio.varStore( + Patio.localRef("x"), + Patio.localRef("z") + )) + .addBodyInstruction(Patio.varLoad( + Patio.localRef("r"), + Patio.localRef("x") + )) + .addBodyInstruction(Patio.return_( + Patio.localRef("r") + )) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result.stderr(), equalTo("")); + assertThat(result.stdout(), equalTo("")); + assertThat(result.exitCode(), equalTo(47)); + } + protected abstract ExecutionResult run(PatioCompilationUnit patio) throws IOException, InterruptedException; } diff --git a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java index 92a23504..8f1c4762 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java @@ -416,6 +416,30 @@ public class PatioSerializerTests { assertThat(string, equalTo("(return (local \"value\"))")); } + @Test + public void canSerializeVarLoad() { + var instruction = Patio.varLoad( + Patio.localRef("target"), + Patio.localRef("v") + ); + + var string = PatioTesting.serializeInstruction(instruction); + + assertThat(string, equalTo("(var-load (local \"target\") (local \"v\"))")); + } + + @Test + public void canSerializeVarStore() { + var instruction = Patio.varStore( + Patio.localRef("target"), + Patio.localRef("value") + ); + + var string = PatioTesting.serializeInstruction(instruction); + + assertThat(string, equalTo("(var-store (local \"target\") (local \"value\"))")); + } + // == Operands == @Test diff --git a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java index c1de1477..90249c1d 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -220,4 +220,24 @@ public class Patio { public static PatioLocal.Val val(String name, PatioType type) { return new PatioLocal.Val(name, type); } + + public static PatioLocal.Var var(String name, PatioType type) { + return new PatioLocal.Var(name, type); + } + + public static PatioInstruction varLoad(PatioLocalRef target, PatioLocalRef value) { + return new PatioVarLoad( + target, + value, + Sources.NULL + ); + } + + public static PatioInstruction varStore(PatioLocalRef target, PatioOperand value) { + return new PatioVarStore( + target, + value, + Sources.NULL + ); + } } |
