diff options
Diffstat (limited to 'compilador/astree/decl.py')
-rw-r--r-- | compilador/astree/decl.py | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/compilador/astree/decl.py b/compilador/astree/decl.py index 1e6de1d..99f5f75 100644 --- a/compilador/astree/decl.py +++ b/compilador/astree/decl.py @@ -1,6 +1,9 @@ +import uuid +import graphviz as gv from dataclasses import dataclass -from typing import Optional +from typing import Optional, cast +from astree.graphable import Graphable from astree.type import Type from astree.ident import Ident from astree.expr import Expr @@ -9,19 +12,41 @@ from astree.expr import Expr # # entero a = 0; @dataclass -class DeclGlobal: +class DeclGlobal(Graphable): ident: Ident _type: Type init: Expr + def graph(self, dot: gv.Digraph, parent: str = None, edge: str = None) -> None: + name = uuid.uuid1().hex + dot.node(name, 'DeclGlobal') + dot.edge(parent, name, label = edge) + name_ident = uuid.uuid1().hex + dot.node(name_ident, self.ident) + dot.edge(name, name_ident, label = 'ident') + if isinstance(self.init, Graphable): + self.init.graph(dot, name, 'init') + # A function declaration. # # funcion vacio main() { ... } @dataclass -class DeclFunc: +class DeclFunc(Graphable): ident: Ident prototype: Type body: Optional[Expr] + def graph(self, dot: gv.Digraph, parent: str = None, edge: str = None) -> None: + name = uuid.uuid1().hex + dot.node(name, 'DeclFunc') + dot.edge(parent, name, label = edge) + name_ident = uuid.uuid1().hex + dot.node(name_ident, self.ident) + dot.edge(name, name_ident, label = 'ident') + if isinstance(self.prototype, Graphable): + self.prototype.graph(dot, name, 'prototype') + if self.body and isinstance(self.body, Graphable): + self.body.graph(dot, name, 'body') + # A Javañol declaration Decl = DeclGlobal | DeclFunc |