Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/language/compiling.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { compile } from './compiling/compiler.js'
export { handlers as keywordHandlers } from './compiling/semantics/keywords.js'
export { keywordHandlers } from './compiling/semantics/keywords.js'
4 changes: 2 additions & 2 deletions src/language/compiling/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { CompilationError } from '../errors.js'
import type { JSONValueForbiddingSymbolicKeys } from '../parsing.js'
import { canonicalize } from '../parsing.js'
import { elaborate, serialize, type Output } from '../semantics.js'
import * as keywordModule from './semantics/keywords.js'
import { keywordHandlers } from './semantics/keywords.js'

export const compile = (
input: JSONValueForbiddingSymbolicKeys,
): Either<CompilationError, Output> => {
const syntaxTree = canonicalize(input)
const semanticGraphResult = elaborate(syntaxTree, keywordModule)
const semanticGraphResult = elaborate(syntaxTree, keywordHandlers)
return either.flatMap(semanticGraphResult, serialize)
}
6 changes: 3 additions & 3 deletions src/language/compiling/semantics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
type ElaboratedSemanticGraph,
type ObjectNode,
} from '../semantics.js'
import { prelude } from '../semantics/prelude.js'
import type { SemanticGraph } from '../semantics/semantic-graph.js'
import * as keywordModule from './semantics/keywords.js'
import { prelude } from './semantics/prelude.js'
import { keywordHandlers } from './semantics/keywords.js'

const elaborationSuite = testCases(
(input: Atom | Molecule) =>
elaborate(withPhantomData<never>()(input), keywordModule),
elaborate(withPhantomData<never>()(input), keywordHandlers),
input => `elaborating expressions in \`${JSON.stringify(input)}\``,
)

Expand Down
91 changes: 0 additions & 91 deletions src/language/compiling/semantics/expressions/runtime-expression.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,60 +1,15 @@
import { either, type Either } from '../../../../adts.js'
import type { ElaborationError } from '../../../errors.js'
import type { Molecule } from '../../../parsing.js'
import { isFunctionNode } from '../../../semantics.js'
import {
isExpression,
asSemanticGraph,
containsAnyUnelaboratedNodes,
isFunctionNode,
readApplyExpression,
type Expression,
type ExpressionContext,
type KeywordHandler,
} from '../../../semantics/expression-elaboration.js'
import { makeUnelaboratedObjectNode } from '../../../semantics/object-node.js'
import {
containsAnyUnelaboratedNodes,
type SemanticGraph,
type unelaboratedKey,
} from '../../../semantics/semantic-graph.js'
import {
asSemanticGraph,
readArgumentsFromExpression,
} from './expression-utilities.js'

export const applyKeyword = '@apply'

export type ApplyExpression = Expression & {
readonly 0: '@apply'
readonly function: SemanticGraph | Molecule
readonly argument: SemanticGraph | Molecule
}

export const readApplyExpression = (
node: SemanticGraph,
): Either<ElaborationError, ApplyExpression> =>
isExpression(node)
? either.map(
readArgumentsFromExpression(node, [
['function', '1'],
['argument', '2'],
]),
([f, argument]) => makeApplyExpression({ function: f, argument }),
)
: either.makeLeft({
kind: 'invalidExpression',
message: 'not an expression',
})

export const makeApplyExpression = ({
function: f,
argument,
}: {
readonly function: SemanticGraph | Molecule
readonly argument: SemanticGraph | Molecule
}): ApplyExpression & { readonly [unelaboratedKey]: true } =>
makeUnelaboratedObjectNode({
0: '@apply',
function: f,
argument,
})
} from '../../../semantics.js'

export const applyKeywordHandler: KeywordHandler = (
expression: Expression,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,16 @@
import { either, option, type Either } from '../../../../adts.js'
import type { ElaborationError } from '../../../errors.js'
import type { Molecule } from '../../../parsing.js'
import { isFunctionNode } from '../../../semantics.js'
import {
isExpression,
asSemanticGraph,
isFunctionNode,
lookupPropertyOfObjectNode,
readCheckExpression,
stringifySemanticGraphForEndUser,
type Expression,
type ExpressionContext,
type KeywordHandler,
} from '../../../semantics/expression-elaboration.js'
import {
lookupPropertyOfObjectNode,
makeUnelaboratedObjectNode,
} from '../../../semantics/object-node.js'
import {
stringifySemanticGraphForEndUser,
type SemanticGraph,
type unelaboratedKey,
} from '../../../semantics/semantic-graph.js'
import {
asSemanticGraph,
readArgumentsFromExpression,
} from './expression-utilities.js'

export const checkKeyword = '@check'

export type CheckExpression = Expression & {
readonly 0: '@check'
readonly value: SemanticGraph | Molecule
readonly type: SemanticGraph | Molecule
}

export const readCheckExpression = (
node: SemanticGraph,
): Either<ElaborationError, CheckExpression> =>
isExpression(node)
? either.map(
readArgumentsFromExpression(node, [
['value', '1'],
['type', '2'],
]),
([value, type]) => makeCheckExpression({ value, type }),
)
: either.makeLeft({
kind: 'invalidExpression',
message: 'not an expression',
})

export const makeCheckExpression = ({
value,
type,
}: {
value: SemanticGraph | Molecule
type: SemanticGraph | Molecule
}): CheckExpression & { readonly [unelaboratedKey]: true } =>
makeUnelaboratedObjectNode({
0: '@check',
value,
type,
})
} from '../../../semantics.js'

export const checkKeywordHandler: KeywordHandler = (
expression: Expression,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,21 @@
import { either, option, type Either } from '../../../../adts.js'
import type { ElaborationError } from '../../../errors.js'
import type { Atom, Molecule } from '../../../parsing.js'
import {
asSemanticGraph,
elaborateWithContext,
makeFunctionNode,
makeObjectNode,
readFunctionExpression,
serialize,
types,
type FunctionNode,
} from '../../../semantics.js'
import {
elaborateWithContext,
isExpression,
updateValueAtKeyPathInSemanticGraph,
type Expression,
type ExpressionContext,
type FunctionExpression,
type FunctionNode,
type KeywordHandler,
} from '../../../semantics/expression-elaboration.js'
import {
makeObjectNode,
makeUnelaboratedObjectNode,
} from '../../../semantics/object-node.js'
import {
updateValueAtKeyPathInSemanticGraph,
type SemanticGraph,
type unelaboratedKey,
} from '../../../semantics/semantic-graph.js'
import { handlers, isKeyword } from '../keywords.js'
import {
asSemanticGraph,
readArgumentsFromExpression,
} from './expression-utilities.js'

export const functionKeyword = '@function'

export type FunctionExpression = Expression & {
readonly 0: '@function'
readonly parameter: Atom
readonly body: SemanticGraph | Molecule
}

export const readFunctionExpression = (
node: SemanticGraph,
): Either<ElaborationError, FunctionExpression> =>
isExpression(node)
? either.flatMap(
readArgumentsFromExpression(node, [
['parameter', '1'],
['body', '2'],
]),
([parameter, body]): Either<ElaborationError, FunctionExpression> =>
typeof parameter !== 'string'
? either.makeLeft({
kind: 'invalidExpression',
message: 'parameter name must be an atom',
})
: either.map(serialize(asSemanticGraph(body)), body =>
makeFunctionExpression(parameter, body),
),
)
: either.makeLeft({
kind: 'invalidExpression',
message: 'not an expression',
})

export const makeFunctionExpression = (
parameter: Atom,
body: SemanticGraph | Molecule,
): FunctionExpression & { readonly [unelaboratedKey]: true } =>
makeUnelaboratedObjectNode({
0: '@function',
parameter,
body,
})
} from '../../../semantics.js'

export const functionKeywordHandler: KeywordHandler = (
expression: Expression,
Expand Down Expand Up @@ -115,14 +61,11 @@ const apply = (
}),
),
updatedProgram =>
elaborateWithContext(
serializedBody,
{ handlers, isKeyword },
{
location: [...context.location, returnKey],
program: updatedProgram,
},
),
elaborateWithContext(serializedBody, {
keywordHandlers: context.keywordHandlers,
location: [...context.location, returnKey],
program: updatedProgram,
}),
),
)

Expand Down
Loading
Loading