diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-08 12:22:36 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-08 12:22:36 +0100 |
| commit | 9a14e772cd0a9574b892cf872c32db1d333b0815 (patch) | |
| tree | e469e620340ec6ae60b38dd5504aa331de823b61 /src/main/java/org/shedlang/compiler/stackir | |
| parent | a0d962a3609abee2148abcffe54fae9b09663830 (diff) | |
Compile string addition into string-add instruction
Diffstat (limited to 'src/main/java/org/shedlang/compiler/stackir')
4 files changed, 17 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index c6544550..baaf4b49 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -141,10 +141,14 @@ public class InstructionSerializer { treeList( treeIdentifier("push-unit") ); + case ReturnInstruction returnInstruction -> treeList( treeIdentifier("return") ); + + case StringAddInstruction stringAddInstruction -> + treeList(treeIdentifier("string-add")); }; } diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 6bda0b6d..449360e1 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -25,7 +25,7 @@ public class StackCompiler { case INT_SUB -> new IntSubInstruction(node.source()); case STRING_ADD -> - throw new UnsupportedOperationException("TODO"); + new StringAddInstruction(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 6d361ddb..a1ab0a39 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir.instructions; import org.shedlang.compiler.sources.Source; public sealed interface Instruction - permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java new file mode 100644 index 00000000..041ae30e --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop the right operand from the stack, then the left operand from the stack, + * assume both operands are Strings, concatenate the left and right operands, + * then push the result onto the stack. + */ +public record StringAddInstruction(Source source) implements Instruction { +} |
