-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d835a2
commit cda32eb
Showing
6 changed files
with
102 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from functools import reduce | ||
import operator | ||
from functools import lru_cache as cached | ||
from .ascii_trees import Pyramid | ||
|
||
# =================================================================================== | ||
# | ||
# ##### ## ## #### ## ##### | ||
# ## ## ## ## ## ## ## ## | ||
# ##### ## ## ## ## ## ## | ||
# ## ## ## ## ## ## ## ## | ||
# ##### ##### #### ###### ##### | ||
# | ||
# =================================================================================== | ||
|
||
|
||
@cached(maxsize=10000) | ||
def build_tree(ast): | ||
"""Build the call tree from the leaves to the root""" | ||
|
||
if isinstance(ast, str): | ||
return Pyramid.from_text(ast) | ||
elif ast is None: | ||
return None | ||
elif isinstance(ast, tuple): | ||
if len(ast) != 3: | ||
raise RuntimeError( | ||
f"Invalid structure of the abstract syntax tree. ({ast})" | ||
) | ||
if not isinstance(ast[0], str): | ||
raise RuntimeError( | ||
"Invalid abstract syntax tree. The first element of each node must be" | ||
f" a string, not a {type(ast[0])}" | ||
) | ||
return build_tree(ast[0]) + (build_tree(ast[1]), build_tree(ast[2])) | ||
else: | ||
raise TypeError( | ||
"Abstract syntax tree must be represented by a list (or just a string) not" | ||
f" a {type(ast)}" | ||
) | ||
|
||
|
||
def build(ast) -> str: | ||
"""Build the program from the abstract syntax tree""" | ||
program = str(reduce(operator.add, (build_tree(a) for a in ast))) | ||
# Remove excessive whitespace | ||
return "\n".join(line[1:].rstrip() for line in program.split("\n")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.