Uwev2/src/inc/trigger/lib/sandbox.mjs
2018-08-31 18:47:40 +02:00

100 lines
2.8 KiB
JavaScript

import rp from "request-promise-native";
const maxoutput = 500;
const hsimports = [
"Control.Applicative", "Control.Arrow", "Control.Monad",
"Control.Monad.Zip", "Data.Bits", "Data.Bool", "Data.Char",
"Data.Complex", "Data.Data", "Data.Eq", "Data.Fixed",
"Data.Foldable", "Data.Function", "Data.Functor",
"Data.Int", "Data.List", "Data.Maybe", "Data.Monoid", "Data.Map",
"Data.Ord", "Data.Set", "Data.String", "Data.Tuple", "Data.Word"
].map(imp => { return `import qualified ${imp}`; }).join("\r\n");
const sandbox = (lang, code) => {
const langs = {
cpp: {
"LanguageChoice": "7",
"Program": "#include <iostream>\r\nint main() { " + code + "}",
"Input": "",
"CompilerArgs": "-Wall -std=c++14 -O2 -o a.out source_file.cpp"
},
hs: {
"LanguageChoice": "11",
"Program": `${hsimports}\r\nmain = ${code}`,
"Input": "",
"CompilerArgs": "-XUnicodeSyntax -XPartialTypeSignatures -o a.out source_file.hs"
},
py: {
"LanguageChoice": "24",
"Program": code,
"Input": "",
"CompilerArgs": "-o a.out source_file.hs"
},
bf: {
"LanguageChoice": "44",
"Program": code,
"Input": "",
"CompilerArgs": ""
},
php: {
"LanguageChoice": "8",
"Program": "<?php\r\n" + code + "\r\n?>",
"Input": "",
"CompilerArgs": ""
},
lua: {
"LanguageChoice": "14",
"Program": code,
"Input": "",
"CompilerArgs": ""
},
bash: {
"LanguageChoice": "38",
"Program": "#!/bin/bash\r\n" + code,
"Input": "",
"CompilerArgs": ""
}
};
return new Promise((resolve, reject) => {
const options = {
url: "http://rextester.com/rundotnet/api",
method: "POST",
body: langs[lang],
json: true
};
rp(options).then(body => {
if (body.Errors) {
if (body.Errors.length > maxoutput)
return reject(`holy fuck, Error wäre viel zu lang! (${body.Errors.length} Zeichen :DDDDDD)`)
return reject(body.Errors);
}
if (body.Result === null)
return reject("Keine Ausgabe :(");
if (body.Result.length > maxoutput)
return reject(`holy fuck, Ausgabe wäre viel zu lang! (${body.Result.length} Zeichen :DDDDDD)`);
if (body.Result.length === 0)
return reject("lol keine Ausgabe");
resolve(body.Result);
})
.catch(err => {
reject("Error!");
})
});
};
const bfgen = text => {
let out = "+".repeat(10) + "[";
let repeat = 0;
for (let i = 0; i < text.length; i++)
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;
};
export { maxoutput, sandbox, bfgen, hsimports };