starfish.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. * A list of stacks for the script to work with
  70. *
  71. * @type {Stack[]}
  72. */
  73. this.stacks = [new Stack()];
  74. /**
  75. * The index of the currently used stack
  76. *
  77. * @type {int}
  78. */
  79. this.curr_stack = 0;
  80. /**
  81. * The current date
  82. *
  83. * This value is updated every tick
  84. * @type {?Date}
  85. */
  86. this.datetime = null;
  87. /**
  88. * Assorted debug options
  89. *
  90. * @type {object}
  91. */
  92. this.debug = {
  93. print: {
  94. codeBox: false,
  95. stacks: false,
  96. }
  97. };
  98. this.codeBoxDOM = document.getElementById(codeBoxID);
  99. if(!this.codeBoxDOM) {
  100. throw new Error(`Failed to find textarea with ID: ${codeBoxID}`);
  101. }
  102. this.outputDOM = document.getElementById(outputID);
  103. if(!this.outputDOM) {
  104. throw new Error(`Failed to find textarea with ID: ${outputID}`);
  105. }
  106. this.initialStackDOM = document.getElementById(initialStackID);
  107. if(!this.initialStackDOM) {
  108. throw new Error(`Failed to find input with ID: ${initialStackID}`);
  109. }
  110. }
  111. /**
  112. * Parse the initial code box
  113. *
  114. * Transforms the textual code box into usable matrix
  115. */
  116. ParseCodeBox() {
  117. // Reset some fields for a clean run
  118. this.box = [];
  119. this.stacks = [new Stack()];
  120. this.curr_stack = 0;
  121. this.pointer = {X: 0, Y: 0};
  122. this.curr_direction = this.directions.EAST;
  123. this.outputDOM.value = "";
  124. console.clear();
  125. // Set the debug values
  126. this.debug.print.codeBox = document.getElementById("debug-code-box")?.checked;
  127. this.debug.print.stacks = document.getElementById("debug-stacks")?.checked;
  128. const cbRaw = this.codeBoxDOM.value;
  129. const rows = cbRaw.split("\n");
  130. let maxRowLength = 0;
  131. for(const row of rows) {
  132. const rowSplit = row.split("");
  133. // Store this for later processing
  134. while(rowSplit.length > maxRowLength) {
  135. maxRowLength = rowSplit.length
  136. }
  137. this.box.push(rowSplit);
  138. }
  139. this.EqualizeBoxWidth(maxRowLength);
  140. this.maxBoxWidth = maxRowLength - 1;
  141. this.maxBoxHeight = this.box.length - 1;
  142. if (this.initialStackDOM.value != "") {
  143. this.ParseInitialStack();
  144. }
  145. this.Run();
  146. }
  147. /**
  148. * Parse the value provided for the stack at run time
  149. */
  150. ParseInitialStack() {
  151. const separator = /(["'].+?["']|\d+)/g;
  152. const stackValues = this.initialStackDOM.value.split(separator).filter((v) => v.trim().length);
  153. for (const val of stackValues) {
  154. const intVal = parseInt(val);
  155. if (!Number.isNaN(intVal)) {
  156. this.stacks[this.curr_stack].Push(intVal);
  157. }
  158. else {
  159. let chars = val.substr(1, val.length - 2).split('');
  160. chars = chars.map((c) => dec(c));
  161. this.stacks[this.curr_stack].Push(chars);
  162. }
  163. }
  164. }
  165. /**
  166. * Prints the code box to the console
  167. */
  168. PrintCodeBox() {
  169. let output = "";
  170. for (let y = 0; y < this.box.length; y++) {
  171. for (let x = 0; x < this.box[y].length; x++) {
  172. let instruction = this.box[y][x];
  173. if (x == this.pointer.X && y == this.pointer.Y) {
  174. instruction = `*${instruction}*`;
  175. }
  176. output += `${instruction} `;
  177. }
  178. output += "\n";
  179. }
  180. console.log(output);
  181. }
  182. /**
  183. * Prints all stacks to the console
  184. */
  185. PrintStacks() {
  186. let output = "{\n";
  187. for (let i = 0; i < this.stacks.length; i++) {
  188. output += `\t${i}: ${JSON.stringify(this.stacks[i].stack)},\n`
  189. }
  190. output += "}";
  191. console.log(output);
  192. }
  193. /**
  194. * Make all the rows in the code box the same length
  195. *
  196. * All rows not long enough will have NOPs added until they're uniform in size.
  197. *
  198. * @param {int} [rowLength] The longest row in the code box
  199. */
  200. EqualizeBoxWidth(rowLength = null) {
  201. if(!rowLength) {
  202. for(const row of this.box) {
  203. if(row.length > rowLength) {
  204. rowLength = row.length;
  205. }
  206. }
  207. }
  208. for(const row of this.box) {
  209. while(row.length < rowLength) {
  210. row.push(" ");
  211. }
  212. }
  213. }
  214. /**
  215. * Print the value to the display
  216. *
  217. * @TODO Set up an actual display
  218. * @param {*} value
  219. */
  220. Output(value) {
  221. this.outputDOM.value += value;
  222. }
  223. /**
  224. * The main loop for the engine
  225. */
  226. Run() {
  227. let fin = null;
  228. try {
  229. while(!fin) {
  230. fin = this.Swim();
  231. }
  232. }
  233. catch(e) {
  234. console.error(e);
  235. }
  236. }
  237. Execute(instruction) {
  238. let output = null;
  239. try{
  240. switch(instruction) {
  241. // NOP
  242. case " ":
  243. break;
  244. // Numbers
  245. case "1":
  246. case "2":
  247. case "3":
  248. case "4":
  249. case "5":
  250. case "6":
  251. case "7":
  252. case "8":
  253. case "9":
  254. case "0":
  255. case "a":
  256. case "b":
  257. case "c":
  258. case "d":
  259. case "e":
  260. case "f":
  261. this.stacks[this.curr_stack].Push(parseInt(instruction, 16));
  262. break;
  263. // Operators
  264. case "+": {
  265. const x = this.stacks[this.curr_stack].Pop();
  266. const y = this.stacks[this.curr_stack].Pop();
  267. this.stacks[this.curr_stack].Push(y + x);
  268. break;
  269. }
  270. case "-": {
  271. const x = this.stacks[this.curr_stack].Pop();
  272. const y = this.stacks[this.curr_stack].Pop();
  273. this.stacks[this.curr_stack].Push(y - x);
  274. break;
  275. }
  276. case "*": {
  277. const x = this.stacks[this.curr_stack].Pop();
  278. const y = this.stacks[this.curr_stack].Pop();
  279. this.stacks[this.curr_stack].Push(y * x);
  280. break;
  281. }
  282. case ",": {
  283. const x = this.stacks[this.curr_stack].Pop();
  284. const y = this.stacks[this.curr_stack].Pop();
  285. this.stacks[this.curr_stack].Push(y / x);
  286. break;
  287. }
  288. case "%": {
  289. const x = this.stacks[this.curr_stack].Pop();
  290. const y = this.stacks[this.curr_stack].Pop();
  291. this.stacks[this.curr_stack].Push(y % x);
  292. break;
  293. }
  294. case "(": {
  295. const x = this.stacks[this.curr_stack].Pop();
  296. const y = this.stacks[this.curr_stack].Pop();
  297. this.stacks[this.curr_stack].Push(y < x ? 1 : 0);
  298. break;
  299. }
  300. case ")": {
  301. const x = this.stacks[this.curr_stack].Pop();
  302. const y = this.stacks[this.curr_stack].Pop();
  303. this.stacks[this.curr_stack].Push(y > x ? 1 : 0);
  304. break;
  305. }
  306. case "=": {
  307. const x = this.stacks[this.curr_stack].Pop();
  308. const y = this.stacks[this.curr_stack].Pop();
  309. this.stacks[this.curr_stack].push(y == x ? 1 : 0);
  310. break;
  311. }
  312. //String mode
  313. case "\"":
  314. case "'":
  315. this.stringMode = !!this.stringMode ? 0 : dec(instruction);
  316. break;
  317. // Movement
  318. case "^":
  319. this.MoveUp();
  320. break;
  321. case ">":
  322. this.MoveRight();
  323. break;
  324. case "v":
  325. this.MoveDown();
  326. break;
  327. case "<":
  328. this.MoveLeft();
  329. break;
  330. // Mirrors
  331. case "/":
  332. this.ReflectForward();
  333. break;
  334. case "\\":
  335. this.ReflectBack();
  336. break;
  337. case "_":
  338. this.VerticalMirror();
  339. break;
  340. case "|":
  341. this.HorizontalMirror();
  342. break;
  343. case "#":
  344. this.OmniMirror();
  345. break;
  346. // Trampolines
  347. case "!":
  348. this.Move();
  349. break;
  350. case "?":
  351. if(this.stacks[this.curr_stack].Pop() === 0){ this.Move(); }
  352. break;
  353. // Stack manipulation
  354. case "&": {
  355. if (this.stacks[this.curr_stack].register == null) {
  356. this.stacks[this.curr_stack].register = this.stacks[this.curr_stack].Pop();
  357. }
  358. else {
  359. this.stacks[this.curr_stack].Push(this.stacks[this.curr_stack].register);
  360. this.stacks[this.curr_stack].register = null;
  361. }
  362. break;
  363. }
  364. case ":":
  365. this.stacks[this.curr_stack].Duplicate();
  366. break;
  367. case "~":
  368. this.stacks[this.curr_stack].Remove();
  369. break;
  370. case "$":
  371. this.stacks[this.curr_stack].SwapTwo();
  372. break;
  373. case "@":
  374. this.stacks[this.curr_stack].SwapThree();
  375. break;
  376. case "{":
  377. this.stacks[this.curr_stack].ShiftLeft();
  378. break;
  379. case "}":
  380. this.stacks[this.curr_stack].ShiftRight();
  381. break;
  382. case "r":
  383. this.stacks[this.curr_stack].Reverse();
  384. break;
  385. case "l":
  386. this.stacks[this.curr_stack].PushLength();
  387. break;
  388. case "[": {
  389. this.SpliceStack(this.stacks[this.curr_stack].Pop());
  390. break;
  391. }
  392. case "]":
  393. this.CollapseStack();
  394. break;
  395. case "I": {
  396. this.curr_stack++;
  397. if (this.curr_stack >= this.stacks.length) {
  398. throw new RangeError("curr_stack value out of bounds");
  399. }
  400. break;
  401. }
  402. case "D": {
  403. this.curr_stack--;
  404. if (this.curr_stack < 0) {
  405. throw new RangeError("curr_stack value out of bounds");
  406. }
  407. break;
  408. }
  409. // Output
  410. case "n":
  411. output = this.stacks[this.curr_stack].Pop();
  412. break;
  413. case "o":
  414. output = String.fromCharCode(this.stacks[this.curr_stack].Pop());
  415. break;
  416. // Time
  417. case "S":
  418. setTimeout(this.Run.bind(this), this.stacks[this.curr_stack].Pop() * 100);
  419. this.Move();
  420. output = true;
  421. break;
  422. case "h":
  423. this.stacks[this.curr_stack].Push(this.datetime.getUTCHours());
  424. break;
  425. case "m":
  426. this.stacks[this.curr_stack].Push(this.datetime.getUTCMinutes());
  427. break;
  428. case "s":
  429. this.stacks[this.curr_stack].Push(this.datetime.getUTCSeconds());
  430. break;
  431. // Code box manipulation
  432. case "g":
  433. this.PushFromCodeBox();
  434. break;
  435. case "p":
  436. this.PlaceIntoCodeBox();
  437. break;
  438. // Functions
  439. case ".": {
  440. this.pointer.Y = this.stacks[this.curr_stack].Pop();
  441. this.pointer.X = this.stacks[this.curr_stack].Pop();
  442. break;
  443. }
  444. case "C": {
  445. const currCoords = new Stack([this.pointer.X, this.pointer.Y]);
  446. this.stacks.splice(this.curr_stack, 0, currCoords);
  447. this.curr_stack++;
  448. this.pointer.Y = this.stacks[this.curr_stack].Pop();
  449. this.pointer.X = this.stacks[this.curr_stack].Pop();
  450. break;
  451. }
  452. case "R": {
  453. const newCoords = this.stacks.splice(this.curr_stack - 1, 1).pop();
  454. this.curr_stack--;
  455. this.pointer.Y = newCoords.Pop()
  456. this.pointer.X = newCoords.Pop();
  457. break;
  458. }
  459. // End execution
  460. case ";":
  461. output = true;
  462. break;
  463. default:
  464. throw new Error(`Unknown instruction: ${instruction}`);
  465. }
  466. }
  467. catch(e) {
  468. console.error(`Something smells fishy!\n${e != "" ? `${e}\n` : ""}Instruction: ${instruction}\nStack: ${JSON.stringify(this.stacks[this.curr_stack].stack)}`);
  469. return true;
  470. }
  471. return output;
  472. }
  473. Swim() {
  474. if(this.debug.print.codeBox) { this.PrintCodeBox(); }
  475. if(this.debug.print.stacks) { this.PrintStacks(); }
  476. const instruction = this.box[this.pointer.Y][this.pointer.X];
  477. this.datetime = new Date();
  478. if(this.stringMode != 0 && dec(instruction) != this.stringMode) {
  479. this.stacks[this.curr_stack].Push(dec(instruction));
  480. }
  481. else {
  482. const exeResult = this.Execute(instruction);
  483. if(exeResult === true) {
  484. return true;
  485. }
  486. else if(exeResult != null) {
  487. this.Output(exeResult);
  488. }
  489. }
  490. this.Move();
  491. }
  492. Move() {
  493. let newX = this.pointer.X + this.curr_direction[0];
  494. let newY = this.pointer.Y + this.curr_direction[1];
  495. // Keep the X coord in the boxes bounds
  496. if(newX < 0) {
  497. newX = this.maxBoxWidth;
  498. }
  499. else if(newX > this.maxBoxWidth) {
  500. newX = 0;
  501. }
  502. // Keep the Y coord in the boxes bounds
  503. if(newY < 0) {
  504. newY = this.maxBoxHeight;
  505. }
  506. else if(newY > this.maxBoxHeight) {
  507. newY = 0;
  508. }
  509. this.SetPointer(newX, newY);
  510. }
  511. /**
  512. * Implement C and .
  513. */
  514. SetPointer(x, y) {
  515. this.pointer = {X: x, Y: y};
  516. }
  517. /**
  518. * Implement ^
  519. *
  520. * Changes the swim direction upward
  521. */
  522. MoveUp() {
  523. this.curr_direction = this.directions.NORTH;
  524. }
  525. /**
  526. * Implement >
  527. *
  528. * Changes the swim direction rightward
  529. */
  530. MoveRight() {
  531. this.curr_direction = this.directions.EAST;
  532. this.dirWasLeft = false;
  533. }
  534. /**
  535. * Implement v
  536. *
  537. * Changes the swim direction downward
  538. */
  539. MoveDown() {
  540. this.curr_direction = this.directions.SOUTH;
  541. }
  542. /**
  543. * Implement <
  544. *
  545. * Changes the swim direction leftward
  546. */
  547. MoveLeft() {
  548. this.curr_direction = this.directions.WEST;
  549. this.dirWasLeft = true;
  550. }
  551. /**
  552. * Implement /
  553. *
  554. * Reflects the swim direction depending on its starting value
  555. */
  556. ReflectForward() {
  557. if (this.curr_direction == this.directions.NORTH) {
  558. this.MoveRight();
  559. }
  560. else if (this.curr_direction == this.directions.EAST) {
  561. this.MoveUp();
  562. }
  563. else if (this.curr_direction == this.directions.SOUTH) {
  564. this.MoveLeft();
  565. }
  566. else {
  567. this.MoveDown();
  568. }
  569. }
  570. /**
  571. * Implement \
  572. *
  573. * Reflects the swim direction depending on its starting value
  574. */
  575. ReflectBack() {
  576. if (this.curr_direction == this.directions.NORTH) {
  577. this.MoveLeft();
  578. }
  579. else if (this.curr_direction == this.directions.EAST) {
  580. this.MoveDown();
  581. }
  582. else if (this.curr_direction == this.directions.SOUTH) {
  583. this.MoveRight();
  584. }
  585. else {
  586. this.MoveUp();
  587. }
  588. }
  589. /**
  590. * Implement |
  591. *
  592. * Swaps the horizontal swim direction to its opposite
  593. */
  594. HorizontalMirror() {
  595. if (this.curr_direction == this.directions.EAST) {
  596. this.MoveLeft();
  597. }
  598. else {
  599. this.MoveRight();
  600. }
  601. }
  602. /**
  603. * Implement _
  604. *
  605. * Swaps the horizontal swim direction to its opposite
  606. */
  607. VerticalMirror() {
  608. if (this.curr_direction == this.directions.NORTH) {
  609. this.MoveDown();
  610. }
  611. else {
  612. this.MoveUp();
  613. }
  614. }
  615. /**
  616. * Implement #
  617. *
  618. * A combination of the vertical and the horizontal mirror
  619. */
  620. OmniMirror() {
  621. if (this.curr_direction[0]) {
  622. this.VerticalMirror();
  623. }
  624. else {
  625. this.HorizontalMirror();
  626. }
  627. }
  628. /**
  629. * Implement x
  630. *
  631. * Pseudo-randomly switches the swim direction
  632. */
  633. ShuffleDirection() {
  634. this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
  635. }
  636. /**
  637. * Implement [
  638. *
  639. * Takes X number of elements out of a stack and into a new stack
  640. *
  641. * This action creates a new stack, and places it on top of the one it was created from.
  642. * So, if you have three stacks, A, B, and C, and you splice a stack off of stack B,
  643. * the new order will be: A, B, D, and C.
  644. *
  645. * @see {@link https://esolangs.org/wiki/Fish#Stacks ><> Documentation}
  646. *
  647. * @param {int} spliceCount The number of elements to pop into a new stack
  648. */
  649. SpliceStack(spliceCount) {
  650. const stackCount = this.stacks[this.curr_stack].stack.length;
  651. if (spliceCount > stackCount) {
  652. throw new RangeError(`Cannot remove ${spliceCount} elements from a stack of only ${stackCount} elements`);
  653. }
  654. const newStack = new Stack(this.stacks[this.curr_stack].stack.splice(stackCount - spliceCount, spliceCount));
  655. // We're at the top of the stacks stack, so we can use .push
  656. if (this.curr_stack == this.stacks.length - 1) {
  657. this.stacks.push(newStack);
  658. }
  659. else {
  660. this.stacks.splice(this.curr_stack + 1, 0, newStack);
  661. }
  662. this.curr_stack++;
  663. }
  664. /**
  665. * Implement ]
  666. *
  667. * Collapses the current stack onto the one below it
  668. * If the current stack is the only one, it is replaced with a blank stack
  669. */
  670. CollapseStack() {
  671. // Undefined behavior collapsing the first stack down when there are other stacks available
  672. if (this.curr_stack == 0 && this.stacks.length != 1) {
  673. throw new Error();
  674. }
  675. if (this.curr_stack == 0) {
  676. this.stacks = [new Stack()];
  677. }
  678. else {
  679. const collapsed = this.stacks.splice(this.curr_stack, 1).pop();
  680. this.curr_stack--;
  681. const currStackCount = this.stacks[this.curr_stack].stack.length;
  682. this.stacks[this.curr_stack].stack.splice(currStackCount, 0, ...collapsed.stack);
  683. }
  684. }
  685. /**
  686. * Implement g
  687. *
  688. * Pops `y` and `x` from the stack, and then pushes the value of the character
  689. * at `[x, y]` in the code box.
  690. *
  691. * NOP's and coords that are out of bounds are converted to 0.
  692. *
  693. * Implements the behavior as defined by the original {@link https://gist.github.com/anonymous/6392418#file-fish-py-L306 ><>}, and not {@link https://github.com/redstarcoder/go-starfish/blob/master/starfish/starfish.go#L378 go-starfish}
  694. */
  695. PushFromCodeBox() {
  696. const y = this.stacks[this.curr_stack].Pop();
  697. const x = this.stacks[this.curr_stack].Pop();
  698. let val = undefined;
  699. try {
  700. val = this.box[y][x] || " ";
  701. }
  702. catch (e) {
  703. val = " ";
  704. }
  705. const valParsed = val == " " ? 0 : dec(val);
  706. this.stacks[this.curr_stack].Push(valParsed);
  707. }
  708. /**
  709. * Implement p
  710. *
  711. * Pops `y`, `x`, and `v` off of the stack, and then places the string
  712. * representation of that value at `[x, y]` in the code box.
  713. */
  714. PlaceIntoCodeBox() {
  715. const y = this.stacks[this.curr_stack].Pop();
  716. const x = this.stacks[this.curr_stack].Pop();
  717. const v = this.stacks[this.curr_stack].Pop();
  718. while(y >= this.box.length) {
  719. this.box.push([]);
  720. }
  721. while(x >= this.box[y].length) {
  722. this.box[y].push(" ");
  723. }
  724. this.EqualizeBoxWidth();
  725. this.box[y][x] = String.fromCharCode(v);
  726. }
  727. /**
  728. * Implement `
  729. *
  730. * Changes the swim direction based on the previous direction
  731. * @see https://esolangs.org/wiki/Starfish#Fisherman
  732. */
  733. Fisherman() {
  734. if (this.curr_direction[0]) {
  735. if (this.dirWasLeft) {
  736. this.MoveLeft();
  737. }
  738. else {
  739. this.MoveRight();
  740. }
  741. }
  742. else {
  743. if (this.onTheHook) {
  744. this.onTheHook = false;
  745. this.MoveUp();
  746. }
  747. else {
  748. this.onTheHook = true;
  749. this.MoveDown();
  750. }
  751. }
  752. }
  753. }
  754. /**
  755. * The stack class
  756. */
  757. class Stack {
  758. /**
  759. * @param {int[]} stackValues An array of values to initialize the stack with
  760. */
  761. constructor(stackValues = []) {
  762. /**
  763. * The stack
  764. * @type {int[]}
  765. */
  766. this.stack = stackValues;
  767. /**
  768. * A single value saved off the stack
  769. * @type {int}
  770. */
  771. this.register = null;
  772. }
  773. /**
  774. * Wrapper function for Array.prototype.push
  775. * @param {*} newValue
  776. */
  777. Push(newValue) {
  778. if(Array.isArray(newValue)) {
  779. this.stack.push(...newValue);
  780. }
  781. else {
  782. this.stack.push(newValue);
  783. }
  784. }
  785. /**
  786. * Wrapper function for Array.prototype.pop
  787. * @returns {*}
  788. */
  789. Pop() {
  790. const value = this.stack.pop();
  791. if(value == undefined){ throw new Error(); }
  792. return value;
  793. }
  794. /**
  795. * Implement }
  796. *
  797. * Shifts the entire stack leftward by one value
  798. */
  799. ShiftLeft() {
  800. const temp = this.stack.shift();
  801. this.stack.push(temp);
  802. }
  803. /**
  804. * Implement {
  805. *
  806. * Shifts the entire stack rightward by one value
  807. */
  808. ShiftRight() {
  809. const temp = this.stack.pop();
  810. this.stack.unshift(temp);
  811. }
  812. /**
  813. * Implement $
  814. *
  815. * Swaps the top two values of the stack
  816. */
  817. SwapTwo() {
  818. if(this.stack.length < 2) { throw new Error(); }
  819. const popped = this.stack.splice(this.stack.length - 2, 2);
  820. this.stack.push(...popped.reverse());
  821. }
  822. /**
  823. * Implement @
  824. *
  825. * Swaps the top three values of the stack
  826. */
  827. SwapThree() {
  828. if(this.stack.length < 3) { throw new Error(); }
  829. // Get the top three values
  830. const popped = this.stack.splice(this.stack.length - 3, 3);
  831. // Shift the elements to the right
  832. popped.unshift(popped.pop());
  833. this.stack.push(...popped);
  834. }
  835. /**
  836. * Implement :
  837. *
  838. * Duplicates the element on the top of the stack
  839. */
  840. Duplicate() {
  841. this.stack.push(this.stack[this.stack.length-1]);
  842. }
  843. /**
  844. * Implements ~
  845. *
  846. * Removes the element on the top of the stack
  847. */
  848. Remove() {
  849. this.stack.pop();
  850. }
  851. /**
  852. * Implement r
  853. *
  854. * Reverses the entire stack
  855. */
  856. Reverse() {
  857. this.stack.reverse();
  858. }
  859. /**
  860. * Implement l
  861. *
  862. * Pushes the length of the stack onto the top of the stack
  863. */
  864. PushLength() {
  865. this.stack.push(this.stack.length);
  866. }
  867. }
  868. /**
  869. * Get the char code of any character
  870. *
  871. * Can actually take any length of a value, but only returns the
  872. * char code of the first character.
  873. *
  874. * @param {*} value Any character
  875. * @returns {int} The value's char code
  876. */
  877. function dec(value) {
  878. return value.toString().charCodeAt(0);
  879. }