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 ++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/shid/instructions/ShidDeclareNativeType.java (limited to 'src/main/java') 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, -- cgit v1.2.3