diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:29:08 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:29:08 +0100 |
| commit | 6820962c44cc7a27fe45d0a2ebe8140cd11f5811 (patch) | |
| tree | 19d8b4208605761f53eb8602a8d5b682e45902ef | |
| parent | bbd0ad7fad36761f55555347b01a32887fc4a5f9 (diff) | |
Add typed int add node
7 files changed, 87 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java new file mode 100644 index 00000000..383bd575 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java @@ -0,0 +1,16 @@ +package org.shedlang.compiler.ast.typed; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.Type; + +public record TypedBinaryOperationNode( + TypedBinaryOperator operator, + TypedExpressionNode left, + TypedExpressionNode right, + Source source +) implements TypedExpressionNode { + @Override + public Type type() { + return operator.resultType(); + } +} diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java new file mode 100644 index 00000000..5f6498f6 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java @@ -0,0 +1,18 @@ +package org.shedlang.compiler.ast.typed; + +import org.shedlang.compiler.types.Type; +import org.shedlang.compiler.types.Types; + +public enum TypedBinaryOperator { + INT_ADD(Types.INT); + + private final Type type; + + TypedBinaryOperator(Type type) { + this.type = type; + } + + public Type resultType() { + return type; + } +} diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java index 4bb9cb8e..963cf8a3 100644 --- a/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java +++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedExpressionNode.java @@ -4,6 +4,6 @@ import org.shedlang.compiler.types.Type; public sealed interface TypedExpressionNode extends TypedNode - permits TypedBoolLiteralNode, TypedCallNode, TypedIntLiteralNode, TypedReferenceNode, TypedStringLiteralNode { + permits TypedBinaryOperationNode, TypedBoolLiteralNode, TypedCallNode, TypedIntLiteralNode, TypedReferenceNode, TypedStringLiteralNode { Type type(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 9cd01151..c96b8cfd 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -12,6 +12,12 @@ public class StackCompiler { private StackCompiler() { } + private static PVector<Instruction> compileBinaryOperation(TypedBinaryOperationNode node) { + return compileExpression(node.left()) + .plusAll(compileExpression(node.right())) + .plus(new IntAddInstruction(node.source())); + } + private static PVector<Instruction> compileBoolLiteral(TypedBoolLiteralNode node) { return P.vector(new PushBoolInstruction(node.value(), node.source())); } @@ -42,6 +48,9 @@ public class StackCompiler { public static PVector<Instruction> compileExpression(TypedExpressionNode node) { return switch (node) { + case TypedBinaryOperationNode typedBinaryOperationNode -> + compileBinaryOperation(typedBinaryOperationNode); + case TypedBoolLiteralNode typedBoolLiteralNode -> compileBoolLiteral(typedBoolLiteralNode); 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 d41842dc..cb4d1f02 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -35,6 +35,18 @@ public class Typed { return new TypedExpressionStatementNode(expression, Sources.NULL); } + public static TypedBinaryOperationNode intAdd( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_ADD, + left, + right, + Sources.NULL + ); + } + public static TypedIntLiteralNode intLiteral(int value) { return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java new file mode 100644 index 00000000..55283682 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isIntAddInstruction; +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushIntInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerIntAddTests { + @Test + public void intAddIsCompiledToIntAddInstruction() { + var node = Typed.intAdd( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushIntInstruction(42), + isPushIntInstruction(47), + isIntAddInstruction() + )); + } +} 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 8e24e131..201a69c2 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,10 @@ public class InstructionMatchers { ); } + public static Matcher<Instruction> isIntAddInstruction() { + return instanceOf(IntAddInstruction.class); + } + public static Matcher<Instruction> isLocalLoadInstruction(String name) { return instanceOf( LocalLoadInstruction.class, |
