starfish.js 25 KB

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