diff options
Diffstat (limited to 'compilador')
-rw-r--r-- | compilador/arbol.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/compilador/arbol.py b/compilador/arbol.py index 7abcbe0..234d2e9 100644 --- a/compilador/arbol.py +++ b/compilador/arbol.py @@ -1,3 +1,7 @@ +import uuid, json +import graphviz as gv +from pprint import pformat + class Nodo: def __init__(self, dato = None): self.dato = dato @@ -12,6 +16,16 @@ class Nodo: s += "\n" return s + def render(self, dot: gv.Digraph, parent: str): + name = uuid.uuid1().hex + fdato = pformat(self.dato, indent=2).replace('\n', '\l') + dot.node(name, fdato) + if parent: + dot.edge(parent, name) + + for h in self.hijos: + h.render(dot, name) + def __str__(self): return self.print() @@ -20,6 +34,13 @@ class Arbol: def __init__(self, raiz: Nodo = Nodo()): self.raiz = raiz + def render(self, filename, view = False): + dot = gv.Digraph() + dot.attr(rankdir='LR') + dot.attr('node', fontname='monospace') + self.raiz.render(dot, None) + dot.render(filename, view = view) + def __str__(self): if self.raiz: return str(self.raiz) |