Befunge93 is an interpreter written in JavaScript for the Befunge93 esoteric programming language.
Befunge93 is available on npm!
npm install befunge93
To run a Befunge program, just create a new interpreter and call #run with your program as the argument.
#run returns a promise that is resolved when the program terminates (by parsing a "@").
const Befunge = require('befunge93');
let befunge = new Befunge();
befunge.run("1234v\n>9 #5>:#._@\n^876<")
.then((output) => {
console.log(output);
});
Outputs: "9 8 7 6 5 4 3 2 1 "
Befunge93 provides you with a few callbacks to hook into the interpreter.
In order to provide the interpreter with input/output, you must supply the #onInput and #onOutput callbacks.
Called when the interpreter needs input from the user. Ie. the ~ and & commands.
Example:
befunge.onInput = (message) => {
return prompt(message);
};
Called when the interpreter outputs a character. Ie. the , and . commands
Example:
befunge.onOutput = (output) => {
console.log(output);
};
Called when the program's cursor changes.
Example:
befunge.onStep = (x, y) => {
console.log(`Current x position: ${x}`);
console.log(`Current y position: ${y}`);
};
Called when the stack is updated.
Example:
befunge.onStackChange = (stack) => {
console.log(`Current stack: ${stack.toString()}`);
};
Called when a cell is updated by the p command.
Example:
befunge.onCellChange = (x, y, newValue) => {
console.log(`Cell at ${x}, ${y} has been updated to ${newValue.toString()}`);
};