From 88fb47120e293fad7cf3ecf5439a28136cb93eb2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Mar 2025 18:37:40 +0000 Subject: Add Shid instruction to declare native type --- .../org/shedlang/compiler/shid/ShidSerializer.java | 11 +++++++++++ .../shid/instructions/ShidDeclareNativeType.java | 18 ++++++++++++++++++ .../compiler/shid/instructions/ShidInstruction.java | 2 +- .../org/shedlang/compiler/shidtopatio/ShidToPatio.java | 4 ++++ .../shedlang/compiler/shid/ShidSerializerTests.java | 12 ++++++++++++ .../org/shedlang/compiler/shid/instructions/Shid.java | 10 ++++++++++ 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeType.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java index fa38365f..6a60ccc8 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.shid; import org.shedlang.compiler.shid.instructions.*; import org.shedlang.compiler.treetext.Tree; import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.SimpleNativeType; import org.shedlang.compiler.types.SimpleStructType; import org.shedlang.compiler.types.UserDefinedEffect; @@ -83,6 +84,12 @@ public class ShidSerializer { namedParamsToTree(declareNativeFunction.namedParams()) ); + case ShidDeclareNativeType declareNativeType -> + treeList( + treeIdentifier("declare-native-type"), + nativeTypeToTree(declareNativeType.type()) + ); + case ShidDefineEffect defineEffectInstruction -> treeList( treeIdentifier("define-effect"), @@ -363,6 +370,10 @@ public class ShidSerializer { return treeString(effect.describe()); } + private static Tree nativeTypeToTree(SimpleNativeType type) { + return treeString(type.describe()); + } + private static Tree structTypeToTree(SimpleStructType type) { return treeString(type.describe()); } diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeType.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeType.java new file mode 100644 index 00000000..d4ffbeda --- /dev/null +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeType.java @@ -0,0 +1,18 @@ +package org.shedlang.compiler.shid.instructions; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.SimpleNativeType; + +/** + * Push a native native type value onto the temporary value stack with the given + * declaration. + */ +public record ShidDeclareNativeType( + SimpleNativeType type, + Source source +) implements ShidInstruction { + @Override + public boolean isBlockTerminator() { + return false; + } +} 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 682a7e6a..36de2a53 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 ShidBlock, ShidCall, ShidDeclareNativeFunction, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntGreaterThan, ShidIntGreaterThanOrEqual, ShidIntLessThan, ShidIntLessThanOrEqual, ShidIntMultiply, 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 { + permits ShidBlock, ShidCall, ShidDeclareNativeFunction, ShidDeclareNativeType, ShidDefineEffect, ShidDefineFunction, ShidDefineStruct, ShidDiscard, ShidDuplicate, ShidEffectHandlerBreak, ShidFieldLoad, ShidIf, ShidIntAdd, ShidIntEquals, ShidIntGreaterThan, ShidIntGreaterThanOrEqual, ShidIntLessThan, ShidIntLessThanOrEqual, ShidIntMultiply, 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/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java index 9d585912..cd315f86 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java @@ -223,6 +223,10 @@ public class ShidToPatio { ).temporaryPush(destination); } + case ShidDeclareNativeType declareNativeType -> { + throw new UnsupportedOperationException("TODO"); + } + case ShidDefineEffect defineEffect -> { var destinationResult = context.allocateVal( ShidToPatioLocalSpace.USER_DEFINED_EFFECT, diff --git a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java index 3fc45b0b..89bfd1f7 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidSerializerTests.java @@ -65,6 +65,18 @@ public class ShidSerializerTests { )""")); } + @Test + public void canSerializeDeclareNativeType() { + var instruction = Shid.declareNativeType( + Types.nativeType(NamespaceName.of("Example", "Project"), "Array") + ); + + var string = serialize(instruction); + + assertThat(string, equalTo(""" + (declare-native-type "Example/Project.Array")""")); + } + @Test public void canSerializeDefineEffect() { var instruction = Shid.defineEffect( 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 51f8ba00..10a020bd 100644 --- a/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java +++ b/src/test/java/org/shedlang/compiler/shid/instructions/Shid.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.SimpleNativeType; import org.shedlang.compiler.types.SimpleStructType; import org.shedlang.compiler.types.UserDefinedEffect; @@ -24,6 +25,15 @@ public class Shid { return new ShidCall(positionalArgCount, namedArgNames, Sources.NULL); } + public static ShidInstruction declareNativeType( + SimpleNativeType type + ) { + return new ShidDeclareNativeType( + type, + Sources.NULL + ); + } + public static ShidInstruction defineEffect(UserDefinedEffect effect) { return new ShidDefineEffect(effect, Sources.NULL); } -- cgit v1.2.3