aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-09-17 18:14:17 +0100
committerMichael Williamson <mike@zwobble.org>2023-09-17 18:14:17 +0100
commit1232307b381528fd21411a602e7a125fdee1fffb (patch)
tree1a19c2bdd65903e05748c6590e8d1eed1f3ffc4e /src
parentc2b45daf5ddab7b518aed9a51026a599fe4bccef (diff)
Compile tuple expression into stack IR
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java11
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java15
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java31
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java7
4 files changed, 59 insertions, 5 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 61932ada..12a32125 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -134,7 +134,7 @@ public class StackCompiler {
compileStringLiteral(typedStringLiteralNode, context);
case TypedTupleNode typedTupleNode ->
- throw new UnsupportedOperationException("TODO");
+ compileTuple(typedTupleNode, context);
};
}
@@ -414,6 +414,15 @@ public class StackCompiler {
};
}
+ private static Context compileTuple(TypedTupleNode node, Context context) {
+ for (var elementNode : node.elements()) {
+ context = compileExpression(elementNode, context);
+ }
+
+ var instruction = new TupleCreateInstruction(node.elements().size(), node.source());
+ return context.addInstruction(instruction);
+ }
+
private static Context compileWhile(TypedWhileNode node, Context context) {
context = context.enterBlock(node.body().boundVariableNames());
context = compileBlockWithoutYield(node.body(), 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 ca4e251a..3f0230c0 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -1,10 +1,7 @@
package org.shedlang.compiler.ast.typed;
import org.shedlang.compiler.sources.Sources;
-import org.shedlang.compiler.types.NamespaceName;
-import org.shedlang.compiler.types.Type;
-import org.shedlang.compiler.types.TypeLevelValue;
-import org.shedlang.compiler.types.Types;
+import org.shedlang.compiler.types.*;
import java.math.BigInteger;
import java.util.List;
@@ -197,6 +194,16 @@ public class Typed {
return new TypedTargetNode.Var(name, Sources.NULL);
}
+ public static TypedTupleNode tuple(List<TypedExpressionNode> elements) {
+ return new TypedTupleNode(
+ elements,
+ new TupleType(elements.stream()
+ .map(element -> element.type())
+ .toList()),
+ Sources.NULL
+ );
+ }
+
public static <T extends TypeLevelValue> TypedTypeLevelExpressionNode<T> typeLevelReference(
String name,
T value
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java
new file mode 100644
index 00000000..f05e9257
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerTupleTests.java
@@ -0,0 +1,31 @@
+package org.shedlang.compiler.stackir;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+
+import java.util.List;
+
+import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class StackCompilerTupleTests {
+ @Test
+ public void stringAddIsCompiledToStringAddInstruction() {
+ var node = Typed.tuple(List.of(
+ Typed.stringLiteral("a"),
+ Typed.boolLiteral(true),
+ Typed.intLiteral(4)
+ ));
+ var context = StackCompiler.Context.EMPTY;
+
+ var result = StackCompiler.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isPushStringInstruction("a"),
+ isPushBoolInstruction(true),
+ isPushIntInstruction(4),
+ isTupleCreateInstruction(3)
+ ));
+ }
+}
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 b70ff8dd..a256c321 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
@@ -136,6 +136,13 @@ public class InstructionMatchers {
return instanceOf(StringAddInstruction.class);
}
+ public static Matcher<Instruction> isTupleCreateInstruction(int elementCount) {
+ return instanceOf(
+ TupleCreateInstruction.class,
+ has("elementCount", x -> x.elementCount(), equalTo(elementCount))
+ );
+ }
+
public static Matcher<Instruction> isVarLoadInstruction(String name) {
return instanceOf(
VarLoadInstruction.class,