From 867dd179530e90ae16fe481c0abc58dc8f023f94 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Dec 2024 10:01:51 +0000 Subject: Add Shid instructions to store and load methods --- .../compiler/shid/ShidCodeGenNamespaceTests.java | 2 +- .../shid/ShidCodeGenStructDefinitionTests.java | 5 +---- .../compiler/shid/ShidSerializerTests.java | 24 ++++++++++++++++++++++ .../shedlang/compiler/shid/instructions/Shid.java | 14 +++++++++++++ .../compiler/shid/instructions/ShidMatchers.java | 22 ++++++++++++++++++++ 5 files changed, 62 insertions(+), 5 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java index e760bdae..e389a34c 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java @@ -156,7 +156,7 @@ public class ShidCodeGenNamespaceTests { ), isShidLocalStore("$methods__Example_Project__User__name__0"), isShidLocalLoad("$methods__Example_Project__User__name__0"), - isShidNamespaceFieldStore(namespaceName, "$methods__Example_Project__User__name__0") + isShidMethodStore(structType, "name") ))); } } diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java index 0ffea260..df6dee0e 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java @@ -82,10 +82,7 @@ public class ShidCodeGenStructDefinitionTests { ), isShidLocalStore("$methods__Example_Project__User__name__0"), isShidLocalLoad("$methods__Example_Project__User__name__0"), - isShidNamespaceFieldStore( - NamespaceName.of("Example", "Project"), - "$methods__Example_Project__User__name__0" - ) + isShidMethodStore(structType, "name") )); } } diff --git a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java index e97a503a..6018fa88 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java @@ -327,6 +327,30 @@ public class ShidSerializerTests { assertThat(string, equalTo("(continue)")); } + @Test + public void canSerializeMethodLoad() { + var instruction = Shid.methodLoad( + Types.struct(NamespaceName.of("Example", "Lib"), "User"), + "name" + ); + + var string = serialize(instruction); + + assertThat(string, equalTo("(method-load \"Example/Lib.User\" \"name\")")); + } + + @Test + public void canSerializeMethodStore() { + var instruction = Shid.methodStore( + Types.struct(NamespaceName.of("Example", "Lib"), "User"), + "name" + ); + + var string = serialize(instruction); + + assertThat(string, equalTo("(method-store \"Example/Lib.User\" \"name\")")); + } + @Test public void canSerializeNamespaceFieldStore() { var instruction = Shid.namespaceFieldStore( diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java index ee78a3e3..70db156b 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java @@ -127,6 +127,20 @@ public class Shid { return new ShidLoopContinue(Sources.NULL); } + public static ShidInstruction methodLoad( + SimpleStructType type, + String methodName + ) { + return new ShidMethodLoad(type, methodName, Sources.NULL); + } + + public static ShidInstruction methodStore( + SimpleStructType type, + String methodName + ) { + return new ShidMethodStore(type, methodName, Sources.NULL); + } + public static ShidInstruction namespaceFieldStore(NamespaceName namespaceName, String fieldName) { return new ShidNamespaceFieldStore(namespaceName, fieldName, Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java index 43226831..8b4a212d 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java @@ -134,6 +134,28 @@ public class ShidMatchers { return instanceOf(ShidLoopContinue.class); } + public static Matcher isShidMethodLoad( + SimpleStructType type, + String methodName + ) { + return instanceOf( + ShidMethodLoad.class, + has("type", x -> x.type(), equalTo(type)), + has("methodName", x -> x.methodName(), equalTo(methodName)) + ); + } + + public static Matcher isShidMethodStore( + SimpleStructType type, + String methodName + ) { + return instanceOf( + ShidMethodStore.class, + has("type", x -> x.type(), equalTo(type)), + has("methodName", x -> x.methodName(), equalTo(methodName)) + ); + } + public static Matcher isShidNamespaceFieldStore( NamespaceName namespaceName, String fieldName -- cgit v1.2.3