starfish.js 25 KB

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