From a836bb39ba435c2addd5a49ba416406337b3f224 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 19 Jul 2023 20:30:54 +0100 Subject: Extract code for representing and serializing trees --- .../compiler/stackir/InstructionSerializer.java | 115 +-------------------- 1 file changed, 3 insertions(+), 112 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/stackir') 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 children) { - return new TreeList(children); - } - - private static Tree treeList(Tree... children) { - return new TreeList(Arrays.asList(children)); - } - - private static Tree treeListIndented(List 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 children) implements Tree { - } - - private record TreeListIndented(List 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(" "); - } - } } -- cgit v1.2.3