An interpreter for the ¤><> (Sunfish) esolang written in TypeScript
ApisNecros 5c74ac6b3a Add StackOutOfBoundsError class | 10 months ago | |
---|---|---|
.vscode | 10 months ago | |
src | 10 months ago | |
tests | 10 months ago | |
.gitignore | 10 months ago | |
README.md | 10 months ago | |
package-lock.json | 10 months ago | |
package.json | 10 months ago | |
tsconfig.json | 10 months ago |
TS-Sunfish is an interpreter for the ¤><>, or Sunfish, esoteric language written in TypeScript.
¤><> is a 2-dimensional, stack-based esoteric language derived from the *><>, or Starfish, 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 one of the Stacks. A full breakdown of the language's working can be found on its Esolang wiki page.
To use the TS-Sunfish interpreter, you'll first need to run npm install ts-sunfish
. From there, you can follow one of the examples shown below.
The following is a bare-bones example of the CodeBox
class. Here, we create a new instance of the CodeBox
class and pass it a simple script that prints the number 42
. At initialization, the Code Box will output to the console.
import { CodeBox } from "ts-sunfish";
const cb = new CodeBox("77*7-n;"); // Prints 42
cb.run();
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.
import { CodeBox } from "ts-sunfish";
const cb = new CodeBox("f-n;", [42]); // Subtract 15 from the first number on the stack, which we provide as 42. Prints 27
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.
import { CodeBox } from "ts-sunfish";
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 "
.
import { CodeBox } from "ts-sunfish";
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();
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 |
tickSleep | number | 0 | The amount of time to delay execution of each instruction in the Code Box. |
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.
class OutputLogger {
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 OutputLogger();
const cb = new CodeBox(`"Hello, World!"r>Ool?u!|;`, [], {
outputCallback: output.push.bind(output),
});
cb.run();
console.log(output.getOutput()); // Prints "Hello, World!"
If you would like to contribute to the development of this package, please email me for access to the repo.
¤><> is forked from RedStarCode's *><> language.