aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-22 10:01:51 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-22 10:01:51 +0000
commit867dd179530e90ae16fe481c0abc58dc8f023f94 (patch)
tree66ebe9e2df599bceeecb36506cdf65563c85a895 /src
parent2dba29dfa74c32a3102a9317455e16cd9a5a35a7 (diff)
Add Shid instructions to store and load methods
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java6
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidSerializer.java14
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java19
-rw-r--r--src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodStore.java19
-rw-r--r--src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java8
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenNamespaceTests.java2
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenStructDefinitionTests.java5
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java24
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/Shid.java14
-rw-r--r--src/test/java/org/shedlang/compiler/shid/instructions/ShidMatchers.java22
11 files changed, 126 insertions, 9 deletions
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
index 280e94b8..8c9d5f96 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
@@ -599,9 +599,9 @@ public class ShidCodeGen {
);
context = context.addInstruction(
- new ShidNamespaceFieldStore(
- structType.namespaceName(),
- shidFunctionName,
+ new ShidMethodStore(
+ structType,
+ methodNode.name(),
methodNode.source()
)
);
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
index 1e36885e..83ec1821 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java
@@ -207,6 +207,20 @@ public class ShidSerializer {
case ShidLoopContinue loopContinue ->
treeList(treeIdentifier("continue"));
+ case ShidMethodLoad methodLoad ->
+ treeList(
+ treeIdentifier("method-load"),
+ structTypeToTree(methodLoad.type()),
+ treeString(methodLoad.methodName())
+ );
+
+ case ShidMethodStore methodStore ->
+ treeList(
+ treeIdentifier("method-store"),
+ structTypeToTree(methodStore.type()),
+ treeString(methodStore.methodName())
+ );
+
case ShidNamespaceFieldStore namespaceFieldStore ->
treeList(
treeIdentifier("namespace-field-store"),
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
index 86fdca54..b58defdd 100644
--- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidInstruction.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.shid.instructions;
import org.shedlang.compiler.sources.Source;
public sealed interface ShidInstruction
- permits ShidEffectHandlerBreak, ShidBlock, ShidCall, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntNotEqual, ShidIntSub, ShidLocalDrop, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore {
+ permits ShidBlock, ShidCall, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntNotEqual, ShidIntSub, ShidLocalDrop, ShidLocalLoad, ShidLocalStore, ShidLoop, ShidLoopBreak, ShidLoopContinue, ShidMethodLoad, ShidMethodStore, ShidNamespaceFieldStore, ShidNamespaceImport, ShidPushBool, ShidPushInt, ShidPushString, ShidPushUnit, ShidResume, ShidReturn, ShidStringAdd, ShidSwitch, ShidTry, ShidTupleCreate, ShidTupleGet, ShidVarLoad, ShidVarStore {
/**
* If an instruction is a terminator, execution is guaranteed to leave the
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java
new file mode 100644
index 00000000..79f2307f
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java
@@ -0,0 +1,19 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+import org.shedlang.compiler.types.SimpleStructType;
+
+/**
+ * Load the method with the given name for the given type and push it onto the
+ * temporary value stack.
+ */
+public record ShidMethodLoad(
+ SimpleStructType type,
+ String methodName,
+ Source source
+) implements ShidInstruction {
+ @Override
+ public boolean isBlockTerminator() {
+ return false;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodStore.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodStore.java
new file mode 100644
index 00000000..fedcf511
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodStore.java
@@ -0,0 +1,19 @@
+package org.shedlang.compiler.shid.instructions;
+
+import org.shedlang.compiler.sources.Source;
+import org.shedlang.compiler.types.SimpleStructType;
+
+/**
+ * Pop a value from the temporary value stack and store it as a method with the
+ * given name for the given type.
+ */
+public record ShidMethodStore(
+ SimpleStructType type,
+ String methodName,
+ Source source
+) implements ShidInstruction {
+ @Override
+ public boolean isBlockTerminator() {
+ return false;
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
index 7b565610..61e7eada 100644
--- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
+++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java
@@ -521,6 +521,14 @@ public class ShidToPatio {
yield context;
}
+ case ShidMethodLoad methodLoad -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+
+ case ShidMethodStore methodStore -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+
case ShidNamespaceFieldStore namespaceFieldStore -> {
var fieldValueResult = context.temporaryPop();
var fieldValue = fieldValueResult.value();
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
@@ -328,6 +328,30 @@ public class ShidSerializerTests {
}
@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(
new NamespaceName(List.of("Example", "Lib")),
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<ShidInstruction> 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<ShidInstruction> 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<ShidInstruction> isShidNamespaceFieldStore(
NamespaceName namespaceName,
String fieldName