diff --git a/packages/components/src/components/SquiggleChart.tsx b/packages/components/src/components/SquiggleChart.tsx index 235a4cee..be65d425 100644 --- a/packages/components/src/components/SquiggleChart.tsx +++ b/packages/components/src/components/SquiggleChart.tsx @@ -7,8 +7,8 @@ import { squiggleExpression, bindings, samplingParams, - parameters, - defaultParameters, + jsImports, + defaultImports, defaultBindings, } from "@quri/squiggle-lang"; import { NumberShower } from "./NumberShower"; @@ -154,7 +154,7 @@ export interface SquiggleChartProps { /** Bindings of previous variables declared */ bindings?: bindings; /** JS imported parameters */ - parameters?: parameters; + imports?: jsImports; } const ChartWrapper = styled.div` @@ -170,19 +170,14 @@ export const SquiggleChart: React.FC = ({ onChange = () => {}, height = 60, bindings = defaultBindings, - parameters = defaultParameters, + imports = defaultImports, width = NaN, }: SquiggleChartProps) => { let samplingInputs: samplingParams = { sampleCount: sampleCount, xyPointLength: outputXYPoints, }; - let expressionResult = run( - squiggleString, - bindings, - samplingInputs, - parameters - ); + let expressionResult = run(squiggleString, bindings, samplingInputs, imports); let internal: JSX.Element; if (expressionResult.tag === "Ok") { let expression = expressionResult.value; diff --git a/packages/components/src/components/SquiggleEditor.tsx b/packages/components/src/components/SquiggleEditor.tsx index 67f2e73b..697e9426 100644 --- a/packages/components/src/components/SquiggleEditor.tsx +++ b/packages/components/src/components/SquiggleEditor.tsx @@ -5,13 +5,14 @@ import { CodeEditor } from "./CodeEditor"; import styled from "styled-components"; import type { squiggleExpression, + samplingParams, bindings, - parameters, + jsImports, } from "@quri/squiggle-lang"; import { runPartial, errorValueToString, - defaultParameters, + defaultImports, defaultBindings, } from "@quri/squiggle-lang"; import { ErrorBox } from "./ErrorBox"; @@ -39,8 +40,8 @@ export interface SquiggleEditorProps { width: number; /** Previous variable declarations */ bindings: bindings; - /** JS Imported parameters */ - parameters: parameters; + /** JS Imports */ + imports: jsImports; } const Input = styled.div` @@ -62,7 +63,7 @@ export let SquiggleEditor: React.FC = ({ onChange, environment, bindings = defaultBindings, - parameters = defaultParameters, + imports = defaultImports, }: SquiggleEditorProps) => { let [expression, setExpression] = React.useState(initialSquiggleString); return ( @@ -89,7 +90,7 @@ export let SquiggleEditor: React.FC = ({ environment={environment} onChange={onChange} bindings={bindings} - parameters={parameters} + imports={imports} /> ); @@ -148,18 +149,29 @@ export interface SquigglePartialProps { width: number; /** Previously declared variables */ bindings?: bindings; - /** Parameters imported from js */ - parameters?: parameters; + /** Variables imported from js */ + imports?: jsImports; } export let SquigglePartial: React.FC = ({ initialSquiggleString = "", onChange, bindings = defaultBindings, - parameters = defaultParameters, + sampleCount = 1000, + outputXYPoints = 1000, + imports = defaultImports, }: SquigglePartialProps) => { + let samplingInputs: samplingParams = { + sampleCount: sampleCount, + xyPointLength: outputXYPoints, + }; let [expression, setExpression] = React.useState(initialSquiggleString); - let squiggleResult = runPartial(expression, bindings); + let squiggleResult = runPartial( + expression, + bindings, + samplingInputs, + imports + ); if (squiggleResult.tag == "Ok") { if (onChange) onChange(squiggleResult.value); } diff --git a/packages/squiggle-lang/__tests__/TS/JS_test.ts b/packages/squiggle-lang/__tests__/TS/JS_test.ts index 004d715e..76871fc8 100644 --- a/packages/squiggle-lang/__tests__/TS/JS_test.ts +++ b/packages/squiggle-lang/__tests__/TS/JS_test.ts @@ -68,13 +68,13 @@ describe("Partials", () => { }); }); -describe("Parameters", () => { +describe("JS Imports", () => { test("Can pass parameters into partials and cells", () => { let bindings = testRunPartial(`y = $x + 2`, defaultBindings, { x: 1 }); let bindings2 = testRunPartial(`z = y + $a`, bindings, { a: 3 }); expect(testRun(`z`, bindings2)).toEqual({ tag: "number", - value: 17, + value: 6, }); }); test("Complicated deep parameters", () => { diff --git a/packages/squiggle-lang/__tests__/TS/TestHelpers.ts b/packages/squiggle-lang/__tests__/TS/TestHelpers.ts index 3bbf42d8..d9d8444f 100644 --- a/packages/squiggle-lang/__tests__/TS/TestHelpers.ts +++ b/packages/squiggle-lang/__tests__/TS/TestHelpers.ts @@ -4,12 +4,15 @@ import { bindings, squiggleExpression, errorValueToString, + defaultImports, + defaultBindings, + jsImports, } from "../../src/js/index"; export function testRun( x: string, - bindings = {}, - parameters = {} + bindings: bindings = defaultBindings, + imports: jsImports = defaultImports ): squiggleExpression { let squiggleResult = run( x, @@ -18,9 +21,8 @@ export function testRun( sampleCount: 1000, xyPointLength: 100, }, - parameters + imports ); - // return squiggleResult.value if (squiggleResult.tag === "Ok") { return squiggleResult.value; } else { @@ -34,8 +36,8 @@ export function testRun( export function testRunPartial( x: string, - bindings: bindings = {}, - parameters = {} + bindings: bindings = defaultBindings, + imports: jsImports = defaultImports ): bindings { let squiggleResult = runPartial( x, @@ -44,7 +46,7 @@ export function testRunPartial( sampleCount: 1000, xyPointLength: 100, }, - parameters + imports ); if (squiggleResult.tag === "Ok") { return squiggleResult.value; diff --git a/packages/squiggle-lang/src/js/index.ts b/packages/squiggle-lang/src/js/index.ts index 04d6050a..ce4d9428 100644 --- a/packages/squiggle-lang/src/js/index.ts +++ b/packages/squiggle-lang/src/js/index.ts @@ -16,7 +16,7 @@ export type { samplingParams, errorValue, externalBindings as bindings, - parameters, + jsImports, }; import { jsValueToBinding, @@ -28,7 +28,7 @@ import { import { result, resultMap, tag, tagged } from "./types"; import { Distribution } from "./distribution"; -export { Distribution, squiggleExpression, result }; +export { Distribution, squiggleExpression, result, resultMap }; export let defaultSamplingInputs: samplingParams = { sampleCount: 10000, @@ -39,16 +39,16 @@ export function run( squiggleString: string, bindings?: externalBindings, samplingInputs?: samplingParams, - parameters?: parameters + imports?: jsImports ): result { let b = bindings ? bindings : defaultBindings; - let p = parameters ? parameters : defaultParameters; + let i = imports ? imports : defaultImports; let si: samplingParams = samplingInputs ? samplingInputs : defaultSamplingInputs; let result: result = - evaluateUsingExternalBindings(squiggleString, mergeParameters(b, p)); + evaluateUsingExternalBindings(squiggleString, mergeImports(b, i)); return resultMap(result, (x) => createTsExport(x, si)); } @@ -57,33 +57,33 @@ export function runPartial( squiggleString: string, bindings?: externalBindings, _samplingInputs?: samplingParams, - parameters?: parameters + imports?: jsImports ): result { let b = bindings ? bindings : defaultBindings; - let p = parameters ? parameters : defaultParameters; + let i = imports ? imports : defaultImports; return evaluatePartialUsingExternalBindings( squiggleString, - mergeParameters(b, p) + mergeImports(b, i) ); } -function mergeParameters( +function mergeImports( bindings: externalBindings, - parameters: parameters + imports: jsImports ): externalBindings { - let transformedParameters = Object.fromEntries( - Object.entries(parameters).map(([key, value]) => [ + let transformedImports = Object.fromEntries( + Object.entries(imports).map(([key, value]) => [ "$" + key, 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 = {}; function createTsExport( @@ -146,4 +146,3 @@ function createTsExport( return tag("symbol", x.value); } } -