starfish.js 13 KB

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