diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-22 10:15:37 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-22 10:15:37 +0000 |
| commit | 35b78f02a0808e420e0f040f44c649b7354cd49d (patch) | |
| tree | c213987d4b3124352d8138518bc51bfbfe64e647 /src | |
| parent | 4dcf0337af8b7cdb9de784d8347399708eae18b8 (diff) | |
Generate Shid code for method call with no args
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java | 79 | ||||
| -rw-r--r-- | src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java | 44 |
2 files changed, 114 insertions, 9 deletions
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<String> 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) { diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java index 74295e01..79453d76 100644 --- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java +++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.shid; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; +import org.shedlang.compiler.shid.instructions.ShidDefineFunction; import org.shedlang.compiler.shid.instructions.ShidFieldLoad; import org.shedlang.compiler.shid.instructions.ShidMatchers; import org.shedlang.compiler.types.Effects; @@ -11,10 +12,10 @@ import org.shedlang.compiler.types.Types; import java.util.List; -import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidFieldLoad; -import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalLoad; +import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; +import static org.zwobble.precisely.Matchers.*; +import static org.zwobble.precisely.Matchers.has; public class ShidCodeGenMemberAccessTests { @Test @@ -59,6 +60,43 @@ public class ShidCodeGenMemberAccessTests { } @Test + public void structMethodAccessIsCompiledToDefineFunctionInstruction() { + var namespaceName = NamespaceName.of("Example", "Project"); + var structType = Types.struct(namespaceName, "User"); + var node = Typed.methodAccess( + Typed.reference("user", structType), + "name", + Types.STRING + ); + var context = ShidCodeGen.Context.EMPTY + .addShedVal("user"); + + var result = ShidCodeGen.compileExpression(node, context); + + // TODO: optimise calls + // TODO: handle params + assertThat(result.instructions(), isSequence( + isShidLocalLoad("user__0"), + isShidLocalStore("receiver__0"), + instanceOf( + ShidDefineFunction.class, + has("name", x -> x.name(), equalTo("name")), + has("positionalParams", x -> x.positionalParams(), isSequence()), + has("namedParams", x -> x.namedParams(), isSequence()), + has("freeVariables", x -> x.freeVariables(), containsExactly( + has("user", x -> x.name(), equalTo("receiver__0")) + )), + has("body", x -> x.body(), isShidBlock(isSequence( + isShidMethodLoad(structType, "name"), + isShidLocalLoad("receiver__0"), + isShidCall(1, List.of()), + isShidReturn() + ))) + ) + )); + } + + @Test public void constructedTypeFieldAccessIsCompiledToFieldLoad() { var structType = Types.struct(NamespaceName.of("Example", "Project"), "Box"); var typeConstructor = Types.typeConstructor( |
