From 1d6917f2aa8234727a97a986009c60a0e95039df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karsten=20Silkenb=C3=A4umer?= Date: Sat, 6 Oct 2018 03:38:53 +0200 Subject: [PATCH] Add Malbolge interpreter --- package-lock.json | 5 ++ package.json | 1 + src/core/config/Categories.json | 6 +++ src/core/operations/Malbolge.mjs | 88 ++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 src/core/operations/Malbolge.mjs diff --git a/package-lock.json b/package-lock.json index 55ad6303..85ecfd03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8172,6 +8172,11 @@ "pify": "^3.0.0" } }, + "malbolge-vm": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/malbolge-vm/-/malbolge-vm-1.0.2.tgz", + "integrity": "sha1-AXFAPb2Js4qi10yUzalcqRGo9ms=" + }, "map-age-cleaner": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/map-age-cleaner/-/map-age-cleaner-0.1.3.tgz", diff --git a/package.json b/package.json index cb59db38..d6922d49 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 8235ab10..f685d6b8 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -395,5 +395,11 @@ "Return", "Comment" ] + }, + { + "name": "Esoteric Programming", + "ops": [ + "Malbolge" + ] } ] diff --git a/src/core/operations/Malbolge.mjs b/src/core/operations/Malbolge.mjs new file mode 100644 index 00000000..06b9f303 --- /dev/null +++ b/src/core/operations/Malbolge.mjs @@ -0,0 +1,88 @@ +/** + * @author kassi [kassi@noreply.com] + * @copyright KS 2018 + * @license Apache-2.0 + */ + +import Operation from '../Operation'; + +/** + * 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 + * @param {Object[]} args + * @returns {String} + */ + run (input, args) { + const [userInput] = args; + const EOF = 59048; + + var mb = require('malbolge-vm'), + vm = mb.load(input), + temp, + userInputLines = userInput.split('\n'), + userInputIndex = 0, + output = ''; + + while (true) { + try { + while ((temp = mb.step(vm)) !== mb.EXIT) { + if (temp !== null) { output += String.fromCharCode(temp); } + } + break; + } catch (e) { + if (e === mb.WANTS_INPUT) { + var txt; + if (userInputIndex < userInputLines.length) { + txt = userInputLines[userInputIndex]; + userInputIndex++; + } else { + txt = window.prompt('User input expected', ''); + } + for (var i = 0; i < txt.length; i++) { + mb.step(vm, txt.charCodeAt(i)); + } + mb.step(vm, EOF); + } else { + console.log('Error: ' + e); + } + } + break; + } + + return output; + } +} + +export default Malbolge;