Skip to content

Commit

Permalink
Merge pull request #46 from endlessm/simplify-instructiontree
Browse files Browse the repository at this point in the history
instruction_tree: Pass depth & output buffer as parameters
  • Loading branch information
manuq authored Jun 18, 2024
2 parents 3050f2e + 5378c50 commit 06392d9
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions addons/block_code/instruction_tree/instruction_tree.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
class_name InstructionTree
extends Object

var depth: int
var out: String


class TreeNode:
var data: String
Expand All @@ -18,24 +15,17 @@ class TreeNode:


func generate_text(root_node: TreeNode, start_depth: int = 0) -> String:
out = ""
depth = start_depth
generate_text_recursive(root_node)
return out


func generate_text_recursive(root_node: TreeNode):
if root_node.data != "":
for i in depth:
out += "\t"
out += root_node.data + "\n"
var out = PackedStringArray()
generate_text_recursive(root_node, start_depth, out)
return "".join(out)

depth += 1

for c in root_node.children:
generate_text_recursive(c)
func generate_text_recursive(node: TreeNode, depth: int, out: PackedStringArray):
if node.data != "":
out.append("\t".repeat(depth) + node.data + "\n")

depth -= 1
for c in node.children:
generate_text_recursive(c, depth + 1, out)

if root_node.next:
generate_text_recursive(root_node.next)
if node.next:
generate_text_recursive(node.next, depth, out)

0 comments on commit 06392d9

Please sign in to comment.