From 538d0c883cb5afaeef7a58a66a876b7c2cc80708 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 26 Dec 2023 18:35:46 +0000 Subject: Support constructed type field accesses in Shid code gen --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 27 ++++++++++++++-------- 1 file changed, 17 insertions(+), 10 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 8c42982b..501e12e5 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -190,7 +190,22 @@ public class ShidCodeGen { ) { context = compileExpression(node.receiver(), context); // TODO: handle not a type with fields - var receiver = switch (node.receiver().type()) { + var receiverType = node.receiver().type(); + var receiver = receiverTypeToReceiver(receiverType); + + context = context.addInstruction(new ShidFieldLoad( + receiver, + node.fieldName(), + node.source() + )); + return context; + } + + private static ShidFieldLoad.Receiver receiverTypeToReceiver(Type receiverType) { + return switch (receiverType) { + case ConstructedType constructedType -> + receiverTypeToReceiver(constructedType.genericType()); + case NamespaceType namespaceType -> new ShidFieldLoad.Receiver.Namespace(namespaceType.namespaceName()); @@ -205,16 +220,8 @@ public class ShidCodeGen { } } - default -> - throw new RuntimeException("not a type with fields"); + default -> throw new RuntimeException("not a type with fields"); }; - - context = context.addInstruction(new ShidFieldLoad( - receiver, - node.fieldName(), - node.source() - )); - return context; } private static Context compileFunction( -- cgit v1.2.3