diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-10 10:10:02 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-10 10:10:02 +0100 |
| commit | e7025e11b5718393d776e42ec83bef1521bf8a20 (patch) | |
| tree | d20d98a46674c209b60c184a51b2b9dd5d51f222 /src/test/java | |
| parent | 4a550a5ae083518815ec70c73346705f3778df93 (diff) | |
Compile int literal to stack instruction
Diffstat (limited to 'src/test/java')
3 files changed, 36 insertions, 0 deletions
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<Instruction> isPushIntInstruction(int value) { + return instanceOf( + PushIntInstruction.class, + has("value", x -> x.value(), equalTo(BigInteger.valueOf(value))) + ); + } } |
