starfish.js 26 KB

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