functions.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "functions.hpp"
  2. #include "color.hpp"
  3. string setchar(char c)
  4. {
  5. string result = "[-]";
  6. for (int i = 0; i < (int)c; i++)
  7. {
  8. result += "+";
  9. }
  10. result += "\n";
  11. return result;
  12. }
  13. string walk(int k)
  14. {
  15. string result = "";
  16. if (k > 0)
  17. {
  18. for (int i = 0; i < k; i++)
  19. {
  20. result += ">";
  21. }
  22. }
  23. if (k < 0)
  24. {
  25. for (int i = 0; i > k; i--)
  26. {
  27. result += "<";
  28. }
  29. }
  30. return result;
  31. }
  32. string change(int count)
  33. {
  34. string result = "";
  35. if (count > 0)
  36. {
  37. for (int i = 0; i <= count; i++)
  38. {
  39. result += "+";
  40. }
  41. }
  42. if (count < 0)
  43. {
  44. for (int i = 0; i >= count; i--)
  45. {
  46. result += "-";
  47. }
  48. }
  49. return result;
  50. }
  51. string go_to(int current_cell, int where)
  52. {
  53. string result = "";
  54. if (current_cell < where)
  55. {
  56. for (int i = current_cell; i < where; i++)
  57. {
  58. result += ">";
  59. }
  60. }
  61. if (current_cell > where)
  62. {
  63. for (int i = current_cell; i > where; i--)
  64. {
  65. result += "<";
  66. }
  67. }
  68. result += "\n";
  69. return result;
  70. }
  71. string copyto(int current_cell, int whereto, int buffer)
  72. {
  73. int copy_whereto, copy_buffer;
  74. copy_whereto = whereto - current_cell;
  75. copy_buffer = buffer - current_cell;
  76. return copy(copy_whereto, copy_buffer);
  77. }
  78. string mov(int whereto)
  79. {
  80. string result = "";
  81. result += walk(whereto);
  82. result += "[-]";
  83. result += walk(-whereto);
  84. result += "[-";
  85. result += walk(whereto);
  86. result += "+";
  87. result += walk(-whereto);
  88. result += "]";
  89. return result;
  90. }
  91. string copy(int whereto, int buffer)
  92. {
  93. string result = "";
  94. result += walk(whereto);
  95. result += "[-]";
  96. result += walk(buffer - whereto);
  97. result += "[-]";
  98. result += walk(-buffer);
  99. result += "[-";
  100. result += walk(whereto);
  101. result += "+";
  102. result += walk(buffer - whereto);
  103. result += "+";
  104. result += walk(-buffer);
  105. result += "]";
  106. result += walk(buffer);
  107. result += mov(-buffer);
  108. result += walk(-buffer);
  109. return result;
  110. }
  111. string print(int lenght)
  112. {
  113. string result = ".";
  114. string come_back = "";
  115. for (int i = 0; i < abs(lenght); i++)
  116. {
  117. result += ">.";
  118. come_back += "<";
  119. }
  120. result += "\n";
  121. result += come_back;
  122. result += "\n";
  123. return result;
  124. }
  125. string split(string what)
  126. {
  127. string result = "";
  128. string come_back = "";
  129. char lastc;
  130. if (what[0] != '\"')
  131. {
  132. cout<<WARNING_MSG("Warning: ")<<"better use '\"'\n";
  133. }
  134. for (char c: what)
  135. {
  136. if (c == '\"' && lastc != '\\')
  137. {
  138. continue;
  139. }
  140. if (c == '\\' && lastc != '\\')
  141. {
  142. lastc = c;
  143. continue;
  144. }
  145. if (lastc == '\\' && c == 'n')
  146. {
  147. c = '\n';
  148. }
  149. else if (lastc == '\\' && c == 't')
  150. {
  151. c = '\t';
  152. }
  153. result += setchar(c);
  154. result += ">";
  155. come_back += "<";
  156. result += "\n";
  157. lastc = c;
  158. }
  159. return result + come_back + "\n";
  160. }
  161. string sum (int p1, int p2, int resultpos, int current_cell)
  162. {
  163. string result = "";
  164. result += go_to(current_cell, 0);
  165. result += "[-]"; // Clear buffer
  166. result += go_to(0, current_cell);
  167. result += go_to(current_cell, p1);
  168. result += "[-";
  169. result += go_to(p1, resultpos);
  170. result += "+";
  171. result += go_to(resultpos, 0);
  172. result += "+";
  173. result += go_to(0, p1);
  174. result += "]"; // Move value from p1 to buffer and result cell
  175. result += go_to(p1, 0);
  176. result += "[-";
  177. result += go_to(0, p1);
  178. result += "+";
  179. result += go_to(p1, 0);
  180. result += "]"; // Return value from buffer to p1
  181. result += go_to(0, p2);
  182. result += "[-";
  183. result += go_to(p2, resultpos);
  184. result += "+";
  185. result += go_to(resultpos, 0);
  186. result += "+";
  187. result += go_to(0, p2);
  188. result += "]"; // Move value from p2 to buffer and add to result cell
  189. result += go_to(p2, 0);
  190. result += "[-";
  191. result += go_to(0, p2);
  192. result += "+";
  193. result += go_to(p2, 0);
  194. result += "]"; // Return value from buffer to p2
  195. result += go_to(0, current_cell); // Come back
  196. return result;
  197. }
  198. string sub (int p1, int p2, int resultpos, int current_cell)
  199. {
  200. string result = "";
  201. result += go_to(current_cell, 0);
  202. result += "[-]"; // Clear buffer
  203. result += go_to(0, current_cell);
  204. result += go_to(current_cell, p1);
  205. result += "[-";
  206. result += go_to(p1, resultpos);
  207. result += "+";
  208. result += go_to(resultpos, 0);
  209. result += "+";
  210. result += go_to(0, p1);
  211. result += "]"; // Move value from p1 to buffer and result cell
  212. result += go_to(p1, 0);
  213. result += "[-";
  214. result += go_to(0, p1);
  215. result += "+";
  216. result += go_to(p1, 0);
  217. result += "]"; // Return value from buffer to p1
  218. result += go_to(0, p2);
  219. result += "[-";
  220. result += go_to(p2, resultpos);
  221. result += "-";
  222. result += go_to(resultpos, 0);
  223. result += "+";
  224. result += go_to(0, p2);
  225. result += "]"; // Move value from p2 to buffer and sub from result cell
  226. result += go_to(p2, 0);
  227. result += "[-";
  228. result += go_to(0, p2);
  229. result += "+";
  230. result += go_to(p2, 0);
  231. result += "]"; // Return value from buffer to p2
  232. result += go_to(0, current_cell); // Come back
  233. return result;
  234. }