From 3fcd30f12e7ef31017603f4f954540e6696d77b1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 11:39:35 +0000 Subject: Compile native type methods from Shed to Shid --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 63 +++++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) (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 9987944a..71f43610 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -613,6 +613,8 @@ public class ShidCodeGen { if (statement instanceof TypedStructDefinitionNode structDefinitionNode) { // TODO: test method hoisting context = compileStructDefinitionMethods(structDefinitionNode, context); + } else if (statement instanceof TypedNativeTypeDeclarationNode nativeTypeDeclarationNode) { + context = compileNativeTypeDeclarationMethods(nativeTypeDeclarationNode, context); } } @@ -665,22 +667,38 @@ public class ShidCodeGen { Context context ) { var signature = node.signature(); - context = context.addInstruction(new ShidDeclareNativeFunction( + context = compileNativeFunction( context.namespaceName(), node.name(), + signature, + node.source(), + context + ); + + var localName = context.shedVariableToLocal(node.name()); + var storeInstruction = new ShidLocalStore(localName, node.source()); + + return context.addInstruction(storeInstruction); + } + + private static Context compileNativeFunction( + NamespaceName namespaceName, + String name, + TypedFunctionTypeNode signature, + Source source, + Context context + ) { + return context.addInstruction(new ShidDeclareNativeFunction( + namespaceName, + name, IntStream.range(0, signature.value().positionalParams().size()) .mapToObj(paramIndex -> new ShidParam("arg" + paramIndex, signature.source())) .toList(), signature.value().namedParams().stream() .map(param -> new ShidParam(param.name(), signature.source())) .toList(), - node.source() + source )); - - var localName = context.shedVariableToLocal(node.name()); - var storeInstruction = new ShidLocalStore(localName, node.source()); - - return context.addInstruction(storeInstruction); } private static Context compileNativeTypeDeclaration( @@ -700,6 +718,37 @@ public class ShidCodeGen { return context.addInstruction(storeInstruction); } + static Context compileNativeTypeDeclarationMethods( + TypedNativeTypeDeclarationNode node, + Context context + ) { + var type = node.typeOrConstructor().toType(); + + for (var methodNode : node.methods()) { + context = compileNativeFunction( + type.namespaceName(), + String.format( + "method__%s__%s", + type.name(), + methodNode.name() + ), + methodNode.signature(), + methodNode.source(), + context + ); + + context = context.addInstruction( + new ShidMethodStore( + type, + methodNode.name(), + methodNode.source() + ) + ); + } + + return context; + } + private static Context compileReference(TypedReferenceNode node, Context context) { var shidVariable = context.shedVariableToShidVariable(node.name()); -- cgit v1.2.3