From 4a550a5ae083518815ec70c73346705f3778df93 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 10:07:16 +0100 Subject: Compile bool literal to stack instruction --- .../java/org/shedlang/compiler/ast/typed/Typed.java | 12 ++++++++++++ .../stackir/StackCompilerBoolLiteralTests.java | 21 +++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 17 +++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/typed/Typed.java create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.java create mode 100644 src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.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 new file mode 100644 index 00000000..742bc658 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -0,0 +1,12 @@ +package org.shedlang.compiler.ast.typed; + +import org.shedlang.compiler.sources.Sources; + +public class Typed { + private Typed() { + } + + public static TypedBoolLiteralNode boolLiteral(boolean value) { + return new TypedBoolLiteralNode(value, Sources.NULL); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.java new file mode 100644 index 00000000..19f3a6a6 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerBoolLiteralTests.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.isPushBoolInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerBoolLiteralTests { + @Test + public void boolLiteralIsCompiledToPushBoolInstruction() { + var node = Typed.boolLiteral(true); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushBoolInstruction(true) + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java new file mode 100644 index 00000000..7b90c614 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -0,0 +1,17 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.*; + +public class InstructionMatchers { + private InstructionMatchers() { + } + + public static Matcher isPushBoolInstruction(boolean value) { + return instanceOf( + PushBoolInstruction.class, + has("value", x -> x.value(), equalTo(value)) + ); + } +} -- cgit v1.2.3