starfish.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 "*": {
  171. const x = this.stack.Pop();
  172. const y = this.stack.Pop();
  173. this.stack.Push(x * y);
  174. break;
  175. }
  176. case ",": {
  177. const x = this.stack.Pop();
  178. const y = this.stack.Pop();
  179. this.stack.Push(x / y);
  180. break;
  181. }
  182. case "%": {
  183. const x = this.stack.Pop();
  184. const y = this.stack.Pop();
  185. this.stack.Push(x % y);
  186. break;
  187. }
  188. case "n":
  189. output = this.stack.Pop();
  190. break;
  191. case "o":
  192. output = String.fromCharCode(this.stack.Pop());
  193. break;
  194. case ";":
  195. output = true;
  196. break;
  197. default:
  198. throw new Error("Something's fishy!");
  199. }
  200. return output;
  201. }
  202. Swim() {
  203. const instruction = this.box[this.pointer[0]][this.pointer[1]];
  204. if(this.stringMode != 0 && instruction != this.stringMode) {
  205. this.stack.Push(dec(instruction));
  206. }
  207. else {
  208. const exeResult = this.Execute(instruction);
  209. if(exeResult === true) {
  210. return true;
  211. }
  212. else if(exeResult != null) {
  213. this.Output(exeResult);
  214. }
  215. }
  216. this.Move();
  217. }
  218. Move() {
  219. const newX = this.pointer[0] + this.curr_direction[0];
  220. const newY = this.pointer[1] + this.curr_direction[1];
  221. this.SetPointer(newX, newY);
  222. }
  223. /**
  224. * Implement C and .
  225. */
  226. SetPointer(x, y) {
  227. this.pointer = [x, y];
  228. }
  229. /**
  230. * Implement ^
  231. *
  232. * Changes the swim direction upward
  233. */
  234. MoveUp() {
  235. this.curr_direction = this.directions.NORTH;
  236. }
  237. /**
  238. * Implement >
  239. *
  240. * Changes the swim direction rightward
  241. */
  242. MoveRight() {
  243. this.curr_direction = this.directions.EAST;
  244. this.dirWasLeft = false;
  245. }
  246. /**
  247. * Implement v
  248. *
  249. * Changes the swim direction downward
  250. */
  251. MoveDown() {
  252. this.curr_direction = this.directions.SOUTH;
  253. }
  254. /**
  255. * Implement <
  256. *
  257. * Changes the swim direction leftward
  258. */
  259. MoveLeft() {
  260. this.curr_direction = this.directions.WEST;
  261. this.dirWasLeft = true;
  262. }
  263. /**
  264. * Implement /
  265. *
  266. * Reflects the swim direction depending on its starting value
  267. */
  268. ReflectForward() {
  269. if (this.curr_direction == this.directions.NORTH) {
  270. this.MoveRight();
  271. }
  272. else if (this.curr_direction == this.directions.EAST) {
  273. this.MoveUp();
  274. }
  275. else if (this.curr_direction == this.directions.SOUTH) {
  276. this.MoveLeft();
  277. }
  278. else {
  279. this.MoveDown();
  280. }
  281. }
  282. /**
  283. * Implement \
  284. *
  285. * Reflects the swim direction depending on its starting value
  286. */
  287. ReflectBack() {
  288. if (this.curr_direction == this.directions.NORTH) {
  289. this.MoveLeft();
  290. }
  291. else if (this.curr_direction == this.directions.EAST) {
  292. this.MoveDown();
  293. }
  294. else if (this.curr_direction == this.directions.SOUTH) {
  295. this.MoveRight();
  296. }
  297. else {
  298. this.MoveUp();
  299. }
  300. }
  301. /**
  302. * Implement |
  303. *
  304. * Swaps the horizontal swim direction to its opposite
  305. */
  306. HorizontalMirror() {
  307. if (this.curr_direction == this.directions.EAST) {
  308. this.MoveLeft();
  309. }
  310. else {
  311. this.MoveRight();
  312. }
  313. }
  314. /**
  315. * Implement _
  316. *
  317. * Swaps the horizontal swim direction to its opposite
  318. */
  319. VerticalMirror() {
  320. if (this.curr_direction == this.directions.NORTH) {
  321. this.MoveDown();
  322. }
  323. else {
  324. this.MoveUp();
  325. }
  326. }
  327. /**
  328. * Implement #
  329. *
  330. * A combination of the vertical and the horizontal mirror
  331. */
  332. OmniMirror() {
  333. if (this.curr_direction[0]) {
  334. this.VerticalMirror();
  335. }
  336. else {
  337. this.HorizontalMirror();
  338. }
  339. }
  340. /**
  341. * Implement x
  342. *
  343. * Pseudo-randomly switches the swim direction
  344. */
  345. ShuffleDirection() {
  346. this.curr_direction = Object.values(this.directions)[Math.floor(Math.random() * 4)];
  347. }
  348. /**
  349. * Implement `
  350. *
  351. * Changes the swim direction based on the previous direction
  352. * @see https://esolangs.org/wiki/Starfish#Fisherman
  353. */
  354. Fisherman() {
  355. if (this.curr_direction[0]) {
  356. if (this.dirWasLeft) {
  357. this.MoveLeft();
  358. }
  359. else {
  360. this.MoveRight();
  361. }
  362. }
  363. else {
  364. if (this.onTheHook) {
  365. this.onTheHook = false;
  366. this.MoveUp();
  367. }
  368. else {
  369. this.onTheHook = true;
  370. this.MoveDown();
  371. }
  372. }
  373. }
  374. }
  375. /**
  376. * The stack class
  377. */
  378. class Stack {
  379. constructor() {
  380. /**
  381. * The stack
  382. * @type {int[]}
  383. */
  384. this.stack = [];
  385. /**
  386. * A single value saved off the stack
  387. * @type {int}
  388. */
  389. this.register = null;
  390. }
  391. /**
  392. * Wrapper function for Array.prototype.push
  393. * @param {*} newValue
  394. */
  395. Push(newValue) {
  396. this.stack.push(newValue);
  397. }
  398. /**
  399. * Wrapper function for Array.prototype.pop
  400. * @returns {*}
  401. */
  402. Pop() {
  403. return this.stack.pop();
  404. }
  405. /**
  406. * Implement }
  407. *
  408. * Shifts the entire stack leftward by one value
  409. */
  410. ShiftLeft() {
  411. const temp = this.stack.shift();
  412. this.stack.push(temp);
  413. }
  414. /**
  415. * Implement {
  416. *
  417. * Shifts the entire stack rightward by one value
  418. */
  419. ShiftRight() {
  420. const temp = this.stack.pop();
  421. this.stack.unshift(temp);
  422. }
  423. /**
  424. * Implement $
  425. *
  426. * Swaps the top two values of the stack
  427. */
  428. SwapTwo() {
  429. // TODO
  430. }
  431. /**
  432. * Implement :
  433. *
  434. * Duplicates the element on the top of the stack
  435. */
  436. Duplicate() {
  437. this.stack.push(this.stack[this.stack.length-1]);
  438. }
  439. /**
  440. * Implements ~
  441. *
  442. * Removes the element on the top of the stack
  443. */
  444. Remove() {
  445. this.stack.pop();
  446. }
  447. /**
  448. * Implement r
  449. *
  450. * Reverses the entire stack
  451. */
  452. Reverse() {
  453. this.stack.reverse();
  454. }
  455. /**
  456. * Implement l
  457. *
  458. * Pushes the length of the stack onto the top of the stack
  459. */
  460. PushLength() {
  461. this.stack.push(this.stack.length);
  462. }
  463. }
  464. /**
  465. * Get the char code of any character
  466. *
  467. * Can actually take any length of a value, but only returns the
  468. * char code of the first character.
  469. *
  470. * @param {*} value Any character
  471. * @returns {int} The value's char code
  472. */
  473. function dec(value) {
  474. return value.toString().charCodeAt(0);
  475. }