Advertisement
Guest User

Shitbrain/brainfuck stuff

a guest
Sep 7th, 2022
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.16 KB | None | 0 0
  1.  
  2.  
  3. IMPORTANT NOTE:
  4.  
  5. THIS IS NOT THE FINAL PASTEBIN.
  6. I just wanted to get something up for you guys to see until I have the final version complete.
  7.  
  8.  
  9.  
  10.  
  11. Some pre-read info:
  12. Whenever I comment something not on the same line as the thing I'm commenting on, I always put the comment on the line(s) above it.
  13. for example:
  14.  
  15. // this is a comment regarding the foo() function
  16. func foo() {}
  17. // this comment does not relate to the foo() function
  18.  
  19.  
  20.  
  21.  
  22. All current functions with explanations
  23. (not all of these are implemented in the interpreter because I stopped work on the project before it was done)
  24.  
  25. These two are separated because they can be used within functions instead of as functions
  26.  
  27. - if valid meaning if the function this is used in allows use of it.
  28. - Treat x as cell position meaning x is a number that represents a
  29. cell instead of a number value input
  30. 'x if valid, treat x as cell position
  31.  
  32. - this is used because my interpreter offset cell positions by about 5 as
  33. to leave the first few cells as "buffers" or "temporary" cells. these are
  34. what would be used whenever a traditional brainfuck algorithm calls for a
  35. temp cell. (temp0, temp1, etc.)
  36. - this modifier allows c to be treated as the absolute position in the cell
  37. array instead of the position + buffer offset
  38. ^c use c as absolute position in array
  39.  
  40.  
  41. - cell c = value x (allows ^c and 'x)
  42. =c:x cell c = x
  43. - just duplicates cell c to cell x (allows ^c and ^x)
  44. - in bf code this looks like this: c[temp+x+c]temp[-c+temp]
  45. &c:x duplicate cell c to cell x
  46. - literally just moves the value of c to position x. deletes whatever was at x. (allows ^c and ^x)
  47. @c:x allocate cell c to cell x
  48. - in bf code: c[-] (allows ^c)
  49. /c clear cell c (set to 0)
  50. - (allows ^c)
  51. :c move pointer to cell c
  52. +c:x:y cell c = cell x + y
  53. +c:x:'y cell c = cell x + cell y
  54. -c:x:y cell c = cell x - y
  55. -c:x:'y cell c = cell x - cell y
  56. *c:x:y cell c = cell x * y
  57. *c:x:'y cell c = cell x * cell y
  58. .n:c creates a new variable called (n) at cell c
  59. %x:y reassign variable (i honestly don't remember what this is supposed to do)
  60. ~c get user input at cell c
  61. A:c:x cell c = ASCII value of x (x is any character)
  62. B:"str" insert brainfuck directly into code
  63. T:"str" output a string of text
  64. P:c output number value at cell c
  65. ?=c:x:(code)\ if cell c == x
  66. ?=c:'x:(code)\ if cell c == cell x
  67. !=c:x:(code)\ if cell c != x
  68. !=c:'x:(code)\ if cell c != cell x
  69. >c:x:(code)\ if cell c > x
  70. >c:'x:(code)\ if cell c > cell x
  71. <c:x:(code)\ if cell c < x
  72. <c:'x:(code)\ if cell c < cell x
  73. >=c:x:(code)\ if cell c >= x
  74. >=c:'x:(code)\ if cell c >= cell x
  75. <=c:x:(code)\ if cell c <= x
  76. <=c:'x:(code)\ if cell c <= cell x
  77. @{ forever loop start
  78. @} forever loop end
  79.  
  80.  
  81.  
  82.  
  83. ==================================================================================
  84.  
  85. interpreter code
  86.  
  87. -written in processing
  88. processing download: processing.org/download
  89. file info:
  90. in the project folder, there is another folder called "data"
  91. in the data folder, there is a .txt file called "sb.txt" where you input your shitbrain code.
  92. the program outputs another text file called "bf.txt" in the same folder and automatically opens it
  93.  
  94. here is the entirety of the interpreter code in its current state:
  95. (i am aware it is very unoptimized but it is also nearly a year old and
  96. i also only have a little more than a year coding experience so yeah)
  97.  
  98. ```
  99. /*
  100.  
  101. 'x if valid, treat x as cell position
  102. ^c use x as absolute position in array
  103.  
  104. =c:x cell c = x
  105. &c:x duplicate cell c to cell x
  106. @c:x allocate cell c to cell x
  107. /c clear cell c (set to 0)
  108. :c move pointer to cell c
  109. +c:x:y cell c = cell x + y
  110. +c:x:'y cell c = cell x + cell y
  111. -c:x:y cell c = cell x - y
  112. -c:x:'y cell c = cell x - cell y
  113. *c:x:y cell c = cell x * y
  114. *c:x:'y cell c = cell x * cell y
  115. .n:c creates a new variable called (n) at cell c
  116. %x:y reassign variable
  117. ~c get user input at cell c
  118. A:c:x cell c = ASCII value of x (x is any character)
  119. B:"str" insert brainfuck directly into code
  120. T:"str" output a string of text
  121. P:c output number value at cell c
  122. ?=c:x:(code)\ if cell c == x
  123. ?=c:'x:(code)\ if cell c == cell x
  124. !=c:x:(code)\ if cell c != x
  125. !=c:'x:(code)\ if cell c != cell x
  126. >c:x:(code)\ if cell c > x
  127. >c:'x:(code)\ if cell c > cell x
  128. <c:x:(code)\ if cell c < x
  129. <c:'x:(code)\ if cell c < cell x
  130. >=c:x:(code)\ if cell c >= x
  131. >=c:'x:(code)\ if cell c >= cell x
  132. <=c:x:(code)\ if cell c <= x
  133. <=c:'x:(code)\ if cell c <= cell x
  134.  
  135. */
  136.  
  137. String[] sb; //shitbrain input code
  138. String[] bf; //brainfuck output code
  139. String[] comments; //comments in sb code
  140. ArrayList<String> b;
  141. ArrayList<String> vars;
  142. ArrayList<String> varLocs;
  143. int buffers = 4;
  144. File bFuck;
  145. boolean open = true;
  146.  
  147. void setup() {
  148. //size(400, 400);
  149. //textSize(80);
  150. //textAlign(CENTER);
  151. //fill(0);
  152. //text("converting...", width/2, height/2);
  153. bFuck = dataFile("bf.txt");
  154. vars = new ArrayList<String>(1);
  155. varLocs = new ArrayList<String>(1);
  156. ArrayList<String> s = new ArrayList<String>();
  157. String[] c = loadStrings("sb.txt"); //load code
  158. for (int i = 0; i < c.length; i++) {
  159. s.add(c[i]); //copy code to array
  160. }
  161. for (int i = s.size()-1; i >= 0; i--) {
  162. if (!s.get(i).substring(0, 1).equals("T")) {
  163. s.set(i, split(s.get(i), ";")[0]); //ignore comments
  164. }
  165. if (s.get(i).length() <= 1 || s.get(i).equals("\n")) {
  166. s.remove(i); //remove
  167. } else {
  168. if (!s.get(i).substring(0, 1).equals("B") && !s.get(i).substring(0, 1).equals("T")
  169. && !s.get(i).substring(0, 1).equals("A")) {
  170. //remove spaces
  171. s.set(i, s.get(i).replace(" ", ""));
  172. //s.set(i, s.get(i).replace(str(char(9)), ""));
  173. }
  174. //println(s.get(i).substring(0, 1));
  175. }
  176. }
  177. sb = new String[s.size()+1];
  178. for (int i = 1; i < sb.length; i++) {
  179. sb[i] = s.get(i-1); //copy s to sb
  180. }
  181.  
  182. b = new ArrayList<String>();
  183. b.add("-");
  184. for (int i = 1; i < sb.length; i++) {
  185. b.add(convert(sb[i], i));
  186. }
  187. for (int i = b.size()-1; i >= 1; i--) {
  188. if (b.get(i).length() <= 1 || b.get(i).equals("\n")) {
  189. b.remove(i); //remove
  190. }
  191. }
  192. bf = new String[b.size()];
  193. for (int i = 0; i < b.size(); i++) {
  194. bf[i] = b.get(i);
  195. }
  196. //for (int i = 0; i < bf.length; i++) {
  197. // bf[i] = bf[i].replace(" ", "");
  198. //}
  199. //pCode();
  200. saveStrings("data/bf.txt", bf);
  201. if (open) launch(bFuck.toString());
  202. exit();
  203. }
  204.  
  205. //----------------------------------------------------------------------------------------------------
  206.  
  207. String convert(String code, int index) {
  208. String op = code.substring(0, 1);
  209. String ln = "";
  210. String name;
  211. String pos;
  212. String arg0, arg1, arg2, act0;
  213. String move0, move1, move2, b1, b2, b3;
  214.  
  215. switch(op) {
  216. default:
  217. throw new UnsupportedOperationException("operator '"+op+"' is invalid. at shitbrain line "+index);
  218. //----------------------------------------------------------------------------------------------------
  219. case ".":
  220. name = split(code.substring(1), ":")[0];
  221. pos = split(code.substring(1), ":")[1];
  222. move0 = "+[-<+]-";
  223. int loc; //int(split(code.substring(1), ":")[1])
  224. if (!pos.substring(0, 1).equals("^")) {
  225. loc = int(pos)+buffers;
  226. } else {
  227. loc = int(pos.substring(1));
  228. }
  229. vars.add(name);
  230. for (int i = 0; i < loc; i++) {
  231. move0+= ">";
  232. }
  233. varLocs.add(move0);
  234. break;
  235. //----------------------------------------------------------------------------------------------------
  236. case ":":
  237. move0 = code.substring(1);
  238. ln = toPos(move0);
  239. break;
  240. //----------------------------------------------------------------------------------------------------
  241. case "=":
  242. arg0 = split(code.substring(1), ":")[0];
  243. arg1 = split(code.substring(1), ":")[1];
  244. act0 = "+[-<+]->";
  245. for (int i = 0; i < factorsOf(int(arg1))[0]; i++) {
  246. act0 += "+";
  247. }
  248. act0 += "["+toPos(arg0);
  249. for (int i = 0; i < factorsOf(int(arg1))[1]; i++) {
  250. act0 += "+";
  251. }
  252. act0 += "+[-<+]->-]";
  253. ln = act0;
  254. break;
  255. //----------------------------------------------------------------------------------------------------
  256. case "/":
  257. arg0 = code.substring(1);
  258. ln = toPos(arg0) + " [-]";
  259. break;
  260. //----------------------------------------------------------------------------------------------------
  261. case "&":
  262. arg0 = split(code.substring(1), ":")[0];
  263. arg1 = split(code.substring(1), ":")[1];
  264. move0 = toPos(arg0);
  265. move1 = toPos(arg1);
  266. ln = move1+"\n\n[-]"+move0+"[-+[-<+]->+"+move1+"+"+move0+"]\n[+[-<+]->-"+move0+"+]+[-<+]->[-"+move0+"++[-<+]->]\n";
  267. break;
  268. //----------------------------------------------------------------------------------------------------
  269. case "@":
  270. arg0 = split(code.substring(1), ":")[0];
  271. arg1 = split(code.substring(1), ":")[1];
  272. move1 = toPos(arg1);
  273. move0 = toPos(arg0);
  274. ln = move1+"[-]"+move0+"[-"+move1+"+"+move0+"]";
  275. break;
  276. //----------------------------------------------------------------------------------------------------
  277. case "B":
  278. arg0 = code.substring(2);
  279. ln = arg0;
  280. break;
  281. //----------------------------------------------------------------------------------------------------
  282. case "P":
  283. arg0 = code.substring(2);
  284. move0 = toPos(arg0);
  285. ln = move0+ ".";
  286. break;
  287. //----------------------------------------------------------------------------------------------------
  288. case "T":
  289. arg0 = code.substring(2);
  290. act0 = "";
  291. ln = "";
  292. for (int j = 0; j < code.substring(2).length(); j++) {
  293. act0 = "+[-<+]->";
  294. int f1a = factorsOf(int(arg0.charAt(j)))[0];
  295. int f2a = factorsOf(int(arg0.charAt(j)))[1];
  296. int f1b = factorsOf(int(arg0.charAt(j))+1)[0];
  297. int f2b = factorsOf(int(arg0.charAt(j))+1)[1];
  298. int f1c = factorsOf(int(arg0.charAt(j))-1)[0];
  299. int f2c = factorsOf(int(arg0.charAt(j))-1)[1];
  300. if (abs(f1a-f2a) < abs(f1b-f2b)) {
  301. for (int i = 0; i < f1a; i++) {
  302. act0 += "+";
  303. }
  304. act0 += "[>";
  305. for (int i = 0; i < f2a; i++) {
  306. act0 += "+";
  307. }
  308. act0 += "<-]";
  309. ln += act0+">.[-]\n";
  310. } else if (abs(f1b-f2b) < abs(f1c-f2c)) {
  311. for (int i = 0; i < f1b; i++) {
  312. act0 += "+";
  313. }
  314. act0 += "[>";
  315. for (int i = 0; i < f2b; i++) {
  316. act0 += "+";
  317. }
  318. act0 += "<-]";
  319. ln += act0+">-.[-]\n";
  320. } else {
  321. for (int i = 0; i < f1c; i++) {
  322. act0 += "+";
  323. }
  324. act0 += "[>";
  325. for (int i = 0; i < f2c; i++) {
  326. act0 += "+";
  327. }
  328. act0 += "<-]";
  329. ln += act0+">+.[-]\n";
  330. }
  331. }
  332. ln += "++[<+++++>-]<.[-]";
  333. break;
  334. //----------------------------------------------------------------------------------------------------
  335. case "~":
  336. arg0 = code.substring(1);
  337. move0 = "+[-<+]-";
  338.  
  339. if (arg0.substring(0, 1).equals("^")) {
  340. for (int i = 0; i < int(arg0.substring(1)); i++) {
  341. move0 += ">";
  342. }
  343. } else {
  344. for (int i = 0; i < buffers; i++) {
  345. move0 += ">";
  346. }
  347. for (int i = 0; i < int(arg0); i++) {
  348. move0 += ">";
  349. }
  350. }
  351. for (int v = 0; v < vars.size(); v++) {
  352. if (arg0.equals(vars.get(v))) {
  353. move0 = varLocs.get(v);
  354. break;
  355. }
  356. }
  357. ln = move0 + ",";
  358. break;
  359. //----------------------------------------------------------------------------------------------------
  360. case "A":
  361. move0 = "+[-<+]- ";
  362. arg0 = split(code.substring(2), ":")[0];
  363. arg1 = split(code.substring(2), ":")[1];
  364. act0 = "+[-<+]->";
  365. int f1a = factorsOf(int(arg1.charAt(0)))[0];
  366. int f2a = factorsOf(int(arg1.charAt(0)))[1];
  367. int f1b = factorsOf(int(arg1.charAt(0))+1)[0];
  368. int f2b = factorsOf(int(arg1.charAt(0))+1)[1];
  369. int f1c = factorsOf(int(arg1.charAt(0))-1)[0];
  370. int f2c = factorsOf(int(arg1.charAt(0))-1)[1];
  371. if (abs(f1a-f2a) < abs(f1b-f2b)) {
  372. for (int i = 0; i < f1a; i++) {
  373. act0 += "+";
  374. }
  375. act0 += "[>";
  376. for (int i = 0; i < f2a; i++) {
  377. act0 += "+";
  378. }
  379. act0 += "<-]";
  380. ln += act0+">[-]";
  381. } else if (abs(f1b-f2b) < abs(f1c-f2c)) {
  382. for (int i = 0; i < f1b; i++) {
  383. act0 += "+";
  384. }
  385. act0 += "[>";
  386. for (int i = 0; i < f2b; i++) {
  387. act0 += "+";
  388. }
  389. act0 += "<-]";
  390. ln += act0+">-[-]";
  391. } else {
  392. for (int i = 0; i < f1c; i++) {
  393. act0 += "+";
  394. }
  395. act0 += "[>";
  396. for (int i = 0; i < f2c; i++) {
  397. act0 += "+";
  398. }
  399. act0 += "<-]";
  400. ln = move0+"[-]"+act0;
  401. }
  402.  
  403. break;
  404. //----------------------------------------------------------------------------------------------------
  405. case "+":
  406. move0 = "+[-<+]- ";
  407. move1 = "+[-<+]- ";
  408. arg0 = split(code.substring(1), ":")[0];
  409. arg1 = split(code.substring(1), ":")[1];
  410. arg2 = split(code.substring(1), ":")[2];
  411. b1 = "+[-<+]->";
  412. b2 = "+[-<+]->>";
  413. b3 = "+[-<+]->>>";
  414.  
  415. move0 = toPos(arg0);
  416. move1 = toPos(arg1);
  417. move2 = toPos(arg2);
  418.  
  419. ln = move1+"\n\n[-"+b1+"+>+"+move1+"]"+b2+"[-"+move1+"+"+b2+"]\n";
  420. ln += move2+"[-"+b2+"+>+"+move2+"]"+b3+"[-"+move2+"+"+b3+"]\n";
  421. ln += b2+"[-<+>]\n";
  422. ln += move0+"[-]\n";
  423. ln += b1+"[-"+move0+"+"+b1+"]\n";
  424. break;
  425. //----------------------------------------------------------------------------------------------------
  426. case "\\":
  427. b1 = "+[-<+]->";
  428. ln = "\n>]"+b1+"[-]";
  429. break;
  430. //----------------------------------------------------------------------------------------------------
  431. case "?":
  432. arg0 = split(code.substring(2), ":")[0];
  433. arg1 = split(code.substring(1), ":")[1];
  434. move0 = toPos(arg0);
  435. move1 = toPos(arg1);
  436. b1 = "+[-<+]->";
  437. b2 = "+[-<+]->>";
  438. b3 = "+[-<+]->>>";
  439.  
  440. ln = "\n\n"+move0+"[-"+b1+"+>+"+move0+"]\n"; //copy arg0 to b1 and b2
  441. ln += b2+"[-"+move0+"+"+b2+"]\n"; //move b2 back to arg0
  442. ln += move1+"[-"+b2+"+>+"+move1+"]\n"; //copy arg1 to b2 and b3
  443. ln += b3+"[-"+move1+"+"+b3+"]\n"; //move b3 back to arg1
  444.  
  445. ln += "<[-<->]\n"; //subtract b2 from b1
  446. ln += "+<[>-]>[-"; //check if b1 is 0
  447.  
  448.  
  449. //in \\ func, clear b1 and add >]
  450. //copy arg0 to b1
  451. //copy arg1 to b2
  452. //subtract b1 and b2 (result in b1)
  453. //if the b1 is 0 run code
  454. //else clear b1
  455. break;
  456. }
  457. return ln;
  458. }
  459.  
  460. //----------------------------------------------------------------------------------------------------
  461.  
  462. void pCode() {
  463. for (int i = 0; i < bf.length; i++) {
  464. println(bf[i]);
  465. }
  466. }
  467.  
  468. //----------------------------------------------------------------------------------------------------
  469.  
  470. String toPos(String a) {
  471. String toPos = "+[-<+]-";
  472. //println(a);
  473. if (a.substring(0, 1).equals("^")) {
  474. for (int i = 0; i < int(a.substring(1)); i++) {
  475. toPos += ">";
  476. }
  477. } else {
  478. for (int i = 0; i < buffers; i++) {
  479. toPos += ">";
  480. }
  481. for (int i = 0; i < int(a); i++) {
  482. toPos += ">";
  483. }
  484. }
  485. for (int v = 0; v < vars.size(); v++) {
  486. if (a.equals(vars.get(v))) {
  487. toPos = varLocs.get(v);
  488. break;
  489. }
  490. }
  491. return toPos;
  492. }
  493.  
  494. //----------------------------------------------------------------------------------------------------
  495.  
  496. int[] factorsOf(int num) {
  497. int[] factors = new int[0];
  498. int[] ret = new int[2];
  499.  
  500. for (int i = 1; i < ceil(sqrt(num))+1; i++) {
  501. if (num % i == 0) {
  502. factors = append(factors, i);
  503. }
  504. }
  505. ret[0] = factors[factors.length-1];
  506. ret[1] = num/ret[0];
  507. return ret;
  508. }
  509. ```
  510.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement