From 7c1478a872495fab3333638c3a1960edc2b0ec03 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 5 Jul 2023 22:09:28 +0100 Subject: Compile return statements to stack IR --- .../org/shedlang/compiler/ast/typed/Typed.java | 4 ++++ .../compiler/stackir/StackCompilerReturnTests.java | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerReturnTests.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 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() + )); + } +} -- cgit v1.2.3