starfish.js 11 KB

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