Skip to content

Commit

Permalink
Renamed PathEdge dictKey and arrayIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Jan 20, 2024
1 parent b4643c1 commit 3674260
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 59 deletions.
4 changes: 2 additions & 2 deletions packages/components/src/components/SquiggleViewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" &&
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/stories/SquiggleChart.stories.tsx
Original file line number Diff line number Diff line change
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: [SqValuePathEdge.fromDictKey("bar")],
items: [SqValuePathEdge.fromKey("bar")],
}),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
}),
},
};
39 changes: 18 additions & 21 deletions packages/squiggle-lang/__tests__/SqValue/SqValuePath_test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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" });
});
});

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),
],
Expand All @@ -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),
],
Expand All @@ -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,
});
});
Expand All @@ -48,35 +48,32 @@ 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);
});

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);
});
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/public/SqValue/SqArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
);
}
}
7 changes: 2 additions & 5 deletions packages/squiggle-lang/src/public/SqValue/SqDict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions packages/squiggle-lang/src/public/SqValue/SqPlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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
);
}
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(SqValuePathEdge.fromDictKey("fn"))
this.context?.extend(SqValuePathEdge.fromKey("fn"))
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/squiggle-lang/src/public/SqValueContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 19 additions & 19 deletions packages/squiggle-lang/src/public/SqValuePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) &&
Expand All @@ -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" });
Expand All @@ -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})`;
Expand All @@ -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})`;
Expand Down Expand Up @@ -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 [];
Expand All @@ -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),
];
}
Expand All @@ -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),
];
}
Expand Down

0 comments on commit 3674260

Please sign in to comment.