aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/stackir
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-19 20:30:54 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-19 20:30:54 +0100
commita836bb39ba435c2addd5a49ba416406337b3f224 (patch)
tree01781a99681cc136a3902b0b44a84bcd08cc55da /src/main/java/org/shedlang/compiler/stackir
parent2d0ea7d1e81f708aaf9066fc92784d7b44ff3e74 (diff)
Extract code for representing and serializing trees
Diffstat (limited to 'src/main/java/org/shedlang/compiler/stackir')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java115
1 files changed, 3 insertions, 112 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
index 17d778df..c2c19f06 100644
--- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
+++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
@@ -1,11 +1,12 @@
package org.shedlang.compiler.stackir;
import org.shedlang.compiler.stackir.instructions.*;
+import org.shedlang.compiler.treetext.Tree;
-import java.util.Arrays;
import java.util.List;
-import static org.shedlang.compiler.util.Iterables.forEachInterspersed;
+import static org.shedlang.compiler.treetext.TreeSerializer.serializeTree;
+import static org.shedlang.compiler.treetext.Trees.*;
public class InstructionSerializer {
private InstructionSerializer() {
@@ -158,114 +159,4 @@ public class InstructionSerializer {
treeList(treeIdentifier("string-add"));
};
}
-
- private static Tree treeIdentifier(String name) {
- return new TreeIdentifier(name);
- }
-
- private static Tree treeInt(int value) {
- return new TreeInt(value);
- }
-
- private static Tree treeList(List<Tree> children) {
- return new TreeList(children);
- }
-
- private static Tree treeList(Tree... children) {
- return new TreeList(Arrays.asList(children));
- }
-
- private static Tree treeListIndented(List<Tree> children) {
- return new TreeListIndented(children);
- }
-
- private static Tree treeListIndented(Tree... children) {
- return new TreeListIndented(Arrays.asList(children));
- }
-
- private static Tree treeNamed(String name, Tree value) {
- return new TreeNamed(name, value);
- }
-
- private static Tree treeString(String value) {
- return new TreeString(value);
- }
-
- private sealed interface Tree {}
-
- private record TreeIdentifier(String name) implements Tree {
- }
-
- private record TreeInt(int value) implements Tree {
- }
-
- private record TreeList(List<Tree> children) implements Tree {
- }
-
- private record TreeListIndented(List<Tree> children) implements Tree {
- }
-
- private record TreeNamed(String name, Tree value) implements Tree {
- }
-
- private record TreeString(String value) implements Tree {
- }
-
- private static void serializeTree(Tree tree, StringBuilder builder, int indentation) {
- switch (tree) {
- case TreeIdentifier identifier -> {
- builder.append(identifier.name);
- }
- case TreeInt treeInt -> {
- builder.append(treeInt.value);
- }
- case TreeList list -> {
- builder.append("(");
- forEachInterspersed(
- list.children,
- child -> serializeTree(child, builder, indentation),
- () -> builder.append(" ")
- );
- builder.append(")");
- }
- case TreeListIndented list -> {
- builder.append("(");
- forEachInterspersed(
- list.children,
- child -> {
- newLine(indentation + 1, builder);
- serializeTree(child, builder, indentation + 1);
- },
- () -> {}
- );
- newLine(indentation, builder);
- builder.append(")");
- }
- case TreeNamed named -> {
- builder.append(named.name);
- builder.append("=");
- serializeTree(named.value, builder, indentation);
- }
- case TreeString string -> {
- builder.append("\"");
- var escapedValue = string.value
- .replace("\\", "\\\\")
- .replace("\b", "\\b")
- .replace("\t", "\\t")
- .replace("\n", "\\n")
- .replace("\f", "\\f")
- .replace("\r", "\\r")
- .replace("\"", "\\\"");
- builder.append(escapedValue);
- builder.append("\"");
- }
- }
- }
-
- private static void newLine(int indentation, StringBuilder builder) {
- builder.append("\n");
- for (var i = 0; i < indentation; i++) {
- builder.append(" ");
- }
- }
}