diff --git a/package-lock.json b/package-lock.json index 4485f48..5a03537 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "please-prototype", "version": "0.0.0", "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" @@ -26,9 +26,9 @@ } }, "node_modules/@matt.kantor/either": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@matt.kantor/either/-/either-1.0.0.tgz", - "integrity": "sha512-Abvg2BkwpQKUIAN5wSnTheBTcNR4dWeRS6E3j6Ra9NbftMfF+fTPtdNGDrtaRT3lTfGnkbhELeLGwd3iokHlpA==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@matt.kantor/either/-/either-1.2.0.tgz", + "integrity": "sha512-DujaG+1kCIa5gASwrAc9y352McRuomA3tH0AQOcGRuFHKPktkfwNcSUbprL+8RNziqXQ1+MJGCMiOS/IduOklA==", "license": "MIT", "dependencies": { "@matt.kantor/either-tag-symbol": "*" diff --git a/package.json b/package.json index 93e958c..307a4e8 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/language/compiling/semantics/keyword-handlers/function-handler.ts b/src/language/compiling/semantics/keyword-handlers/function-handler.ts index 700833f..adbc83f 100644 --- a/src/language/compiling/semantics/keyword-handlers/function-handler.ts +++ b/src/language/compiling/semantics/keyword-handlers/function-handler.ts @@ -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, + })) } diff --git a/src/language/runtime/keywords.ts b/src/language/runtime/keywords.ts index 61495f9..0d66aa2 100644 --- a/src/language/runtime/keywords.ts +++ b/src/language/runtime/keywords.ts @@ -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, + })) } }, ), diff --git a/src/language/semantics/stdlib/atom.ts b/src/language/semantics/stdlib/atom.ts index 96c777c..9d7e464 100644 --- a/src/language/semantics/stdlib/atom.ts +++ b/src/language/semantics/stdlib/atom.ts @@ -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), ), ), ), diff --git a/src/language/unparsing/plz-utilities.ts b/src/language/unparsing/plz-utilities.ts index 3363e36..6fa5649 100644 --- a/src/language/unparsing/plz-utilities.ts +++ b/src/language/unparsing/plz-utilities.ts @@ -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) } @@ -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), + ), ) } @@ -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({ kind: 'unserializableValue', message: 'Invalid index expression', }) @@ -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 = (