From e73983a9a1862a99f2efe3f1c6ed81f978d57403 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Sep 2023 13:55:15 +0100 Subject: Create separate notion of receiver for FieldLoadInstruction --- .../org/shedlang/compiler/patio/PatioCodeGen.java | 20 ++++++++++++-------- .../compiler/stackir/InstructionSerializer.java | 8 +++++++- .../org/shedlang/compiler/stackir/StackCompiler.java | 6 +++++- .../stackir/instructions/FieldLoadInstruction.java | 5 ++++- 4 files changed, 28 insertions(+), 11 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 684ed468..41df54f4 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -246,14 +246,18 @@ public class PatioCodeGen { var target = targetResult.value(); context = targetResult.context(); - context = PatioCodeGenNamespaces.fieldLoad( - target, - receiver, - fieldLoad.namespaceName(), - fieldLoad.fieldName(), - fieldLoad.source(), - context - ); + switch (fieldLoad.receiver()) { + case FieldLoadInstruction.Receiver.Namespace receiverNamespace -> { + context = PatioCodeGenNamespaces.fieldLoad( + target, + receiver, + receiverNamespace.name(), + fieldLoad.fieldName(), + fieldLoad.source(), + context + ); + } + } context = context.temporaryPush(target); diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index d8ded6d3..53d0ce44 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -84,7 +84,13 @@ public class InstructionSerializer { case FieldLoadInstruction fieldLoad -> treeList( treeIdentifier("field-load"), - treeString(fieldLoad.namespaceName().toString()), + switch (fieldLoad.receiver()) { + case FieldLoadInstruction.Receiver.Namespace namespace -> + treeList( + treeIdentifier("namespace"), + treeString(namespace.name().toString()) + ); + }, treeString(fieldLoad.fieldName()) ); diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 35d6f497..8f46ca1d 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -310,7 +310,11 @@ public class StackCompiler { context = context .addInstruction(new NamespaceImportInstruction(ShedBuiltins.NAMESPACE_NAME, node.source())) - .addInstruction(new FieldLoadInstruction(ShedBuiltins.NAMESPACE_NAME, ShedBuiltins.PRINT_NAME, node.source())) + .addInstruction(new FieldLoadInstruction( + new FieldLoadInstruction.Receiver.Namespace(ShedBuiltins.NAMESPACE_NAME), + ShedBuiltins.PRINT_NAME, + node.source() + )) .addInstruction(new LocalStoreInstruction(printLocalName, node.source())); for (var boundVariableName : node.boundVariableNames()) { diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java index 98ea9952..6428e30e 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java @@ -8,8 +8,11 @@ import org.shedlang.compiler.types.NamespaceName; * push the field's value onto the stack. */ public record FieldLoadInstruction( - NamespaceName namespaceName, + Receiver receiver, String fieldName, Source source ) implements Instruction { + public sealed interface Receiver { + record Namespace(NamespaceName name) implements Receiver {} + } } -- cgit v1.2.3