aboutsummaryrefslogtreecommitdiff
path: root/compilador/astree/decl.py
blob: 99f5f7598168f0565bf2dc3c5da1251f0743e7b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import uuid
import graphviz as gv
from dataclasses import dataclass
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

# A global declaration.
#
#     entero a = 0;
@dataclass
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(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