From 386df535db2970c6ab8acc47adaf803d61934a3f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 21 Oct 2023 13:47:49 +0100 Subject: Generate stack IR for field access on namespace --- .../org/shedlang/compiler/stackir/StackCompiler.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 7a9fc502..130b5fdf 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -4,7 +4,7 @@ import org.pcollections.*; import org.shedlang.compiler.ShedBuiltins; import org.shedlang.compiler.ast.typed.*; import org.shedlang.compiler.stackir.instructions.*; -import org.shedlang.compiler.types.StructType; +import org.shedlang.compiler.types.*; import org.shedlang.compiler.util.Iterables; import org.shedlang.compiler.util.P; @@ -161,10 +161,20 @@ public class StackCompiler { Context context ) { context = compileExpression(node.receiver(), context); - // TODO: handle not a struct type - var receiverType = (StructType) node.receiver().type(); + // TODO: handle not a type with fields + var receiver = switch (node.receiver().type()) { + case NamespaceType namespaceType -> + new FieldLoadInstruction.Receiver.Namespace(namespaceType.namespaceName()); + + case StructType structType -> + new FieldLoadInstruction.Receiver.Struct(structType); + + default -> + throw new RuntimeException("not a type with fields"); + }; + context = context.addInstruction(new FieldLoadInstruction( - new FieldLoadInstruction.Receiver.Struct(receiverType), + receiver, node.fieldName(), node.source() )); -- cgit v1.2.3