-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodigo.cpp
More file actions
296 lines (256 loc) · 7.56 KB
/
Codigo.cpp
File metadata and controls
296 lines (256 loc) · 7.56 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "Codigo.hpp"
using namespace std;
const string Codigo::NUMERO_INT = "ent";
const string Codigo::NUMERO_FLOAT = "real";
const string Codigo::NUMERO = NUMERO_INT + NUMERO_FLOAT;
const string Codigo::BOOLEANO = "bool";
/****************/
/* Constructora */
/****************/
Codigo::Codigo()
{
siguienteIdentificador = 1;
TablaSimbolos ts;
this->empilar(ts);
}
/************************/
/* siguienteInstruccion */
/************************/
int Codigo::siguienteInstruccion() const
{
return instrucciones.size() + 1;
}
/***********/
/* nuevoId */
/***********/
string Codigo::nuevoId()
{
stringstream cadena;
cadena << "_t" << siguienteIdentificador++;
return cadena.str();
}
/*********************/
/* anadirInstruccion */
/*********************/
void Codigo::anadirInstruccion(const string &instruccion)
{
stringstream cadena;
cadena << siguienteInstruccion() << ": " << instruccion;
instrucciones.push_back(cadena.str());
}
/***********************/
/* anadirDeclaraciones */
/***********************/
void Codigo::anadirDeclaraciones(const vector<string> &idNombres, const string &tipoNombre)
{
vector<string>::const_iterator iter;
for (iter = idNombres.begin(); iter != idNombres.end(); iter++)
{
pilaTS.tope().anadirVariable(*iter, tipoNombre);
anadirInstruccion(tipoNombre + " " + *iter + ";");
}
}
/*********************/
/* anadirParametros */
/*********************/
void Codigo::anadirParametros(const vector<string> &idNombres, const string &pTipo, const string &tipoNombre)
{
vector<string>::const_iterator iter;
for (iter = idNombres.begin(); iter != idNombres.end(); iter++)
{
pilaTS.anadirParametro(this->procedimientoActual, *iter, pTipo, tipoNombre);
anadirInstruccion(pTipo + "_" + tipoNombre + " " + *iter + ";");
}
}
/**************************/
/* completarInstrucciones */
/**************************/
void Codigo::completarInstrucciones(vector<int> &numerosInstrucciones, const int referencia)
{
stringstream cadena;
vector<int>::iterator iter;
cadena << " " << referencia;
for (iter = numerosInstrucciones.begin(); iter != numerosInstrucciones.end(); iter++)
{
instrucciones[*iter - 1].append(cadena.str() + ";");
}
}
/************/
/* escribir */
/************/
void Codigo::escribir() const
{
//const string nombreFichero("output.txt");
//fstream f(nombreFichero.c_str(), fstream::out);
vector<string>::const_iterator iter;
for (iter = instrucciones.begin(); iter != instrucciones.end(); iter++)
{
cout << *iter << endl;
//f << *iter << endl;
}
//f.close();
}
/************/
/* obtenRef */
/************/
int Codigo::obtenRef() const
{
return siguienteInstruccion();
}
void Codigo::empilar(const TablaSimbolos &ts)
{
pilaTS.empilar(ts);
}
void Codigo::desempilar()
{
pilaTS.desempilar();
}
void Codigo::declararProcedimiento(const string &pProc)
{
this->procedimientoActual = pProc;
pilaTS.tope().anadirProcedimiento(pProc);
TablaSimbolos ts;
this->empilar(ts);
this->anadirInstruccion("proc " + pProc + ";");
}
void Codigo::finProcedimiento()
{
this->procedimientoActual = "";
this->desempilar();
}
void Codigo::comprobarTipos(const string &pTipo1, const string &pTipo2)
{
if (pTipo2.find(pTipo1) == string::npos)
{
throw string("Error semántico. Los tipos " + pTipo1 + " y " + pTipo2 + " no concuerdan.");
}
}
bool Codigo::esTipo(const string &pTipo, const string &pQuery)
{
return pTipo.find(pQuery) != string::npos;
}
void Codigo::llamadaProcedimiento(const string &proc, const vector<pair<string, string>> &lparam)
{
try
{
this->pilaTS.verificarNumArgs(proc, lparam.size());
for (unsigned int i = 0; i < lparam.size(); i++)
{
pair<string, string> p = this->pilaTS.obtenerTiposParametro(proc, i);
if (this->pilaTS.tope().existeId(lparam[i].first))
{
this->comprobarTipos(p.second, this->pilaTS.obtenerTipo(lparam[i].first));
}
else
{ // Se trata de una variable temporal generada por pasar como parámetro una expresion (por lo tanto no figura en la TS)
this->comprobarTipos(p.second, lparam[i].second);
}
this->anadirInstruccion("param_" + p.first + " " + lparam[i].first + ";");
}
this->anadirInstruccion("call " + proc + ";");
}
catch (string s)
{
throw s;
}
}
string Codigo::iniNom()
{
return "";
}
string Codigo::obtenerTipo(const string &id)
{
return pilaTS.obtenerTipo(id);
}
void Codigo::operacionAritmetica(expresionstruct *dobleDolar, const expresionstruct &op1, const expresionstruct &op2, const string &operacion)
{
string tipoFinal, tmp;
int opCast = 0;
if (this->esTipo(this->NUMERO_INT, op1.tipo) && this->esTipo(this->NUMERO_FLOAT, op2.tipo))
{
tipoFinal = this->NUMERO_FLOAT;
opCast = 1;
tmp = this->nuevoId();
}
else if (this->esTipo(this->NUMERO_FLOAT, op1.tipo) && this->esTipo(this->NUMERO_INT, op2.tipo))
{
tipoFinal = this->NUMERO_FLOAT;
opCast = 2;
tmp = this->nuevoId();
}
else if (this->esTipo(op1.tipo, op2.tipo))
{
if (operacion.find("/") != string::npos)
tipoFinal = this->NUMERO_FLOAT;
else
tipoFinal = op2.tipo;
}
else
{
throw string("Error semántico. La operación " + operacion + " no se aplica sobre variables booleanas.");
}
dobleDolar->nom = this->nuevoId();
dobleDolar->trues = this->iniLista(0);
dobleDolar->falses = this->iniLista(0);
dobleDolar->tipo = tipoFinal;
switch (opCast)
{
case 0:
this->anadirInstruccion(dobleDolar->nom + " := " + op1.nom + " " + operacion + " " + op2.nom + ";");
break;
case 1:
this->anadirInstruccion(tmp + " := ent2real " + op1.nom + ";");
this->anadirInstruccion(dobleDolar->nom + " := " + tmp + " " + operacion + " " + op2.nom + ";");
break;
case 2:
this->anadirInstruccion(tmp + " := ent2real " + op2.nom + ";");
this->anadirInstruccion(dobleDolar->nom + " := " + op1.nom + " " + operacion + " " + tmp + ";");
break;
}
}
vector<int> Codigo::iniLista(const int &arg)
{
vector<int> result;
if (arg != 0)
result.push_back(arg);
return result;
}
vector<string> Codigo::iniLista(const string &arg)
{
vector<string> result;
if (arg != "")
result.push_back(arg);
return result;
}
vector<pair<string, string>> Codigo::iniLista(const string &id, const string &tipo)
{
vector<pair<string, string>> result;
if (id != "")
{
pair<string, string> par(id, tipo);
result.push_back(par);
}
return result;
}
bool esVacia(const vector<int> &lista)
{
return lista.empty();
}
vector<int> *Codigo::unir(const vector<int> &list1, const vector<int> &list2)
{
vector<int> *merged = new vector<int>(list1);
merged->insert(merged->end(), list2.begin(), list2.end());
return merged;
}
vector<string> *Codigo::unir(const vector<string> &list1, const vector<string> &list2)
{
vector<string> *merged = new vector<string>(list1);
merged->insert(merged->end(), list2.begin(), list2.end());
return merged;
}
vector<pair<string, string>> *Codigo::unir(const vector<pair<string, string>> &list1, const vector<pair<string, string>> &list2)
{
vector<pair<string, string>> *merged = new vector<pair<string, string>>(list1);
merged->insert(merged->end(), list2.begin(), list2.end());
return merged;
}