From 345b4b4c0b03e2331ffb0b5b33a80bee795e70e2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 10:12:21 +0100 Subject: Compile string literal to stack instruction --- .../java/org/shedlang/compiler/ast/typed/Typed.java | 4 ++++ .../stackir/StackCompilerStringLiteralTests.java | 21 +++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 +++++++ 3 files changed, 32 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.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 ba7199a8..2468449a 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -15,4 +15,8 @@ public class Typed { public static TypedIntLiteralNode intLiteral(int value) { return new TypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL); } + + public static TypedStringLiteralNode stringLiteral(String value) { + return new TypedStringLiteralNode(value, Sources.NULL); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.java new file mode 100644 index 00000000..2d2e7af1 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerStringLiteralTests.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.isPushStringInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerStringLiteralTests { + @Test + public void stringLiteralIsCompiledToPushStringInstruction() { + var node = Typed.stringLiteral("hello"); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isPushStringInstruction("hello") + )); + } +} 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 053cf645..f2d153cc 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -23,4 +23,11 @@ public class InstructionMatchers { has("value", x -> x.value(), equalTo(BigInteger.valueOf(value))) ); } + + public static Matcher isPushStringInstruction(String value) { + return instanceOf( + PushStringInstruction.class, + has("value", x -> x.value(), equalTo(value)) + ); + } } -- cgit v1.2.3