diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-11-23 12:10:54 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-11-23 12:10:54 -0600 |
commit | 02e70483ad490b83118ea43770c995c61ffa4ce7 (patch) | |
tree | 0b0be4a69a7f58cda17e6f281263c3f2ad5cc0fa /compilador | |
parent | f6b89fb8abcc613277f2faa8b1a462234151a346 (diff) | |
download | javanol-02e70483ad490b83118ea43770c995c61ffa4ce7.tar.gz javanol-02e70483ad490b83118ea43770c995c61ffa4ce7.tar.bz2 javanol-02e70483ad490b83118ea43770c995c61ffa4ce7.zip |
Se añade función para renderizar árbol
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) |