From 35b78f02a0808e420e0f040f44c649b7354cd49d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Dec 2024 10:15:37 +0000 Subject: Generate Shid code for method call with no args --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 79 ++++++++++++++++++++-- 1 file changed, 73 insertions(+), 6 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 64a61a03..1fe776d4 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -396,12 +396,60 @@ public class ShidCodeGen { var receiverType = node.receiver().type(); var receiver = receiverTypeToReceiver(receiverType); - context = context.addInstruction(new ShidFieldLoad( - receiver, - node.memberName(), - node.source() - )); - return context; + return switch (node.memberKind()) { + case FIELD -> { + yield context.addInstruction(new ShidFieldLoad( + receiver, + node.memberName(), + node.source() + )); + } + case METHOD -> { + // TODO: handle not a struct type + var structType = ((ShidFieldLoad.Receiver.Struct) receiver).type(); + + var receiverLocalNameResult = context.generateShidVal("receiver"); + var receiverLocalName = receiverLocalNameResult.value(); + context = receiverLocalNameResult.context(); + + context = context.addInstruction( + new ShidLocalStore(receiverLocalName, node.source()) + ); + + context = context.enterBlock(List.of(), node.source()); + + context = context.addInstruction( + new ShidMethodLoad(structType, node.memberName(), node.source()) + ); + context = context.addInstruction( + new ShidLocalLoad(receiverLocalName, node.source()) + ); + + context = context.addInstruction( + new ShidCall(1, List.of(), node.source()) + ); + + context = context.addInstruction(new ShidReturn(node.source())); + + var bodyResult = context.exitBlock(); + var body = bodyResult.value(); + context = bodyResult.context(); + + yield context.addInstruction( + new ShidDefineFunction( + node.memberName(), + List.of(), + List.of(), + FreeVariables.builder() + .plus(receiverLocalName, node.source()) + .build(), + body, + ShidDefineFunction.Variant.FUNCTION, + node.source() + ) + ); + } + }; } private static ShidFieldLoad.Receiver receiverTypeToReceiver(Type receiverType) { @@ -873,6 +921,15 @@ public class ShidCodeGen { this.source ); } + + public BlockContext addShidVal(String localName) { + return new BlockContext( + this.instructions, + this.shedVariablesByName, + this.boundVariables.plus(new ShidVariable(localName, ShidVariable.Variant.VAL)), + this.source + ); + } } public static class Context { @@ -963,6 +1020,16 @@ public class ShidCodeGen { ); } + public Result generateShidVal(String baseName) { + var localName = generateLocalNameForVariable(baseName); + + return new Context( + this.blockStack, + this.block.addShidVal(localName), + this.localNames.plus(localName) + ).toResult(localName); + } + private String generateLocalNameForVariable(String variableName) { var index = 0; while (true) { -- cgit v1.2.3