Skip to content

Commit 0184991

Browse files
authored
Merge pull request #7 from mkantor/organize
Improve separation of concerns between `language/compiling` and `language/semantics`
2 parents 5377e51 + 3660504 commit 0184991

25 files changed

+517
-481
lines changed

src/language/compiling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { compile } from './compiling/compiler.js'
2-
export { handlers as keywordHandlers } from './compiling/semantics/keywords.js'
2+
export { keywordHandlers } from './compiling/semantics/keywords.js'

src/language/compiling/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { CompilationError } from '../errors.js'
33
import type { JSONValueForbiddingSymbolicKeys } from '../parsing.js'
44
import { canonicalize } from '../parsing.js'
55
import { elaborate, serialize, type Output } from '../semantics.js'
6-
import * as keywordModule from './semantics/keywords.js'
6+
import { keywordHandlers } from './semantics/keywords.js'
77

88
export const compile = (
99
input: JSONValueForbiddingSymbolicKeys,
1010
): Either<CompilationError, Output> => {
1111
const syntaxTree = canonicalize(input)
12-
const semanticGraphResult = elaborate(syntaxTree, keywordModule)
12+
const semanticGraphResult = elaborate(syntaxTree, keywordHandlers)
1313
return either.flatMap(semanticGraphResult, serialize)
1414
}

src/language/compiling/semantics.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import {
1212
type ElaboratedSemanticGraph,
1313
type ObjectNode,
1414
} from '../semantics.js'
15+
import { prelude } from '../semantics/prelude.js'
1516
import type { SemanticGraph } from '../semantics/semantic-graph.js'
16-
import * as keywordModule from './semantics/keywords.js'
17-
import { prelude } from './semantics/prelude.js'
17+
import { keywordHandlers } from './semantics/keywords.js'
1818

1919
const elaborationSuite = testCases(
2020
(input: Atom | Molecule) =>
21-
elaborate(withPhantomData<never>()(input), keywordModule),
21+
elaborate(withPhantomData<never>()(input), keywordHandlers),
2222
input => `elaborating expressions in \`${JSON.stringify(input)}\``,
2323
)
2424

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

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/language/compiling/semantics/expressions/apply-expression.ts renamed to src/language/compiling/semantics/keyword-handlers/apply-handler.ts

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,15 @@
11
import { either, type Either } from '../../../../adts.js'
22
import type { ElaborationError } from '../../../errors.js'
3-
import type { Molecule } from '../../../parsing.js'
4-
import { isFunctionNode } from '../../../semantics.js'
53
import {
6-
isExpression,
4+
asSemanticGraph,
5+
containsAnyUnelaboratedNodes,
6+
isFunctionNode,
7+
readApplyExpression,
78
type Expression,
89
type ExpressionContext,
910
type KeywordHandler,
10-
} from '../../../semantics/expression-elaboration.js'
11-
import { makeUnelaboratedObjectNode } from '../../../semantics/object-node.js'
12-
import {
13-
containsAnyUnelaboratedNodes,
1411
type SemanticGraph,
15-
type unelaboratedKey,
16-
} from '../../../semantics/semantic-graph.js'
17-
import {
18-
asSemanticGraph,
19-
readArgumentsFromExpression,
20-
} from './expression-utilities.js'
21-
22-
export const applyKeyword = '@apply'
23-
24-
export type ApplyExpression = Expression & {
25-
readonly 0: '@apply'
26-
readonly function: SemanticGraph | Molecule
27-
readonly argument: SemanticGraph | Molecule
28-
}
29-
30-
export const readApplyExpression = (
31-
node: SemanticGraph,
32-
): Either<ElaborationError, ApplyExpression> =>
33-
isExpression(node)
34-
? either.map(
35-
readArgumentsFromExpression(node, [
36-
['function', '1'],
37-
['argument', '2'],
38-
]),
39-
([f, argument]) => makeApplyExpression({ function: f, argument }),
40-
)
41-
: either.makeLeft({
42-
kind: 'invalidExpression',
43-
message: 'not an expression',
44-
})
45-
46-
export const makeApplyExpression = ({
47-
function: f,
48-
argument,
49-
}: {
50-
readonly function: SemanticGraph | Molecule
51-
readonly argument: SemanticGraph | Molecule
52-
}): ApplyExpression & { readonly [unelaboratedKey]: true } =>
53-
makeUnelaboratedObjectNode({
54-
0: '@apply',
55-
function: f,
56-
argument,
57-
})
12+
} from '../../../semantics.js'
5813

5914
export const applyKeywordHandler: KeywordHandler = (
6015
expression: Expression,

src/language/compiling/semantics/expressions/check-expression.ts renamed to src/language/compiling/semantics/keyword-handlers/check-handler.ts

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,16 @@
11
import { either, option, type Either } from '../../../../adts.js'
22
import type { ElaborationError } from '../../../errors.js'
3-
import type { Molecule } from '../../../parsing.js'
4-
import { isFunctionNode } from '../../../semantics.js'
53
import {
6-
isExpression,
4+
asSemanticGraph,
5+
isFunctionNode,
6+
lookupPropertyOfObjectNode,
7+
readCheckExpression,
8+
stringifySemanticGraphForEndUser,
79
type Expression,
810
type ExpressionContext,
911
type KeywordHandler,
10-
} from '../../../semantics/expression-elaboration.js'
11-
import {
12-
lookupPropertyOfObjectNode,
13-
makeUnelaboratedObjectNode,
14-
} from '../../../semantics/object-node.js'
15-
import {
16-
stringifySemanticGraphForEndUser,
1712
type SemanticGraph,
18-
type unelaboratedKey,
19-
} from '../../../semantics/semantic-graph.js'
20-
import {
21-
asSemanticGraph,
22-
readArgumentsFromExpression,
23-
} from './expression-utilities.js'
24-
25-
export const checkKeyword = '@check'
26-
27-
export type CheckExpression = Expression & {
28-
readonly 0: '@check'
29-
readonly value: SemanticGraph | Molecule
30-
readonly type: SemanticGraph | Molecule
31-
}
32-
33-
export const readCheckExpression = (
34-
node: SemanticGraph,
35-
): Either<ElaborationError, CheckExpression> =>
36-
isExpression(node)
37-
? either.map(
38-
readArgumentsFromExpression(node, [
39-
['value', '1'],
40-
['type', '2'],
41-
]),
42-
([value, type]) => makeCheckExpression({ value, type }),
43-
)
44-
: either.makeLeft({
45-
kind: 'invalidExpression',
46-
message: 'not an expression',
47-
})
48-
49-
export const makeCheckExpression = ({
50-
value,
51-
type,
52-
}: {
53-
value: SemanticGraph | Molecule
54-
type: SemanticGraph | Molecule
55-
}): CheckExpression & { readonly [unelaboratedKey]: true } =>
56-
makeUnelaboratedObjectNode({
57-
0: '@check',
58-
value,
59-
type,
60-
})
13+
} from '../../../semantics.js'
6114

6215
export const checkKeywordHandler: KeywordHandler = (
6316
expression: Expression,

src/language/compiling/semantics/expressions/function-expression.ts renamed to src/language/compiling/semantics/keyword-handlers/function-handler.ts

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,21 @@
11
import { either, option, type Either } from '../../../../adts.js'
22
import type { ElaborationError } from '../../../errors.js'
3-
import type { Atom, Molecule } from '../../../parsing.js'
43
import {
4+
asSemanticGraph,
5+
elaborateWithContext,
56
makeFunctionNode,
7+
makeObjectNode,
8+
readFunctionExpression,
69
serialize,
710
types,
8-
type FunctionNode,
9-
} from '../../../semantics.js'
10-
import {
11-
elaborateWithContext,
12-
isExpression,
11+
updateValueAtKeyPathInSemanticGraph,
1312
type Expression,
1413
type ExpressionContext,
14+
type FunctionExpression,
15+
type FunctionNode,
1516
type KeywordHandler,
16-
} from '../../../semantics/expression-elaboration.js'
17-
import {
18-
makeObjectNode,
19-
makeUnelaboratedObjectNode,
20-
} from '../../../semantics/object-node.js'
21-
import {
22-
updateValueAtKeyPathInSemanticGraph,
2317
type SemanticGraph,
24-
type unelaboratedKey,
25-
} from '../../../semantics/semantic-graph.js'
26-
import { handlers, isKeyword } from '../keywords.js'
27-
import {
28-
asSemanticGraph,
29-
readArgumentsFromExpression,
30-
} from './expression-utilities.js'
31-
32-
export const functionKeyword = '@function'
33-
34-
export type FunctionExpression = Expression & {
35-
readonly 0: '@function'
36-
readonly parameter: Atom
37-
readonly body: SemanticGraph | Molecule
38-
}
39-
40-
export const readFunctionExpression = (
41-
node: SemanticGraph,
42-
): Either<ElaborationError, FunctionExpression> =>
43-
isExpression(node)
44-
? either.flatMap(
45-
readArgumentsFromExpression(node, [
46-
['parameter', '1'],
47-
['body', '2'],
48-
]),
49-
([parameter, body]): Either<ElaborationError, FunctionExpression> =>
50-
typeof parameter !== 'string'
51-
? either.makeLeft({
52-
kind: 'invalidExpression',
53-
message: 'parameter name must be an atom',
54-
})
55-
: either.map(serialize(asSemanticGraph(body)), body =>
56-
makeFunctionExpression(parameter, body),
57-
),
58-
)
59-
: either.makeLeft({
60-
kind: 'invalidExpression',
61-
message: 'not an expression',
62-
})
63-
64-
export const makeFunctionExpression = (
65-
parameter: Atom,
66-
body: SemanticGraph | Molecule,
67-
): FunctionExpression & { readonly [unelaboratedKey]: true } =>
68-
makeUnelaboratedObjectNode({
69-
0: '@function',
70-
parameter,
71-
body,
72-
})
18+
} from '../../../semantics.js'
7319

7420
export const functionKeywordHandler: KeywordHandler = (
7521
expression: Expression,
@@ -115,14 +61,11 @@ const apply = (
11561
}),
11662
),
11763
updatedProgram =>
118-
elaborateWithContext(
119-
serializedBody,
120-
{ handlers, isKeyword },
121-
{
122-
location: [...context.location, returnKey],
123-
program: updatedProgram,
124-
},
125-
),
64+
elaborateWithContext(serializedBody, {
65+
keywordHandlers: context.keywordHandlers,
66+
location: [...context.location, returnKey],
67+
program: updatedProgram,
68+
}),
12669
),
12770
)
12871

0 commit comments

Comments
 (0)