123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #include "functions.hpp"
- #include "color.hpp"
- string setchar(char c)
- {
- string result = "[-]";
- for (int i = 0; i < (int)c; i++)
- {
- result += "+";
- }
- result += "\n";
- return result;
- }
- string walk(int k)
- {
- string result = "";
- if (k > 0)
- {
- for (int i = 0; i < k; i++)
- {
- result += ">";
- }
- }
- if (k < 0)
- {
- for (int i = 0; i > k; i--)
- {
- result += "<";
- }
- }
- return result;
- }
- string change(int count)
- {
- string result = "";
- if (count > 0)
- {
- for (int i = 0; i <= count; i++)
- {
- result += "+";
- }
- }
- if (count < 0)
- {
- for (int i = 0; i >= count; i--)
- {
- result += "-";
- }
- }
- return result;
- }
- string go_to(int current_cell, int where)
- {
- string result = "";
- if (current_cell < where)
- {
- for (int i = current_cell; i < where; i++)
- {
- result += ">";
- }
- }
- if (current_cell > where)
- {
- for (int i = current_cell; i > where; i--)
- {
- result += "<";
- }
- }
- result += "\n";
- return result;
- }
- string copyto(int current_cell, int whereto, int buffer)
- {
- int copy_whereto, copy_buffer;
- copy_whereto = whereto - current_cell;
- copy_buffer = buffer - current_cell;
- return copy(copy_whereto, copy_buffer);
- }
- string mov(int whereto)
- {
- string result = "";
- result += walk(whereto);
- result += "[-]";
- result += walk(-whereto);
- result += "[-";
- result += walk(whereto);
- result += "+";
- result += walk(-whereto);
- result += "]";
- return result;
- }
- string copy(int whereto, int buffer)
- {
- string result = "";
- result += walk(whereto);
- result += "[-]";
- result += walk(buffer - whereto);
- result += "[-]";
- result += walk(-buffer);
- result += "[-";
- result += walk(whereto);
- result += "+";
- result += walk(buffer - whereto);
- result += "+";
- result += walk(-buffer);
- result += "]";
- result += walk(buffer);
- result += mov(-buffer);
- result += walk(-buffer);
- return result;
- }
- string print(int lenght)
- {
- string result = ".";
- string come_back = "";
- for (int i = 0; i < abs(lenght); i++)
- {
- result += ">.";
- come_back += "<";
- }
- result += "\n";
- result += come_back;
- result += "\n";
- return result;
- }
- string split(string what)
- {
- string result = "";
- string come_back = "";
- char lastc;
- if (what[0] != '\"')
- {
- cout<<WARNING_MSG("Warning: ")<<"better use '\"'\n";
- }
- for (char c: what)
- {
- if (c == '\"' && lastc != '\\')
- {
- continue;
- }
- if (c == '\\' && lastc != '\\')
- {
- lastc = c;
- continue;
- }
- if (lastc == '\\' && c == 'n')
- {
- c = '\n';
- }
- else if (lastc == '\\' && c == 't')
- {
- c = '\t';
- }
- result += setchar(c);
- result += ">";
- come_back += "<";
- result += "\n";
- lastc = c;
- }
- return result + come_back + "\n";
- }
- string sum (int p1, int p2, int resultpos, int current_cell)
- {
- string result = "";
- result += go_to(current_cell, 0);
- result += "[-]"; // Clear buffer
- result += go_to(0, current_cell);
- result += go_to(current_cell, p1);
- result += "[-";
- result += go_to(p1, resultpos);
- result += "+";
- result += go_to(resultpos, 0);
- result += "+";
- result += go_to(0, p1);
- result += "]"; // Move value from p1 to buffer and result cell
- result += go_to(p1, 0);
- result += "[-";
- result += go_to(0, p1);
- result += "+";
- result += go_to(p1, 0);
- result += "]"; // Return value from buffer to p1
-
- result += go_to(0, p2);
- result += "[-";
- result += go_to(p2, resultpos);
- result += "+";
- result += go_to(resultpos, 0);
- result += "+";
- result += go_to(0, p2);
- result += "]"; // Move value from p2 to buffer and add to result cell
- result += go_to(p2, 0);
- result += "[-";
- result += go_to(0, p2);
- result += "+";
- result += go_to(p2, 0);
- result += "]"; // Return value from buffer to p2
- result += go_to(0, current_cell); // Come back
- return result;
- }
- string sub (int p1, int p2, int resultpos, int current_cell)
- {
- string result = "";
- result += go_to(current_cell, 0);
- result += "[-]"; // Clear buffer
- result += go_to(0, current_cell);
- result += go_to(current_cell, p1);
- result += "[-";
- result += go_to(p1, resultpos);
- result += "+";
- result += go_to(resultpos, 0);
- result += "+";
- result += go_to(0, p1);
- result += "]"; // Move value from p1 to buffer and result cell
- result += go_to(p1, 0);
- result += "[-";
- result += go_to(0, p1);
- result += "+";
- result += go_to(p1, 0);
- result += "]"; // Return value from buffer to p1
-
- result += go_to(0, p2);
- result += "[-";
- result += go_to(p2, resultpos);
- result += "-";
- result += go_to(resultpos, 0);
- result += "+";
- result += go_to(0, p2);
- result += "]"; // Move value from p2 to buffer and sub from result cell
- result += go_to(p2, 0);
- result += "[-";
- result += go_to(0, p2);
- result += "+";
- result += go_to(p2, 0);
- result += "]"; // Return value from buffer to p2
- result += go_to(0, current_cell); // Come back
- return result;
- }
|