Hello. Well... as the tittle says, im trying to figure out how to jump back or forward depending on the command a string in a vector of strings. For example, I have the following texr
INT a
INT b
a = 4
b = 1
IF ( a < 1 ) THEN JUMP 9
b = ( b * a )
a = ( a - 1 )
JUMP 5
SHOW b
if a JUMP comes i need to go to the line of the jump, i tried a lot of combinations but i keep getting either an inifinite loop or nothing at all. This is the code. I know it might be wrong or not very efficient but its one of my first projects.
Code:
bool AYED2019::operaciones(vector<string> Original) { bool cond;
int aux1 = 0, aux2 = 0, o2 = 0, o1 = 0, tempA = 0, tempB = 0;
char auxiliar;
int d = 0;
char h, k, operacion;
for (linea_actual = Script.begin(); linea_actual != Script.end(); linea_actual++) {
string& cadena = *linea_actual;
if (cadena.find("INT") == 0) {
Entorno[cadena.at(4)];
}
//}
//for (linea_actual = Script.begin(); linea_actual != Script.end(); linea_actual++) {
//string& cadena = *linea_actual;
if (cadena.at(2) == '=') {
for (iterador_mapa = Entorno.begin(); iterador_mapa != Entorno.end(); iterador_mapa++) {
//cout << "int: " << iterador_mapa->first << " " << "value: " << iterador_mapa->second;
if (Entorno.find(cadena.at(0)) != Entorno.end() && isnumS(cadena.at(4))) {
Entorno[cadena.at(0)] = atoi(&cadena.at(4));
}
}
}
//}
//for (linea_actual = Script.begin(); linea_actual != Script.end(); linea_actual++) {
iterador_mapa = Entorno.begin();
//string& cadena = *linea_actual;
if(cadena.size()>8){
if (cadena.at(2) == '=') {
if (Entorno.find(cadena.at(6)) != Entorno.end()) {
auxiliar = iterador_mapa->first;
tempA = Entorno[cadena.at(6)];
}
iterador_mapa++;
if (isnumS(cadena.at(10))) {
tempB = atoi(&cadena.at(10));
}
else {
tempB = Entorno[cadena.at(10)];
}
operacion = cadena.at(8);
calcular(tempA, tempB, operacion);
Entorno[cadena.at(0)] = calcular(tempA, tempB, operacion);
}
}
// }
//for (linea_actual = Script.begin(); linea_actual != Script.end(); linea_actual++) {
iterador_mapa = Entorno.begin();
//string& cadena = *linea_actual;
if (cadena.at(0) == 'I' && cadena.at(1) == 'F') {
size_t pos1 = cadena.find("IF");
string str1 = cadena.substr(pos1);
for (iterador_mapa = Entorno.begin(); iterador_mapa != Entorno.end(); iterador_mapa++) {
if (Entorno.find(cadena.at(5)) != Entorno.end()) {
o1 = Entorno[cadena.at(5)];
// cout << "El valor de o1 es: " << o1 << endl;
aux1 = 1;
iterador_mapa++;
if (Entorno.find(cadena.at(9)) != Entorno.end()) {
o2 = Entorno[cadena.at(9)];
// cout << "El valor de o2 es: " << o2 << endl;
aux2 = 1;
}
else {
o2 = atoi(&str1.at(9));
// cout << "El valor de o2 es: " << o2 << endl;
aux2 = 1;
}
if (aux1 == 1 && aux2 == 1) {
h = str1.at(7);
condicionIfElse(h, o1, o2);
break;
}
}
}
if (condicionchequeada) {
if (size_t pos = cadena.find("THEN")) {
string str3 = cadena.substr(pos);
iterador_mapa = Entorno.begin();
if (str3.find("SHOW")) {
for (int i = 0; i < str3.size(); i++) {
if (Entorno.find(cadena.at(5)) != Entorno.end()) {
cout << "Showing: " << Entorno[cadena.at(5)] << endl;
break;
}
}
}
if (str3.find("JUMP")) {
if (isnumS(cadena.at(5))) {
linea_actual = Script.begin();
for(int i = 0; i < (int) cadena.at(5) - '0' - 2; i++){
linea_actual++;
}
}
}
}
}
}
//}
//for (linea_actual = Script.begin(); linea_actual != Script.end(); linea_actual++) {
//iterador_mapa = Entorno.begin();
// string& cadena = *linea_actual;
if (cadena.at(0) == 'S' && cadena.at(1) == 'H' && cadena.at(2) == 'O' && cadena.at(3) == 'W') {
if ((iterador_mapa = Entorno.find(cadena.at(5))) != Entorno.end()) {
cout << "Showing: " << Entorno[cadena.at(5)] << endl;;
}
}
}
for (linea_actual = Script.begin(); linea_actual != Script.end(); linea_actual++) {
iterador_mapa = Entorno.begin();
string& cadena = *linea_actual;
if (cadena.at(0) == 'J' && cadena.at(1) == 'U' && cadena.at(2) == 'M' && cadena.at(3) == 'P') {
if (isnumS(cadena.at(5))) {
d = (atoi(&cadena.at(5)));
linea_actual = Script.begin();
for(int i = 0; i < d - 2; i++){
linea_actual++;
}
}
}
}
//for (iterador_mapa = Entorno.begin(); iterador_mapa != Entorno.end(); iterador_mapa++) {
//cout << " " << iterador_mapa->first << " : " << iterador_mapa->second << endl;
//}
}
Thank you so much in advance. Oh I almost forgot...
Code:
typedef char Nombre; typedef int Valor;
unordered_map <Nombre, Valor> Entorno;
unordered_map <char, int>::iterator iterador_mapa, iterador_aux ;
vector<string> Script;
vector<string>::iterator linea_actual;
Those are the iterators and vectors im using