starfish.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. * The stack for the code box to work with
  70. *
  71. * @TODO Implement multiple stacks
  72. *
  73. * @type {Stack}
  74. */
  75. this.stack = new Stack();
  76. this.codeBoxDOM = document.getElementById(codeBoxID);
  77. if(!this.codeBoxDOM) {
  78. throw new Error(`Failed to find textarea with ID: ${codeBoxID}`);
  79. }
  80. this.outputDOM = document.getElementById(outputID);
  81. if(!this.outputDOM) {
  82. throw new Error(`Failed to find textarea with ID: ${outputID}`);
  83. }
  84. this.initialStackDOM = document.getElementById(initialStackID);
  85. if(!this.initialStackDOM) {
  86. throw new Error(`Failed to find input with ID: ${initialStackID}`);
  87. }
  88. }
  89. /**
  90. * Parse the initial code box
  91. *
  92. * Transforms the textual code box into usable matrix
  93. */
  94. ParseCodeBox() {
  95. // Reset some field for a clean run
  96. this.box = [];
  97. this.pointer = {X: 0, Y: 0};
  98. this.curr_direction = this.directions.EAST;
  99. this.outputDOM.value = "";
  100. const cbRaw = this.codeBoxDOM.value;
  101. const rows = cbRaw.split("\n").filter((r) => r.length);
  102. let maxRowLength = 0;
  103. for(const row of rows) {
  104. const rowSplit = row.split("");
  105. // Store this for later processing
  106. while(rowSplit.length > maxRowLength) {
  107. maxRowLength = rowSplit.length
  108. }
  109. this.box.push(rowSplit);
  110. }
  111. this.EqualizeBoxWidth(maxRowLength);
  112. this.maxBoxWidth = maxRowLength - 1;
  113. this.maxBoxHeight = this.box.length - 1;
  114. let fin = null;
  115. try {
  116. while(!fin) {
  117. fin = this.Swim();
  118. }
  119. }
  120. catch(e) {
  121. console.error(e);
  122. }
  123. }
  124. /**
  125. * Make all the rows in the code box the same length
  126. *
  127. * All rows not long enough will have NOPs added until they're uniform in size.
  128. *
  129. * @param {int} [rowLength] The longest row in the code box
  130. */
  131. EqualizeBoxWidth(rowLength = null) {
  132. if(!rowLength) {
  133. for(const row of this.box) {
  134. if(row.length > rowLength) {
  135. rowLength = row.length;
  136. }
  137. }
  138. }
  139. for(const row of this.box) {
  140. while(row.length < rowLength) {
  141. row.push(" ");
  142. }
  143. }
  144. }
  145. /**
  146. * Print the value to the display
  147. *
  148. * @TODO Set up an actual display
  149. * @param {*} value
  150. */
  151. Output(value) {
  152. this.outputDOM.value += value;
  153. }
  154. Execute(instruction) {
  155. let output = null;
  156. switch(instruction) {
  157. // NOP
  158. case " ":
  159. break;
  160. // Numbers
  161. case "1":
  162. case "2":
  163. case "3":
  164. case "4":
  165. case "5":
  166. case "6":
  167. case "7":
  168. case "8":
  169. case "9":
  170. case "0":
  171. case "a":
  172. case "b":
  173. case "c":
  174. case "d":
  175. case "e":
  176. case "f":
  177. this.stack.Push(parseInt(instruction, 16));
  178. break;
  179. // Operators
  180. case "+": {
  181. const x = this.stack.Pop();
  182. const y = this.stack.Pop();
  183. this.stack.Push(y + x);
  184. break;
  185. }
  186. case "-": {
  187. const x = this.stack.Pop();
  188. const y = this.stack.Pop();
  189. this.stack.Push(y - x);
  190. break;
  191. }
  192. case "*": {
  193. const x = this.stack.Pop();
  194. const y = this.stack.Pop();
  195. this.stack.Push(y * x);
  196. break;
  197. }
  198. case ",": {
  199. const x = this.stack.Pop();
  200. const y = this.stack.Pop();
  201. this.stack.Push(y / x);
  202. break;
  203. }
  204. case "%": {
  205. const x = this.stack.Pop();
  206. const y = this.stack.Pop();
  207. this.stack.Push(y % x);
  208. break;
  209. }
  210. // Movement
  211. case "^":
  212. this.MoveUp();
  213. break;
  214. case ">":
  215. this.MoveRight();
  216. break;
  217. case "v":
  218. this.MoveDown();
  219. break;
  220. case "<":
  221. this.MoveLeft();
  222. break;
  223. // Mirrors
  224. case "/":
  225. this.ReflectForward();
  226. break;
  227. case "\\":
  228. this.ReflectBack();
  229. break;
  230. case "_":
  231. this.VerticalMirror();
  232. break;
  233. case "|":
  234. this.HorizontalMirror();
  235. break;
  236. case "#":
  237. this.OmniMirror();
  238. break;
  239. // Output
  240. case "n":
  241. output = this.stack.Pop();
  242. break;
  243. case "o":
  244. output = String.fromCharCode(this.stack.Pop());
  245. break;
  246. // End execution
  247. case ";":
  248. output = true;
  249. break;
  250. default:
  251. throw new Error("Something's fishy!");
  252. }
  253. return output;
  254. }
  255. Swim() {
  256. const instruction = this.box[this.pointer.Y][this.pointer.X];
  257. if(this.stringMode != 0 && instruction != this.stringMode) {
  258. this.stack.Push(dec(instruction));
  259. }
  260. else {
  261. const exeResult = this.Execute(instruction);
  262. if(exeResult === true) {
  263. return true;
  264. }
  265. else if(exeResult != null) {
  266. this.Output(exeResult);
  267. }
  268. }
  269. this.Move();
  270. }
  271. Move() {
  272. let newX = this.pointer.X + this.curr_direction[0];
  273. let newY = this.pointer.Y + this.curr_direction[1];
  274. // Keep the X coord in the boxes bounds
  275. if(newX < 0) {
  276. newX = this.maxBoxWidth;
  277. }
  278. else if(newX > this.maxBoxWidth) {
  279. newX = 0;
  280. }
  281. // Keep the Y coord in the boxes bounds
  282. if(newY < 0) {
  283. newY = this.maxBoxHeight;
  284. }
  285. else if(newY > this.maxBoxHeight) {
  286. newY = 0;
  287. }
  288. this.SetPointer(newX, newY);
  289. }
  290. /**
  291. * Implement C and .
  292. */
  293. SetPointer(x, y) {
  294. this.pointer = {X: x, Y: y};
  295. }
  296. /**
  297. * Implement ^
  298. *
  299. * Changes the swim direction upward
  300. */
  301. MoveUp() {
  302. this.curr_direction = this.directions.NORTH;
  303. }
  304. /**
  305. * Implement >
  306. *
  307. * Changes the swim direction rightward
  308. */
  309. MoveRight() {
  310. this.curr_direction = this.directions.EAST;
  311. this.dirWasLeft = false;
  312. }
  313. /**
  314. * Implement v
  315. *
  316. * Changes the swim direction downward
  317. */
  318. MoveDown() {
  319. this.curr_direction = this.directions.SOUTH;
  320. }
  321. /**
  322. * Implement <
  323. *
  324. * Changes the swim direction leftward
  325. */
  326. MoveLeft() {
  327. this.curr_direction = this.directions.WEST;
  328. this.dirWasLeft = true;
  329. }
  330. /**
  331. * Implement /
  332. *
  333. * Reflects the swim direction depending on its starting value
  334. */
  335. ReflectForward() {
  336. if (this.curr_direction == this.directions.NORTH) {
  337. this.MoveRight();
  338. }
  339. else if (this.curr_direction == this.directions.EAST) {
  340. this.MoveUp();
  341. }
  342. else if (this.curr_direction == this.directions.SOUTH) {
  343. this.MoveLeft();
  344. }
  345. else {
  346. this.MoveDown();
  347. }
  348. }
  349. /**
  350. * Implement \
  351. *
  352. * Reflects the swim direction depending on its starting value
  353. */
  354. ReflectBack() {
  355. if (this.curr_direction == this.directions.NORTH) {
  356. this.MoveLeft();
  357. }
  358. else if (this.curr_direction == this.directions.EAST) {
  359. this.MoveDown();
  360. }
  361. else if (this.curr_direction == this.directions.SOUTH) {
  362. this.MoveRight();
  363. }
  364. else {
  365. this.MoveUp();
  366. }
  367. }
  368. /**
  369. * Implement |
  370. *
  371. * Swaps the horizontal swim direction to its opposite
  372. */
  373. HorizontalMirror() {
  374. if (this.curr_direction == this.directions.EAST) {
  375. this.MoveLeft();
  376. }
  377. else {
  378. this.MoveRight();
  379. }
  380. }
  381. /**
  382. * Implement _
  383. *
  384. * Swaps the horizontal swim direction to its opposite
  385. */
  386. VerticalMirror() {
  387. if (this.curr_direction == this.directions.NORTH) {
  388. this.MoveDown();
  389. }
  390. else {
  391. this.MoveUp();
  392. }
  393. }
  394. /**
  395. * Implement #
  396. *
  397. * A combination of the vertical and the horizontal mirror
  398. */
  399. OmniMirror() {
  400. if (this.curr_direction[0]) {
  401. this.VerticalMirror();
  402. }
  403. else {
  404. this.HorizontalMirror();
  405. }
  406. }
  407. /**
  408. * Implement x
  409. *
  410. * Pseudo-randomly switches the swim direction
  411. */
  412. ShuffleDirection() {
  413. this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
  414. }
  415. /**
  416. * Implement `
  417. *
  418. * Changes the swim direction based on the previous direction
  419. * @see https://esolangs.org/wiki/Starfish#Fisherman
  420. */
  421. Fisherman() {
  422. if (this.curr_direction[0]) {
  423. if (this.dirWasLeft) {
  424. this.MoveLeft();
  425. }
  426. else {
  427. this.MoveRight();
  428. }
  429. }
  430. else {
  431. if (this.onTheHook) {
  432. this.onTheHook = false;
  433. this.MoveUp();
  434. }
  435. else {
  436. this.onTheHook = true;
  437. this.MoveDown();
  438. }
  439. }
  440. }
  441. }
  442. /**
  443. * The stack class
  444. */
  445. class Stack {
  446. constructor() {
  447. /**
  448. * The stack
  449. * @type {int[]}
  450. */
  451. this.stack = [];
  452. /**
  453. * A single value saved off the stack
  454. * @type {int}
  455. */
  456. this.register = null;
  457. }
  458. /**
  459. * Wrapper function for Array.prototype.push
  460. * @param {*} newValue
  461. */
  462. Push(newValue) {
  463. this.stack.push(newValue);
  464. }
  465. /**
  466. * Wrapper function for Array.prototype.pop
  467. * @returns {*}
  468. */
  469. Pop() {
  470. return this.stack.pop();
  471. }
  472. /**
  473. * Implement }
  474. *
  475. * Shifts the entire stack leftward by one value
  476. */
  477. ShiftLeft() {
  478. const temp = this.stack.shift();
  479. this.stack.push(temp);
  480. }
  481. /**
  482. * Implement {
  483. *
  484. * Shifts the entire stack rightward by one value
  485. */
  486. ShiftRight() {
  487. const temp = this.stack.pop();
  488. this.stack.unshift(temp);
  489. }
  490. /**
  491. * Implement $
  492. *
  493. * Swaps the top two values of the stack
  494. */
  495. SwapTwo() {
  496. // TODO
  497. }
  498. /**
  499. * Implement :
  500. *
  501. * Duplicates the element on the top of the stack
  502. */
  503. Duplicate() {
  504. this.stack.push(this.stack[this.stack.length-1]);
  505. }
  506. /**
  507. * Implements ~
  508. *
  509. * Removes the element on the top of the stack
  510. */
  511. Remove() {
  512. this.stack.pop();
  513. }
  514. /**
  515. * Implement r
  516. *
  517. * Reverses the entire stack
  518. */
  519. Reverse() {
  520. this.stack.reverse();
  521. }
  522. /**
  523. * Implement l
  524. *
  525. * Pushes the length of the stack onto the top of the stack
  526. */
  527. PushLength() {
  528. this.stack.push(this.stack.length);
  529. }
  530. }
  531. /**
  532. * Get the char code of any character
  533. *
  534. * Can actually take any length of a value, but only returns the
  535. * char code of the first character.
  536. *
  537. * @param {*} value Any character
  538. * @returns {int} The value's char code
  539. */
  540. function dec(value) {
  541. return value.toString().charCodeAt(0);
  542. }