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 --- .../java/org/shedlang/compiler/shid/ShidCodeGen.java | 6 +++--- .../org/shedlang/compiler/shid/ShidSerializer.java | 14 ++++++++++++++ .../compiler/shid/instructions/ShidInstruction.java | 2 +- .../compiler/shid/instructions/ShidMethodLoad.java | 19 +++++++++++++++++++ .../compiler/shid/instructions/ShidMethodStore.java | 19 +++++++++++++++++++ .../shedlang/compiler/shidtopatio/ShidToPatio.java | 8 ++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodStore.java (limited to 'src/main/java') 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(); -- cgit v1.2.3