|
@@ -4,7 +4,7 @@ TS-Starfish is an implementation of the [*><>, or Starfish,](https://esolangs.or
|
|
|
|
|
|
## Summary
|
|
|
|
|
|
-*><> is a 2-dimensional stack based esoteric language derived from the [><>, or Fish](https://esolangs.org/wiki/Fish), language. The language is comprised of 3 main components; the CodeBox, the Instruction Pointer, and the Stacks. Upon initialization, the Instruction Pointer moves through the Code Box from left to right. Each instruction encountered either changes the movement and direction of the Instruction Pointer, or performs an operation on the Stack. A full breakdown of the language's working can be found on its [EsoLang Wiki page](https://esolangs.org/wiki/Starfish).
|
|
|
+*><> is a 2-dimensional stack based esoteric language derived from the [><>, or Fish](https://esolangs.org/wiki/Fish), language. The language is comprised of 3 main components; the Code Box, the Instruction Pointer, and the Stacks. Upon initialization, the Instruction Pointer moves through the Code Box from left to right. Each instruction encountered either changes the movement and direction of the Instruction Pointer, or performs an operation on the Stack. A full breakdown of the language's working can be found on its [EsoLang Wiki page](https://esolangs.org/wiki/Starfish).
|
|
|
|
|
|
## Usage
|
|
|
|
|
@@ -21,6 +21,81 @@ const cb = new CodeBox("77*7-n;"); // Prints 42
|
|
|
cb.run();
|
|
|
```
|
|
|
|
|
|
+### Initial Input Stack
|
|
|
+
|
|
|
+The *><> language allows the user to provide an initial stack for the Code Box to run on. This initial stack can be passed to the Code Box as either a string, or an array of numbers.
|
|
|
+
|
|
|
+**Basic Example**
|
|
|
+
|
|
|
+In this example, we initialize the stack as `[42]`. Then, we add 15 and subtract that from 42. Finally, we output the result, 27.
|
|
|
+
|
|
|
+```Javascript
|
|
|
+import { CodeBox } from "ts-starfish";
|
|
|
+
|
|
|
+const cb = new CodeBox("f-n;", [42]); // Subtract 15 from the first number on the stack, which we provide as 42
|
|
|
+cb.run();
|
|
|
+```
|
|
|
+
|
|
|
+**Multi-valued Initial Stack**
|
|
|
+
|
|
|
+You can pass any number of values to the initial stack, even as a string. For strings, each value needs to either be comma separated, or space separated.
|
|
|
+
|
|
|
+```Javascript
|
|
|
+import { CodeBox } from "ts-starfish";
|
|
|
+
|
|
|
+const cb = new CodeBox(`+l1=?v
|
|
|
+ ;n<`, "10 12 14 16 18 20"); // Sum all numbers on the stack and then print. Prints 90
|
|
|
+cb.run();
|
|
|
+```
|
|
|
+
|
|
|
+You can also include strings on the initial stack! These will be added to the stack in the form of their decimal ASCII value, and must be wrapped in either a `'` or a `"`.
|
|
|
+
|
|
|
+```Javascript
|
|
|
+import { CodeBox } from "ts-starfish";
|
|
|
+
|
|
|
+const cb = new CodeBox(`+l1=?v
|
|
|
+ ;n<`, "'Hello, World!' 11"); // Sum the ASCII values of the input string plus the 11, and then print. Prints 1140
|
|
|
+cb.run();
|
|
|
+```
|
|
|
+
|
|
|
+### CodeBox Options
|
|
|
+
|
|
|
+The `CodeBox` class constructor's third parameter is an optional object of options to run the Code Box with.
|
|
|
+
|
|
|
+| Key | Data Type | Default | Description |
|
|
|
+|---|---|---|---|
|
|
|
+| **outputCallback** | Function | console.log | A function to send all output from the Code Box to |
|
|
|
+| **debug.print.logger** | Function | this.outputCallback | A function to send all debugging output to. |
|
|
|
+| **debug.print.codeBox** | boolean | false | Output the current state of the Code Box at the start of each instruction |
|
|
|
+| **debug.print.stacks** | boolean | false | Output the current state of all stacks in the Code Box at the start of each instruction |
|
|
|
+
|
|
|
+In the following example, we create an `OutputLogger` object that stores the output from the Code Box. Then, after the Code Box has run, we can output all of the values as one whole string. If we were to just use the initial value of `console.log` as the output function, we would get each character on a new line.
|
|
|
+
|
|
|
+```Javascript
|
|
|
+class OutputJoiner {
|
|
|
+ constructor(joinChar = "") {
|
|
|
+ this.out = "";
|
|
|
+ this.JOIN_CHAR = joinChar;
|
|
|
+ }
|
|
|
+
|
|
|
+ push(newOutput) {
|
|
|
+ if (this.out != "") { this.out += this.JOIN_CHAR; }
|
|
|
+ this.out += newOutput;
|
|
|
+ }
|
|
|
+
|
|
|
+ getOutput() {
|
|
|
+ return this.out;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const output = new OutputJoiner();
|
|
|
+const cb = new CodeBox(`"Hello, World!"r>Ool?u!|;`, [], {
|
|
|
+ outputCallback: output.push.bind(output),
|
|
|
+});
|
|
|
+cb.run();
|
|
|
+console.log(output.getOutput()); // Prints "Hello, World!"
|
|
|
+```
|
|
|
+
|
|
|
## Acknowledgements
|
|
|
|
|
|
The original specification for *><> was written by GitHub user [RedStarCode](https://github.com/redstarcoder) in [go-starfish](https://github.com/redstarcoder/go-starfish).
|