refactor scripts
This commit is contained in:
parent
a15aa26439
commit
8e396c44f9
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
41
packages/squiggle-lang/scripts/lib.mjs
Normal file
41
packages/squiggle-lang/scripts/lib.mjs
Normal 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() : ""
|
||||||
|
);
|
||||||
|
};
|
|
@ -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() : ""
|
|
||||||
);
|
|
||||||
|
|
|
@ -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());
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user