refactor scripts

This commit is contained in:
Vyacheslav Matyukhin 2022-09-20 00:55:32 +04:00
parent a15aa26439
commit 8e396c44f9
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
5 changed files with 47 additions and 67 deletions

View File

@ -1,15 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
import { SqProject } from "@quri/squiggle-lang"; import { SqProject } from "@quri/squiggle-lang";
import { measure } from "./lib.mjs";
const measure = (cb, times = 1) => {
const t1 = new Date();
for (let i = 1; i <= times; i++) {
cb();
}
const t2 = new Date();
return (t2 - t1) / 1000;
};
const maxP = 5; const maxP = 5;

View File

@ -1,15 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
import { SqProject } from "@quri/squiggle-lang"; import { SqProject } from "@quri/squiggle-lang";
import { measure } from "./lib.mjs";
const measure = (cb, times = 1) => {
const t1 = new Date();
for (let i = 1; i <= times; i++) {
cb();
}
const t2 = new Date();
return (t2 - t1) / 1000;
};
const maxP = 7; const maxP = 7;

View File

@ -0,0 +1,41 @@
import { SqProject } from "@quri/squiggle-lang";
export const measure = (cb, times = 1) => {
const t1 = new Date();
for (let i = 1; i <= times; i++) {
cb();
}
const t2 = new Date();
return (t2 - t1) / 1000;
};
export const red = (str) => `\x1b[31m${str}\x1b[0m`;
export const green = (str) => `\x1b[32m${str}\x1b[0m`;
export const run = (src, { output, sampleCount }) => {
const project = SqProject.create();
if (sampleCount) {
project.setEnvironment({
sampleCount: Number(sampleCount),
xyPointLength: Number(sampleCount),
});
}
project.setSource("main", src);
const time = measure(() => project.run("main"));
const bindings = project.getBindings("main");
const result = project.getResult("main");
if (output) {
console.log("Result:", result.tag, result.value.toString());
console.log("Bindings:", bindings.toString());
}
console.log(
"Time:",
String(time),
result.tag === "Error" ? red(result.tag) : green(result.tag),
result.tag === "Error" ? result.value.toString() : ""
);
};

View File

@ -1,21 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
import { SqProject } from "@quri/squiggle-lang";
import fs from "fs"; import fs from "fs";
import { Command } from "commander"; import { Command } from "commander";
const measure = (cb, times = 1) => { import { run } from "./lib.mjs";
const t1 = new Date();
for (let i = 1; i <= times; i++) {
cb();
}
const t2 = new Date();
return (t2 - t1) / 1000;
};
const red = (str) => `\x1b[31m${str}\x1b[0m`;
const green = (str) => `\x1b[32m${str}\x1b[0m`;
const program = new Command(); const program = new Command();
@ -24,34 +12,11 @@ program.arguments("<string>");
const options = program.parse(process.argv); const options = program.parse(process.argv);
const project = SqProject.create();
const sampleCount = process.env.SAMPLE_COUNT; const sampleCount = process.env.SAMPLE_COUNT;
if (sampleCount) {
project.setEnvironment({
sampleCount: Number(sampleCount),
xyPointLength: Number(sampleCount),
});
}
const src = fs.readFileSync(program.args[0], "utf-8"); const src = fs.readFileSync(program.args[0], "utf-8");
if (!src) { if (!src) {
throw new Error("Expected src"); throw new Error("Expected src");
} }
project.setSource("main", src); run(src, { output: options.output, sampleCount });
const time = measure(() => project.run("main"));
const bindings = project.getBindings("main");
const result = project.getResult("main");
if (options.output) {
console.log("Result:", result.tag, result.value.toString());
console.log("Bindings:", bindings.toString());
}
console.log(
"Time:",
String(time),
result.tag === "Error" ? red(result.tag) : green(result.tag),
result.tag === "Error" ? result.value.toString() : ""
);

View File

@ -1,18 +1,10 @@
#!/usr/bin/env node #!/usr/bin/env node
import { SqProject } from "@quri/squiggle-lang"; import { run } from "./lib.mjs";
const project = SqProject.create();
const src = process.argv[2]; const src = process.argv[2];
if (!src) { if (!src) {
throw new Error("Expected src"); throw new Error("Expected src");
} }
console.log(`Running ${src}`); console.log(`Running ${src}`);
project.setSource("a", src);
project.run("a");
const result = project.getResult("a"); run(src);
console.log(result.tag, result.value.toString());
const bindings = project.getBindings("a");
console.log(bindings.asValue().toString());