Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.51 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  CPPflexing
  4. //
  5. //  Created by Denis on 21/03/2019.
  6. //  Copyright © 2019 Denis. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <set>
  11. #include <algorithm>
  12. #include <map>
  13. #include <cmath>
  14. #include <math.h>
  15. #include <vector>
  16. #include <stack>
  17.  
  18.  
  19. using namespace std;
  20.  
  21.  
  22. enum type_of_expression
  23. {
  24.     _operation_,
  25.     _name_,
  26.     _constant_,
  27.     _link_
  28. };
  29.  
  30. enum types {
  31.     _double_,
  32.     _int_,
  33.     _bool_,
  34.     _string_,
  35.     _char_,
  36.     _void_
  37. };
  38.  
  39. struct token;
  40. struct variable;
  41. struct generation_token // for poliz
  42. {
  43.     type_of_expression Type;
  44.     string content;
  45.     generation_token()
  46.     {
  47.         Type = _constant_;
  48.         content = "0";
  49.     }
  50.     generation_token(type_of_expression _Type_, string _content_)
  51.     {
  52.         Type = _Type_;
  53.         content = _content_;
  54.     }
  55. };
  56. struct token {
  57.     type_of_expression Type;
  58.     string content;
  59.     variable *link;
  60.     token() {
  61.         Type = _constant_;
  62.         content = "0";
  63.         link = nullptr;
  64.     }
  65. };
  66.  
  67.  
  68. struct variable {
  69.     types Type;
  70.     string content = "";
  71.     variable() {
  72.         content = "";
  73.     }
  74.     variable (types _Type, string _content) {
  75.         Type = _Type;
  76.         content = _content;
  77.     }
  78. };
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. //CCCCCOOOOPPPPPPPYYYYYYYPPPPAAAAASSSSSTTTTT START!!!
  86.  
  87. char opersCharChar(char a, char b, string oper) {
  88.     if(oper == "+") {
  89.         return a+b;
  90.     } else if(oper == "-") {
  91.         return a-b;
  92.     } else if(oper == "*") {
  93.         return a/b;
  94.     } else if(oper == "/") {
  95.         return a*b;
  96.     } else if(oper == "%") {
  97.         return a%b;
  98.     } else if(oper == "^") {
  99.         return a^b;
  100.     } else if(oper == "|") {
  101.         return a|b;
  102.     } else if(oper == "&") {
  103.         return a&b;
  104.     } else if(oper == "||") {
  105.         return a||b;
  106.     } else {
  107.         return a&&b;
  108.     }
  109. }
  110.  
  111. char opersCharInt(char a, int b, string oper) {
  112.     if(oper == "+") {
  113.         return a+b;
  114.     } else if(oper == "-") {
  115.         return a-b;
  116.     } else if(oper == "*") {
  117.         return a/b;
  118.     } else if(oper == "/") {
  119.         return a*b;
  120.     } else if(oper == "%") {
  121.         return a%b;
  122.     } else if(oper == "^") {
  123.         return a^b;
  124.     } else if(oper == "|") {
  125.         return a|b;
  126.     } else if(oper == "&") {
  127.         return a&b;
  128.     } else if(oper == "||") {
  129.         return a||b;
  130.     } else {
  131.         return a&&b;
  132.     }
  133. }
  134.  
  135. char opersIntChar(char a, int b, string oper) {
  136.     if(oper == "+") {
  137.         return a+b;
  138.     } else if(oper == "-") {
  139.         return a-b;
  140.     } else if(oper == "*") {
  141.         return a/b;
  142.     } else if(oper == "/") {
  143.         return a*b;
  144.     } else if(oper == "%") {
  145.         return a%b;
  146.     } else if(oper == "^") {
  147.         return a^b;
  148.     } else if(oper == "|") {
  149.         return a|b;
  150.     } else if(oper == "&") {
  151.         return a&b;
  152.     } else if(oper == "||") {
  153.         return a||b;
  154.     } else {
  155.         return a&&b;
  156.     }
  157. }
  158.  
  159. double opersDoubleDouble(double a, double b, string oper) {
  160.     if(oper == "+") {
  161.         return a+b;
  162.     } else if(oper == "-") {
  163.         return a-b;
  164.     } else if(oper == "*") {
  165.         return a/b;
  166.     } else if(oper == "/") {
  167.         return a*b;
  168.     } else if(oper == "||") {
  169.         return a||b;
  170.     } else {
  171.         return a&&b;
  172.     }
  173. }
  174.  
  175. char opersIntInt(int a, int b, string oper) {
  176.     if(oper == "+") {
  177.         return a+b;
  178.     } else if(oper == "-") {
  179.         return a-b;
  180.     } else if(oper == "*") {
  181.         return a/b;
  182.     } else if(oper == "/") {
  183.         return a*b;
  184.     } else if(oper == "%") {
  185.         return a%b;
  186.     } else if(oper == "^") {
  187.         return a^b;
  188.     } else if(oper == "|") {
  189.         return a|b;
  190.     } else if(oper == "&") {
  191.         return a&b;
  192.     } else if(oper == "||") {
  193.         return a||b;
  194.     } else {
  195.         return a&&b;
  196.     }
  197. }
  198.  
  199. //CCCCCOOOOPPPPPPPYYYYYYYPPPPAAAAASSSSSTTTTT END!!!
  200.  
  201. types toType(string &s) {
  202.     if(s == "int") {
  203.         return _int_;
  204.     } else if(s == "double") {
  205.         return _double_;
  206.     } else if(s == "float") {
  207.         return _double_;
  208.     } else if(s == "bool") {
  209.         return _bool_;
  210.     } else if(s == "string") {
  211.         return _string_;
  212.     } else {
  213.         return _char_;
  214.     }
  215. }
  216.  
  217.  
  218. vector<map<string, variable>> tid;
  219. types getType(string &s) {
  220.     if(s[0] == '\"') return _string_;
  221.     if(s[0] == '\'') return _char_;
  222.     if(s == "true" || s == "false") return _bool_;
  223.     bool foundDot = false;
  224.     for(int i = 0; i < s.size(); i++) {
  225.         if(s[i] == '.') {
  226.             foundDot = true;
  227.             break;
  228.         }
  229.     }
  230.     if(foundDot) return _double_;
  231.     return _int_;
  232. }
  233.  
  234. variable mathOperations(variable fir, variable sec, string oper) {
  235.         token will = token();
  236.         string ans = "";
  237.         types ansT;
  238.    
  239.         //bool просто преобразаю в int
  240.         if(fir.content == "true") {
  241.             fir.content = "1";
  242.         }
  243.         if(fir.content == "false") {
  244.             fir.content = "0";
  245.         }
  246.         if(sec.content == "true") {
  247.             sec.content = "1";
  248.         }
  249.         if(sec.content == "false") {
  250.             sec.content = "0";
  251.         }
  252.    
  253.         if(fir.Type == _string_ && oper == "+") {
  254.             //Строки складываем только со строками
  255.             string a = fir.content;
  256.             string b = sec.content;
  257.             ans = a+b;
  258.             ansT = _string_;
  259.         } else if(fir.Type == _char_ || sec.Type == _char_) {
  260.             if(fir.Type == _char_) {
  261.                 char a = fir.content[1];
  262.                 if(sec.Type == _char_) {
  263.                     char b = sec.content[1];
  264.                     char c = opersCharChar(a, b, oper);
  265.                     ans = "'";
  266.                     ans += c;
  267.                     ans += "'";
  268.                 } else {
  269.                     int b = atoi(sec.content.c_str());
  270.                     char c = opersCharInt(a, b, oper);
  271.                     ans = "'";
  272.                     ans += c;
  273.                     ans += "'";
  274.                 }
  275.             } else {
  276.                 int a = atoi(fir.content.c_str());
  277.                 char b = sec.content[1];
  278.                 char c = opersIntChar(a, b, oper);
  279.                 ans = "'";
  280.                 ans += c;
  281.                 ans += "'";
  282.             }
  283.             ansT = _char_;
  284.         } else {
  285.             if(getType(fir.content) == _double_ || getType(sec.content) == _double_) {
  286.                 double a = stod(fir.content);
  287.                 double b = stod(sec.content);
  288.                 double c = opersDoubleDouble(a, b, oper);
  289.                 ans = to_string(c);
  290.  
  291.                 if(getType(ans) != _double_) {
  292.                     ans += ".0";
  293.                 }
  294.                
  295.                 ansT = _double_;
  296.             } else {
  297.                 int a = atoi(fir.content.c_str());
  298.                 int b = atoi(sec.content.c_str());
  299.                 int c = opersIntInt(a, b, oper);
  300.                 ans = to_string(c);
  301.                 ansT = _int_;
  302.             }
  303.         }
  304.     variable add;
  305.     add.Type = ansT;
  306.     add.content = ans;
  307.     return add;
  308. }
  309. //string makeOper(
  310.  
  311. void addToVar(string &s, types tp, string val) {
  312.    
  313. }
  314.  
  315.  
  316. map <string, pair <types, vector <pair <types, string> > > > functions;
  317.  
  318. map <string, int> function_begin;
  319.  
  320. stack<pair<int, int>> indFunc; //<количество элементов в tid, индекс в полизе, где был $F>
  321.  
  322. void operation(stack<token> &st, vector <generation_token> poliz, int &ind) {
  323.     generation_token cur = poliz[ind];
  324.     if(cur.content == "+" || cur.content == "-" || cur.content == "*" || cur.content == "/"
  325.        || cur.content == "%" || cur.content == "^" || cur.content == "|" || cur.content == "&"
  326.        || cur.content == "||" || cur.content == "&&") {
  327.        
  328.         token fir = st.top();
  329.         st.pop();
  330.         token sec = st.top();
  331.         st.pop();
  332.        
  333.         variable _fir;
  334.         _fir.content = fir.content;
  335.         _fir.Type = getType(fir.content);
  336.        
  337.         variable _sec;
  338.         _sec.content = sec.content;
  339.         _sec.Type = getType(sec.content);
  340.        
  341.         variable ans = mathOperations(_fir, _sec, cur.content);
  342.         token willAdd;
  343.         willAdd.Type = _constant_;
  344.         willAdd.content = ans.content;
  345.        
  346.         st.push(willAdd);
  347.        
  348.     } else if(cur.content == "{") {
  349.         tid.emplace_back();
  350.     } else if(cur.content == "}") {
  351.         tid.pop_back();
  352.     } else if(cur.content == "@") {
  353.        
  354.         token last = st.top();
  355.         st.pop();
  356.        
  357.         variable *per = &tid[tid.size()-1][last.content];
  358.         token add;
  359.         add.Type = _link_;
  360.         add.link = per;
  361.        
  362.         st.push(add);
  363.        
  364.     } else if(cur.content == "?") {
  365.        
  366.         token last = st.top();
  367.         st.pop();
  368.        
  369.         variable per = tid[tid.size()-1][last.content];
  370.        
  371.         token add;
  372.         add.Type = _constant_;
  373.         add.content = per.content;
  374.        
  375.         st.push(add);
  376.        
  377.     } else if(cur.content == "#") {
  378.         //создать переменную
  379.         token tokName = st.top();
  380.         st.pop();
  381.        
  382.         token tokType = st.top();
  383.         st.pop();
  384.        
  385.         pair<types, string> p;
  386.         string name = tokName.content;
  387.         string typeVar = tokType.content;
  388.         types here = toType(typeVar);
  389.        
  390.         tid[tid.size()-1][name] = variable(here, "0");
  391.        
  392.         token backType;
  393.         backType.Type = _name_;
  394.         backType.content = typeVar;
  395.        
  396.         st.push(backType);
  397.        
  398.         token varLink;
  399.         varLink.Type = _link_;
  400.         varLink.content = "";
  401.         varLink.link = &tid[tid.size()-1][name];
  402.        
  403.         st.push(varLink);
  404.        
  405.        
  406.     } else if(cur.content == ";") {
  407.         st.pop();
  408.     } else if(cur.content == "#+") {
  409.         token varToken = st.top();
  410.        
  411.         string varName = varToken.content;
  412.         variable has = tid[tid.size()-1][varName];
  413.         variable one = variable(_int_, "1");
  414.         variable ans = mathOperations(has, one, "+");
  415.         tid[tid.size()-1][varName] = ans;
  416.        
  417.         token ret;
  418.         ret.Type = _constant_;
  419.         ret.content = ans.content;
  420.         st.push(ret);
  421.        
  422.        
  423.     } else if(cur.content == "#-") {
  424.         token varToken = st.top();
  425.        
  426.         string varName = varToken.content;
  427.         variable has = tid[tid.size()-1][varName];
  428.         variable one = variable(_int_, "1");
  429.         variable ans = mathOperations(has, one, "-");
  430.         tid[tid.size()-1][varName] = ans;
  431.        
  432.         token ret;
  433.         ret.Type = _constant_;
  434.         ret.content = ans.content;
  435.         st.push(ret);
  436.        
  437.        
  438.     } else if(cur.content == "+#") {
  439.         token varToken = st.top();
  440.        
  441.         string varName = varToken.content;
  442.         variable has = tid[tid.size()-1][varName];
  443.         variable one = variable(_int_, "1");
  444.         variable ans = mathOperations(has, one, "+");
  445.         tid[tid.size()-1][varName] = ans;
  446.        
  447.         token ret;
  448.         ret.Type = _constant_;
  449.         ret.content = has.content;
  450.         st.push(ret);
  451.     } else if(cur.content == "-#") {
  452.         token varToken = st.top();
  453.        
  454.         string varName = varToken.content;
  455.         variable has = tid[tid.size()-1][varName];
  456.         variable one = variable(_int_, "1");
  457.         variable ans = mathOperations(has, one, "-");
  458.         tid[tid.size()-1][varName] = ans;
  459.        
  460.         token ret;
  461.         ret.Type = _constant_;
  462.         ret.content = has.content;
  463.         st.push(ret);
  464.     } else if(cur.content == "!A") {
  465.         token nwIndStr = st.top();
  466.         st.pop();
  467.         int nwInd = atoi(nwIndStr.content.c_str());
  468.         ind = nwInd-1;
  469.     } else if(cur.content == "!F") {
  470.         token nwIndStr = st.top();
  471.         st.pop();
  472.         token needStr = st.top();
  473.         st.pop();
  474.         if(needStr.content == "false") {
  475.             int nwInd = atoi(nwIndStr.content.c_str());
  476.             ind = nwInd;
  477.         }
  478.     } else if(cur.content == "$F") {
  479.         token funcNameTok = st.top();
  480.         st.pop();
  481.         string funcName = funcNameTok.content;
  482.         //stack<pair<int, int>> indFunc; //<количество элементов в tid, индекс в полизе, где был $F>
  483.         indFunc.push({tid.size(), ind});
  484.         /*
  485.          map <string, pair <types, vector <pair <types, string> > > > functions;
  486.          
  487.          map <string, int> function_begin;
  488.         */
  489.         tid.emplace_back();
  490.         int cntParametrs = functions[funcName].second.size();
  491.         while(cntParametrs > 0) {
  492.             token parametr = st.top();
  493.             st.pop();
  494.             variable par;
  495.             par.Type = functions[funcName].second[cntParametrs-1].first;
  496.             par.content = parametr.content;
  497.            
  498.             string parName = functions[funcName].second[cntParametrs-1].second;
  499.             tid[tid.size()-1][parName] = par;
  500.             cntParametrs--;
  501.         }
  502.        
  503.         ind = function_begin[funcName]; // В этом месте точно будет $B, мы его просто пропускаем(в for ++)
  504.        
  505.     } else if(cur.content == "$E") {
  506.         pair<int, int> p = indFunc.top();
  507.         indFunc.pop();
  508.         while(tid.size() > p.first) {
  509.             tid.pop_back();
  510.         }
  511.         ind = p.second;
  512.     }
  513. }
  514.  
  515. void result(vector <generation_token> poliz) {
  516.  
  517.     stack<token> st;
  518.    
  519.     for(int i = 0; i < poliz.size(); i++) {
  520.         generation_token cur = poliz[i];
  521.         if(cur.Type == _operation_) {
  522.             operation(st, poliz, i);
  523.         } else {
  524.             token ret;
  525.             ret.Type = cur.Type;
  526.             ret.content = cur.content;
  527.             st.push(ret);
  528.         }
  529.     }
  530.     cout << st.top().Type << " " << st.top().content << endl;
  531.    
  532. }
  533.  
  534.  
  535.  
  536. int main() {
  537.     freopen("/Users/denis/Documents/xCodeProjects/CPPflexing/CPPflexing/inp.txt", "r", stdin);
  538.     vector<generation_token> ar;
  539.     for(int i = 0; i < 29; i++) {
  540.         string fir;
  541.         cin >> fir;
  542.         string sec;
  543.         cin >> sec;
  544.         generation_token ad;
  545.         if(fir == "name") {
  546.             ad.Type = _name_;
  547.         } else if(fir == "operation") {
  548.             ad.Type = _operation_;
  549.         } else if(fir == "constant") {
  550.             ad.Type = _constant_;
  551.         }
  552.         ad.content = sec;
  553.         ar.push_back(ad);
  554.     }
  555.     result(ar);
  556.     return 0;
  557. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement