From 6b85df2f3f1e6a40f1bde94db8b43297baf11e39 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Tue, 18 Jun 2024 11:28:52 +0100 Subject: [PATCH 1/3] instruction_tree: Rename generate_text_recursive parameter The parameter here is not necessarily the root of the tree. --- .../block_code/instruction_tree/instruction_tree.gd | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/addons/block_code/instruction_tree/instruction_tree.gd b/addons/block_code/instruction_tree/instruction_tree.gd index 18772122..1873e7be 100644 --- a/addons/block_code/instruction_tree/instruction_tree.gd +++ b/addons/block_code/instruction_tree/instruction_tree.gd @@ -24,18 +24,18 @@ func generate_text(root_node: TreeNode, start_depth: int = 0) -> String: return out -func generate_text_recursive(root_node: TreeNode): - if root_node.data != "": +func generate_text_recursive(node: TreeNode): + if node.data != "": for i in depth: out += "\t" - out += root_node.data + "\n" + out += node.data + "\n" depth += 1 - for c in root_node.children: + for c in node.children: generate_text_recursive(c) depth -= 1 - if root_node.next: - generate_text_recursive(root_node.next) + if node.next: + generate_text_recursive(node.next) From ad756039a4cd80d0343aa460fcee7057928f6a1e Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Tue, 18 Jun 2024 11:30:19 +0100 Subject: [PATCH 2/3] instruction_tree: Pass depth explicitly in recursive call The depth is a transient property of where in the tree we happen to be generating code, so it should not be a persistent property of the class. Pass it explicitly. --- .../instruction_tree/instruction_tree.gd | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/addons/block_code/instruction_tree/instruction_tree.gd b/addons/block_code/instruction_tree/instruction_tree.gd index 1873e7be..a06f9260 100644 --- a/addons/block_code/instruction_tree/instruction_tree.gd +++ b/addons/block_code/instruction_tree/instruction_tree.gd @@ -1,7 +1,6 @@ class_name InstructionTree extends Object -var depth: int var out: String @@ -19,23 +18,18 @@ class TreeNode: func generate_text(root_node: TreeNode, start_depth: int = 0) -> String: out = "" - depth = start_depth - generate_text_recursive(root_node) + generate_text_recursive(root_node, start_depth) return out -func generate_text_recursive(node: TreeNode): +func generate_text_recursive(node: TreeNode, depth: int): if node.data != "": for i in depth: out += "\t" out += node.data + "\n" - depth += 1 - for c in node.children: - generate_text_recursive(c) - - depth -= 1 + generate_text_recursive(c, depth + 1) if node.next: - generate_text_recursive(node.next) + generate_text_recursive(node.next, depth) From 5378c5069c2858a5847f6ef7dc6011a849534986 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Mon, 17 Jun 2024 16:50:08 +0100 Subject: [PATCH 3/3] instruction_tree: Build source in PackedStringArray MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From an efficiency point of view, repeatedly appending to an immutable string probably involves repeatedly copying the string. (Some languages have an optimisation where if `s` is the only reference to a string, then `s += "foo"` modifies `s` in place; I don't know if GDScript has that.) From a clarity perspective, it is strange for the output buffer to be an instance variable of the InstructionTree – it is a transient buffer used during a call to generate_text(). Replace it with a PackedStringArray, passed through the recursive calls, containing lines of code. Join it to produce the generated code as a whole. --- .../instruction_tree/instruction_tree.gd | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/addons/block_code/instruction_tree/instruction_tree.gd b/addons/block_code/instruction_tree/instruction_tree.gd index a06f9260..5122074e 100644 --- a/addons/block_code/instruction_tree/instruction_tree.gd +++ b/addons/block_code/instruction_tree/instruction_tree.gd @@ -1,8 +1,6 @@ class_name InstructionTree extends Object -var out: String - class TreeNode: var data: String @@ -17,19 +15,17 @@ class TreeNode: func generate_text(root_node: TreeNode, start_depth: int = 0) -> String: - out = "" - generate_text_recursive(root_node, start_depth) - return out + var out = PackedStringArray() + generate_text_recursive(root_node, start_depth, out) + return "".join(out) -func generate_text_recursive(node: TreeNode, depth: int): +func generate_text_recursive(node: TreeNode, depth: int, out: PackedStringArray): if node.data != "": - for i in depth: - out += "\t" - out += node.data + "\n" + out.append("\t".repeat(depth) + node.data + "\n") for c in node.children: - generate_text_recursive(c, depth + 1) + generate_text_recursive(c, depth + 1, out) if node.next: - generate_text_recursive(node.next, depth) + generate_text_recursive(node.next, depth, out)