diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-03 13:39:48 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-03 13:39:48 +0100 |
| commit | 6acf0ef2012b6cbc412efb6958274504bc4c8fce (patch) | |
| tree | ece325ecb6dce8146ff3c41bcab86ebd2fdb1e98 /src | |
| parent | 31a177c56a6e0efe9e433567d9c771008e0f47e8 (diff) | |
Compile val node to IR instructions
Diffstat (limited to 'src')
3 files changed, 49 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 7ff1338a..c0d1b04b 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -99,7 +99,7 @@ public class StackCompiler { compileExpressionStatement(expressionStatement); case TypedValNode val -> - throw new UnsupportedOperationException("TODO"); + compileVal(val); }; } @@ -146,4 +146,16 @@ public class StackCompiler { private static PVector<Instruction> compileStringLiteral(TypedStringLiteralNode node) { return P.vector(new PushStringInstruction(node.value(), node.source())); } + + private static PVector<Instruction> compileTarget(TypedTargetNode target) { + return switch (target) { + case TypedTargetNode.Variable variable -> + P.vector(new LocalStoreInstruction(variable.name(), variable.source())); + }; + } + + private static PVector<Instruction> compileVal(TypedValNode val) { + return compileExpression(val.expression()) + .plusAll(compileTarget(val.target())); + } } 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 ff87c8e8..2b9defee 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -74,10 +74,21 @@ public class Typed { return new TypedReferenceNode(name, type, Sources.NULL); } + public static TypedTargetNode targetVariable(String name) { + return new TypedTargetNode.Variable(name, Sources.NULL); + } + public static <T extends TypeLevelValue> TypedTypeLevelExpressionNode<T> typeLevelReference( String name, T value ) { return new TypedTypeLevelReferenceNode<>(name, value, Sources.NULL); } + + public static TypedValNode val( + TypedTargetNode target, + TypedExpressionNode expression + ) { + return new TypedValNode(target, expression, Sources.NULL); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java new file mode 100644 index 00000000..3126c82b --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerValTests.java @@ -0,0 +1,25 @@ +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 StackCompilerValTests { + @Test + public void expressionIsEvaluatedAndStoredAsLocal() { + var node = Typed.val( + Typed.targetVariable("x"), + Typed.stringLiteral("hello") + ); + + var instructions = StackCompiler.compileFunctionStatement(node); + + assertThat(instructions, isSequence( + isPushStringInstruction("hello"), + isLocalStoreInstruction("x") + )); + } +} |
