feat: Code reorg, add cli

This commit is contained in:
NunoSempere 2022-04-15 12:12:38 -04:00
parent 13550fbdda
commit 2ebba9f79d
4 changed files with 114 additions and 69 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
## Vim artifacts
.index.js.swp

34
cli.js Normal file
View File

@ -0,0 +1,34 @@
import readline from "readline";
import { transformer } from "./index.js";
let VERBOSE = true;
let print = (x) => {
if (VERBOSE) {
console.log(x);
}
};
let runTransformer = (string) => {
console.log(`Received: ${string}`);
console.group();
print("");
let result = transformer(string, print);
print("");
console.groupEnd();
console.log(`=> ${result}`);
print("-".repeat(52));
console.log("");
};
let cliWrapper = async (message, callback) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(message, async (answer) => {
rl.close();
await callback(answer);
});
};
cliWrapper("Model: ", runTransformer);

View File

@ -203,7 +203,7 @@ let customToStringHandler = (node, options) => {
} }
}; };
let preprocessor = (string) => { let preprocessor = (string, print = console.log) => {
// work in progress, currently not working // work in progress, currently not working
let regex = /([\d]+\.?[\d]*|\.[\d]+) to ([\d]+\.?[\d]*|\.[\d]+)/g; let regex = /([\d]+\.?[\d]*|\.[\d]+) to ([\d]+\.?[\d]*|\.[\d]+)/g;
function replacer(match, p1, p2) { function replacer(match, p1, p2) {
@ -221,8 +221,8 @@ let preprocessor = (string) => {
}; };
// preprocessor("1.2 to 10.5 * 1.1 to 20 * 1 to 2.5 * 1 to 5"); // preprocessor("1.2 to 10.5 * 1.1 to 20 * 1 to 2.5 * 1 to 5");
let transformer = (string) => { export function transformer(string, print = console.log) {
string = preprocessor(string); string = preprocessor(string, print);
let transformerOutput = transformerInner(string); let transformerOutput = transformerInner(string);
let stringNew = transformerOutput.toString(); let stringNew = transformerOutput.toString();
while (stringNew != string) { while (stringNew != string) {
@ -234,70 +234,4 @@ let transformer = (string) => {
stringNew = transformerOutput.toString(); stringNew = transformerOutput.toString();
} }
return stringNew; return stringNew;
};
let testTransformer = (string) => {
print(string);
console.group();
print("");
let result = transformer(string);
print("");
console.groupEnd();
print(`=> ${result}`);
print("-".repeat(52));
print("");
};
// Defs
let tests1 = [
`lognormal(1,10) * lognormal(1,10) + lognormal(1,10)`,
`lognormal(1,10) * lognormal(1,10) * lognormal(1,10)`,
`1 to 10 * lognormal(1, 10)`,
`lognormal(1, 10) * 1 to 20`,
`1 to 20 * 100 to 1000`,
`(lognormal(1,10) / lognormal(1,10)) + lognormal(1,10)`,
`lognormal(1,10) * lognormal(1,10) / lognormal(1,10)`,
`1 to 10 * lognormal(1, 10) / 1 to 10`,
`lognormal(1, 10) * 1 to 20 / 1 to 20`,
`1 to 20 * 100 to 1000 / 1 to 100`,
];
let runTests1 = false;
if (runTests1) {
console.clear();
tests.forEach((test) => testTransformer(test));
}
let tests2 = [
`3 * lognormal(1,10)`,
`lognormal(1,10) * 4`,
`lognormal(1, 10) / 3`,
`3 / lognormal(1, 10)`,
`lognormal(1,10) * lognormal(1/10) / 3`,
`lognormal(1, 10) / (1 to 3)`,
];
let runTests2 = false;
if (runTests2) {
console.clear();
tests2.forEach((test) => testTransformer(test));
}
let tests3 = [
`(lognormal(1,10))`,
`lognormal(1,10) * (lognormal(1, 10) * 3) / (4 * lognormal(1,10))`,
];
let runTests3 = false;
if (runTests3) {
console.clear();
tests3.forEach((test) => testTransformer(test));
}
let tests4 = [
`(1 to 2) * 3 * lognormal(1,10) * (1/lognormal(1,10)) / (1 to 10)`,
`lognormal(2.4451858789480823, 10.002219515733781) * lognormal(-1, 10) `,
];
let runTests4 = true;
if (runTests4) {
console.clear();
tests4.forEach((test) => testTransformer(test));
} }

74
tests.js Normal file
View File

@ -0,0 +1,74 @@
import { transformer } from "./index.js";
let VERBOSE = false;
let print = (x) => {
if (VERBOSE) {
console.log(x);
}
};
let testTransformer = (string) => {
console.log(string);
console.group();
print("");
let result = transformer(string, print);
print("");
console.groupEnd();
console.log(`=> ${result}`);
print("-".repeat(52));
console.log("");
};
// Defs
let tests1 = [
`lognormal(1,10) * lognormal(1,10) + lognormal(1,10)`,
`lognormal(1,10) * lognormal(1,10) * lognormal(1,10)`,
`1 to 10 * lognormal(1, 10)`,
`lognormal(1, 10) * 1 to 20`,
`1 to 20 * 100 to 1000`,
`(lognormal(1,10) / lognormal(1,10)) + lognormal(1,10)`,
`lognormal(1,10) * lognormal(1,10) / lognormal(1,10)`,
`1 to 10 * lognormal(1, 10) / 1 to 10`,
`lognormal(1, 10) * 1 to 20 / 1 to 20`,
`1 to 20 * 100 to 1000 / 1 to 100`,
];
let runTests1 = false;
if (runTests1) {
console.clear();
tests.forEach((test) => testTransformer(test));
}
let tests2 = [
`3 * lognormal(1,10)`,
`lognormal(1,10) * 4`,
`lognormal(1, 10) / 3`,
`3 / lognormal(1, 10)`,
`lognormal(1,10) * lognormal(1/10) / 3`,
`lognormal(1, 10) / (1 to 3)`,
];
let runTests2 = false;
if (runTests2) {
console.clear();
tests2.forEach((test) => testTransformer(test));
}
let tests3 = [
`(lognormal(1,10))`,
`lognormal(1,10) * (lognormal(1, 10) * 3) / (4 * lognormal(1,10))`,
];
let runTests3 = false;
if (runTests3) {
console.clear();
tests3.forEach((test) => testTransformer(test));
}
let tests4 = [
`(1 to 2) * 3 * lognormal(1,10) * (1/lognormal(1,10)) / (1 to 10)`,
`lognormal(2.4451858789480823, 10.002219515733781) * lognormal(-1, 10) `,
];
let runTests4 = true;
if (runTests4) {
console.clear();
tests4.forEach((test) => testTransformer(test));
}