From e7025e11b5718393d776e42ec83bef1521bf8a20 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 10:10:02 +0100 Subject: Compile int literal to stack instruction --- .../java/org/shedlang/compiler/ast/typed/Typed.java | 6 ++++++ .../stackir/StackCompilerIntLiteralTests.java | 21 +++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 9 +++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerIntLiteralTests.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 742bc658..ba7199a8 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -2,6 +2,8 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Sources; +import java.math.BigInteger; + public class Typed { private Typed() { } @@ -9,4 +11,8 @@ public class Typed { public static TypedBoolLiteralNode boolLiteral(boolean value) { return new TypedBoolLiteralNode(value, 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/StackCompilerIntLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntLiteralTests.java new file mode 100644 index 00000000..38448bb9 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerIntLiteralTests.java @@ -0,0 +1,21 @@ +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.isPushIntInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerIntLiteralTests { + @Test + public void intLiteralIsCompiledToPushIntInstruction() { + var node = Typed.intLiteral(42); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushIntInstruction(42) + )); + } +} 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 7b90c614..053cf645 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -2,6 +2,8 @@ package org.shedlang.compiler.stackir.instructions; import org.zwobble.precisely.Matcher; +import java.math.BigInteger; + import static org.zwobble.precisely.Matchers.*; public class InstructionMatchers { @@ -14,4 +16,11 @@ public class InstructionMatchers { has("value", x -> x.value(), equalTo(value)) ); } + + public static Matcher isPushIntInstruction(int value) { + return instanceOf( + PushIntInstruction.class, + has("value", x -> x.value(), equalTo(BigInteger.valueOf(value))) + ); + } } -- cgit v1.2.3