brainfuckgenerator

This commit is contained in:
Flummi 2017-11-21 17:36:01 +01:00
parent 47a133e862
commit a8c4bc6813
2 changed files with 94 additions and 4 deletions

View File

@ -0,0 +1,80 @@
function gcd(a, b) {
if (b === 0)
return a;
else
return gcd(b, a % b);
}
function inverse_mod(n, m) {
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,8 +1,7 @@
import { generate as bfgen } from "./lib/brainfuck.js";
const vm = require("vm") const vm = require("vm")
, http = require("http") , request = require("request");
, https = require("https")
, request = require("request")
, querystring = require("querystring");
const maxoutput = 400; const maxoutput = 400;
@ -141,4 +140,15 @@ module.exports = bot => {
}); });
} }
}); });
bot._trigger.set("bfgen", {
call: /^\!bfgen .*/i,
level: 100,
active: true,
clients: ["irc", "tg"],
f: e => {
let args = e.message.trim().substring(7);
e.reply(bfgen(args));
}
});
}; };