#include <iostream>
#include <vector>
#include <map>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
string trim(string str)
{
char const* delims = " \t\r\n";
// trim leading whitespace
string::size_type notwhite = str.find_first_not_of(delims);
str.erase(0,notwhite);
// trim trailing whitespace
notwhite = str.find_last_not_of(delims);
str.erase(notwhite+1);
return str;
};
int toint(const string &s) {
istringstream myStream(s);
int i = 0;
myStream>>i;
return i;
};
void main(int argc, char** argv) {
ifstream in(argv[1]);
string line;
map<string, int> var;
map<string, vector<string>::iterator> navest;
vector<string> pole;
size_t hlp;
string premenna;
string hodnota;
string navestie;
while(getline(in,line,'\n')) {
pole.insert(pole.end(), line);
}
//spracovanie navesti
for(vector<string>::iterator it = pole.begin(); it != pole.end(); it++) {
if ((it->find("=",0) == string::npos)&&(it->find(">>",0) == string::npos)
&&(it->find("<<",0) == string::npos)&&(it->find(":",0) == string::npos)) {
if (it->find(";",0) != string::npos) {
hlp = it->find(";",0);
navestie = trim(it->substr(0,hlp));
}
if (it->find("!",0) != string::npos) {
hlp = it->find("!",0);
navestie = trim(it->substr(0,hlp));
}
if (it->find("+",0) != string::npos) {
hlp = it->find("+",0);
navestie = trim(it->substr(0,hlp));
}
if (it->find("-",0) != string::npos) {
hlp = it->find("-",0);
navestie = trim(it->substr(0,hlp));
}
for(vector<string>::iterator it2 = pole.begin(); it2 != pole.end(); it2++) {
if ((it2->find(navestie,0) != string::npos)&&(it2->find(":",0) != string::npos)) {
navest[*it] = it2;
break;
}
}
}
}
//spracovanie premennych
for(vector<string>::iterator it = pole.begin(); it != pole.end(); it++) {
bool somdnu = false;
if (it->find("=",0) != string::npos) {
bool nasielsom = false;
if (it->find("+=",0) != string::npos) {
hlp = it->find("+=",0);
premenna = trim(it->substr(0,hlp));
hodnota = trim(it->substr(hlp+2,it->length()));
if (isdigit(hodnota[0])) {
var[premenna]+=toint(hodnota);
} else var[premenna]+=var[hodnota];
nasielsom = true;
}
if (it->find("-=",0) != string::npos) {
hlp = it->find("-=",0);
premenna = trim(it->substr(0,hlp));
hodnota = trim(it->substr(hlp+2,it->length()));
if (isdigit(hodnota[0])) {
var[premenna]-=toint(hodnota);
} else var[premenna]-=var[hodnota];
nasielsom = true;
}
if (it->find("*=",0) != string::npos) {
hlp = it->find("*=",0);
premenna = trim(it->substr(0,hlp));
hodnota = trim(it->substr(hlp+2,it->length()));
if (isdigit(hodnota[0])) {
var[premenna]*=toint(hodnota);
} else var[premenna]*=var[hodnota];
nasielsom = true;
}
if (it->find("/=",0) != string::npos) {
hlp = it->find("/=",0);
premenna = trim(it->substr(0,hlp));
hodnota = trim(it->substr(hlp+2,it->length()));
if (isdigit(hodnota[0])) {
var[premenna]/=toint(hodnota);
} else var[premenna]/=var[hodnota];
nasielsom = true;
}
if (!nasielsom) {
hlp = it->find("=",0);
premenna = trim(it->substr(0,hlp));
hodnota = trim(it->substr(hlp+2,it->length()));
if (isdigit(hodnota[0])) {
var[premenna]=toint(hodnota);
} else var[premenna]=var[hodnota];
}
somdnu = true;
} //end if "="
if (it->find(">>",0) != string::npos) {
hlp = it->find(">>",0);
premenna = trim(it->substr(0,hlp));
cout << var[premenna] << endl;
somdnu = true;
}
if (it->find("<<",0) != string::npos) {
hlp = it->find("<<",0);
premenna = trim(it->substr(0,hlp));
cin >> var[premenna];
somdnu = true;
}
if (!somdnu) { //to znamena ze ide o navestie
if (it->find(";",0) != string::npos) {
it = navest[*it];
}
if (it->find("!",0) != string::npos) {
hlp = it->find("!",0);
premenna = trim(it->substr(hlp+1,it->length()));
if (var[premenna] == 0) it = navest[*it];
}
if (it->find("+",0) != string::npos) {
hlp = it->find("+",0);
premenna = trim(it->substr(hlp+1,it->length()));
if (var[premenna] > 0) it = navest[*it];
}
if (it->find("-",0) != string::npos) {
hlp = it->find("-",0);
premenna = trim(it->substr(hlp+1,it->length()));
if (var[premenna] < 0) it = navest[*it];
}
}
}
}