⚙️ Petites simplifications du code du moteur

- évite l'indirection vers "parseReferenceTransforms" pour les
  références simples (il faut encore simplifier ce code)
- déplacement de fonctions de parsage vers les "stateless"
- typage de registerEvaluationFunction
- simplifie fragments.join()
pull/1197/head
Maxime Quandalle 2020-11-05 17:48:19 +01:00
parent edcd1d46d1
commit fcd8306ace
7 changed files with 21 additions and 20 deletions

View File

@ -208,7 +208,7 @@ export const evaluationFunctions = {
export function registerEvaluationFunction(
nodeKind: string,
evaluationFunction: any // TODO: type evaluationFunction
evaluationFunction: evaluationFunction
) {
if (evaluationFunctions[nodeKind]) {
throw Error(

View File

@ -39,9 +39,7 @@ export let variable = ([firstFragment, nextFragment], _, reject) => {
return reject
}
return {
variable: {
fragments
}
variable: fragments.join(' . ')
}
}

View File

@ -86,7 +86,7 @@ export const evaluateInversion: evaluationFunction = function(node) {
}
}
export const mecanismInversion = dottedName => (recurse, v) => {
export const mecanismInversion = (recurse, v, dottedName) => {
if (!v.avec) {
throw new Error(
"Une formule d'inversion doit préciser _avec_ quoi on peut inverser la variable"

View File

@ -1,14 +1,17 @@
import { registerEvaluationFunction } from '../evaluation'
// TODO : This isn't a real mecanism, cf. #963
export const mecanismOnePossibility = dottedName => (recurse, v) => ({
export const mecanismOnePossibility = (recurse, v, dottedName) => ({
...v,
'une possibilité': 'oui',
context: dottedName,
nodeKind: 'une possibilité'
})
registerEvaluationFunction('une possibilité', node => ({
...node,
missingVariables: { [node.context]: 1 }
}))
registerEvaluationFunction(
'une possibilité',
(node: ReturnType<typeof mecanismOnePossibility>) => ({
...node,
missingVariables: { [node.context]: 1 }
})
)

View File

@ -53,7 +53,7 @@ const evaluateRecalcul: evaluationFunction = function(node) {
}
}
export const mecanismRecalcul = dottedNameContext => (recurse, v) => {
export const mecanismRecalcul = (recurse, v, dottedNameContext) => {
const amendedSituation = Object.keys(v.avec).map(dottedName => [
recurse(dottedName),
recurse(v.avec[dottedName])

View File

@ -29,7 +29,7 @@ import { mecanismSynchronisation } from './mecanisms/synchronisation'
import tauxProgressif from './mecanisms/tauxProgressif'
import variableTemporelle from './mecanisms/variableTemporelle'
import variations, { devariate } from './mecanisms/variations'
import { parseReferenceTransforms } from './parseReference'
import { parseReference, parseReferenceTransforms } from './parseReference'
import { EvaluatedRule } from './types'
export const parse = (rules, rule, parsedRules) => rawNode => {
@ -101,11 +101,10 @@ Cela vient probablement d'une erreur dans l'indentation
const mecanismName = Object.keys(rawNode)[0]
const values = rawNode[mecanismName]
// TODO: All parse functions should be "stateless" (ie be simple functions
// with the same list of parameters and not curried fantasy):
const parseFunctions = {
...statelessParseFunction,
'une possibilité': mecanismOnePossibility(rule.dottedName),
'inversion numérique': mecanismInversion(rule.dottedName),
recalcul: mecanismRecalcul(rule.dottedName),
filter: () =>
parseReferenceTransforms(
rules,
@ -115,8 +114,7 @@ Cela vient probablement d'une erreur dans l'indentation
filter: values.filter,
variable: values.explanation
}),
variable: () =>
parseReferenceTransforms(rules, rule, parsedRules)({ variable: values }),
variable: () => parseReference(rules, rule, parsedRules, '')(values),
unitConversion: () =>
parseReferenceTransforms(
rules,
@ -147,7 +145,7 @@ Vérifiez qu'il n'y ait pas d'erreur dans l'orthographe du nom.`
if (values?.variations) {
return devariate(recurse, mecanismName, values)
}
return parseFn(recurse, values)
return parseFn(recurse, values, rule.dottedName)
} catch (e) {
if (e instanceof EngineError) {
throw e
@ -205,6 +203,9 @@ const statelessParseFunction = {
allègement: mecanismReduction,
variations,
synchronisation: mecanismSynchronisation,
'une possibilité': mecanismOnePossibility,
'inversion numérique': mecanismInversion,
recalcul: mecanismRecalcul,
valeur: (recurse, v) => recurse(v),
constant: (_, v) => ({
type: v.type,

View File

@ -44,13 +44,12 @@ export const parseReferenceTransforms = (rules, rule, parsedRules) => ({
filter,
unit
}: parseReferenceTransformsParameters) => {
const referenceName = variable.fragments.join(' . ')
const originalNode = parseReference(
rules,
rule,
parsedRules,
filter
)(referenceName)
)(variable)
return {
...originalNode,