Skip to content

Commit 9304f3c

Browse files
committed
Eliminate a redundant function
1 parent 6caa90f commit 9304f3c

File tree

2 files changed

+24
-59
lines changed

2 files changed

+24
-59
lines changed

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
makeUnelaboratedObjectNode,
2020
} from '../../../semantics/object-node.js'
2121
import {
22-
updateValueAtKeyPathInSemanticGraphIfValid,
22+
updateValueAtKeyPathInSemanticGraph,
2323
type SemanticGraph,
2424
type unelaboratedKey,
2525
} from '../../../semantics/semantic-graph.js'
@@ -102,27 +102,30 @@ const apply = (
102102
? 'return with a different key to avoid collision with a stupidly-named parameter'
103103
: 'return'
104104

105-
const result = either.flatMap(serialize(body), serializedBody => {
106-
const updatedProgram = updateValueAtKeyPathInSemanticGraphIfValid(
107-
context.program,
108-
context.location,
109-
_ =>
110-
makeObjectNode({
111-
// Put the argument in scope.
112-
[expression.parameter]: argument,
113-
[returnKey]: body,
114-
}),
115-
)
105+
const result = either.flatMap(serialize(body), serializedBody =>
106+
either.flatMap(
107+
updateValueAtKeyPathInSemanticGraph(
108+
context.program,
109+
context.location,
110+
_ =>
111+
makeObjectNode({
112+
// Put the argument in scope.
113+
[expression.parameter]: argument,
114+
[returnKey]: body,
115+
}),
116+
),
117+
updatedProgram =>
118+
elaborateWithContext(
119+
serializedBody,
120+
{ handlers, isKeyword },
121+
{
122+
location: [...context.location, returnKey],
123+
program: updatedProgram,
124+
},
125+
),
126+
),
127+
)
116128

117-
return elaborateWithContext(
118-
serializedBody,
119-
{ handlers, isKeyword },
120-
{
121-
location: [...context.location, returnKey],
122-
program: updatedProgram,
123-
},
124-
)
125-
})
126129
if (either.isLeft(result)) {
127130
return either.makeLeft({
128131
kind: 'panic',

src/language/semantics/semantic-graph.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { Canonicalized } from '../parsing/syntax-tree.js'
99
import { serializeFunctionNode, type FunctionNode } from './function-node.js'
1010
import { stringifyKeyPathForEndUser, type KeyPath } from './key-path.js'
1111
import {
12-
lookupPropertyOfObjectNode,
1312
makeObjectNode,
1413
makeUnelaboratedObjectNode,
1514
serializeObjectNode,
@@ -52,43 +51,6 @@ export const applyKeyPathToSemanticGraph = (
5251
}
5352
}
5453

55-
export const updateValueAtKeyPathInSemanticGraphIfValid = (
56-
value: SemanticGraph,
57-
keyPath: KeyPath,
58-
operation: (valueAtKeyPath: SemanticGraph) => SemanticGraph,
59-
): SemanticGraph => {
60-
const [firstKey, ...remainingKeyPath] = keyPath
61-
if (firstKey === undefined) {
62-
// If the key path is empty, this is the value to operate on.
63-
return operation(value)
64-
} else {
65-
return matchSemanticGraph(value, {
66-
atom: (node): SemanticGraph => node,
67-
function: node => node,
68-
object: node => {
69-
if (typeof firstKey === 'string') {
70-
return option.match(lookupPropertyOfObjectNode(firstKey, node), {
71-
none: () => node,
72-
some: propertyValue =>
73-
(isUnelaborated(node)
74-
? makeUnelaboratedObjectNode
75-
: makeObjectNode)({
76-
...node,
77-
[firstKey]: updateValueAtKeyPathInSemanticGraphIfValid(
78-
propertyValue,
79-
remainingKeyPath,
80-
operation,
81-
),
82-
}),
83-
})
84-
} else {
85-
return node
86-
}
87-
},
88-
})
89-
}
90-
}
91-
9254
export const isUnelaborated = (node: SemanticGraph | Molecule): boolean =>
9355
typeof node !== 'string' &&
9456
unelaboratedKey in node &&

0 commit comments

Comments
 (0)