aboutsummaryrefslogtreecommitdiff
path: root/compilador/tabla.py
blob: 7871a42ef07389fecfcc0be37d1a8606fc45551a (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json, os
from enum import Enum, auto
from dataclasses import dataclass
from typing import Any
# from more_itertools import seekable

from nanoiter import NanoIter

reservadas = [
    'booleano',
    'cadena',
    'caracter',
    'continuar',
    'detener',
    'entero',
    'funcion',
    'imprimir',
    'leer',
    'mientras',
    'retornar',
    'si',
    'sino',
    'vacio',
]

literales = [
    'BOOLEAN_LIT',
    'CHAR_LIT',
    'DOUBLE_LIT',
    'INT_LIT',
    'STRING_LIT'
]

tokens = reservadas + literales + [
    '{', '}', '(', ')', ',', '\'',
    '"', ';', '=', '*', '/', '+',
    '-', '>', '<', '>=', '<=', '&&',
    '||', '==', '!='
]

class Token(Enum):
    BOOLEAN = 'booleano'
    CHAR = 'caracter'
    DOUBLE = 'doble'
    ELSE = 'sino'
    IDENT = 'IDENT'
    IF = 'si'
    INT = 'entero'
    PRINT = 'imprimir'
    READ = 'leer'
    BREAK = 'detener'
    CONTINUE = 'continuar'
    RETURN = 'retornar'
    STRING = 'cadena'
    VOID = 'vacio'
    FUNCTION = 'funcion'
    WHILE = 'mientras'
    BOOLEAN_LIT = 'BOOLEAN_LIT'
    INT_LIT = 'INT_LIT'
    CHAR_LIT = 'CHAR_LIT'
    STRING_LIT = 'STRING_LIT'
    L_BRACKET = '{'
    R_BRACKET = '}'
    L_PAREN = '('
    R_PAREN = ')'
    COMMA = ','
    SQUOTE = '\''
    DQUOTE = '"'
    SEMICOLON = ';'
    EQUAL = '='
    TIMES = '*'
    SLASH = '/'
    PLUS = '+'
    MINUS = '-'
    GT = '>'
    LT = '<'
    GEQ = '>='
    LEQ = '<='
    AND = '&&'
    OR = '||'
    EQEQ = '=='
    NOTEQ = '!='
    EOF = 'EOF'



@dataclass
class LexToken:
    tipo: Token
    nombre: str
    valor: Any
    numlinea: int

    def __str__(self):
        return "LexToken(%s,%s,%s,%i)" % (
            self.tipo.name, self.nombre, self.valor, self.numlinea
        )

class TablaLex:
    def __init__(self):
        self.tabla = []

    def insertar(self, tok: LexToken):
        self.tabla.append(tok)

    def buscar(self, nombre: str):
        return [t for t in self.tabla if t.nombre == nombre][0]

    def iterar(self):
        return NanoIter(self.tabla)

    def actualizar(self, nombre: str, tok: LexToken):
        for i, t in enumerate(self.tabla):
            if t.nombre == nombre:
                self.tabla[i] = tok
                return

    def exportar(self, output_file):
        data = []
        for t in self.tabla:
            data.append({
                'tipo': t.tipo.value,
                'nombre': t.nombre,
                'valor': t.valor,
                'numlinea': t.numlinea
            })
        output = json.dumps(data)
        if os.path.exists(output_file):
            os.remove(output_file)
        with open(output_file, 'w+') as f:
            f.truncate(0)
            f.write(output)

    def importar(self, input_file):
        with open(input_file, 'r') as f:
            data = json.loads(f.read())
            for t in data:
                self.insertar(LexToken(Token(t['tipo']),
                                       t['nombre'],
                                       t['valor'],
                                       t['numlinea']))

    def __str__(self):
        output = ""
        for t in self.tabla:
            output += str(t) + "\n"
        return output