2020-12-01 09:17:27 +00:00
|
|
|
import { evaluationFunction } from '.'
|
|
|
|
import { ASTNode } from './AST/types'
|
2020-11-04 17:05:46 +00:00
|
|
|
|
|
|
|
export let evaluationFunctions = {
|
2020-12-01 09:17:27 +00:00
|
|
|
constant: (node) => node,
|
|
|
|
} as any
|
2020-11-04 17:05:46 +00:00
|
|
|
|
|
|
|
export function registerEvaluationFunction<
|
|
|
|
NodeName extends ASTNode['nodeKind']
|
|
|
|
>(nodeKind: NodeName, evaluationFunction: evaluationFunction<NodeName>) {
|
2020-12-01 09:17:27 +00:00
|
|
|
evaluationFunctions ??= {}
|
2020-11-04 17:05:46 +00:00
|
|
|
if (evaluationFunctions[nodeKind]) {
|
|
|
|
throw Error(
|
|
|
|
`Multiple evaluation functions registered for the nodeKind \x1b[4m${nodeKind}`
|
2020-12-01 09:17:27 +00:00
|
|
|
)
|
2020-11-04 17:05:46 +00:00
|
|
|
}
|
2020-12-01 09:17:27 +00:00
|
|
|
evaluationFunctions[nodeKind] = evaluationFunction
|
2020-11-04 17:05:46 +00:00
|
|
|
}
|