starfish.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /**
  2. * The code box class
  3. */
  4. class CodeBox {
  5. constructor(codeBoxID, initialStackID, outputID) {
  6. /**
  7. * Possible vectors the pointer can move in
  8. * @type {Object}
  9. */
  10. this.directions = {
  11. NORTH: [ 0, -1],
  12. EAST: [ 1, 0],
  13. SOUTH: [ 0, 1],
  14. WEST: [-1, 0],
  15. };
  16. /**
  17. * The current vector of the pointer
  18. * @type {int[]}
  19. */
  20. this.curr_direction = this.directions.EAST;
  21. /**
  22. * The Set of instructions to execute
  23. *
  24. * Either a 1 or 2-dimensional array
  25. * @type {Array|Array[]}
  26. */
  27. this.box = [];
  28. /**
  29. * The farthest right the box goes
  30. * @type {int}
  31. */
  32. this.maxBoxWidth = 0;
  33. /**
  34. * The bottom of the box
  35. *
  36. * @type {int}
  37. */
  38. this.maxBoxHeight = 0;
  39. /**
  40. * The coordinates of the currently executing instruction inside the code box
  41. * @type {int[]}
  42. */
  43. this.pointer = [0,0];
  44. /**
  45. * Was the instruction last moving in the left direction
  46. *
  47. * Used by the {@link Fisherman}
  48. * @type {boolean}
  49. */
  50. this.dirWasLeft = false;
  51. /**
  52. * Are we currently under the influence of the {@link Fisherman}
  53. * @type {boolean}
  54. */
  55. this.onTheHook = false;
  56. /**
  57. * Are we currently processing code box instructions as a string
  58. *
  59. * 0 when false, otherwise it holds the char code for string delimiter, either
  60. * 34 or 39
  61. *
  62. * @type {int}
  63. */
  64. this.stringMode = 0;
  65. /**
  66. * The stack for the code box to work with
  67. *
  68. * @TODO Implement multiple stacks
  69. *
  70. * @type {Stack}
  71. */
  72. this.stack = new Stack();
  73. this.codeBoxDOM = document.getElementById(codeBoxID);
  74. if(!this.codeBoxDOM) {
  75. throw new Error(`Failed to find textarea with ID: ${codeBoxID}`);
  76. }
  77. this.outputDOM = document.getElementById(outputID);
  78. if(!this.outputDOM) {
  79. throw new Error(`Failed to find textarea with ID: ${outputID}`);
  80. }
  81. this.initialStackDOM = document.getElementById(initialStackID);
  82. if(!this.initialStackDOM) {
  83. throw new Error(`Failed to find input with ID: ${initialStackID}`);
  84. }
  85. }
  86. /**
  87. * Parse the initial code box
  88. *
  89. * Transforms the textual code box into usable matrix
  90. */
  91. ParseCodeBox() {
  92. // Reset some field for a clean run
  93. this.box = [];
  94. this.pointer = [0, 0];
  95. this.curr_direction = this.directions.EAST;
  96. this.outputDOM.value = "";
  97. const cbRaw = this.codeBoxDOM.value;
  98. const rows = cbRaw.split("\n").filter((r) => r.length);
  99. let maxRowLength = 0;
  100. for(const row of rows) {
  101. const rowSplit = row.split("");
  102. // Store this for later processing
  103. while(rowSplit.length > maxRowLength) {
  104. maxRowLength = rowSplit.length
  105. }
  106. this.box.push(rowSplit);
  107. }
  108. this.EqualizeBoxWidth(maxRowLength);
  109. this.maxBoxWidth = maxRowLength - 1;
  110. this.maxBoxHeight = this.box.length - 1;
  111. let fin = null;
  112. try {
  113. while(!fin) {
  114. fin = this.Swim();
  115. }
  116. }
  117. catch(e) {
  118. console.error(e);
  119. }
  120. }
  121. /**
  122. * Make all the rows in the code box the same length
  123. *
  124. * All rows not long enough will have NOPs added until they're uniform in size.
  125. *
  126. * @param {int} [rowLength] The longest row in the code box
  127. */
  128. EqualizeBoxWidth(rowLength = null) {
  129. if(!rowLength) {
  130. for(const row of this.box) {
  131. if(row.length > rowLength) {
  132. rowLength = row.length;
  133. }
  134. }
  135. }
  136. for(const row of this.box) {
  137. while(row.length < rowLength) {
  138. row.push(" ");
  139. }
  140. }
  141. }
  142. /**
  143. * Print the value to the display
  144. *
  145. * @TODO Set up an actual display
  146. * @param {*} value
  147. */
  148. Output(value) {
  149. this.outputDOM.value += value;
  150. }
  151. Execute(instruction) {
  152. let output = null;
  153. switch(instruction) {
  154. // NOP
  155. case " ":
  156. break;
  157. // Numbers
  158. case "1":
  159. case "2":
  160. case "3":
  161. case "4":
  162. case "5":
  163. case "6":
  164. case "7":
  165. case "8":
  166. case "9":
  167. case "0":
  168. case "a":
  169. case "b":
  170. case "c":
  171. case "d":
  172. case "e":
  173. case "f":
  174. this.stack.Push(parseInt(instruction, 16));
  175. break;
  176. // Operators
  177. case "+": {
  178. const x = this.stack.Pop();
  179. const y = this.stack.Pop();
  180. this.stack.Push(y + x);
  181. break;
  182. }
  183. case "-": {
  184. const x = this.stack.Pop();
  185. const y = this.stack.Pop();
  186. this.stack.Push(y - x);
  187. break;
  188. }
  189. case "*": {
  190. const x = this.stack.Pop();
  191. const y = this.stack.Pop();
  192. this.stack.Push(y * x);
  193. break;
  194. }
  195. case ",": {
  196. const x = this.stack.Pop();
  197. const y = this.stack.Pop();
  198. this.stack.Push(y / x);
  199. break;
  200. }
  201. case "%": {
  202. const x = this.stack.Pop();
  203. const y = this.stack.Pop();
  204. this.stack.Push(y % x);
  205. break;
  206. }
  207. // Movement
  208. case "^":
  209. this.MoveUp();
  210. break;
  211. case ">":
  212. this.MoveRight();
  213. break;
  214. case "v":
  215. this.MoveDown();
  216. break;
  217. case "<":
  218. this.MoveLeft();
  219. break;
  220. // Mirrors
  221. case "/":
  222. this.ReflectForward();
  223. break;
  224. case "\\":
  225. this.ReflectBack();
  226. break;
  227. case "_":
  228. this.VerticalMirror();
  229. break;
  230. case "|":
  231. this.HorizontalMirror();
  232. break;
  233. case "#":
  234. this.OmniMirror();
  235. break;
  236. // Output
  237. case "n":
  238. output = this.stack.Pop();
  239. break;
  240. case "o":
  241. output = String.fromCharCode(this.stack.Pop());
  242. break;
  243. // End execution
  244. case ";":
  245. output = true;
  246. break;
  247. default:
  248. throw new Error("Something's fishy!");
  249. }
  250. return output;
  251. }
  252. Swim() {
  253. const instruction = this.box[this.pointer[0]][this.pointer[1]];
  254. if(this.stringMode != 0 && instruction != this.stringMode) {
  255. this.stack.Push(dec(instruction));
  256. }
  257. else {
  258. const exeResult = this.Execute(instruction);
  259. if(exeResult === true) {
  260. return true;
  261. }
  262. else if(exeResult != null) {
  263. this.Output(exeResult);
  264. }
  265. }
  266. this.Move();
  267. }
  268. Move() {
  269. let newX = this.pointer[0] + this.curr_direction[0];
  270. let newY = this.pointer[1] + this.curr_direction[1];
  271. // Keep the X coord in the boxes bounds
  272. if(newX < 0) {
  273. newX = this.maxBoxWidth;
  274. }
  275. else if(newX > this.maxBoxWidth) {
  276. newX = 0;
  277. }
  278. // Keep the Y coord in the boxes bounds
  279. if(newY < 0) {
  280. newY = this.maxBoxHeight;
  281. }
  282. else if(newY > this.maxBoxHeight) {
  283. newY = 0;
  284. }
  285. this.SetPointer(newX, newY);
  286. }
  287. /**
  288. * Implement C and .
  289. */
  290. SetPointer(x, y) {
  291. this.pointer = [x, y];
  292. }
  293. /**
  294. * Implement ^
  295. *
  296. * Changes the swim direction upward
  297. */
  298. MoveUp() {
  299. this.curr_direction = this.directions.NORTH;
  300. }
  301. /**
  302. * Implement >
  303. *
  304. * Changes the swim direction rightward
  305. */
  306. MoveRight() {
  307. this.curr_direction = this.directions.EAST;
  308. this.dirWasLeft = false;
  309. }
  310. /**
  311. * Implement v
  312. *
  313. * Changes the swim direction downward
  314. */
  315. MoveDown() {
  316. this.curr_direction = this.directions.SOUTH;
  317. }
  318. /**
  319. * Implement <
  320. *
  321. * Changes the swim direction leftward
  322. */
  323. MoveLeft() {
  324. this.curr_direction = this.directions.WEST;
  325. this.dirWasLeft = true;
  326. }
  327. /**
  328. * Implement /
  329. *
  330. * Reflects the swim direction depending on its starting value
  331. */
  332. ReflectForward() {
  333. if (this.curr_direction == this.directions.NORTH) {
  334. this.MoveRight();
  335. }
  336. else if (this.curr_direction == this.directions.EAST) {
  337. this.MoveUp();
  338. }
  339. else if (this.curr_direction == this.directions.SOUTH) {
  340. this.MoveLeft();
  341. }
  342. else {
  343. this.MoveDown();
  344. }
  345. }
  346. /**
  347. * Implement \
  348. *
  349. * Reflects the swim direction depending on its starting value
  350. */
  351. ReflectBack() {
  352. if (this.curr_direction == this.directions.NORTH) {
  353. this.MoveLeft();
  354. }
  355. else if (this.curr_direction == this.directions.EAST) {
  356. this.MoveDown();
  357. }
  358. else if (this.curr_direction == this.directions.SOUTH) {
  359. this.MoveRight();
  360. }
  361. else {
  362. this.MoveUp();
  363. }
  364. }
  365. /**
  366. * Implement |
  367. *
  368. * Swaps the horizontal swim direction to its opposite
  369. */
  370. HorizontalMirror() {
  371. if (this.curr_direction == this.directions.EAST) {
  372. this.MoveLeft();
  373. }
  374. else {
  375. this.MoveRight();
  376. }
  377. }
  378. /**
  379. * Implement _
  380. *
  381. * Swaps the horizontal swim direction to its opposite
  382. */
  383. VerticalMirror() {
  384. if (this.curr_direction == this.directions.NORTH) {
  385. this.MoveDown();
  386. }
  387. else {
  388. this.MoveUp();
  389. }
  390. }
  391. /**
  392. * Implement #
  393. *
  394. * A combination of the vertical and the horizontal mirror
  395. */
  396. OmniMirror() {
  397. if (this.curr_direction[0]) {
  398. this.VerticalMirror();
  399. }
  400. else {
  401. this.HorizontalMirror();
  402. }
  403. }
  404. /**
  405. * Implement x
  406. *
  407. * Pseudo-randomly switches the swim direction
  408. */
  409. ShuffleDirection() {
  410. this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
  411. }
  412. /**
  413. * Implement `
  414. *
  415. * Changes the swim direction based on the previous direction
  416. * @see https://esolangs.org/wiki/Starfish#Fisherman
  417. */
  418. Fisherman() {
  419. if (this.curr_direction[0]) {
  420. if (this.dirWasLeft) {
  421. this.MoveLeft();
  422. }
  423. else {
  424. this.MoveRight();
  425. }
  426. }
  427. else {
  428. if (this.onTheHook) {
  429. this.onTheHook = false;
  430. this.MoveUp();
  431. }
  432. else {
  433. this.onTheHook = true;
  434. this.MoveDown();
  435. }
  436. }
  437. }
  438. }
  439. /**
  440. * The stack class
  441. */
  442. class Stack {
  443. constructor() {
  444. /**
  445. * The stack
  446. * @type {int[]}
  447. */
  448. this.stack = [];
  449. /**
  450. * A single value saved off the stack
  451. * @type {int}
  452. */
  453. this.register = null;
  454. }
  455. /**
  456. * Wrapper function for Array.prototype.push
  457. * @param {*} newValue
  458. */
  459. Push(newValue) {
  460. this.stack.push(newValue);
  461. }
  462. /**
  463. * Wrapper function for Array.prototype.pop
  464. * @returns {*}
  465. */
  466. Pop() {
  467. return this.stack.pop();
  468. }
  469. /**
  470. * Implement }
  471. *
  472. * Shifts the entire stack leftward by one value
  473. */
  474. ShiftLeft() {
  475. const temp = this.stack.shift();
  476. this.stack.push(temp);
  477. }
  478. /**
  479. * Implement {
  480. *
  481. * Shifts the entire stack rightward by one value
  482. */
  483. ShiftRight() {
  484. const temp = this.stack.pop();
  485. this.stack.unshift(temp);
  486. }
  487. /**
  488. * Implement $
  489. *
  490. * Swaps the top two values of the stack
  491. */
  492. SwapTwo() {
  493. // TODO
  494. }
  495. /**
  496. * Implement :
  497. *
  498. * Duplicates the element on the top of the stack
  499. */
  500. Duplicate() {
  501. this.stack.push(this.stack[this.stack.length-1]);
  502. }
  503. /**
  504. * Implements ~
  505. *
  506. * Removes the element on the top of the stack
  507. */
  508. Remove() {
  509. this.stack.pop();
  510. }
  511. /**
  512. * Implement r
  513. *
  514. * Reverses the entire stack
  515. */
  516. Reverse() {
  517. this.stack.reverse();
  518. }
  519. /**
  520. * Implement l
  521. *
  522. * Pushes the length of the stack onto the top of the stack
  523. */
  524. PushLength() {
  525. this.stack.push(this.stack.length);
  526. }
  527. }
  528. /**
  529. * Get the char code of any character
  530. *
  531. * Can actually take any length of a value, but only returns the
  532. * char code of the first character.
  533. *
  534. * @param {*} value Any character
  535. * @returns {int} The value's char code
  536. */
  537. function dec(value) {
  538. return value.toString().charCodeAt(0);
  539. }