From c6005eba5727e711516ccc17fa64cf153a602260 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Mar 2025 18:40:55 +0000 Subject: Compile typed native type declaration node into Shid --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 19 ++++++++++++- .../ShidCodeGenNativeTypeDeclarationTests.java | 33 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java index 356323cc..05f42ae2 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -647,7 +647,7 @@ public class ShidCodeGen { compileNativeFunctionDeclaration(nativeFunctionDeclaration, context); case TypedNativeTypeDeclarationNode nativeTypeDeclarationNode -> - throw new UnsupportedOperationException("TODO"); + compileNativeTypeDeclaration(nativeTypeDeclarationNode, context); case TypedStructDefinitionNode structDefinition -> compileStructDefinition(structDefinition, context); @@ -680,6 +680,23 @@ public class ShidCodeGen { return context.addInstruction(storeInstruction); } + private static Context compileNativeTypeDeclaration( + TypedNativeTypeDeclarationNode node, + Context context + ) { + var nativeType = node.typeOrConstructor().toType(); + + context = context.addInstruction(new ShidDeclareNativeType( + nativeType, + node.source() + )); + + var localName = context.shedVariableToLocal(nativeType.name()); + var storeInstruction = new ShidLocalStore(localName, node.source()); + + return context.addInstruction(storeInstruction); + } + private static Context compileReference(TypedReferenceNode node, Context context) { var shidVariable = context.shedVariableToShidVariable(node.name()); diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java new file mode 100644 index 00000000..6a4070c5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenNativeTypeDeclarationTests.java @@ -0,0 +1,33 @@ +package org.shedlang.compiler.shid; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.TypedNativeTypeDeclarationNode; +import org.shedlang.compiler.shid.instructions.ShidDeclareNativeType; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.Types; + +import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalStore; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.*; + +public class ShidCodeGenNativeTypeDeclarationTests { + @Test + public void nativeTypeDeclarationIsCompiledToDeclareNativeTypeInstruction() { + var nativeType = Types.nativeType(NamespaceName.of("Example", "Project"), "Array"); + var node = TypedNativeTypeDeclarationNode.builder() + .withType(nativeType) + .build(); + var context = ShidCodeGen.Context.EMPTY + .addShedVal("Array"); + + var result = ShidCodeGen.compileNamespaceStatement(node, context); + + assertThat(result.instructions(), isSequence( + instanceOf( + ShidDeclareNativeType.class, + has("type", x -> x.type(), equalTo(nativeType)) + ), + isShidLocalStore("Array__0") + )); + } +} -- cgit v1.2.3