Skip to content

Commit

Permalink
SqPathItem -> SqValuePathEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Jan 20, 2024
1 parent 643acef commit b4643c1
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 89 deletions.
20 changes: 10 additions & 10 deletions packages/components/src/components/SquiggleViewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,33 @@ export function useGetSubvalueByPath() {
});

for (const subValuePath of subValuePaths) {
const pathItem = subValuePath.lastItem()!; // We know it's not empty, because includeRoot is false.
const pathEdge = subValuePath.lastItem()!; // We know it's not empty, because includeRoot is false.
const currentTag = currentValue.tag;
const pathItemType = pathItem.value.type;
const pathEdgeType = pathEdge.value.type;

let nextValue: SqValue | undefined;

if (currentTag === "Array" && pathItemType === "arrayIndex") {
nextValue = currentValue.value.getValues()[pathItem.value.value];
} else if (currentTag === "Dict" && pathItemType === "dictKey") {
nextValue = currentValue.value.get(pathItem.value.value);
if (currentTag === "Array" && pathEdgeType === "arrayIndex") {
nextValue = currentValue.value.getValues()[pathEdge.value.value];
} else if (currentTag === "Dict" && pathEdgeType === "dictKey") {
nextValue = currentValue.value.get(pathEdge.value.value);
} else if (
currentTag === "TableChart" &&
pathItemType === "cellAddress"
pathEdgeType === "cellAddress"
) {
// Maybe it would be better to get the environment in a different way.
const environment = context.project.getEnvironment();
const item = currentValue.value.item(
pathItem.value.value.row,
pathItem.value.value.column,
pathEdge.value.value.row,
pathEdge.value.value.column,
environment
);
if (item.ok) {
nextValue = item.value;
} else {
return;
}
} else if (pathItem.type === "calculator") {
} else if (pathEdge.type === "calculator") {
// The previous path item is the one that is the parent of the calculator result.
// This is the one that we use in the ViewerContext to store information about the calculator.
const calculatorState = itemStore.getCalculator(subValuePath);
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/stories/SquiggleChart.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Meta, StoryObj } from "@storybook/react";

import { SqPathItem, SqValuePath } from "@quri/squiggle-lang";
import { SqValuePath, SqValuePathEdge } from "@quri/squiggle-lang";

import { SquiggleChart } from "../components/SquiggleChart.js";

Expand Down Expand Up @@ -50,7 +50,7 @@ export const RootPathOverride: Story = {
code: "{foo: 35 to 50, bar: [1,2,3]}",
rootPathOverride: new SqValuePath({
root: "result",
items: [SqPathItem.fromDictKey("bar")],
items: [SqValuePathEdge.fromDictKey("bar")],
}),
},
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Meta, StoryObj } from "@storybook/react";

import { SqPathItem, SqValuePath } from "@quri/squiggle-lang";
import { SqValuePath, SqValuePathEdge } from "@quri/squiggle-lang";

import { SquiggleChart } from "../../components/SquiggleChart.js";

Expand Down Expand Up @@ -43,7 +43,10 @@ export const WithPathOverride: Story = {
`,
rootPathOverride: new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("foo"), SqPathItem.fromDictKey("bar")],
items: [
SqValuePathEdge.fromDictKey("foo"),
SqValuePathEdge.fromDictKey("bar"),
],
}),
},
};
47 changes: 25 additions & 22 deletions packages/squiggle-lang/__tests__/SqValue/SqValuePath_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SqPathItem, SqValuePath } from "../../src/index.js";
import { SqValuePath, SqValuePathEdge } from "../../src/index.js";

describe("SqPathItem", () => {
describe("SqValuePathEdge", () => {
test("fromDictKey creates a string item", () => {
const item = SqPathItem.fromDictKey("test");
const item = SqValuePathEdge.fromDictKey("test");
expect(item.value).toEqual({ type: "dictKey", value: "test" });
});
});
Expand All @@ -11,21 +11,21 @@ describe("SqValuePath", () => {
const path = new SqValuePath({
root: "bindings",
items: [
SqPathItem.fromDictKey("foo"),
SqPathItem.fromArrayIndex(2),
SqPathItem.fromCalculator(),
SqPathItem.fromCellAddress(1, 2),
SqValuePathEdge.fromDictKey("foo"),
SqValuePathEdge.fromArrayIndex(2),
SqValuePathEdge.fromCalculator(),
SqValuePathEdge.fromCellAddress(1, 2),
],
});

test("isEqual()", () => {
const path2 = new SqValuePath({
root: "bindings",
items: [
SqPathItem.fromDictKey("foo"),
SqPathItem.fromArrayIndex(2),
SqPathItem.fromCalculator(),
SqPathItem.fromCellAddress(1, 2),
SqValuePathEdge.fromDictKey("foo"),
SqValuePathEdge.fromArrayIndex(2),
SqValuePathEdge.fromCalculator(),
SqValuePathEdge.fromCellAddress(1, 2),
],
});
expect(path.isEqual(path2)).toBe(true);
Expand All @@ -34,9 +34,9 @@ describe("SqValuePath", () => {
test("extend()", () => {
const path = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("foo")],
items: [SqValuePathEdge.fromDictKey("foo")],
});
const extendedPath = path.extend(SqPathItem.fromArrayIndex(2));
const extendedPath = path.extend(SqValuePathEdge.fromArrayIndex(2));
expect(extendedPath.items.length).toBe(2);
expect(extendedPath.items[1].value).toEqual({
type: "arrayIndex",
Expand All @@ -48,40 +48,43 @@ describe("SqValuePath", () => {
test("path fully contains a shorter path", () => {
const basePath = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("foo"), SqPathItem.fromArrayIndex(2)],
items: [
SqValuePathEdge.fromDictKey("foo"),
SqValuePathEdge.fromArrayIndex(2),
],
});
const subPath = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("foo")],
items: [SqValuePathEdge.fromDictKey("foo")],
});
expect(basePath.contains(subPath)).toBe(true);
});

test("path does not contain longer path", () => {
const basePath = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("foo")],
items: [SqValuePathEdge.fromDictKey("foo")],
});
const longerPath = basePath.extend(SqPathItem.fromArrayIndex(2));
const longerPath = basePath.extend(SqValuePathEdge.fromArrayIndex(2));
expect(basePath.contains(longerPath)).toBe(false);
});

test("path does not contain different path", () => {
const path1 = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("foo")],
items: [SqValuePathEdge.fromDictKey("foo")],
});
const path2 = new SqValuePath({
root: "imports",
items: [SqPathItem.fromDictKey("bar")],
items: [SqValuePathEdge.fromDictKey("bar")],
});
expect(path1.contains(path2)).toBe(false);
});

test("path contains empty path (with same root)", () => {
const nonEmptyPath = new SqValuePath({
root: "exports",
items: [SqPathItem.fromCalculator()],
items: [SqValuePathEdge.fromCalculator()],
});
const emptyPath = new SqValuePath({
root: "exports",
Expand All @@ -93,11 +96,11 @@ describe("SqValuePath", () => {
test("equal paths contain each other", () => {
const path1 = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("test")],
items: [SqValuePathEdge.fromDictKey("test")],
});
const path2 = new SqValuePath({
root: "bindings",
items: [SqPathItem.fromDictKey("test")],
items: [SqValuePathEdge.fromDictKey("test")],
});
expect(path1.contains(path2)).toBe(true);
expect(path2.contains(path1)).toBe(true);
Expand Down
6 changes: 1 addition & 5 deletions packages/squiggle-lang/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ export { SqTableChart } from "./public/SqValue/SqTableChart.js";
export { SqCalculator } from "./public/SqValue/SqCalculator.js";
export { SqDict } from "./public/SqValue/SqDict.js";
export { SqScale } from "./public/SqValue/SqScale.js";
export {
type PathItem,
SqPathItem,
SqValuePath,
} from "./public/SqValuePath.js";
export { SqValuePath, SqValuePathEdge } from "./public/SqValuePath.js";
export { parse } from "./public/parse.js";
export { fmap as resultMap, type result } from "./utility/result.js";

Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/public/SqValue/SqArray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Value } from "../../value/index.js";
import { SqValueContext } from "../SqValueContext.js";
import { SqPathItem } from "../SqValuePath.js";
import { SqValuePathEdge } from "../SqValuePath.js";
import { wrapValue } from "./index.js";

export class SqArray {
Expand All @@ -11,7 +11,7 @@ export class SqArray {

getValues() {
return this._value.map((v, i) =>
wrapValue(v, this.context?.extend(SqPathItem.fromArrayIndex(i)))
wrapValue(v, this.context?.extend(SqValuePathEdge.fromArrayIndex(i)))
);
}
}
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/public/SqValue/SqCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Result from "../../utility/result.js";
import { Calculator } from "../../value/VCalculator.js";
import { SqError, SqOtherError } from "../SqError.js";
import { SqValueContext } from "../SqValueContext.js";
import { SqPathItem } from "../SqValuePath.js";
import { SqValuePathEdge } from "../SqValuePath.js";
import { SqValue, wrapValue } from "./index.js";
import { SqInput, wrapInput } from "./SqInput.js";
import { SqLambda } from "./SqLambda.js";
Expand All @@ -18,7 +18,7 @@ export class SqCalculator {
const sqLambda = new SqLambda(this._value.fn, undefined);
const response = sqLambda.call(_arguments, env);

const newContext = this.context?.extend(SqPathItem.fromCalculator());
const newContext = this.context?.extend(SqValuePathEdge.fromCalculator());

if (!newContext) {
return Result.Err(
Expand Down
9 changes: 6 additions & 3 deletions packages/squiggle-lang/src/public/SqValue/SqDict.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VDict } from "../../value/VDict.js";
import { vString } from "../../value/VString.js";
import { SqValueContext } from "../SqValueContext.js";
import { SqPathItem } from "../SqValuePath.js";
import { SqValuePathEdge } from "../SqValuePath.js";
import { SqDictValue, SqValue, wrapValue } from "./index.js";

export class SqDict {
Expand All @@ -15,7 +15,7 @@ export class SqDict {
([key, v]) =>
[
key,
wrapValue(v, this.context?.extend(SqPathItem.fromDictKey(key))),
wrapValue(v, this.context?.extend(SqValuePathEdge.fromDictKey(key))),
] as const
);
}
Expand All @@ -25,7 +25,10 @@ export class SqDict {
if (value === undefined) {
return undefined;
}
return wrapValue(value, this.context?.extend(SqPathItem.fromDictKey(key)));
return wrapValue(
value,
this.context?.extend(SqValuePathEdge.fromDictKey(key))
);
}

toString() {
Expand Down
8 changes: 4 additions & 4 deletions packages/squiggle-lang/src/public/SqValue/SqPlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Result from "../../utility/result.js";
import { Plot, vPlot } from "../../value/VPlot.js";
import { SqError, SqOtherError } from "../SqError.js";
import { SqValueContext } from "../SqValueContext.js";
import { SqPathItem } from "../SqValuePath.js";
import { SqValuePathEdge } from "../SqValuePath.js";
import { SqPlotValue } from "./index.js";
import {
SqDistribution,
Expand Down Expand Up @@ -160,7 +160,7 @@ export class SqNumericFnPlot extends SqAbstractPlot<"numericFn"> {
this.context
? this.createdProgrammatically
? this.context
: this.context.extend(SqPathItem.fromDictKey("fn"))
: this.context.extend(SqValuePathEdge.fromDictKey("fn"))
: undefined
);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ export class SqDistFnPlot extends SqAbstractPlot<"distFn"> {
this.context
? this.createdProgrammatically
? this.context
: this.context.extend(SqPathItem.fromDictKey("fn"))
: this.context.extend(SqValuePathEdge.fromDictKey("fn"))
: undefined
);
}
Expand Down Expand Up @@ -315,7 +315,7 @@ export class SqRelativeValuesPlot extends SqAbstractPlot<"relativeValues"> {
get fn(): SqLambda {
return new SqLambda(
this._value.fn,
this.context?.extend(SqPathItem.fromDictKey("fn"))
this.context?.extend(SqValuePathEdge.fromDictKey("fn"))
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/public/SqValue/SqTableChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Result from "../../utility/result.js";
import { TableChart } from "../../value/VTableChart.js";
import { SqError, SqOtherError } from "../SqError.js";
import { SqValueContext } from "../SqValueContext.js";
import { SqPathItem } from "../SqValuePath.js";
import { SqValuePathEdge } from "../SqValuePath.js";
import { SqValue, wrapValue } from "./index.js";
import { SqLambda } from "./SqLambda.js";

Expand All @@ -22,7 +22,7 @@ const getItem = (
): Result.result<SqValue, SqError> => {
const response = fn.call([element], env);
const newContext: SqValueContext | undefined = context?.extend(
SqPathItem.fromCellAddress(row, column)
SqValuePathEdge.fromCellAddress(row, column)
);

if (response.ok && context) {
Expand Down
20 changes: 10 additions & 10 deletions packages/squiggle-lang/src/public/SqValueContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AST, ASTNode } from "../ast/parse.js";
import { isBindingStatement } from "../ast/utils.js";
import { SqProject } from "./SqProject/index.js";
import { SqPathItem, SqValuePath } from "./SqValuePath.js";
import { SqValuePath, SqValuePathEdge } from "./SqValuePath.js";

export class SqValueContext {
public project: SqProject;
Expand Down Expand Up @@ -37,13 +37,13 @@ export class SqValueContext {
this.path = props.path;
}

extend(item: SqPathItem): SqValueContext {
extend(item: SqValuePathEdge): SqValueContext {
let ast = this.valueAst;
const pathItem = item.value;
const pathEdge = item.value;

let newAst: ASTNode | undefined;
const itemisNotTableIndexOrCalculator =
pathItem.type !== "cellAddress" && pathItem.type !== "calculator";
pathEdge.type !== "cellAddress" && pathEdge.type !== "calculator";

if (this.valueAstIsPrecise && itemisNotTableIndexOrCalculator) {
// now we can try to look for the next nested valueAst
Expand All @@ -66,20 +66,20 @@ export class SqValueContext {

switch (ast.type) {
case "Program": {
if (this.path.root === "bindings" && pathItem.type === "dictKey") {
newAst = ast.symbols[pathItem.value];
if (this.path.root === "bindings" && pathEdge.type === "dictKey") {
newAst = ast.symbols[pathEdge.value];
break;
}
break;
}
case "Dict":
if (pathItem.type === "dictKey") {
newAst = ast.symbols[pathItem.value];
if (pathEdge.type === "dictKey") {
newAst = ast.symbols[pathEdge.value];
}
break;
case "Array":
if (pathItem.type === "arrayIndex") {
const element = ast.elements[pathItem.value];
if (pathEdge.type === "arrayIndex") {
const element = ast.elements[pathEdge.value];
if (element) {
newAst = element;
}
Expand Down
Loading

0 comments on commit b4643c1

Please sign in to comment.