bfgenerator

This commit is contained in:
Flummi 2017-11-21 20:29:30 +01:00
parent a8c4bc6813
commit e92dbbbcb0
2 changed files with 15 additions and 81 deletions

View File

@ -1,80 +1,13 @@
function gcd(a, b) { function bfgen(text) {
if (b === 0) let out = "+".repeat(10) + "[";
return a; let repeat = 0;
else for (let i = 0; i < text.length; i++)
return gcd(b, a % b); repeat = text.charCodeAt(i) - text.charCodeAt(i) % 10;
out += repeat > 10 ? '>' + "+".repeat(repeat / 10) : null;
out += "<".repeat(out.split(">").length - 1) + "-]";
for (let i = 0; i < text.length; i++)
out += ">" + "+".repeat(text.charCodeAt(i) % 10) + ".";
return out;
} }
function inverse_mod(n, m) { export default { bfgen };
var inv1 = 1,
inv2 = 0,
tmp;
while (m) {
tmp = inv1;
inv1 = inv2;
inv2 = tmp - inv2 * (n / m | 0);
tmp = n;
n = m;
m = tmp % m;
}
return inv1;
}
function shortest_str(arr) {
var min = 0;
for (var i = 1; i < arr.length; i++)
if (arr[i].length < arr[min].length)
min = i;
return min;
}
var map = [],
plus_map = [""],
minus_map = [""],
iter = true,
repeat = 2,
start;
for (var i = 1; i < 256; i++) {
plus_map[i] = plus_map[i - 1] + "+";
minus_map[i] = minus_map[i - 1] + "-";
}
for (var x = 0; x < 256; x++) {
map[x] = [];
for (var y = 0; y < 256; y++) {
var delta = y - x;
if (delta > 128)
delta -= 256;
if (delta < -128)
delta += 256;
if (delta >= 0)
map[x][y] = plus_map[delta];
else
map[x][y] = minus_map[-delta];
}
}
function generate(str) {
var last = 0,
len = str.length,
char_map,
result = "",
memory = [0],
index = 0;
for (var i = 0; i < len; i++) {
var
chr = str.charCodeAt(i) & 255,
options = [
">" + map[0][chr],
map[last][chr],
],
shortest;
shortest = shortest_str(options);
result += options[shortest] + ".";
last = chr;
}
return result;
}
export default { generate };

View File

@ -1,4 +1,4 @@
import { generate as bfgen } from "./lib/brainfuck.js"; import { bfgen } from "./lib/brainfuck.js";
const vm = require("vm") const vm = require("vm")
, request = require("request"); , request = require("request");
@ -42,7 +42,7 @@ module.exports = bot => {
}, },
hs: { hs: {
"LanguageChoice": "11", "LanguageChoice": "11",
"Program": "main = " + code, "Program": "{-# LANGUAGE UnicodeSyntax #-}\r\nmain = " + code,
"Input": "", "Input": "",
"CompilerArgs": "-o a.out source_file.hs" "CompilerArgs": "-o a.out source_file.hs"
}, },
@ -148,7 +148,8 @@ module.exports = bot => {
clients: ["irc", "tg"], clients: ["irc", "tg"],
f: e => { f: e => {
let args = e.message.trim().substring(7); let args = e.message.trim().substring(7);
e.reply(bfgen(args)); let output = bfgen(args);
e.reply(output.length > maxoutput ? `holy fuck, Ausgabe wäre viel zu lang! (${output.length} Zeichen :DDDDDD)` : output);
} }
}); });
}; };