Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. stack<pair<int, int>> indFunc; //<количество элементов в tid, индекс в полизе, где был $F>
  2.  
  3. void operation(stack<token> &st, vector <generation_token> poliz, int &ind) {
  4. generation_token cur = poliz[ind];
  5. if(cur.content == "+" || cur.content == "-" || cur.content == "*" || cur.content == "/"
  6. || cur.content == "%" || cur.content == "^" || cur.content == "|" || cur.content == "&"
  7. || cur.content == "||" || cur.content == "&&") {
  8.  
  9. token fir = st.top();
  10. st.pop();
  11. token sec = st.top();
  12. st.pop();
  13.  
  14. variable _fir;
  15. _fir.content = fir.content;
  16. _fir.Type = getType(fir.content);
  17.  
  18. variable _sec;
  19. _sec.content = sec.content;
  20. _sec.Type = getType(sec.content);
  21.  
  22. variable ans = mathOperations(_fir, _sec, cur.content);
  23. token willAdd;
  24. willAdd.Type = _constant_;
  25. willAdd.content = ans.content;
  26.  
  27. st.push(willAdd);
  28.  
  29. } else if(cur.content == "{") {
  30. tid.emplace_back();
  31. } else if(cur.content == "}") {
  32. tid.pop_back();
  33. } else if(cur.content == "@") {
  34.  
  35. token last = st.top();
  36. st.pop();
  37.  
  38. variable *per = &tid[tid.size()-1][last.content];
  39. token add;
  40. add.Type = _link_;
  41. add.link = per;
  42.  
  43. st.push(add);
  44.  
  45. } else if(cur.content == "?") {
  46.  
  47. token last = st.top();
  48. st.pop();
  49.  
  50. variable per = tid[tid.size()-1][last.content];
  51.  
  52. token add;
  53. add.Type = _constant_;
  54. add.content = per.content;
  55.  
  56. st.push(add);
  57.  
  58. } else if(cur.content == "#") {
  59. //создать переменную
  60. token tokName = st.top();
  61. st.pop();
  62.  
  63. token tokType = st.top();
  64. st.pop();
  65.  
  66. pair<types, string> p;
  67. string name = tokName.content;
  68. string typeVar = tokType.content;
  69. types here = toType(typeVar);
  70.  
  71. tid[tid.size()-1][name] = variable(here, "0");
  72.  
  73. token backType;
  74. backType.Type = _name_;
  75. backType.content = typeVar;
  76.  
  77. st.push(backType);
  78.  
  79. token varLink;
  80. varLink.Type = _link_;
  81. varLink.content = "";
  82. varLink.link = &tid[tid.size()-1][name];
  83.  
  84. st.push(varLink);
  85.  
  86.  
  87. } else if(cur.content == ";") {
  88. st.pop();
  89. } else if(cur.content == "#+") {
  90. token varToken = st.top();
  91.  
  92. string varName = varToken.content;
  93. variable has = tid[tid.size()-1][varName];
  94. variable one = variable(_int_, "1");
  95. variable ans = mathOperations(has, one, "+");
  96. tid[tid.size()-1][varName] = ans;
  97.  
  98. token ret;
  99. ret.Type = _constant_;
  100. ret.content = ans.content;
  101. st.push(ret);
  102.  
  103.  
  104. } else if(cur.content == "#-") {
  105. token varToken = st.top();
  106.  
  107. string varName = varToken.content;
  108. variable has = tid[tid.size()-1][varName];
  109. variable one = variable(_int_, "1");
  110. variable ans = mathOperations(has, one, "-");
  111. tid[tid.size()-1][varName] = ans;
  112.  
  113. token ret;
  114. ret.Type = _constant_;
  115. ret.content = ans.content;
  116. st.push(ret);
  117.  
  118.  
  119. } else if(cur.content == "+#") {
  120. token varToken = st.top();
  121.  
  122. string varName = varToken.content;
  123. variable has = tid[tid.size()-1][varName];
  124. variable one = variable(_int_, "1");
  125. variable ans = mathOperations(has, one, "+");
  126. tid[tid.size()-1][varName] = ans;
  127.  
  128. token ret;
  129. ret.Type = _constant_;
  130. ret.content = has.content;
  131. st.push(ret);
  132. } else if(cur.content == "-#") {
  133. token varToken = st.top();
  134.  
  135. string varName = varToken.content;
  136. variable has = tid[tid.size()-1][varName];
  137. variable one = variable(_int_, "1");
  138. variable ans = mathOperations(has, one, "-");
  139. tid[tid.size()-1][varName] = ans;
  140.  
  141. token ret;
  142. ret.Type = _constant_;
  143. ret.content = has.content;
  144. st.push(ret);
  145. } else if(cur.content == "!A") {
  146. token nwIndStr = st.top();
  147. st.pop();
  148. int nwInd = atoi(nwIndStr.content.c_str());
  149. ind = nwInd-1;
  150. } else if(cur.content == "!F") {
  151. token nwIndStr = st.top();
  152. st.pop();
  153. token needStr = st.top();
  154. st.pop();
  155. if(needStr.content == "false") {
  156. int nwInd = atoi(nwIndStr.content.c_str());
  157. ind = nwInd;
  158. }
  159. } else if(cur.content == "$F") {
  160. token funcNameTok = st.top();
  161. st.pop();
  162. string funcName = funcNameTok.content;
  163. //stack<pair<int, int>> indFunc; //<количество элементов в tid, индекс в полизе, где был $F>
  164. indFunc.push({tid.size(), ind});
  165. /*
  166. map <string, pair <types, vector <pair <types, string> > > > functions;
  167.  
  168. map <string, int> function_begin;
  169. */
  170. tid.emplace_back();
  171. int cntParametrs = functions[funcName].second.size();
  172. while(cntParametrs > 0) {
  173. token parametr = st.top();
  174. st.pop();
  175. variable par;
  176. par.Type = functions[funcName].second[cntParametrs-1].first;
  177. par.content = parametr.content;
  178.  
  179. string parName = functions[funcName].second[cntParametrs-1].second;
  180. tid[tid.size()-1][parName] = par;
  181. cntParametrs--;
  182. }
  183.  
  184. ind = function_begin[funcName]; // В этом месте точно будет $B, мы его просто пропускаем(в for ++)
  185.  
  186. } else if(cur.content == "$E") {
  187. pair<int, int> p = indFunc.top();
  188. indFunc.pop();
  189. while(tid.size() > p.first) {
  190. tid.pop_back();
  191. }
  192. ind = p.second;
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement