Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const string fname("text.");
- ifstream fin(fname + "in");
- ofstream fout(fname + "out");
- ///************************
- void finish(), procIfElse(), exePrint(), executeFunction();
- bool setVar(), createVar();
- #define ERR_FUN fout << "ERROR" << endl; return;
- string ans, s;
- unordered_map<string, string> variables;
- bool errInCurrFn = false, retInCurrFn = false;
- void exePrint() {
- fin >> s;
- if (variables.count(s)) {
- ans += variables[s];
- } else {
- ans += s;
- }
- }
- void finish() {
- fout << ans << endl;
- }
- void procIfElse() {
- unordered_map<string, int> st;
- fin >> s;//if cond
- st["if"]++;
- if (variables.count(s) && s != "true" && s != "false") {
- if (variables[s] == "true" || variables[s] == "false") {
- s = variables[s];
- } else {
- errInCurrFn = true;
- }
- }
- if (s == "true") {
- while (fin >> s && s != "end") {
- if (s == "if") {
- procIfElse();
- } else if (s == "var") {
- if (!createVar()) {
- errInCurrFn = true;
- }
- } else if (s == "set") {
- if (!setVar()) {
- errInCurrFn = true;
- }
- } else if (s == "print") {
- exePrint();
- } else if (s == "return") {
- retInCurrFn = true;
- return;
- }
- }
- st["if"]--;
- fin >> s;//else
- while (fin >> s && st["if"] >= 0) {
- if (s == "if") st["if"]++;
- if (s == "else") st["if"]++;
- if (s == "end") {
- st["if"]--;
- }
- }
- } else {
- while (st["else"] >= 0 && fin >> s) {
- if (s == "else") st["else"]++;
- if (s == "end") {
- st["else"]--;
- }
- }
- while (fin >> s && s != "end") {
- if (s == "var") {
- if (!createVar()) {
- errInCurrFn = true;
- }
- }
- else if (s == "set") {
- if (!setVar()) {
- errInCurrFn = true;
- }
- }
- else if (s == "print") {
- exePrint();
- }
- else if (s == "return") {
- retInCurrFn = true;
- return;
- }
- else if (s == "if") {
- procIfElse();
- }
- }
- }
- }
- bool createVar() {
- string name, val;
- fin >> name >> val;
- if (variables.count(name)) {
- return false;
- }
- if (variables.count(val)) {
- variables[name] = variables[val];
- } else {
- variables[name] = val;
- }
- return true;
- }
- bool setVar() {
- string name, val;
- fin >> name >> val;
- if (!variables.count(name)) {
- return false;
- }
- if (variables.count(val)) {
- variables[name] = variables[val];
- } else {
- variables[name] = val;
- }
- return true;
- }
- void executeFunction() {
- ans = "";
- variables.clear();
- errInCurrFn = false;
- retInCurrFn = false;
- //cerr << "HERE" << endl;
- while (fin >> s) {
- if (retInCurrFn) {
- if (s == "start") {
- finish();
- return;
- }
- continue;
- }
- if (s == "print") exePrint();
- else if (s == "var") {
- if (!createVar()) {
- errInCurrFn = true;
- }
- }
- else if (s == "set") {
- if (!setVar()) {
- errInCurrFn = true;
- }
- }
- else if (s == "end") {
- break;
- }
- else if (s == "if") {
- procIfElse();
- } else if (s == "return" || retInCurrFn) {
- while (fin >> s && s != "start");
- }
- }
- if (errInCurrFn) {
- ans = "";
- variables.clear();
- errInCurrFn = false;
- ERR_FUN
- }
- finish();
- }
- int main() {
- int n;
- fin >> n;
- while (s == "start" || fin >> s) {
- if (s == "start") {
- executeFunction();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement