aboutsummaryrefslogtreecommitdiff
path: root/src/test/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/test/java
parent4dcf0337af8b7cdb9de784d8347399708eae18b8 (diff)
Generate Shid code for method call with no args
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java44
1 files changed, 41 insertions, 3 deletions
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java
index 74295e01..79453d76 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenMemberAccessTests.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.shid;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
import org.shedlang.compiler.shid.instructions.ShidFieldLoad;
import org.shedlang.compiler.shid.instructions.ShidMatchers;
import org.shedlang.compiler.types.Effects;
@@ -11,10 +12,10 @@ import org.shedlang.compiler.types.Types;
import java.util.List;
-import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidFieldLoad;
-import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalLoad;
+import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
-import static org.zwobble.precisely.Matchers.isSequence;
+import static org.zwobble.precisely.Matchers.*;
+import static org.zwobble.precisely.Matchers.has;
public class ShidCodeGenMemberAccessTests {
@Test
@@ -59,6 +60,43 @@ public class ShidCodeGenMemberAccessTests {
}
@Test
+ public void structMethodAccessIsCompiledToDefineFunctionInstruction() {
+ var namespaceName = NamespaceName.of("Example", "Project");
+ var structType = Types.struct(namespaceName, "User");
+ var node = Typed.methodAccess(
+ Typed.reference("user", structType),
+ "name",
+ Types.STRING
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("user");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ // TODO: optimise calls
+ // TODO: handle params
+ assertThat(result.instructions(), isSequence(
+ isShidLocalLoad("user__0"),
+ isShidLocalStore("receiver__0"),
+ instanceOf(
+ ShidDefineFunction.class,
+ has("name", x -> x.name(), equalTo("name")),
+ has("positionalParams", x -> x.positionalParams(), isSequence()),
+ has("namedParams", x -> x.namedParams(), isSequence()),
+ has("freeVariables", x -> x.freeVariables(), containsExactly(
+ has("user", x -> x.name(), equalTo("receiver__0"))
+ )),
+ has("body", x -> x.body(), isShidBlock(isSequence(
+ isShidMethodLoad(structType, "name"),
+ isShidLocalLoad("receiver__0"),
+ isShidCall(1, List.of()),
+ isShidReturn()
+ )))
+ )
+ ));
+ }
+
+ @Test
public void constructedTypeFieldAccessIsCompiledToFieldLoad() {
var structType = Types.struct(NamespaceName.of("Example", "Project"), "Box");
var typeConstructor = Types.typeConstructor(