starfish.js 25 KB

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