Skip to content

Commit

Permalink
Added string formatter to Tree[T]
Browse files Browse the repository at this point in the history
  • Loading branch information
christofsteel committed Aug 12, 2024
1 parent 0aafe3e commit 8993d4c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions clsp/enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ def __eq__(self, other: Any) -> bool:
def __lt__(self, other: "Tree[T]") -> bool:
return self.size < other.size

def __rec_to_str__(self, outermost: bool) -> str:
str_root = [f"{str(self.root)}"]
str_params = [
f"{{{name}={subtree.__rec_to_str__(True)}}})"
for name, subtree in self.parameters.items()
]
str_args = [f"{subtree.__rec_to_str__(False)}" for subtree in self.arguments]

strings = str_root + str_params + str_args
if not outermost and len(strings) > 1:
return f"({' '.join(strings)})"
return " ".join(strings)

def __str__(self) -> str:
return self.__rec_to_str__(True)


def tree_size(tree: Tree[T]) -> int:
"""The number of nodes in a tree."""
Expand Down

0 comments on commit 8993d4c

Please sign in to comment.