-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_dir.py
More file actions
246 lines (203 loc) · 10.7 KB
/
functions_dir.py
File metadata and controls
246 lines (203 loc) · 10.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""Modulo que contiene la clase directorio de funciones
-----------------------------------------------------------------
Compilers Design Project
Tec de Monterrey
Julio Cesar Aguilar Villanueva A01152537
Jose Fernando Davila Orta A00999281
-----------------------------------------------------------------
DOCUMENTATION: For complete Documentation see UserManual.pdf"""
from stack import Stack
from function import Function
from variable import Variable
def get_var_type(var_type):
'''retorna el identificador de cada tipo de variable'''
if var_type == 'int':
return 'i'
elif var_type == 'double':
return 'd'
elif var_type == 'string':
return 's'
elif var_type == 'bool':
return 'b'
def get_var_scope(scope):
'''retorna el identificador de cada tipo de scope'''
if scope == 'global':
return 'g'
elif scope == 'main':
return 'l'
else:
return 't'
def get_var_name(var_type, scope, var_name):
'''construct the direccion of a variable based on
the type, scope and variable name.'''
name_type = get_var_type(var_type)
name_scope = get_var_scope(scope)
name = name_type + name_scope + var_name
return name
class FunctionsDir(object):
'''Las funciones son entradas en el diccionario functions.
Las funciones son objetos con diccionarios de variables.
Scope global del programa se inicia con una funcion global
sin variables.
Scope es el function_id de cada funcion.'''
def __init__(self):
'''Metodo de inicializacion'''
self.functions = {}
self.functions['global'] = Function()
self.scope = 'global'
# Define si se esta evaluando la existencia de variables o se estan agregando al directorio
self.evaluating = True
# Indica si es necesario acutlaizar la lista de prametros de una funcion
self.updating_params = False
# Indica si se va a leer variable con funcion read
self.reading = False
# Ultimo token ID, usado para el read
self.last_id = Stack()
# Ultimo token de tipo que fue leido por el directorio de funciones
self.last_type = None
'''Funciones que estan siendo llamadas.
Se utiliza una pila para llamadas nesteadas a funciones'''
self.call_function = Stack()
'''Cantidad de argumentos que estan siendo utilizados al llamar a una funcion.
Se utiliza una pilla para llamadas nesteadas'''
self.call_arguments = Stack()
self.last_read = Stack()
def add_function(self, function_id):
'''Add function to fuctions directory. Verify if function already exists'''
if self.functions.get(function_id, None) is not None:
raise NameError('Error: 1001 Function already declared! Function: ' + str(function_id))
else:
self.functions[function_id] = Function()
def validate_function(self, function_id):
'''Validate function exists'''
if self.functions.get(function_id, None) is None:
raise NameError('Error: 1002 Function not declared! Name: ' + str(function_id))
def increase_expected_arguments(self):
'''Manda llamar el metodo increase expected arguments de la clase Function'''
self.functions[self.scope].increase_expected_arguments()
def update_function_params(self, var_id, var_type):
'''Manda llamar metodo update params de la clase Funcion'''
self.functions[self.scope].update_params(var_id, var_type)
def set_return_type(self, function_return_type):
'''Manda llamar el metodo set return type de la clase Function'''
self.functions[self.scope].set_return_type(function_return_type)
def set_func_quad(self, func_quad):
'''Manda llamar el metodo set_func_quad de la clase Function'''
self.functions[self.scope].set_func_quad(func_quad)
def set_scope(self, scope):
'''Cambia el scope actual del directorio de funciones al scope que recibe'''
self.scope = scope
def reset_scope(self):
'''Reset del scope a global scope'''
self.scope = 'global'
# Add variable to current function scope
def add_var(self, variable_id, var_type, value=0, size=1):
'''Agrega variable a el diccionario de variables de una Funcion'''
if self.functions[self.scope].variables_dict.get(variable_id, None) is None:
var_name = get_var_name(var_type, self.scope, variable_id)
self.functions[self.scope].variables_dict[variable_id] = Variable(var_name, value, var_type, self.scope, size)
else:
variable_type = self.functions[self.scope].variables_dict[variable_id].get_type()
msg = 'Error 2001: Variable already declared! ' + str(variable_id) + '. TYPE: ' + variable_type
raise NameError(msg)
def add_for_var(self, variable_id, var_type):
'''Agrega variable al diccionario del current scope, si ya existe sobreescribe valor
Marca error si existe y no es tipo int'''
if self.functions[self.scope].variables_dict.get(variable_id, None) is None:
var_name = get_var_name(var_type, self.scope, variable_id)
self.functions[self.scope].variables_dict[variable_id] = Variable(var_name, -1, var_type, self.scope, 1)
else:
variable_type = self.functions[self.scope].variables_dict[variable_id].get_type()
if variable_type != 'int':
msg = 'Error 2001: Variable already declared! ' + str(variable_id) + '. TYPE: ' + variable_type
raise NameError(msg)
else:
self.functions[self.scope].variables_dict[variable_id].value = -1
def validate_variable(self, variable_id):
'''Busca variable en el scope actual'''
if self.functions[self.scope].variables_dict.get(variable_id, None) is None:
# Busca variable en el scope global
if self.functions['global'].variables_dict.get(variable_id, None) is None:
raise NameError('Error 2002: Variable not declared! VAR: ' + variable_id)
def start_evaluating(self):
'''Indica que el directorio de funciones esta evaluando la existencia de variables'''
self.evaluating = True
def finish_evaluating(self):
'''Indica que el directorio de funciones deja de evaluar funciones'''
self.evaluating = False
def set_type(self, last_type):
'''Set del ultimo token de tipo que fue leido'''
self.last_type = last_type
def get_func_dir(self):
'''Obtiene el diccionario de funciones'''
return self.functions
def get_var(self, variable_id):
'''Obtiene la lista con los datos de la variable del
diccionario de funciones en el scope actual o el global'''
if variable_id in self.functions[self.scope].variables_dict:
return self.functions[self.scope].variables_dict.get(variable_id)
elif variable_id in self.functions['global'].variables_dict:
return self.functions['global'].variables_dict.get(variable_id)
return None
def set_call_function(self, function_id):
'''Set del id de la funcion que esta siendo llamada
una vez que se valido su existencia en el diccionario de funciones'''
self.call_function.push(function_id)
self.call_arguments.push(0)
def increase_call_arguments(self):
'''# Incrementa la cantidad de argumentos que estan siendo usados para llamar una funcion.
Obtiene el tope de la pila, aumenta y vuelve a insertar en la pila'''
curr = self.call_arguments.pop()
curr += 1
self.call_arguments.push(curr)
def update_var_size(self, size):
'''Actualiza el size de una variable en caso de ser dimensionada'''
if size <= 0:
raise ValueError('Error 7005: Array size must be a positive integer')
else:
self.functions[self.scope].variables_dict[self.last_id.top].size = size
self.functions[self.scope].variables_dict[self.last_id.top].is_dim = True
def validate_call_arguments(self):
'''Funcion que valida que la cantidad de argumentos utilizados en una llamada a funcion
sea igual a los parametros que espera recibir'''
if self.functions[self.call_function.top].expected_arguments != self.call_arguments.top:
if self.functions[self.call_function.top].expected_arguments > self.call_arguments.top:
msg = 'Error 3001: Missing arguments in function call for function: ' + str(self.call_function)
elif self.functions[self.call_function.top].expected_arguments < self.call_arguments.top:
msg = 'Error 3002: Too many arguments in function call for function: ' + str(self.call_function)
msg += '. Expected arguments: ' + str(self.functions[self.call_function.top].expected_arguments) + '. Got: ' + str(self.call_arguments.top)
self.call_arguments.pop()
self.call_function.pop()
raise ValueError(msg)
else:
self.call_arguments.pop()
return self.call_function.pop()
def validate_arg_type(self, var_type):
'''Funcion que valida que el tipo de argumento que se manda sea del tipo esperado'''
expected_type = self.functions[self.call_function.top].params[self.call_arguments.top - 1][1]
if var_type != expected_type:
msg = 'Error 3003: Expected type in function call ' + str(self.scope) + ': ' + expected_type
msg += '. Got: ' + var_type
raise ValueError(msg)
return self.functions[self.call_function.top].params[self.call_arguments.top - 1]
def verify_var_dim(self):
'''Verifica que el id de una variable sea dimensionada'''
var = self.get_var(self.last_id.top)
if not var.is_dim:
raise ValueError('Error 7003: Variable is not array')
@property
def current_scope(self):
'''Propiedad del directorio de funciones para obtener el scope actual'''
return self.scope
def printeame(self):
'''Funcion auxiliar para imprimir el contenido del directorio de funciones'''
print('************ Functions Directory ************\n')
for key, val in self.functions.iteritems():
print(str(val.return_type) + ' ' + str(key) + '('),
for var in val.params:
print(str(var[1]) + ' ' + str(var[0]) + ', '),
print('): quad_num ' + str(val.get_function_quad()))
for k, vals in val.variables_dict.iteritems():
print('\t' + vals.get_type() + ' ' + k + ' = ' + str(vals.get_value()) + ' size: ' + str(vals.get_size()))
print('')
print('*********************************************')