starfish.js 24 KB

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