Skip to content

Commit c658a22

Browse files
committed
Extract keyPathToMolecule function
1 parent 7ff99e6 commit c658a22

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
type ExpressionContext,
1111
type KeywordHandler,
1212
} from '../../../semantics/expression-elaboration.js'
13-
import { stringifyKeyPathForEndUser } from '../../../semantics/key-path.js'
13+
import {
14+
keyPathToMolecule,
15+
stringifyKeyPathForEndUser,
16+
} from '../../../semantics/key-path.js'
1417
import {
1518
isObjectNode,
1619
makeObjectNode,
@@ -52,11 +55,7 @@ export const readLookupExpression = (
5255
} else {
5356
const canonicalizedQuery =
5457
typeof query === 'string'
55-
? makeObjectNode(
56-
Object.fromEntries(
57-
query.split('.').map((key, index) => [index, key]),
58-
),
59-
)
58+
? makeObjectNode(keyPathToMolecule(query.split('.')))
6059
: query
6160

6261
return either.map(
@@ -212,11 +211,7 @@ const lookup = ({
212211
return either.makeRight(
213212
option.makeSome(
214213
makeLookupExpression(
215-
makeObjectNode(
216-
Object.fromEntries(
217-
relativePath.map((key, index) => [index, key]),
218-
),
219-
),
214+
makeObjectNode(keyPathToMolecule(relativePath)),
220215
),
221216
),
222217
)

src/language/compiling/semantics/prelude.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
types,
1010
type ObjectNode,
1111
} from '../../semantics.js'
12+
import { keyPathToMolecule } from '../../semantics/key-path.js'
1213
import {
1314
lookupPropertyOfObjectNode,
1415
makeUnelaboratedObjectNode,
@@ -60,7 +61,7 @@ const preludeFunction = (
6061
either.makeRight(
6162
makeUnelaboratedObjectNode({
6263
0: '@lookup',
63-
query: Object.fromEntries(keyPath.map((key, index) => [index, key])),
64+
query: keyPathToMolecule(keyPath),
6465
}),
6566
),
6667
option.none,

src/language/semantics/key-path.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Atom } from '../parsing.js'
1+
import type { Atom, Molecule } from '../parsing.js'
22

33
export const functionParameter = Symbol('functionParameter')
44
export const functionReturn = Symbol('functionReturn')
@@ -7,10 +7,19 @@ export const typeParameterAssignableToConstraint = Symbol(
77
)
88
export type KeyPath = readonly (
99
| Atom
10+
// These symbol keys are somewhat "internal" at the moment. If they end up not being expressible
11+
// in the surface syntax then `KeyPath` should be split into two separate types.
1012
| typeof functionParameter
1113
| typeof functionReturn
1214
| typeof typeParameterAssignableToConstraint
1315
)[]
1416

1517
export const stringifyKeyPathForEndUser = (keyPath: KeyPath): string =>
1618
JSON.stringify(keyPath)
19+
20+
export const keyPathToMolecule = (keyPath: KeyPath): Molecule =>
21+
Object.fromEntries(
22+
keyPath.flatMap((key, index) =>
23+
typeof key === 'symbol' ? [] : [[index, key]],
24+
),
25+
)

0 commit comments

Comments
 (0)