Skip to content

Commit d299777

Browse files
committed
Move keyPathToLookupExpression
1 parent f57a3ed commit d299777

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/language/semantics.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export {
3535
type IndexExpression,
3636
} from './semantics/expressions/index-expression.js'
3737
export {
38+
keyPathToLookupExpression,
3839
makeLookupExpression,
3940
readLookupExpression,
4041
type LookupExpression,

src/language/semantics/expressions/lookup-expression.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import either, { type Either } from '@matt.kantor/either'
22
import type { ElaborationError } from '../../errors.js'
33
import type { Atom, Molecule } from '../../parsing.js'
44
import { isExpressionWithArgument } from '../expression.js'
5+
import { keyPathToMolecule, type NonEmptyKeyPath } from '../key-path.js'
56
import { makeObjectNode, type ObjectNode } from '../object-node.js'
67
import {
78
stringifySemanticGraphForEndUser,
@@ -11,6 +12,7 @@ import {
1112
asSemanticGraph,
1213
readArgumentsFromExpression,
1314
} from './expression-utilities.js'
15+
import { makeIndexExpression } from './index-expression.js'
1416

1517
export type LookupExpression = ObjectNode & {
1618
readonly 0: '@lookup'
@@ -45,3 +47,16 @@ export const makeLookupExpression = (key: Atom): LookupExpression =>
4547
0: '@lookup',
4648
1: makeObjectNode({ key }),
4749
})
50+
51+
export const keyPathToLookupExpression = (keyPath: NonEmptyKeyPath) => {
52+
const [initialKey, ...indexes] = keyPath
53+
const initialLookup = makeLookupExpression(initialKey)
54+
if (indexes.length === 0) {
55+
return initialLookup
56+
} else {
57+
return makeIndexExpression({
58+
object: initialLookup,
59+
query: keyPathToMolecule(indexes),
60+
})
61+
}
62+
}

src/language/semantics/key-path.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { inlinePlz, unparse } from '../unparsing.js'
55
import type { ObjectNode } from './object-node.js'
66

77
export type KeyPath = readonly Atom[]
8+
export type NonEmptyKeyPath = readonly [Atom, ...KeyPath]
89

910
export const stringifyKeyPathForEndUser = (keyPath: KeyPath): string =>
1011
either.match(unparse(keyPathToMolecule(keyPath), inlinePlz), {

src/language/semantics/stdlib/stdlib-utilities.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import either, { type Either } from '@matt.kantor/either'
22
import option from '@matt.kantor/option'
33
import type { DependencyUnavailable, Panic } from '../../errors.js'
4-
import type { Atom } from '../../parsing.js'
54
import {
5+
keyPathToLookupExpression,
66
makeApplyExpression,
7-
makeIndexExpression,
8-
makeLookupExpression,
97
} from '../../semantics.js'
108
import { makeFunctionNode } from '../function-node.js'
11-
import { keyPathToMolecule, type KeyPath } from '../key-path.js'
9+
import { type NonEmptyKeyPath } from '../key-path.js'
1210
import {
1311
containsAnyUnelaboratedNodes,
1412
type SemanticGraph,
@@ -34,21 +32,6 @@ const handleUnavailableDependencies =
3432
}
3533
}
3634

37-
type NonEmptyKeyPath = readonly [Atom, ...KeyPath]
38-
39-
const keyPathToLookupExpression = (keyPath: NonEmptyKeyPath) => {
40-
const [initialKey, ...indexes] = keyPath
41-
const initialLookup = makeLookupExpression(initialKey)
42-
if (indexes.length === 0) {
43-
return initialLookup
44-
} else {
45-
return makeIndexExpression({
46-
object: initialLookup,
47-
query: keyPathToMolecule(indexes),
48-
})
49-
}
50-
}
51-
5235
export const serializeOnceAppliedFunction =
5336
(keyPath: NonEmptyKeyPath, argument: SemanticGraph) => () =>
5437
either.makeRight(

0 commit comments

Comments
 (0)