-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentorno.js
89 lines (76 loc) · 2.59 KB
/
entorno.js
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
export class Entorno {
//tipar el entorno
/**
* @param {Entorno} padre
*/
constructor(padre = undefined) {
this.valores = {};
this.padre = padre;
}
/**
* @param {string} nombre
* @param {any} valor
* @param {string} tipo
* @param {string} simbType
* @param {any} linea
* @param {any} columna
*/
addVariable(nombre, valor, tipo, simbType, linea, columna) {
this.valores[nombre] = { valor, tipo, simbType, linea, columna };
//console.log("Estado actual de valores:", this.valores);
}
/**
* @param {string} nombre
*/
getVariable(nombre) {
const currentV = this.valores[nombre];
// Ver si existe y devolver el valor
if (currentV !== undefined){
console.log("ENTORNO valor:", currentV)
return currentV;
}
// Si no existe, revisar el padre y así en recursividad
if (!currentV && this.padre) {
console.log(` ${nombre} ---buscando`);
return this.padre.getVariable(nombre);
}
//console.log(`la variable: ${nombre} - aun no existe en el entorno`);
return null;
}
/**
* @param {string} nombre
*/
getBracketVar(nombre) {
const currentV = this.valores[nombre];
// Solo revisa si existe en el entorno actual
if (currentV !== undefined) return currentV;
console.log(`la variable: ${nombre} no existe en el entorno actual`);
return null;
}
/**
* @param {string} nombre
* @param {any} valor
* @param {string} tipo
* @param {string} simbType
* @param {any} linea
* @param {any} columna
*/
updateVariable(nombre, valor, tipo, simbType, linea, columna) {
const valorAssign = this.valores[nombre];
// Si existe la variable, actualizar su valor y demás propiedades
if (valorAssign !== undefined) {
this.valores[nombre] = {
...valorAssign, // solo para mantener las propiedades anteriores
valor // update solo valor
};
//console.log("Estado actual de valores:", this.valores);
return;
}
// Si no existe en este entorno, buscar en el entorno padre
if (!valorAssign && this.padre) {
this.padre.updateVariable(nombre, valor, tipo, simbType, linea, columna);
return;
}
throw new Error(`La variable ${nombre} no se encuentra definida dentro del código ejecutado`);
}
}