aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-22 10:15:37 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-22 10:15:37 +0000
commit35b78f02a0808e420e0f040f44c649b7354cd49d (patch)
treec213987d4b3124352d8138518bc51bfbfe64e647 /src/main/java
parent4dcf0337af8b7cdb9de784d8347399708eae18b8 (diff)
Generate Shid code for method call with no args
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java79
1 files changed, 73 insertions, 6 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) {