starfish.js 16 KB

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