From fd4137b596d03698d635582db077eb5b31360520 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sun, 18 Sep 2022 18:32:53 +0400 Subject: [PATCH] run-file.js improvements --- packages/squiggle-lang/scripts/run-file.js | 38 ------------- packages/squiggle-lang/scripts/run-file.mjs | 60 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 38 deletions(-) delete mode 100755 packages/squiggle-lang/scripts/run-file.js create mode 100755 packages/squiggle-lang/scripts/run-file.mjs diff --git a/packages/squiggle-lang/scripts/run-file.js b/packages/squiggle-lang/scripts/run-file.js deleted file mode 100755 index f697f909..00000000 --- a/packages/squiggle-lang/scripts/run-file.js +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env node -const s = require("@quri/squiggle-lang"); -const fs = require("fs"); - -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 project = s.SqProject.create(); -const sampleCount = process.env.SAMPLE_COUNT; -if (sampleCount) { - project.setEnvironment({ - sampleCount, - xyPointLength: sampleCount, - }); -} - -const src = fs.readFileSync(process.argv[2], "utf-8"); -if (!src) { - throw new Error("Expected src"); -} -console.log(`Running ${src}`); -project.setSource("a", src); - -const t = measure(() => project.run("a")); -console.log(`Time: ${t}`); - -const result = project.getResult("a"); -console.log("Result:", result.tag, result.value.toString()); - -const bindings = project.getBindings("a"); -console.log("Bindings:", bindings.toString()); diff --git a/packages/squiggle-lang/scripts/run-file.mjs b/packages/squiggle-lang/scripts/run-file.mjs new file mode 100755 index 00000000..65d67cdc --- /dev/null +++ b/packages/squiggle-lang/scripts/run-file.mjs @@ -0,0 +1,60 @@ +#!/usr/bin/env node +import { SqProject } from "@quri/squiggle-lang"; +import fs from "fs"; + +import { Command } from "commander"; + +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 red = (s) => `\x1b[31m${s}\x1b[0m`; +const green = (s) => `\x1b[32m${s}\x1b[0m`; + +const program = new Command(); + +program.option("-t, --time"); +program.option("-o, --output"); +program.arguments(""); + +const options = program.parse(process.argv); + +const project = SqProject.create(); +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"); +if (!src) { + throw new Error("Expected src"); +} + +project.setSource("main", src); +const t = 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()); +} + +if (options.time) { + console.log( + "Time:", + String(t), + result.tag === "Error" ? red(result.tag) : green(result.tag), + result.tag === "Error" ? result.value.toString() : "" + ); +}