Skip to content

Commit

Permalink
🎨 C3 printer code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michprev committed Dec 24, 2023
1 parent 1d38065 commit 0248709
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions wake_printers/c3.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

import networkx as nx
import rich_click as click
from rich import print
from rich.tree import Tree

import wake.ir as ir
import wake.ir.types as types
from rich import print
from wake.printers import Printer, printer
from rich.tree import Tree


# C3 linearization printer
class C3Printer(Printer):
Expand All @@ -17,38 +20,54 @@ def print(self) -> None:

def visit_contract_definition(self, node: ir.ContractDefinition) -> None:
print(f"[italic]C3 linearization ordered[/italic]")
tree = Tree('.')
tree = Tree(".")

# Using Rich Tree strcutrue to print the C3 linearization
def add_contract_to_tree(parent_node, n, index):
contract_name = n.canonical_name
if not self._interfaces and contract_name.startswith('I'):
if not self._interfaces and contract_name.startswith("I"):
return


contract_node = parent_node.add(f"{index:2d}.[bold][link={self.generate_link(n)}]{contract_name}[/link][/bold]")
contract_node = parent_node.add(
f"{index:2d}.[bold][link={self.generate_link(n)}]{contract_name}[/link][/bold]"
)
# If verobose (-v/--verbose), add inheritance and constructor info
if self._verbose:
# Inheritance - base contracts
if n.base_contracts:
base_contracts_str = ", ".join(f"[blue]{x.base_name.name}[/blue]" for x in n.base_contracts)
contract_node.add(f"[italic]Base Contracts:[/italic] {base_contracts_str}")
base_contracts_str = ", ".join(
f"[blue]{x.base_name.name}[/blue]" for x in n.base_contracts
)
contract_node.add(
f"[italic]Base Contracts:[/italic] {base_contracts_str}"
)

# Constructor and unherited constructors
for x in n.functions:
if x.kind == "constructor":
constructor_str = f"[green]{x.canonical_name.split('.')[1]}[/green]"
constructor_str = (
f"[green]{x.canonical_name.split('.')[1]}[/green]"
)
if x.modifiers:
modifiers_str = ", ".join(f"[green]{op.source}[/green]" for op in x.modifiers if op.kind == "baseConstructorSpecifier")
modifiers_str = ", ".join(
f"[green]{op.source}[/green]"
for op in x.modifiers
if op.kind == "baseConstructorSpecifier"
)
constructor_str += f", {modifiers_str}"
contract_node.add(f"[italic]Constructor:[/italic] {constructor_str}")
contract_node.add(
f"[italic]Constructor:[/italic] {constructor_str}"
)

return contract_node

# Counter for more readable output
counter = 1
for n in node.linearized_base_contracts:
add_contract_to_tree(tree, n, counter)
counter += 1 if self._interfaces or not n.canonical_name.startswith('I') else 0
counter += (
1 if self._interfaces or not n.canonical_name.startswith("I") else 0
)

print(tree)

Expand All @@ -70,7 +89,3 @@ def add_contract_to_tree(parent_node, n, index):
def cli(self, interfaces: bool, verbose: bool) -> None:
self._interfaces = interfaces
self._verbose = verbose
pass



0 comments on commit 0248709

Please sign in to comment.