starfish.js 11 KB

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