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
12 changes: 11 additions & 1 deletion src/language/semantics/key-path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { either } from '../../adts.js'
import type { Atom, Molecule } from '../parsing.js'
import { unparse } from '../unparsing.js'
import { prettyPlz } from '../unparsing/pretty-plz.js'

export const functionParameter = Symbol('functionParameter')
export const functionReturn = Symbol('functionReturn')
Expand All @@ -15,7 +18,14 @@ export type KeyPath = readonly (
)[]

export const stringifyKeyPathForEndUser = (keyPath: KeyPath): string =>
JSON.stringify(keyPath)
either.match(
// TODO: Use single-line plz notation.
unparse(keyPathToMolecule(keyPath), prettyPlz),
{
right: stringifiedOutput => stringifiedOutput,
left: error => `(unserializable key path: ${error.message})`,
},
)

export const keyPathToMolecule = (keyPath: KeyPath): Molecule =>
Object.fromEntries(
Expand Down
14 changes: 13 additions & 1 deletion src/language/semantics/semantic-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {
} from '../errors.js'
import type { Atom, Molecule } from '../parsing.js'
import type { Canonicalized } from '../parsing/syntax-tree.js'
import { unparse } from '../unparsing.js'
import { prettyPlz } from '../unparsing/pretty-plz.js'
import { serializeFunctionNode, type FunctionNode } from './function-node.js'
import { stringifyKeyPathForEndUser, type KeyPath } from './key-path.js'
import {
Expand Down Expand Up @@ -174,7 +176,17 @@ export const serialize = (

export const stringifySemanticGraphForEndUser = (
graph: SemanticGraph,
): string => JSON.stringify(serialize(graph))
): string =>
either.match(
either.flatMap(serialize(graph), output =>
// TODO: Use single-line plz notation.
unparse(output, prettyPlz),
),
{
right: stringifiedOutput => stringifiedOutput,
left: error => `(unserializable value: ${error.message})`,
},
)

export const isSemanticGraph = (
value:
Expand Down
Loading