diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-03-08 11:39:35 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-03-08 11:39:35 +0000 |
| commit | 3fcd30f12e7ef31017603f4f954540e6696d77b1 (patch) | |
| tree | 9724a46a51d646cfb91abfe0141d7614d1ef09b3 /src/main/java | |
| parent | 2d03ff53b4f1c73bfdb02045d5b632a80f8caeeb (diff) | |
Compile native type methods from Shed to Shid
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java | 63 |
1 files changed, 56 insertions, 7 deletions
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()); |
