|
| 1 | +import { either, option, type Either } from '../../../../adts.js' |
| 2 | +import type { ElaborationError } from '../../../errors.js' |
| 3 | +import type { Molecule } from '../../../parsing.js' |
| 4 | +import { isFunctionNode } from '../../../semantics.js' |
| 5 | +import { |
| 6 | + isExpression, |
| 7 | + type Expression, |
| 8 | + type ExpressionContext, |
| 9 | + type KeywordHandler, |
| 10 | +} from '../../../semantics/expression-elaboration.js' |
| 11 | +import { |
| 12 | + lookupPropertyOfObjectNode, |
| 13 | + makeUnelaboratedObjectNode, |
| 14 | +} from '../../../semantics/object-node.js' |
| 15 | +import { |
| 16 | + stringifySemanticGraphForEndUser, |
| 17 | + 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 | + }) |
| 61 | + |
| 62 | +export const checkKeywordHandler: KeywordHandler = ( |
| 63 | + expression: Expression, |
| 64 | + context: ExpressionContext, |
| 65 | +): Either<ElaborationError, SemanticGraph> => |
| 66 | + either.flatMap(readCheckExpression(expression), ({ value, type }) => |
| 67 | + check({ |
| 68 | + value: asSemanticGraph(value), |
| 69 | + type: asSemanticGraph(type), |
| 70 | + context, |
| 71 | + }), |
| 72 | + ) |
| 73 | + |
| 74 | +const check = ({ |
| 75 | + context, |
| 76 | + value, |
| 77 | + type, |
| 78 | +}: { |
| 79 | + readonly context: ExpressionContext |
| 80 | + readonly value: SemanticGraph |
| 81 | + readonly type: SemanticGraph |
| 82 | +}): Either<ElaborationError, SemanticGraph> => { |
| 83 | + if (typeof type === 'string') { |
| 84 | + return typeof value === 'string' && |
| 85 | + typeof type === 'string' && |
| 86 | + value === type |
| 87 | + ? either.makeRight(value) |
| 88 | + : either.makeLeft({ |
| 89 | + kind: 'typeMismatch', |
| 90 | + message: `the value \`${stringifySemanticGraphForEndUser( |
| 91 | + value, |
| 92 | + )}\` is not assignable to the type \`${stringifySemanticGraphForEndUser( |
| 93 | + type, |
| 94 | + )}\``, |
| 95 | + }) |
| 96 | + } else if (isFunctionNode(value)) { |
| 97 | + // TODO: Model function signatures as data and allow checking them. |
| 98 | + return either.makeLeft({ |
| 99 | + kind: 'invalidSyntaxTree', |
| 100 | + message: 'functions cannot be type checked', |
| 101 | + }) |
| 102 | + } else if (isFunctionNode(type)) { |
| 103 | + const result = type(value) |
| 104 | + if (either.isLeft(result)) { |
| 105 | + // The compile-time-evaluated function panicked or had an unavailable dependency (which |
| 106 | + // results in a panic anyway in this context). |
| 107 | + return either.makeLeft({ |
| 108 | + kind: 'panic', |
| 109 | + message: result.value.message, |
| 110 | + }) |
| 111 | + } else if (typeof result.value !== 'string' || result.value !== 'true') { |
| 112 | + return either.makeLeft({ |
| 113 | + kind: 'typeMismatch', |
| 114 | + message: `the value \`${stringifySemanticGraphForEndUser( |
| 115 | + value, |
| 116 | + )}\` did not pass the given type guard`, |
| 117 | + }) |
| 118 | + } else { |
| 119 | + // The value was valid according to the type guard. |
| 120 | + return either.makeRight(value) |
| 121 | + } |
| 122 | + } else if (typeof value === 'string') { |
| 123 | + // The only remaining case is when the type is an object, so we must have a type error. |
| 124 | + return either.makeLeft({ |
| 125 | + kind: 'typeMismatch', |
| 126 | + message: `the value \`${stringifySemanticGraphForEndUser( |
| 127 | + value, |
| 128 | + )}\` is not assignable to the type \`${stringifySemanticGraphForEndUser( |
| 129 | + type, |
| 130 | + )}\``, |
| 131 | + }) |
| 132 | + } else { |
| 133 | + // Make sure all properties in the type are present and valid in the value (recursively). |
| 134 | + // Values may legally have additional properties beyond what is required by the type. |
| 135 | + for (const [key, typePropertyValue] of Object.entries(type)) { |
| 136 | + const valuePropertyValue = lookupPropertyOfObjectNode(key, value) |
| 137 | + if (option.isNone(valuePropertyValue)) { |
| 138 | + return either.makeLeft({ |
| 139 | + kind: 'typeMismatch', |
| 140 | + message: `the value \`${stringifySemanticGraphForEndUser( |
| 141 | + value, |
| 142 | + )}\` is not assignable to the type \`${stringifySemanticGraphForEndUser( |
| 143 | + type, |
| 144 | + )}\` because it is missing the property \`${key}\``, |
| 145 | + }) |
| 146 | + } else { |
| 147 | + // Recursively check the property: |
| 148 | + const resultOfCheckingProperty = check({ |
| 149 | + context, |
| 150 | + value: valuePropertyValue.value, |
| 151 | + type: asSemanticGraph(typePropertyValue), |
| 152 | + }) |
| 153 | + if (either.isLeft(resultOfCheckingProperty)) { |
| 154 | + return resultOfCheckingProperty |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + // If this function has not yet returned then the value is assignable to the type. |
| 159 | + return either.makeRight(value) |
| 160 | + } |
| 161 | +} |
0 commit comments