diff --git a/packages/components/src/components/SquiggleViewer/utils.ts b/packages/components/src/components/SquiggleViewer/utils.ts index 4991672b99..bf50cf359e 100644 --- a/packages/components/src/components/SquiggleViewer/utils.ts +++ b/packages/components/src/components/SquiggleViewer/utils.ts @@ -66,9 +66,9 @@ export function useGetSubvalueByPath() { let nextValue: SqValue | undefined; - if (currentTag === "Array" && pathEdgeType === "arrayIndex") { + if (currentTag === "Array" && pathEdgeType === "index") { nextValue = currentValue.value.getValues()[pathEdge.value.value]; - } else if (currentTag === "Dict" && pathEdgeType === "dictKey") { + } else if (currentTag === "Dict" && pathEdgeType === "key") { nextValue = currentValue.value.get(pathEdge.value.value); } else if ( currentTag === "TableChart" && diff --git a/packages/components/src/stories/SquiggleChart.stories.tsx b/packages/components/src/stories/SquiggleChart.stories.tsx index 40ac596310..17368a02e7 100644 --- a/packages/components/src/stories/SquiggleChart.stories.tsx +++ b/packages/components/src/stories/SquiggleChart.stories.tsx @@ -50,7 +50,7 @@ export const RootPathOverride: Story = { code: "{foo: 35 to 50, bar: [1,2,3]}", rootPathOverride: new SqValuePath({ root: "result", - items: [SqValuePathEdge.fromDictKey("bar")], + items: [SqValuePathEdge.fromKey("bar")], }), }, }; diff --git a/packages/components/src/stories/SquiggleChart/Basic.stories.tsx b/packages/components/src/stories/SquiggleChart/Basic.stories.tsx index f05457606a..3c1d636f70 100644 --- a/packages/components/src/stories/SquiggleChart/Basic.stories.tsx +++ b/packages/components/src/stories/SquiggleChart/Basic.stories.tsx @@ -43,10 +43,7 @@ export const WithPathOverride: Story = { `, rootPathOverride: new SqValuePath({ root: "bindings", - items: [ - SqValuePathEdge.fromDictKey("foo"), - SqValuePathEdge.fromDictKey("bar"), - ], + items: [SqValuePathEdge.fromKey("foo"), SqValuePathEdge.fromKey("bar")], }), }, }; diff --git a/packages/squiggle-lang/__tests__/SqValue/SqValuePath_test.ts b/packages/squiggle-lang/__tests__/SqValue/SqValuePath_test.ts index 29fdee7a2c..f8a005d63f 100644 --- a/packages/squiggle-lang/__tests__/SqValue/SqValuePath_test.ts +++ b/packages/squiggle-lang/__tests__/SqValue/SqValuePath_test.ts @@ -1,9 +1,9 @@ import { SqValuePath, SqValuePathEdge } from "../../src/index.js"; describe("SqValuePathEdge", () => { - test("fromDictKey creates a string item", () => { - const item = SqValuePathEdge.fromDictKey("test"); - expect(item.value).toEqual({ type: "dictKey", value: "test" }); + test("fromKey creates a string item", () => { + const item = SqValuePathEdge.fromKey("test"); + expect(item.value).toEqual({ type: "key", value: "test" }); }); }); @@ -11,8 +11,8 @@ describe("SqValuePath", () => { const path = new SqValuePath({ root: "bindings", items: [ - SqValuePathEdge.fromDictKey("foo"), - SqValuePathEdge.fromArrayIndex(2), + SqValuePathEdge.fromKey("foo"), + SqValuePathEdge.fromIndex(2), SqValuePathEdge.fromCalculator(), SqValuePathEdge.fromCellAddress(1, 2), ], @@ -22,8 +22,8 @@ describe("SqValuePath", () => { const path2 = new SqValuePath({ root: "bindings", items: [ - SqValuePathEdge.fromDictKey("foo"), - SqValuePathEdge.fromArrayIndex(2), + SqValuePathEdge.fromKey("foo"), + SqValuePathEdge.fromIndex(2), SqValuePathEdge.fromCalculator(), SqValuePathEdge.fromCellAddress(1, 2), ], @@ -34,12 +34,12 @@ describe("SqValuePath", () => { test("extend()", () => { const path = new SqValuePath({ root: "bindings", - items: [SqValuePathEdge.fromDictKey("foo")], + items: [SqValuePathEdge.fromKey("foo")], }); - const extendedPath = path.extend(SqValuePathEdge.fromArrayIndex(2)); + const extendedPath = path.extend(SqValuePathEdge.fromIndex(2)); expect(extendedPath.items.length).toBe(2); expect(extendedPath.items[1].value).toEqual({ - type: "arrayIndex", + type: "index", value: 2, }); }); @@ -48,14 +48,11 @@ describe("SqValuePath", () => { test("path fully contains a shorter path", () => { const basePath = new SqValuePath({ root: "bindings", - items: [ - SqValuePathEdge.fromDictKey("foo"), - SqValuePathEdge.fromArrayIndex(2), - ], + items: [SqValuePathEdge.fromKey("foo"), SqValuePathEdge.fromIndex(2)], }); const subPath = new SqValuePath({ root: "bindings", - items: [SqValuePathEdge.fromDictKey("foo")], + items: [SqValuePathEdge.fromKey("foo")], }); expect(basePath.contains(subPath)).toBe(true); }); @@ -63,20 +60,20 @@ describe("SqValuePath", () => { test("path does not contain longer path", () => { const basePath = new SqValuePath({ root: "bindings", - items: [SqValuePathEdge.fromDictKey("foo")], + items: [SqValuePathEdge.fromKey("foo")], }); - const longerPath = basePath.extend(SqValuePathEdge.fromArrayIndex(2)); + const longerPath = basePath.extend(SqValuePathEdge.fromIndex(2)); expect(basePath.contains(longerPath)).toBe(false); }); test("path does not contain different path", () => { const path1 = new SqValuePath({ root: "bindings", - items: [SqValuePathEdge.fromDictKey("foo")], + items: [SqValuePathEdge.fromKey("foo")], }); const path2 = new SqValuePath({ root: "imports", - items: [SqValuePathEdge.fromDictKey("bar")], + items: [SqValuePathEdge.fromKey("bar")], }); expect(path1.contains(path2)).toBe(false); }); @@ -96,11 +93,11 @@ describe("SqValuePath", () => { test("equal paths contain each other", () => { const path1 = new SqValuePath({ root: "bindings", - items: [SqValuePathEdge.fromDictKey("test")], + items: [SqValuePathEdge.fromKey("test")], }); const path2 = new SqValuePath({ root: "bindings", - items: [SqValuePathEdge.fromDictKey("test")], + items: [SqValuePathEdge.fromKey("test")], }); expect(path1.contains(path2)).toBe(true); expect(path2.contains(path1)).toBe(true); diff --git a/packages/squiggle-lang/src/public/SqValue/SqArray.ts b/packages/squiggle-lang/src/public/SqValue/SqArray.ts index 6b58c65f37..6d36f1e2b9 100644 --- a/packages/squiggle-lang/src/public/SqValue/SqArray.ts +++ b/packages/squiggle-lang/src/public/SqValue/SqArray.ts @@ -11,7 +11,7 @@ export class SqArray { getValues() { return this._value.map((v, i) => - wrapValue(v, this.context?.extend(SqValuePathEdge.fromArrayIndex(i))) + wrapValue(v, this.context?.extend(SqValuePathEdge.fromIndex(i))) ); } } diff --git a/packages/squiggle-lang/src/public/SqValue/SqDict.ts b/packages/squiggle-lang/src/public/SqValue/SqDict.ts index af58684349..38a85aa405 100644 --- a/packages/squiggle-lang/src/public/SqValue/SqDict.ts +++ b/packages/squiggle-lang/src/public/SqValue/SqDict.ts @@ -15,7 +15,7 @@ export class SqDict { ([key, v]) => [ key, - wrapValue(v, this.context?.extend(SqValuePathEdge.fromDictKey(key))), + wrapValue(v, this.context?.extend(SqValuePathEdge.fromKey(key))), ] as const ); } @@ -25,10 +25,7 @@ export class SqDict { if (value === undefined) { return undefined; } - return wrapValue( - value, - this.context?.extend(SqValuePathEdge.fromDictKey(key)) - ); + return wrapValue(value, this.context?.extend(SqValuePathEdge.fromKey(key))); } toString() { diff --git a/packages/squiggle-lang/src/public/SqValue/SqPlot.ts b/packages/squiggle-lang/src/public/SqValue/SqPlot.ts index 1524487de1..aa02489630 100644 --- a/packages/squiggle-lang/src/public/SqValue/SqPlot.ts +++ b/packages/squiggle-lang/src/public/SqValue/SqPlot.ts @@ -160,7 +160,7 @@ export class SqNumericFnPlot extends SqAbstractPlot<"numericFn"> { this.context ? this.createdProgrammatically ? this.context - : this.context.extend(SqValuePathEdge.fromDictKey("fn")) + : this.context.extend(SqValuePathEdge.fromKey("fn")) : undefined ); } @@ -228,7 +228,7 @@ export class SqDistFnPlot extends SqAbstractPlot<"distFn"> { this.context ? this.createdProgrammatically ? this.context - : this.context.extend(SqValuePathEdge.fromDictKey("fn")) + : this.context.extend(SqValuePathEdge.fromKey("fn")) : undefined ); } @@ -315,7 +315,7 @@ export class SqRelativeValuesPlot extends SqAbstractPlot<"relativeValues"> { get fn(): SqLambda { return new SqLambda( this._value.fn, - this.context?.extend(SqValuePathEdge.fromDictKey("fn")) + this.context?.extend(SqValuePathEdge.fromKey("fn")) ); } } diff --git a/packages/squiggle-lang/src/public/SqValueContext.ts b/packages/squiggle-lang/src/public/SqValueContext.ts index 492b52600d..c4c8ff1e44 100644 --- a/packages/squiggle-lang/src/public/SqValueContext.ts +++ b/packages/squiggle-lang/src/public/SqValueContext.ts @@ -66,19 +66,19 @@ export class SqValueContext { switch (ast.type) { case "Program": { - if (this.path.root === "bindings" && pathEdge.type === "dictKey") { + if (this.path.root === "bindings" && pathEdge.type === "key") { newAst = ast.symbols[pathEdge.value]; break; } break; } case "Dict": - if (pathEdge.type === "dictKey") { + if (pathEdge.type === "key") { newAst = ast.symbols[pathEdge.value]; } break; case "Array": - if (pathEdge.type === "arrayIndex") { + if (pathEdge.type === "index") { const element = ast.elements[pathEdge.value]; if (element) { newAst = element; diff --git a/packages/squiggle-lang/src/public/SqValuePath.ts b/packages/squiggle-lang/src/public/SqValuePath.ts index 6234626e1b..cecedf3d4a 100644 --- a/packages/squiggle-lang/src/public/SqValuePath.ts +++ b/packages/squiggle-lang/src/public/SqValuePath.ts @@ -4,8 +4,8 @@ import { locationContains } from "../ast/utils.js"; export type RootPathItem = "result" | "bindings" | "imports" | "exports"; export type ValuePathEdge = - | { type: "dictKey"; value: string } - | { type: "arrayIndex"; value: number } + | { type: "key"; value: string } + | { type: "index"; value: number } | { type: "cellAddress"; value: { row: number; column: number } } | { type: "calculator"; @@ -22,10 +22,10 @@ function pathItemIsEqual(a: ValuePathEdge, b: ValuePathEdge): boolean { return false; } switch (a.type) { - case "dictKey": - return a.value === (b as { type: "dictKey"; value: string }).value; - case "arrayIndex": - return a.value === (b as { type: "arrayIndex"; value: number }).value; + case "key": + return a.value === (b as { type: "key"; value: string }).value; + case "index": + return a.value === (b as { type: "index"; value: number }).value; case "cellAddress": return ( isCellAddressPathItem(b) && @@ -39,11 +39,11 @@ function pathItemIsEqual(a: ValuePathEdge, b: ValuePathEdge): boolean { export class SqValuePathEdge { private constructor(public value: ValuePathEdge) {} - static fromDictKey(str: string): SqValuePathEdge { - return new SqValuePathEdge({ type: "dictKey", value: str }); + static fromKey(str: string): SqValuePathEdge { + return new SqValuePathEdge({ type: "key", value: str }); } - static fromArrayIndex(num: number): SqValuePathEdge { - return new SqValuePathEdge({ type: "arrayIndex", value: num }); + static fromIndex(num: number): SqValuePathEdge { + return new SqValuePathEdge({ type: "index", value: num }); } static fromCalculator(): SqValuePathEdge { return new SqValuePathEdge({ type: "calculator" }); @@ -63,9 +63,9 @@ export class SqValuePathEdge { toDisplayString(): string { const item = this.value; switch (item.type) { - case "dictKey": + case "key": return item.value; - case "arrayIndex": + case "index": return String(item.value); case "cellAddress": return `Cell(${item.value.row},${item.value.column})`; @@ -77,9 +77,9 @@ export class SqValuePathEdge { uid(): string { const item = this.value; switch (item.type) { - case "dictKey": + case "key": return `DictKey:(${item.value})`; - case "arrayIndex": + case "index": return `ArrayIndex:(${item.value})`; case "cellAddress": return `CellAddress:(${item.value.row}:${item.value.column})`; @@ -121,11 +121,11 @@ function astOffsetToPathItems(ast: ASTNode, offset: number): SqValuePathEdge[] { pair.key.type === "String" // only string keys are supported ) { return [ - SqValuePathEdge.fromDictKey(pair.key.value), + SqValuePathEdge.fromKey(pair.key.value), ...buildRemainingPathItems(pair.value), ]; } else if (pair.type === "Identifier") { - return [SqValuePathEdge.fromDictKey(pair.value)]; // this is a final node, no need to buildRemainingPathItems recursively + return [SqValuePathEdge.fromKey(pair.value)]; // this is a final node, no need to buildRemainingPathItems recursively } } return []; @@ -135,7 +135,7 @@ function astOffsetToPathItems(ast: ASTNode, offset: number): SqValuePathEdge[] { const element = ast.elements[i]; if (locationContains(element.location, offset)) { return [ - SqValuePathEdge.fromArrayIndex(i), + SqValuePathEdge.fromIndex(i), ...buildRemainingPathItems(element), ]; } @@ -144,13 +144,13 @@ function astOffsetToPathItems(ast: ASTNode, offset: number): SqValuePathEdge[] { } case "LetStatement": { return [ - SqValuePathEdge.fromDictKey(ast.variable.value), + SqValuePathEdge.fromKey(ast.variable.value), ...buildRemainingPathItems(ast.value), ]; } case "DefunStatement": { return [ - SqValuePathEdge.fromDictKey(ast.variable.value), + SqValuePathEdge.fromKey(ast.variable.value), ...buildRemainingPathItems(ast.value), ]; }