aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-14 21:37:52 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-14 21:37:52 +0100
commit0b8c4b6a27838e89ff49e1ab66bf2ed4378ec7da (patch)
treea3ae6fac6167191e9d76a2ecb9528951ddbe3e7a /src
parent76fc235449f03fd76ab9a5bd96d9e8f2aec025c3 (diff)
Compile if expression with single conditional branch
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java44
-rw-r--r--src/main/java/org/shedlang/compiler/util/PCollectors.java27
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/Typed.java28
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java46
-rw-r--r--src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java11
5 files changed, 147 insertions, 9 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 7365a8b4..e062ab78 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -5,8 +5,7 @@ import org.shedlang.compiler.ShedBuiltins;
import org.shedlang.compiler.ast.typed.*;
import org.shedlang.compiler.stackir.instructions.*;
import org.shedlang.compiler.util.P;
-
-import java.util.stream.Collectors;
+import org.shedlang.compiler.util.PCollectors;
public class StackCompiler {
private StackCompiler() {
@@ -29,6 +28,23 @@ public class StackCompiler {
});
}
+ private static PVector<Instruction> compileBlock(TypedBlockNode node) {
+ var body = node.statements().stream()
+ .flatMap(statement -> compileFunctionStatement(statement).stream())
+ .collect(PCollectors.toVector());
+
+ switch (node.behaviour()) {
+ case TypedFunctionStatementBehaviour.BlockReturn blockReturn -> {
+ }
+ case TypedFunctionStatementBehaviour.Continue continue_ -> {
+ body = body.plus(new PushUnitInstruction(node.source()));
+ }
+ case TypedFunctionStatementBehaviour.Terminate terminate -> {
+ }
+ }
+ return body;
+ }
+
private static PVector<Instruction> compileBlockReturn(TypedBlockReturnNode blockReturn) {
return compileExpression(blockReturn.expression());
}
@@ -73,7 +89,7 @@ public class StackCompiler {
compileCall(typedCallNode);
case TypedIfNode typedIfNode ->
- throw new UnsupportedOperationException("TODO");
+ compileIf(typedIfNode);
case TypedIntLiteralNode typedIntLiteralNode ->
compileIntLiteral(typedIntLiteralNode);
@@ -99,17 +115,14 @@ public class StackCompiler {
.map(param -> new DefineFunctionInstruction.Param(param.name(), param.source()))
.toList();
- var body = node.body().statements().stream()
- .flatMap(statement -> compileFunctionStatement(statement).stream())
- .collect(Collectors.toList());
+ var body = compileBlock(node.body());
switch (node.body().behaviour()) {
case TypedFunctionStatementBehaviour.BlockReturn blockReturn -> {
- body.add(new ReturnInstruction(node.source()));
+ body = body.plus(new ReturnInstruction(node.source()));
}
case TypedFunctionStatementBehaviour.Continue continue_ -> {
- body.add(new PushUnitInstruction(node.source()));
- body.add(new ReturnInstruction(node.source()));
+ body = body.plus(new ReturnInstruction(node.source()));
}
case TypedFunctionStatementBehaviour.Terminate terminate -> {
}
@@ -150,6 +163,19 @@ public class StackCompiler {
};
}
+ private static PVector<Instruction> compileIf(TypedIfNode node) {
+ var conditionalBranch = node.conditionalBranches().get(0);
+ var conditionInstructions = compileExpression(conditionalBranch.condition());
+
+ var ifInstruction = new IfInstruction(
+ compileBlock(conditionalBranch.body()),
+ compileBlock(node.elseBody()),
+ node.source()
+ );
+
+ return conditionInstructions.plus(ifInstruction);
+ }
+
private static PVector<Instruction> compileIntLiteral(TypedIntLiteralNode node) {
return P.vector(new PushIntInstruction(node.value(), node.source()));
}
diff --git a/src/main/java/org/shedlang/compiler/util/PCollectors.java b/src/main/java/org/shedlang/compiler/util/PCollectors.java
index 26033670..a0019eef 100644
--- a/src/main/java/org/shedlang/compiler/util/PCollectors.java
+++ b/src/main/java/org/shedlang/compiler/util/PCollectors.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.util;
import org.pcollections.Empty;
import org.pcollections.PMap;
+import org.pcollections.PVector;
import java.util.Map;
import java.util.stream.Collector;
@@ -61,4 +62,30 @@ public class PCollectors {
return map;
}
}
+
+ public static <T> Collector<T, PVectorCollector<T>, PVector<T>> toVector() {
+ return Collector.of(
+ PVectorCollector::new,
+ PVectorCollector::add,
+ PVectorCollector::addAll,
+ PVectorCollector::toVector
+ );
+ }
+
+ private static class PVectorCollector<T> {
+ private PVector<T> vector = Empty.vector();
+
+ void add(T value) {
+ vector = vector.plus(value);
+ }
+
+ PVectorCollector<T> addAll(PVectorCollector<T> other) {
+ vector = vector.plusAll(other.vector);
+ return this;
+ }
+
+ PVector<T> toVector() {
+ return vector;
+ }
+ }
}
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 d52f67a5..160950b6 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java
@@ -13,6 +13,10 @@ public class Typed {
private Typed() {
}
+ public static TypedBlockNode block(List<TypedFunctionStatementNode> statements) {
+ return new TypedBlockNode(statements, Sources.NULL);
+ }
+
public static TypedBlockReturnNode blockReturn(TypedExpressionNode expression) {
return new TypedBlockReturnNode(expression, Sources.NULL);
}
@@ -35,6 +39,17 @@ public class Typed {
);
}
+ public static TypedConditionalBranchNode conditionalBranch(
+ TypedExpressionNode condition,
+ TypedBlockNode body
+ ) {
+ return new TypedConditionalBranchNode(
+ condition,
+ body,
+ Sources.NULL
+ );
+ }
+
public static TypedExpressionStatementNode expressionStatement(TypedExpressionNode expression) {
return new TypedExpressionStatementNode(expression, Sources.NULL);
}
@@ -43,6 +58,19 @@ public class Typed {
return new TypedFunctionReturnNode(expression, Sources.NULL);
}
+ public static TypedIfNode if_(
+ List<TypedConditionalBranchNode> conditionalBranches,
+ TypedBlockNode elseBody,
+ Type type
+ ) {
+ return new TypedIfNode(
+ conditionalBranches,
+ elseBody,
+ type,
+ Sources.NULL
+ );
+ }
+
public static TypedBinaryOperationNode intAdd(
TypedExpressionNode left,
TypedExpressionNode right
diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java
new file mode 100644
index 00000000..49c47208
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIfTests.java
@@ -0,0 +1,46 @@
+package org.shedlang.compiler.stackir;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.ast.typed.Typed;
+import org.shedlang.compiler.types.Types;
+
+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 StackCompilerIfTests {
+ @Test
+ public void ifWithSingleConditionalBranchIsCompiledToSingleIfInstruction() {
+ var node = Typed.if_(
+ List.of(
+ Typed.conditionalBranch(
+ Typed.reference("c"),
+ Typed.block(List.of(
+ Typed.blockReturn(Typed.intLiteral(42))
+ ))
+ )
+ ),
+ Typed.block(List.of(
+ Typed.functionReturn(Typed.stringLiteral("x"))
+ )),
+ Types.INT
+ );
+
+ var instructions = StackCompiler.compileExpression(node);
+
+ assertThat(instructions, isSequence(
+ isLocalLoadInstruction("c"),
+ isIfInstruction(
+ isSequence(
+ isPushIntInstruction(42)
+ ),
+ isSequence(
+ isPushStringInstruction("x"),
+ isReturnInstruction()
+ )
+ )
+ ));
+ }
+}
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 8b2661f7..4be747b7 100644
--- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
+++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java
@@ -33,6 +33,17 @@ public class InstructionMatchers {
);
}
+ public static Matcher<Instruction> isIfInstruction(
+ Matcher<Iterable<? extends Instruction>> trueBody,
+ Matcher<Iterable<? extends Instruction>> falseBody
+ ) {
+ return instanceOf(
+ IfInstruction.class,
+ has("trueBody", x -> x.trueBody(), trueBody),
+ has("falseBody", x -> x.falseBody(), falseBody)
+ );
+ }
+
public static Matcher<Instruction> isIntAddInstruction() {
return instanceOf(IntAddInstruction.class);
}