From 6820962c44cc7a27fe45d0a2ebe8140cd11f5811 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:29:08 +0100 Subject: Add typed int add node --- .../ast/typed/TypedBinaryOperationNode.java | 16 +++++++++++++ .../compiler/ast/typed/TypedBinaryOperator.java | 18 +++++++++++++++ .../compiler/ast/typed/TypedExpressionNode.java | 2 +- .../shedlang/compiler/stackir/StackCompiler.java | 9 ++++++++ .../org/shedlang/compiler/ast/typed/Typed.java | 12 ++++++++++ .../compiler/stackir/StackCompilerIntAddTests.java | 27 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 4 ++++ 7 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperationNode.java create mode 100644 src/main/java/org/shedlang/compiler/ast/typed/TypedBinaryOperator.java create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerIntAddTests.java 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 compileBinaryOperation(TypedBinaryOperationNode node) { + return compileExpression(node.left()) + .plusAll(compileExpression(node.right())) + .plus(new IntAddInstruction(node.source())); + } + private static PVector compileBoolLiteral(TypedBoolLiteralNode node) { return P.vector(new PushBoolInstruction(node.value(), node.source())); } @@ -42,6 +48,9 @@ public class StackCompiler { public static PVector 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 isIntAddInstruction() { + return instanceOf(IntAddInstruction.class); + } + public static Matcher isLocalLoadInstruction(String name) { return instanceOf( LocalLoadInstruction.class, -- cgit v1.2.3