starfish.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. try{
  157. switch(instruction) {
  158. // NOP
  159. case " ":
  160. break;
  161. // Numbers
  162. case "1":
  163. case "2":
  164. case "3":
  165. case "4":
  166. case "5":
  167. case "6":
  168. case "7":
  169. case "8":
  170. case "9":
  171. case "0":
  172. case "a":
  173. case "b":
  174. case "c":
  175. case "d":
  176. case "e":
  177. case "f":
  178. this.stack.Push(parseInt(instruction, 16));
  179. break;
  180. // Operators
  181. case "+": {
  182. const x = this.stack.Pop();
  183. const y = this.stack.Pop();
  184. this.stack.Push(y + x);
  185. break;
  186. }
  187. case "-": {
  188. const x = this.stack.Pop();
  189. const y = this.stack.Pop();
  190. this.stack.Push(y - x);
  191. break;
  192. }
  193. case "*": {
  194. const x = this.stack.Pop();
  195. const y = this.stack.Pop();
  196. this.stack.Push(y * x);
  197. break;
  198. }
  199. case ",": {
  200. const x = this.stack.Pop();
  201. const y = this.stack.Pop();
  202. this.stack.Push(y / x);
  203. break;
  204. }
  205. case "%": {
  206. const x = this.stack.Pop();
  207. const y = this.stack.Pop();
  208. this.stack.Push(y % x);
  209. break;
  210. }
  211. case "(": {
  212. const x = this.stack.Pop();
  213. const y = this.stack.Pop();
  214. this.stack.Push(y < x ? 1 : 0);
  215. break;
  216. }
  217. case ")": {
  218. const x = this.stack.Pop();
  219. const y = this.stack.Pop();
  220. this.stack.Push(y > x ? 1 : 0);
  221. break;
  222. }
  223. case "=": {
  224. const x = this.stack.Pop();
  225. const y = this.stack.Pop();
  226. this.stack.push(y == x ? 1 : 0);
  227. break;
  228. }
  229. //String mode
  230. case "\"":
  231. case "'":
  232. this.stringMode = !!this.stringMode ? 0 : dec(instruction);
  233. break;
  234. // Movement
  235. case "^":
  236. this.MoveUp();
  237. break;
  238. case ">":
  239. this.MoveRight();
  240. break;
  241. case "v":
  242. this.MoveDown();
  243. break;
  244. case "<":
  245. this.MoveLeft();
  246. break;
  247. // Mirrors
  248. case "/":
  249. this.ReflectForward();
  250. break;
  251. case "\\":
  252. this.ReflectBack();
  253. break;
  254. case "_":
  255. this.VerticalMirror();
  256. break;
  257. case "|":
  258. this.HorizontalMirror();
  259. break;
  260. case "#":
  261. this.OmniMirror();
  262. break;
  263. // Trampolines
  264. case "!":
  265. this.Move();
  266. break;
  267. case "?":
  268. if(this.stack.Pop() === 0){ this.Move(); }
  269. break;
  270. // Stack manipulation
  271. case ":":
  272. this.stack.Duplicate();
  273. break;
  274. case "~":
  275. this.stack.Remove();
  276. break;
  277. case "$":
  278. this.stack.SwapTwo();
  279. break;
  280. case "@":
  281. this.stack.SwapThree();
  282. break;
  283. case "{":
  284. this.stack.ShiftLeft();
  285. break;
  286. case "}":
  287. this.stack.ShiftRight();
  288. break;
  289. case "r":
  290. this.stack.Reverse();
  291. break;
  292. case "l":
  293. this.stack.PushLength();
  294. break;
  295. // case "[":
  296. // break;
  297. // case "]":
  298. // break;
  299. // case "I":
  300. // this.curr_stack++;
  301. // break;
  302. // case "D":
  303. // this.curr_stack--;
  304. // break;
  305. // Output
  306. case "n":
  307. output = this.stack.Pop();
  308. break;
  309. case "o":
  310. output = String.fromCharCode(this.stack.Pop());
  311. break;
  312. // End execution
  313. case ";":
  314. output = true;
  315. break;
  316. default:
  317. throw new Error();
  318. }
  319. }
  320. catch(e) {
  321. console.error(`Something smells fishy!\n${e != "" ? `${e}\n` : ""}Instruction: ${instruction}\nStack: ${JSON.stringify(this.stack.stack)}`);
  322. return true;
  323. }
  324. return output;
  325. }
  326. Swim() {
  327. const instruction = this.box[this.pointer.Y][this.pointer.X];
  328. if(this.stringMode != 0 && dec(instruction) != this.stringMode) {
  329. this.stack.Push(dec(instruction));
  330. }
  331. else {
  332. const exeResult = this.Execute(instruction);
  333. if(exeResult === true) {
  334. return true;
  335. }
  336. else if(exeResult != null) {
  337. this.Output(exeResult);
  338. }
  339. }
  340. this.Move();
  341. }
  342. Move() {
  343. let newX = this.pointer.X + this.curr_direction[0];
  344. let newY = this.pointer.Y + this.curr_direction[1];
  345. // Keep the X coord in the boxes bounds
  346. if(newX < 0) {
  347. newX = this.maxBoxWidth;
  348. }
  349. else if(newX > this.maxBoxWidth) {
  350. newX = 0;
  351. }
  352. // Keep the Y coord in the boxes bounds
  353. if(newY < 0) {
  354. newY = this.maxBoxHeight;
  355. }
  356. else if(newY > this.maxBoxHeight) {
  357. newY = 0;
  358. }
  359. this.SetPointer(newX, newY);
  360. }
  361. /**
  362. * Implement C and .
  363. */
  364. SetPointer(x, y) {
  365. this.pointer = {X: x, Y: y};
  366. }
  367. /**
  368. * Implement ^
  369. *
  370. * Changes the swim direction upward
  371. */
  372. MoveUp() {
  373. this.curr_direction = this.directions.NORTH;
  374. }
  375. /**
  376. * Implement >
  377. *
  378. * Changes the swim direction rightward
  379. */
  380. MoveRight() {
  381. this.curr_direction = this.directions.EAST;
  382. this.dirWasLeft = false;
  383. }
  384. /**
  385. * Implement v
  386. *
  387. * Changes the swim direction downward
  388. */
  389. MoveDown() {
  390. this.curr_direction = this.directions.SOUTH;
  391. }
  392. /**
  393. * Implement <
  394. *
  395. * Changes the swim direction leftward
  396. */
  397. MoveLeft() {
  398. this.curr_direction = this.directions.WEST;
  399. this.dirWasLeft = true;
  400. }
  401. /**
  402. * Implement /
  403. *
  404. * Reflects the swim direction depending on its starting value
  405. */
  406. ReflectForward() {
  407. if (this.curr_direction == this.directions.NORTH) {
  408. this.MoveRight();
  409. }
  410. else if (this.curr_direction == this.directions.EAST) {
  411. this.MoveUp();
  412. }
  413. else if (this.curr_direction == this.directions.SOUTH) {
  414. this.MoveLeft();
  415. }
  416. else {
  417. this.MoveDown();
  418. }
  419. }
  420. /**
  421. * Implement \
  422. *
  423. * Reflects the swim direction depending on its starting value
  424. */
  425. ReflectBack() {
  426. if (this.curr_direction == this.directions.NORTH) {
  427. this.MoveLeft();
  428. }
  429. else if (this.curr_direction == this.directions.EAST) {
  430. this.MoveDown();
  431. }
  432. else if (this.curr_direction == this.directions.SOUTH) {
  433. this.MoveRight();
  434. }
  435. else {
  436. this.MoveUp();
  437. }
  438. }
  439. /**
  440. * Implement |
  441. *
  442. * Swaps the horizontal swim direction to its opposite
  443. */
  444. HorizontalMirror() {
  445. if (this.curr_direction == this.directions.EAST) {
  446. this.MoveLeft();
  447. }
  448. else {
  449. this.MoveRight();
  450. }
  451. }
  452. /**
  453. * Implement _
  454. *
  455. * Swaps the horizontal swim direction to its opposite
  456. */
  457. VerticalMirror() {
  458. if (this.curr_direction == this.directions.NORTH) {
  459. this.MoveDown();
  460. }
  461. else {
  462. this.MoveUp();
  463. }
  464. }
  465. /**
  466. * Implement #
  467. *
  468. * A combination of the vertical and the horizontal mirror
  469. */
  470. OmniMirror() {
  471. if (this.curr_direction[0]) {
  472. this.VerticalMirror();
  473. }
  474. else {
  475. this.HorizontalMirror();
  476. }
  477. }
  478. /**
  479. * Implement x
  480. *
  481. * Pseudo-randomly switches the swim direction
  482. */
  483. ShuffleDirection() {
  484. this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
  485. }
  486. /**
  487. * Implement `
  488. *
  489. * Changes the swim direction based on the previous direction
  490. * @see https://esolangs.org/wiki/Starfish#Fisherman
  491. */
  492. Fisherman() {
  493. if (this.curr_direction[0]) {
  494. if (this.dirWasLeft) {
  495. this.MoveLeft();
  496. }
  497. else {
  498. this.MoveRight();
  499. }
  500. }
  501. else {
  502. if (this.onTheHook) {
  503. this.onTheHook = false;
  504. this.MoveUp();
  505. }
  506. else {
  507. this.onTheHook = true;
  508. this.MoveDown();
  509. }
  510. }
  511. }
  512. }
  513. /**
  514. * The stack class
  515. */
  516. class Stack {
  517. constructor() {
  518. /**
  519. * The stack
  520. * @type {int[]}
  521. */
  522. this.stack = [];
  523. /**
  524. * A single value saved off the stack
  525. * @type {int}
  526. */
  527. this.register = null;
  528. }
  529. /**
  530. * Wrapper function for Array.prototype.push
  531. * @param {*} newValue
  532. */
  533. Push(newValue) {
  534. this.stack.push(newValue);
  535. }
  536. /**
  537. * Wrapper function for Array.prototype.pop
  538. * @returns {*}
  539. */
  540. Pop() {
  541. const value = this.stack.pop();
  542. if(value == undefined){ throw new Error(); }
  543. return value;
  544. }
  545. /**
  546. * Implement }
  547. *
  548. * Shifts the entire stack leftward by one value
  549. */
  550. ShiftLeft() {
  551. const temp = this.stack.shift();
  552. this.stack.push(temp);
  553. }
  554. /**
  555. * Implement {
  556. *
  557. * Shifts the entire stack rightward by one value
  558. */
  559. ShiftRight() {
  560. const temp = this.stack.pop();
  561. this.stack.unshift(temp);
  562. }
  563. /**
  564. * Implement $
  565. *
  566. * Swaps the top two values of the stack
  567. */
  568. SwapTwo() {
  569. if(this.stack.length < 2) { throw new Error(); }
  570. const popped = this.stack.splice(this.stack.length - 2, 2);
  571. this.stack.push(...popped.reverse());
  572. }
  573. /**
  574. * Implement @
  575. *
  576. * Swaps the top three values of the stack
  577. */
  578. SwapThree() {
  579. if(this.stack.length < 3) { throw new Error(); }
  580. // Get the top three values
  581. const popped = this.stack.splice(this.stack.length - 3, 3);
  582. // Shift the elements to the right
  583. popped.unshift(popped.pop());
  584. this.stack.push(...popped);
  585. }
  586. /**
  587. * Implement :
  588. *
  589. * Duplicates the element on the top of the stack
  590. */
  591. Duplicate() {
  592. this.stack.push(this.stack[this.stack.length-1]);
  593. }
  594. /**
  595. * Implements ~
  596. *
  597. * Removes the element on the top of the stack
  598. */
  599. Remove() {
  600. this.stack.pop();
  601. }
  602. /**
  603. * Implement r
  604. *
  605. * Reverses the entire stack
  606. */
  607. Reverse() {
  608. this.stack.reverse();
  609. }
  610. /**
  611. * Implement l
  612. *
  613. * Pushes the length of the stack onto the top of the stack
  614. */
  615. PushLength() {
  616. this.stack.push(this.stack.length);
  617. }
  618. }
  619. /**
  620. * Get the char code of any character
  621. *
  622. * Can actually take any length of a value, but only returns the
  623. * char code of the first character.
  624. *
  625. * @param {*} value Any character
  626. * @returns {int} The value's char code
  627. */
  628. function dec(value) {
  629. return value.toString().charCodeAt(0);
  630. }