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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"typescript": "^5.8.3"
},
"dependencies": {
"@matt.kantor/either": "^1.0.0",
"@matt.kantor/either": "^1.2.0",
"@matt.kantor/option": "^1.0.0",
"@matt.kantor/parsing": "^2.0.0",
"kleur": "^4.1.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ const apply = (
),
)

if (either.isLeft(result)) {
return either.makeLeft({
kind: 'panic',
message: result.value.message,
})
} else {
return result
}
return either.mapLeft(result, error => ({
kind: 'panic',
message: error.message,
}))
}
16 changes: 6 additions & 10 deletions src/language/runtime/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,12 @@ export const keywordHandlers: KeywordHandlers = {
const result = runtimeFunction(
runtimeContext(runtimeFunction.parameterName),
)
if (either.isLeft(result)) {
// The runtime function panicked or had an unavailable dependency (which results in a panic
// anyway in this context).
return either.makeLeft({
kind: 'panic',
message: result.value.message,
})
} else {
return result
}
return either.mapLeft(result, error => ({
// The runtime function panicked or had an unavailable dependency (which results in a
// panic anyway in this context).
kind: 'panic',
message: error.message,
}))
}
},
),
Expand Down
21 changes: 8 additions & 13 deletions src/language/semantics/stdlib/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,14 @@ export const atom = {
},
serializeOnceAppliedFunction(['atom', 'prepend'], atomToPrepend),
option.none,
atomToPrependTo => {
if (
typeof atomToPrepend !== 'string' ||
typeof atomToPrependTo !== 'string'
) {
return either.makeLeft({
kind: 'panic',
message: 'prepend received a non-atom argument',
})
} else {
return either.makeRight(atomToPrepend + atomToPrependTo)
}
},
atomToPrependTo =>
typeof atomToPrepend !== 'string' ||
typeof atomToPrependTo !== 'string'
? either.makeLeft({
kind: 'panic',
message: 'prepend received a non-atom argument',
})
: either.makeRight(atomToPrepend + atomToPrependTo),
),
),
),
Expand Down
66 changes: 29 additions & 37 deletions src/language/unparsing/plz-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ export const moleculeUnparser =
value,
unparseAtomOrMolecule,
)
if (either.isLeft(result)) {
return unparseSugarFreeMolecule(value, unparseAtomOrMolecule)
} else {
return result
}
return either.flatMapLeft(result, _ =>
unparseSugarFreeMolecule(value, unparseAtomOrMolecule),
)
} else {
return unparseSugarFreeMolecule(value, unparseAtomOrMolecule)
}
Expand Down Expand Up @@ -156,34 +154,30 @@ const unparseSugaredApply = (
unparseAtomOrMolecule: UnparseAtomOrMolecule,
) => {
const { closeParenthesis, openParenthesis } = punctuation(kleur)
const functionUnparseResult = either.map(
either.flatMap(
serializeIfNeeded(expression[1].function),
unparseAtomOrMolecule,
return either.flatMap(
either.map(
either.flatMap(
serializeIfNeeded(expression[1].function),
unparseAtomOrMolecule,
),
unparsedFunction =>
either.isRight(readFunctionExpression(expression[1].function))
? // Immediately-applied function expressions need parentheses.
openParenthesis.concat(unparsedFunction).concat(closeParenthesis)
: unparsedFunction,
),
unparsedFunction =>
either.isRight(readFunctionExpression(expression[1].function))
? // Immediately-applied function expressions need parentheses.
openParenthesis.concat(unparsedFunction).concat(closeParenthesis)
: unparsedFunction,
)
if (either.isLeft(functionUnparseResult)) {
return functionUnparseResult
}

const argumentUnparseResult = either.flatMap(
serializeIfNeeded(expression[1].argument),
unparseAtomOrMolecule,
)
if (either.isLeft(argumentUnparseResult)) {
return argumentUnparseResult
}

return either.makeRight(
functionUnparseResult.value
.concat(openParenthesis)
.concat(argumentUnparseResult.value)
.concat(closeParenthesis),
either.map(
either.flatMap(
serializeIfNeeded(expression[1].argument),
unparseAtomOrMolecule,
),
unparsedArgument =>
unparsedFunction
.concat(openParenthesis)
.concat(unparsedArgument)
.concat(closeParenthesis),
),
)
}

Expand All @@ -209,12 +203,10 @@ const unparseSugaredIndex = (
serializeIfNeeded(expression[1].object),
unparseAtomOrMolecule,
)
if (either.isLeft(objectUnparseResult)) {
return objectUnparseResult
} else {
return either.flatMap(objectUnparseResult, unparsedObject => {
if (typeof expression[1].query !== 'object') {
// TODO: It would be nice if this were provably impossible.
return either.makeLeft({
return either.makeLeft<UnserializableValueError>({
kind: 'unserializableValue',
message: 'Invalid index expression',
})
Expand Down Expand Up @@ -248,13 +240,13 @@ const unparseSugaredIndex = (
} else {
const { dot } = punctuation(kleur)
return either.makeRight(
objectUnparseResult.value
unparsedObject
.concat(dot)
.concat(keyPath.map(quoteKeyPathComponentIfNecessary).join(dot)),
)
}
}
}
})
}

const unparseSugaredLookup = (
Expand Down