starfish.js 11 KB

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