Rename parameters to imports

This commit is contained in:
Sam Nolan 2022-04-29 20:29:42 +00:00
parent 74df093a42
commit b710289096
5 changed files with 53 additions and 45 deletions

View File

@ -7,8 +7,8 @@ import {
squiggleExpression, squiggleExpression,
bindings, bindings,
samplingParams, samplingParams,
parameters, jsImports,
defaultParameters, defaultImports,
defaultBindings, defaultBindings,
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { NumberShower } from "./NumberShower"; import { NumberShower } from "./NumberShower";
@ -154,7 +154,7 @@ export interface SquiggleChartProps {
/** Bindings of previous variables declared */ /** Bindings of previous variables declared */
bindings?: bindings; bindings?: bindings;
/** JS imported parameters */ /** JS imported parameters */
parameters?: parameters; imports?: jsImports;
} }
const ChartWrapper = styled.div` const ChartWrapper = styled.div`
@ -170,19 +170,14 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = ({
onChange = () => {}, onChange = () => {},
height = 60, height = 60,
bindings = defaultBindings, bindings = defaultBindings,
parameters = defaultParameters, imports = defaultImports,
width = NaN, width = NaN,
}: SquiggleChartProps) => { }: SquiggleChartProps) => {
let samplingInputs: samplingParams = { let samplingInputs: samplingParams = {
sampleCount: sampleCount, sampleCount: sampleCount,
xyPointLength: outputXYPoints, xyPointLength: outputXYPoints,
}; };
let expressionResult = run( let expressionResult = run(squiggleString, bindings, samplingInputs, imports);
squiggleString,
bindings,
samplingInputs,
parameters
);
let internal: JSX.Element; let internal: JSX.Element;
if (expressionResult.tag === "Ok") { if (expressionResult.tag === "Ok") {
let expression = expressionResult.value; let expression = expressionResult.value;

View File

@ -5,13 +5,14 @@ import { CodeEditor } from "./CodeEditor";
import styled from "styled-components"; import styled from "styled-components";
import type { import type {
squiggleExpression, squiggleExpression,
samplingParams,
bindings, bindings,
parameters, jsImports,
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { import {
runPartial, runPartial,
errorValueToString, errorValueToString,
defaultParameters, defaultImports,
defaultBindings, defaultBindings,
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { ErrorBox } from "./ErrorBox"; import { ErrorBox } from "./ErrorBox";
@ -39,8 +40,8 @@ export interface SquiggleEditorProps {
width: number; width: number;
/** Previous variable declarations */ /** Previous variable declarations */
bindings: bindings; bindings: bindings;
/** JS Imported parameters */ /** JS Imports */
parameters: parameters; imports: jsImports;
} }
const Input = styled.div` const Input = styled.div`
@ -62,7 +63,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
onChange, onChange,
environment, environment,
bindings = defaultBindings, bindings = defaultBindings,
parameters = defaultParameters, imports = defaultImports,
}: SquiggleEditorProps) => { }: SquiggleEditorProps) => {
let [expression, setExpression] = React.useState(initialSquiggleString); let [expression, setExpression] = React.useState(initialSquiggleString);
return ( return (
@ -89,7 +90,7 @@ export let SquiggleEditor: React.FC<SquiggleEditorProps> = ({
environment={environment} environment={environment}
onChange={onChange} onChange={onChange}
bindings={bindings} bindings={bindings}
parameters={parameters} imports={imports}
/> />
</div> </div>
); );
@ -148,18 +149,29 @@ export interface SquigglePartialProps {
width: number; width: number;
/** Previously declared variables */ /** Previously declared variables */
bindings?: bindings; bindings?: bindings;
/** Parameters imported from js */ /** Variables imported from js */
parameters?: parameters; imports?: jsImports;
} }
export let SquigglePartial: React.FC<SquigglePartialProps> = ({ export let SquigglePartial: React.FC<SquigglePartialProps> = ({
initialSquiggleString = "", initialSquiggleString = "",
onChange, onChange,
bindings = defaultBindings, bindings = defaultBindings,
parameters = defaultParameters, sampleCount = 1000,
outputXYPoints = 1000,
imports = defaultImports,
}: SquigglePartialProps) => { }: SquigglePartialProps) => {
let samplingInputs: samplingParams = {
sampleCount: sampleCount,
xyPointLength: outputXYPoints,
};
let [expression, setExpression] = React.useState(initialSquiggleString); let [expression, setExpression] = React.useState(initialSquiggleString);
let squiggleResult = runPartial(expression, bindings); let squiggleResult = runPartial(
expression,
bindings,
samplingInputs,
imports
);
if (squiggleResult.tag == "Ok") { if (squiggleResult.tag == "Ok") {
if (onChange) onChange(squiggleResult.value); if (onChange) onChange(squiggleResult.value);
} }

View File

@ -68,13 +68,13 @@ describe("Partials", () => {
}); });
}); });
describe("Parameters", () => { describe("JS Imports", () => {
test("Can pass parameters into partials and cells", () => { test("Can pass parameters into partials and cells", () => {
let bindings = testRunPartial(`y = $x + 2`, defaultBindings, { x: 1 }); let bindings = testRunPartial(`y = $x + 2`, defaultBindings, { x: 1 });
let bindings2 = testRunPartial(`z = y + $a`, bindings, { a: 3 }); let bindings2 = testRunPartial(`z = y + $a`, bindings, { a: 3 });
expect(testRun(`z`, bindings2)).toEqual({ expect(testRun(`z`, bindings2)).toEqual({
tag: "number", tag: "number",
value: 17, value: 6,
}); });
}); });
test("Complicated deep parameters", () => { test("Complicated deep parameters", () => {

View File

@ -4,12 +4,15 @@ import {
bindings, bindings,
squiggleExpression, squiggleExpression,
errorValueToString, errorValueToString,
defaultImports,
defaultBindings,
jsImports,
} from "../../src/js/index"; } from "../../src/js/index";
export function testRun( export function testRun(
x: string, x: string,
bindings = {}, bindings: bindings = defaultBindings,
parameters = {} imports: jsImports = defaultImports
): squiggleExpression { ): squiggleExpression {
let squiggleResult = run( let squiggleResult = run(
x, x,
@ -18,9 +21,8 @@ export function testRun(
sampleCount: 1000, sampleCount: 1000,
xyPointLength: 100, xyPointLength: 100,
}, },
parameters imports
); );
// return squiggleResult.value
if (squiggleResult.tag === "Ok") { if (squiggleResult.tag === "Ok") {
return squiggleResult.value; return squiggleResult.value;
} else { } else {
@ -34,8 +36,8 @@ export function testRun(
export function testRunPartial( export function testRunPartial(
x: string, x: string,
bindings: bindings = {}, bindings: bindings = defaultBindings,
parameters = {} imports: jsImports = defaultImports
): bindings { ): bindings {
let squiggleResult = runPartial( let squiggleResult = runPartial(
x, x,
@ -44,7 +46,7 @@ export function testRunPartial(
sampleCount: 1000, sampleCount: 1000,
xyPointLength: 100, xyPointLength: 100,
}, },
parameters imports
); );
if (squiggleResult.tag === "Ok") { if (squiggleResult.tag === "Ok") {
return squiggleResult.value; return squiggleResult.value;

View File

@ -16,7 +16,7 @@ export type {
samplingParams, samplingParams,
errorValue, errorValue,
externalBindings as bindings, externalBindings as bindings,
parameters, jsImports,
}; };
import { import {
jsValueToBinding, jsValueToBinding,
@ -28,7 +28,7 @@ import {
import { result, resultMap, tag, tagged } from "./types"; import { result, resultMap, tag, tagged } from "./types";
import { Distribution } from "./distribution"; import { Distribution } from "./distribution";
export { Distribution, squiggleExpression, result }; export { Distribution, squiggleExpression, result, resultMap };
export let defaultSamplingInputs: samplingParams = { export let defaultSamplingInputs: samplingParams = {
sampleCount: 10000, sampleCount: 10000,
@ -39,16 +39,16 @@ export function run(
squiggleString: string, squiggleString: string,
bindings?: externalBindings, bindings?: externalBindings,
samplingInputs?: samplingParams, samplingInputs?: samplingParams,
parameters?: parameters imports?: jsImports
): result<squiggleExpression, errorValue> { ): result<squiggleExpression, errorValue> {
let b = bindings ? bindings : defaultBindings; let b = bindings ? bindings : defaultBindings;
let p = parameters ? parameters : defaultParameters; let i = imports ? imports : defaultImports;
let si: samplingParams = samplingInputs let si: samplingParams = samplingInputs
? samplingInputs ? samplingInputs
: defaultSamplingInputs; : defaultSamplingInputs;
let result: result<expressionValue, errorValue> = let result: result<expressionValue, errorValue> =
evaluateUsingExternalBindings(squiggleString, mergeParameters(b, p)); evaluateUsingExternalBindings(squiggleString, mergeImports(b, i));
return resultMap(result, (x) => createTsExport(x, si)); return resultMap(result, (x) => createTsExport(x, si));
} }
@ -57,33 +57,33 @@ export function runPartial(
squiggleString: string, squiggleString: string,
bindings?: externalBindings, bindings?: externalBindings,
_samplingInputs?: samplingParams, _samplingInputs?: samplingParams,
parameters?: parameters imports?: jsImports
): result<externalBindings, errorValue> { ): result<externalBindings, errorValue> {
let b = bindings ? bindings : defaultBindings; let b = bindings ? bindings : defaultBindings;
let p = parameters ? parameters : defaultParameters; let i = imports ? imports : defaultImports;
return evaluatePartialUsingExternalBindings( return evaluatePartialUsingExternalBindings(
squiggleString, squiggleString,
mergeParameters(b, p) mergeImports(b, i)
); );
} }
function mergeParameters( function mergeImports(
bindings: externalBindings, bindings: externalBindings,
parameters: parameters imports: jsImports
): externalBindings { ): externalBindings {
let transformedParameters = Object.fromEntries( let transformedImports = Object.fromEntries(
Object.entries(parameters).map(([key, value]) => [ Object.entries(imports).map(([key, value]) => [
"$" + key, "$" + key,
jsValueToBinding(value), jsValueToBinding(value),
]) ])
); );
return _.merge(bindings, transformedParameters); return _.merge(bindings, transformedImports);
} }
type parameters = { [key: string]: jsValue }; type jsImports = { [key: string]: jsValue };
export let defaultParameters: parameters = {}; export let defaultImports: jsImports = {};
export let defaultBindings: externalBindings = {}; export let defaultBindings: externalBindings = {};
function createTsExport( function createTsExport(
@ -146,4 +146,3 @@ function createTsExport(
return tag("symbol", x.value); return tag("symbol", x.value);
} }
} }