starfish.js 23 KB

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