starfish.js 13 KB

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