Advertisement
STANAANDREY

nuj

Nov 12th, 2021
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const string fname("text.");
  4. ifstream fin(fname + "in");
  5. ofstream fout(fname + "out");
  6. ///************************
  7. void finish(), procIfElse(), exePrint(), executeFunction();
  8. bool setVar(), createVar();
  9. #define ERR_FUN fout << "ERROR" << endl; return;
  10. string ans, s;
  11. unordered_map<string, string> variables;
  12. bool errInCurrFn = false, retInCurrFn = false;
  13.  
  14. void exePrint() {
  15.     fin >> s;
  16.     if (variables.count(s)) {
  17.         ans += variables[s];
  18.     } else {
  19.         ans += s;
  20.     }
  21. }
  22.  
  23. void finish() {
  24.     fout << ans << endl;
  25. }
  26.  
  27. void procIfElse() {
  28.     unordered_map<string, int> st;
  29.     fin >> s;//if cond
  30.     st["if"]++;
  31.     if (variables.count(s) && s != "true" && s != "false") {
  32.         if (variables[s] == "true" || variables[s] == "false") {
  33.             s = variables[s];
  34.         } else {
  35.             errInCurrFn = true;
  36.         }
  37.     }
  38.     if (s == "true") {
  39.         while (fin >> s && s != "end") {
  40.             if (s == "if") {
  41.                 procIfElse();
  42.             } else if (s == "var") {
  43.                 if (!createVar()) {
  44.                     errInCurrFn = true;
  45.                 }
  46.             } else if (s == "set") {
  47.                 if (!setVar()) {
  48.                     errInCurrFn = true;
  49.                 }
  50.             } else if (s == "print") {
  51.                 exePrint();
  52.             } else if (s == "return") {
  53.                 retInCurrFn = true;
  54.                 return;
  55.             }
  56.         }
  57.         st["if"]--;
  58.         fin >> s;//else
  59.         while (fin >> s && st["if"] >= 0) {
  60.             if (s == "if") st["if"]++;
  61.             if (s == "else") st["if"]++;
  62.             if (s == "end") {
  63.                 st["if"]--;
  64.             }
  65.         }
  66.     } else {
  67.         while (st["else"] >= 0 && fin >> s) {
  68.             if (s == "else") st["else"]++;
  69.             if (s == "end") {
  70.                 st["else"]--;
  71.             }
  72.         }
  73.         while (fin >> s && s != "end") {
  74.             if (s == "var") {
  75.                 if (!createVar()) {
  76.                     errInCurrFn = true;
  77.                 }
  78.             }
  79.             else if (s == "set") {
  80.                 if (!setVar()) {
  81.                     errInCurrFn = true;
  82.                 }
  83.             }
  84.             else if (s == "print") {
  85.                 exePrint();
  86.             }
  87.             else if (s == "return") {
  88.                 retInCurrFn = true;
  89.                 return;
  90.             }
  91.             else if (s == "if") {
  92.                 procIfElse();
  93.             }
  94.         }
  95.     }
  96. }
  97.  
  98. bool createVar() {
  99.     string name, val;
  100.     fin >> name >> val;
  101.     if (variables.count(name)) {
  102.         return false;
  103.     }
  104.     if (variables.count(val)) {
  105.         variables[name] = variables[val];
  106.     } else {
  107.         variables[name] = val;
  108.     }
  109.     return true;
  110. }
  111.  
  112. bool setVar() {
  113.     string name, val;
  114.     fin >> name >> val;
  115.     if (!variables.count(name)) {
  116.         return false;
  117.     }
  118.     if (variables.count(val)) {
  119.         variables[name] = variables[val];
  120.     } else {
  121.         variables[name] = val;
  122.     }
  123.     return true;
  124. }
  125.  
  126.  
  127. void executeFunction() {
  128.     ans = "";
  129.     variables.clear();
  130.     errInCurrFn = false;
  131.     retInCurrFn = false;
  132.     //cerr << "HERE" << endl;
  133.     while (fin >> s) {
  134.         if (retInCurrFn) {
  135.             if (s == "start") {
  136.                 finish();
  137.                 return;
  138.             }
  139.             continue;
  140.         }
  141.         if (s == "print") exePrint();
  142.         else if (s == "var") {
  143.             if (!createVar()) {
  144.                 errInCurrFn = true;
  145.             }
  146.         }
  147.         else if (s == "set") {
  148.             if (!setVar()) {
  149.                 errInCurrFn = true;
  150.             }
  151.         }
  152.         else if (s == "end") {
  153.             break;
  154.         }
  155.         else if (s == "if") {
  156.             procIfElse();
  157.         } else if (s == "return" || retInCurrFn) {
  158.             while (fin >> s && s != "start");
  159.         }
  160.     }
  161.     if (errInCurrFn) {
  162.         ans = "";
  163.         variables.clear();
  164.         errInCurrFn = false;
  165.         ERR_FUN
  166.     }
  167.     finish();
  168. }
  169.  
  170. int main() {
  171.     int n;
  172.     fin >> n;
  173.     while (s == "start" || fin >> s) {
  174.         if (s == "start") {
  175.             executeFunction();
  176.         }
  177.     }
  178.     return 0;
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement