diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-10 10:12:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-10 10:12:21 +0100 |
| commit | 345b4b4c0b03e2331ffb0b5b33a80bee795e70e2 (patch) | |
| tree | b3fcc1fb8c394760429b9d2dd23c6ef5e1e198fa /src/test/java | |
| parent | e7025e11b5718393d776e42ec83bef1521bf8a20 (diff) | |
Compile string literal to stack instruction
Diffstat (limited to 'src/test/java')
3 files changed, 32 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 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<Instruction> isPushStringInstruction(String value) { + return instanceOf( + PushStringInstruction.class, + has("value", x -> x.value(), equalTo(value)) + ); + } } |
