aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-09-28 14:07:01 +0100
committerMichael Williamson <mike@zwobble.org>2023-09-28 14:07:01 +0100
commit49aee72f35f1ffdfc90a89162a500193746ef467 (patch)
treed4b4c2c1b013dde4cbc396c35e14bca7b57c1013
parent09da904f2de0b16f883add671469989bafc726e9 (diff)
Generate stack IR for field accesses
-rw-r--r--src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java18
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java8
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java32
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java22
5 files changed, 80 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
index a78c5eaf..1a194db8 100644
--- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java
@@ -258,7 +258,7 @@ public class PatioCodeGen {
);
}
- case FieldLoadInstruction.Receiver.Struct struct -> {
+ case FieldLoadInstruction.Receiver.Struct receiverStruct -> {
throw new UnsupportedOperationException("TODO");
}
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 8f46ca1d..8e0ec59e 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -4,6 +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.util.P;
import java.util.List;
@@ -122,7 +123,7 @@ public class StackCompiler {
compileCall(typedCallNode, context);
case TypedFieldAccessNode typedFieldAccessNode ->
- throw new UnsupportedOperationException("TODO");
+ compileFieldAccess(typedFieldAccessNode, context);
case TypedIfNode typedIfNode ->
compileIf(typedIfNode, context);
@@ -150,6 +151,21 @@ public class StackCompiler {
return context;
}
+ private static Context compileFieldAccess(
+ TypedFieldAccessNode node,
+ Context context
+ ) {
+ context = compileExpression(node.receiver(), context);
+ // TODO: handle not a struct type
+ var receiverType = (StructType) node.receiver().type();
+ context = context.addInstruction(new FieldLoadInstruction(
+ new FieldLoadInstruction.Receiver.Struct(receiverType),
+ node.fieldName(),
+ node.source()
+ ));
+ return context;
+ }
+
private static Context compileFunctionDefinition(
TypedFunctionDefinitionNode node,
Context context
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
index 42a46859..110696a7 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -65,6 +65,14 @@ public class Typed {
return new TypedExpressionStatementNode(expression, Sources.NULL);
}
+ public static TypedFieldAccessNode fieldAccess(
+ TypedExpressionNode receiver,
+ String fieldName,
+ Type type
+ ) {
+ return new TypedFieldAccessNode(receiver, fieldName, type, Sources.NULL);
+ }
+
public static TypedFunctionReturnNode functionReturn(TypedExpressionNode expression) {
return new TypedFunctionReturnNode(expression, Sources.NULL);
}
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java
new file mode 100644
index 00000000..a9d76db3
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerFieldAccessTests.java
@@ -0,0 +1,32 @@
+package org.shedlang.compiler.stackir;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+
+import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isFieldLoadInstruction;
+import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalLoadInstruction;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class StackCompilerFieldAccessTests {
+ @Test
+ public void structFieldAccessIsCompiledToFieldLoad() {
+ var structType = Types.struct(NamespaceName.of("Example", "Project"), "User");
+ var node = Typed.fieldAccess(
+ Typed.reference("user", structType),
+ "name",
+ Types.STRING
+ );
+ var context = StackCompiler.Context.EMPTY
+ .addShedVariable("user");
+
+ var result = StackCompiler.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isLocalLoadInstruction("user__0"),
+ isFieldLoadInstruction(structType, "name")
+ ));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
index b50e4903..3d1689e1 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
@@ -1,6 +1,7 @@
package org.shedlang.compiler.stackir.instructions;
import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.StructType;
import org.zwobble.precisely.Matcher;
import java.math.BigInteger;
@@ -37,6 +38,27 @@ public class InstructionMatchers {
return instanceOf(DuplicateInstruction.class);
}
+ public static Matcher<Instruction> isFieldLoadInstruction(
+ FieldLoadInstruction.Receiver receiver,
+ String fieldName
+ ) {
+ return instanceOf(
+ FieldLoadInstruction.class,
+ has("receiver", x -> x.receiver(), equalTo(receiver)),
+ has("fieldName", x -> x.fieldName(), equalTo(fieldName))
+ );
+ }
+
+ public static Matcher<Instruction> isFieldLoadInstruction(
+ StructType receiverType,
+ String fieldName
+ ) {
+ return isFieldLoadInstruction(
+ new FieldLoadInstruction.Receiver.Struct(receiverType),
+ fieldName
+ );
+ }
+
public static Matcher<Instruction> isIfInstruction(
Matcher<Iterable<? extends Instruction>> trueBody,
Matcher<Iterable<? extends Instruction>> falseBody