starfish.js 12 KB

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