From d42333cb45df71c033d8b542f6df5b3b4fcdf8b4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:31:11 +0100 Subject: Add typed sub node --- .../org/shedlang/compiler/ast/typed/Typed.java | 12 ++++++++++ .../compiler/stackir/StackCompilerIntSubTests.java | 26 ++++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 4 ++++ 3 files changed, 42 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java (limited to 'src/test/java') 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 cb4d1f02..18fe79cf 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -51,6 +51,18 @@ public class Typed { return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL); } + public static TypedBinaryOperationNode intSub( + TypedExpressionNode left, + TypedExpressionNode right + ) { + return new TypedBinaryOperationNode( + TypedBinaryOperator.INT_SUB, + left, + right, + Sources.NULL + ); + } + public static TypedNamedArgNode namedArg(String name, TypedExpressionNode expression) { return new TypedNamedArgNode(name, expression, Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java new file mode 100644 index 00000000..b11863c5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntSubTests.java @@ -0,0 +1,26 @@ +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.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerIntSubTests { + @Test + public void intSubIsCompiledToIntSubInstruction() { + var node = Typed.intSub( + Typed.intLiteral(42), + Typed.intLiteral(47) + ); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushIntInstruction(42), + isPushIntInstruction(47), + isIntSubInstruction() + )); + } +} 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 201a69c2..08f02227 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -37,6 +37,10 @@ public class InstructionMatchers { return instanceOf(IntAddInstruction.class); } + public static Matcher isIntSubInstruction() { + return instanceOf(IntSubInstruction.class); + } + public static Matcher isLocalLoadInstruction(String name) { return instanceOf( LocalLoadInstruction.class, -- cgit v1.2.3