From 3227422c3f62cba3c414b5ce0bf71137553cdf6b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Jul 2023 16:47:10 +0100 Subject: Add vars to Patio --- .../shedlang/compiler/patio/PatioExampleTests.java | 43 ++++++++++++++++++++++ .../compiler/patio/PatioSerializerTests.java | 24 ++++++++++++ .../compiler/patio/instructions/Patio.java | 20 ++++++++++ 3 files changed, 87 insertions(+) (limited to 'src/test/java') 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 + ); + } } -- cgit v1.2.3