From 8c346ff940f7336a9426bc61d7f321b0944f6bdc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 10:18:56 +0100 Subject: Compile reference to stack instruction --- .../shedlang/compiler/stackir/StackCompiler.java | 11 ++++++----- .../compiler/stackir/instructions/Instruction.java | 2 +- .../stackir/instructions/LocalLoadInstruction.java | 6 ++++++ .../java/org/shedlang/compiler/ast/typed/Typed.java | 10 ++++++++++ .../stackir/StackCompilerReferenceTests.java | 21 +++++++++++++++++++++ .../stackir/instructions/InstructionMatchers.java | 7 +++++++ 6 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 93fd5086..8a268c4c 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -1,10 +1,7 @@ package org.shedlang.compiler.stackir; import org.shedlang.compiler.ast.typed.*; -import org.shedlang.compiler.stackir.instructions.Instruction; -import org.shedlang.compiler.stackir.instructions.PushBoolInstruction; -import org.shedlang.compiler.stackir.instructions.PushIntInstruction; -import org.shedlang.compiler.stackir.instructions.PushStringInstruction; +import org.shedlang.compiler.stackir.instructions.*; import java.util.List; @@ -25,7 +22,7 @@ public class StackCompiler { compileIntLiteral(typedIntLiteralNode); case TypedReferenceNode typedReferenceNode -> - throw new UnsupportedOperationException("TODO"); + compileReference(typedReferenceNode); case TypedStringLiteralNode typedStringLiteralNode -> compileStringLiteral(typedStringLiteralNode); @@ -36,6 +33,10 @@ public class StackCompiler { return List.of(new PushIntInstruction(node.value(), node.source())); } + private static List compileReference(TypedReferenceNode node) { + return List.of(new LocalLoadInstruction(node.name(), node.source())); + } + private static List compileStringLiteral(TypedStringLiteralNode node) { return List.of(new PushStringInstruction(node.value(), node.source())); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java index 857d7d6a..1f84ba23 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -1,4 +1,4 @@ package org.shedlang.compiler.stackir.instructions; -public sealed interface Instruction permits PushBoolInstruction, PushIntInstruction, PushStringInstruction { +public sealed interface Instruction permits LocalLoadInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction { } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java new file mode 100644 index 00000000..b7656ed3 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +public record LocalLoadInstruction(String name, Source source) implements Instruction { +} 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 2468449a..75f54d36 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -1,6 +1,8 @@ package org.shedlang.compiler.ast.typed; import org.shedlang.compiler.sources.Sources; +import org.shedlang.compiler.types.Type; +import org.shedlang.compiler.types.Types; import java.math.BigInteger; @@ -19,4 +21,12 @@ public class Typed { public static TypedStringLiteralNode stringLiteral(String value) { return new TypedStringLiteralNode(value, Sources.NULL); } + + public static TypedReferenceNode reference(String name) { + return reference(name, Types.STRING); + } + + public static TypedReferenceNode reference(String name, Type type) { + return new TypedReferenceNode(name, type, Sources.NULL); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java new file mode 100644 index 00000000..2b5115c5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.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.isLocalLoadInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerReferenceTests { + @Test + public void referenceIsCompiledToLocalLoad() { + var node = Typed.reference("x"); + + var instructions = StackCompiler.compileExpression(node); + + assertThat(instructions, isSequence( + isLocalLoadInstruction("x") + )); + } +} 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 f2d153cc..2fc1aa21 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -10,6 +10,13 @@ public class InstructionMatchers { private InstructionMatchers() { } + public static Matcher isLocalLoadInstruction(String name) { + return instanceOf( + LocalLoadInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } + public static Matcher isPushBoolInstruction(boolean value) { return instanceOf( PushBoolInstruction.class, -- cgit v1.2.3