diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-05 22:09:28 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-05 22:09:28 +0100 |
| commit | 7c1478a872495fab3333638c3a1960edc2b0ec03 (patch) | |
| tree | ce92176f6e4338a9c2a3c10125faba18de6b1485 /src/test/java | |
| parent | 372ab8fc6035169c7fb902a9e1df397b458d9c0c (diff) | |
Compile return statements to stack IR
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/ast/typed/Typed.java | 4 | ||||
| -rw-r--r-- | src/test/java/org/shedlang/compiler/stackir/StackCompilerReturnTests.java | 22 |
2 files changed, 26 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 2b9defee..d41842dc 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -74,6 +74,10 @@ public class Typed { return new TypedReferenceNode(name, type, Sources.NULL); } + public static TypedReturnNode return_(TypedExpressionNode expression) { + return new TypedReturnNode(expression, Sources.NULL); + } + public static TypedTargetNode targetVariable(String name) { return new TypedTargetNode.Variable(name, Sources.NULL); } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerReturnTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReturnTests.java new file mode 100644 index 00000000..69a3f735 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReturnTests.java @@ -0,0 +1,22 @@ +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.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerReturnTests { + @Test + public void expressionIsEvaluatedAndTheResultDiscarded() { + var node = Typed.return_(Typed.stringLiteral("hello")); + + var instructions = StackCompiler.compileFunctionStatement(node); + + assertThat(instructions, isSequence( + isPushStringInstruction("hello"), + isReturnInstruction() + )); + } +} |
