Skip to content

Commit c018fc2

Browse files
committed
Put keyword handlers in expression context
1 parent df787b2 commit c018fc2

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

src/language/compiling/semantics/keyword-handlers/function-handler.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
updateValueAtKeyPathInSemanticGraph,
2121
type SemanticGraph,
2222
} from '../../../semantics/semantic-graph.js'
23-
import { keywordHandlers } from '../keywords.js'
2423

2524
export const functionKeywordHandler: KeywordHandler = (
2625
expression: Expression,
@@ -66,16 +65,11 @@ const apply = (
6665
}),
6766
),
6867
updatedProgram =>
69-
elaborateWithContext(
70-
serializedBody,
71-
// TODO: This should use compile-time or runtime handlers when appropriate. Perhaps
72-
// keyword handlers should be part of `ExpressionContext`?
73-
keywordHandlers,
74-
{
75-
location: [...context.location, returnKey],
76-
program: updatedProgram,
77-
},
78-
),
68+
elaborateWithContext(serializedBody, {
69+
keywordHandlers: context.keywordHandlers,
70+
location: [...context.location, returnKey],
71+
program: updatedProgram,
72+
}),
7973
),
8074
)
8175

src/language/compiling/semantics/keyword-handlers/lookup-handler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ const lookup = ({
181181
// Try the parent scope.
182182
lookup({
183183
relativePath,
184-
context: { program: context.program, location: pathToCurrentScope },
184+
context: {
185+
keywordHandlers: context.keywordHandlers,
186+
location: pathToCurrentScope,
187+
program: context.program,
188+
},
185189
}),
186190
some: lookedUpValue => either.makeRight(option.makeSome(lookedUpValue)),
187191
}),

src/language/semantics/expression-elaboration.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ type Elaborated = { readonly [_elaborated]: true }
2222
export type ElaboratedSemanticGraph = WithPhantomData<SemanticGraph, Elaborated>
2323

2424
export type ExpressionContext = {
25-
readonly program: SemanticGraph
25+
readonly keywordHandlers: KeywordHandlers
2626
readonly location: KeyPath
27+
readonly program: SemanticGraph
2728
}
2829

2930
export type KeywordElaborationResult = Either<ElaborationError, SemanticGraph>
@@ -39,7 +40,8 @@ export const elaborate = (
3940
program: SyntaxTree,
4041
keywordHandlers: KeywordHandlers,
4142
): Either<ElaborationError, ElaboratedSemanticGraph> =>
42-
elaborateWithContext(program, keywordHandlers, {
43+
elaborateWithContext(program, {
44+
keywordHandlers,
4345
location: [],
4446
program:
4547
typeof program === 'string'
@@ -49,19 +51,17 @@ export const elaborate = (
4951

5052
export const elaborateWithContext = (
5153
program: SyntaxTree,
52-
keywordHandlers: KeywordHandlers,
5354
context: ExpressionContext,
5455
): Either<ElaborationError, ElaboratedSemanticGraph> =>
5556
either.map(
5657
typeof program === 'string'
5758
? handleAtomWhichMayNotBeAKeyword(program)
58-
: elaborateWithinMolecule(program, keywordHandlers, context),
59+
: elaborateWithinMolecule(program, context),
5960
withPhantomData<Elaborated>(),
6061
)
6162

6263
const elaborateWithinMolecule = (
6364
molecule: Molecule,
64-
keywordHandlers: KeywordHandlers,
6565
context: ExpressionContext,
6666
): Either<ElaborationError, SemanticGraph> => {
6767
const possibleExpressionAsObjectNode: Writable<ObjectNode> = makeObjectNode(
@@ -79,14 +79,11 @@ const elaborateWithinMolecule = (
7979
if (typeof value === 'string') {
8080
possibleExpressionAsObjectNode[updatedKey] = value
8181
} else {
82-
const elaborationResult = elaborateWithinMolecule(
83-
value,
84-
keywordHandlers,
85-
{
86-
location: [...context.location, key],
87-
program: updatedProgram,
88-
},
89-
)
82+
const elaborationResult = elaborateWithinMolecule(value, {
83+
keywordHandlers: context.keywordHandlers,
84+
location: [...context.location, key],
85+
program: updatedProgram,
86+
})
9087
if (either.isLeft(elaborationResult)) {
9188
// Immediately bail on error.
9289
return elaborationResult
@@ -146,8 +143,8 @@ const elaborateWithinMolecule = (
146143
...possibleExpressionAsObjectNode,
147144
0: possibleKeywordAsString,
148145
},
149-
keywordHandlers,
150146
{
147+
keywordHandlers: context.keywordHandlers,
151148
program: updatedProgram,
152149
location: context.location,
153150
},
@@ -158,7 +155,6 @@ const elaborateWithinMolecule = (
158155

159156
const handleObjectNodeWhichMayBeAExpression = (
160157
node: ObjectNode & { readonly 0: Atom },
161-
keywordHandlers: KeywordHandlers,
162158
context: ExpressionContext,
163159
): Either<ElaborationError, SemanticGraph> => {
164160
const { 0: possibleKeyword, ...possibleArguments } = node
@@ -174,7 +170,7 @@ const handleObjectNodeWhichMayBeAExpression = (
174170
0: unescapeKeywordSigil(possibleKeyword),
175171
}),
176172
some: keyword =>
177-
keywordHandlers[keyword](
173+
context.keywordHandlers[keyword](
178174
makeObjectNode({ ...possibleArguments, 0: keyword }),
179175
context,
180176
),

0 commit comments

Comments
 (0)