Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Malbolge operation in new esoteric programming category #503

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"lodash": "^4.17.11",
"loglevel": "^1.6.1",
"loglevel-message-prefix": "^3.0.0",
"malbolge-vm": "^1.0.2",
"moment": "^2.23.0",
"moment-timezone": "^0.5.23",
"ngeohash": "^0.6.3",
Expand Down
6 changes: 6 additions & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@
"TCP/IP Checksum"
]
},
{
"name": "Esoteric Programming",
"ops": [
"Malbolge"
]
},
{
"name": "Code tidy",
"ops": [
Expand Down
90 changes: 90 additions & 0 deletions src/core/operations/Malbolge.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @author Karsten Silkenbäumer [github.com/kassi]
* @copyright Karsten Silkenbäumer 2019
* @license Apache-2.0
*/

import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import mb from "malbolge-vm";

/**
* Malbolge operation
*/
class Malbolge extends Operation {
/**
* Malbolge constructor
*/
constructor () {
super();

this.name = "Malbolge";
this.module = "Default";
this.description = "Malbolge, invented by Ben Olmstead in 1998, is an esoteric programming language designed to be as difficult to program in as possible. The first ‘Hello, world!’ program written in it was produced by a Lisp program using a local beam search of the space of all possible programs. It is modeled as a virtual machine based on ternary digits.";
this.infoURL = "http://esolangs.org/wiki/Malbolge";
this.inputType = "string";
this.outputType = "string";
this.args = [
{
"name": "User Input (line by line)",
"type": "text",
"value": ""
}
];
this.patterns = [
{
match: "^(?:[\\x21-\\x7e])?$",
flags: "i",
args: []
}
];
}

/**
* @param {String} input - A Malbolge program
* @param {Object[]} args
* @returns {String}
*/
run (input, args) {
if (input.length === 0) {
return "";
}

const [userInputString] = args,
vm = mb.load(input),
userInputArray = userInputString.split("").map(e => e.charCodeAt(0));

let userInput = null,
output = "",
loop = true,
temp;

while (loop) {
try {
while ((temp = mb.step(vm, userInput)) !== mb.EXIT) {
userInput = null;

if (temp !== null) {
output += String.fromCharCode(temp);
}
}
loop = false;
} catch (err) {
if (err === mb.WANTS_INPUT) {
if (userInputArray.length) {
userInput = userInputArray.shift();
continue;
}
output += "Error: Input required";
loop = false;
} else {
throw new OperationError(err);
}
}
}

return output;
}
}

export default Malbolge;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import "./tests/JWTSign";
import "./tests/JWTVerify";
import "./tests/MS";
import "./tests/Magic";
import "./tests/Malbolge";
import "./tests/MorseCode";
import "./tests/NetBIOS";
import "./tests/OTP";
Expand Down
44 changes: 44 additions & 0 deletions tests/operations/tests/Malbolge.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Malbolge tests.
*
* @author Karsten Silkenbäumer [github.com/kassi]
* @copyright Karsten Silkenbäumer 2019
* @license Apache-2.0
*/
import TestRegister from "../TestRegister";

TestRegister.addTests([
{
name: "Malbolge: no program",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "Malbolge",
args: [""],
},
],
},
{
name: "Malbolge: simple program",
input: "('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#\"`CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~| 4XzyTT43Qsqq(Lnmkj\"Fhg${z@>",
expectedOutput: "Hello World!",
recipeConfig: [
{
op: "Malbolge",
args: [""],
},
],
},
{
name: "Malbolge: simple cat with infinite user input",
input: "(=BA#9\"=<;:3y7x54-21q/p-,+*)\"!h%B0/.\n~P<\n<:(8&\n66#\"!~}|{zyxwvu\ngJ%",
expectedOutput: "First line\nSecond Line\nError: Input required",
recipeConfig: [
{
op: "Malbolge",
args: ["First line\nSecond Line\n"],
},
],
},
]);