diff --git a/source/engine/mecanisms/operation.js b/source/engine/mecanisms/operation.js new file mode 100644 index 000000000..3ccb083b2 --- /dev/null +++ b/source/engine/mecanisms/operation.js @@ -0,0 +1,62 @@ +import React from 'react' +import { curry, map } from 'ramda' +import { inferUnit } from 'Engine/units' +import { + evaluateNode, + makeJsx, + mergeMissing, + rewriteNode +} from 'Engine/evaluation' +import { Node } from 'Engine/mecanismViews/common' + +export default (k, operatorFunction, symbol) => (recurse, k, v) => { + let evaluate = (cache, situation, parsedRules, node) => { + let explanation = map( + curry(evaluateNode)(cache, situation, parsedRules), + node.explanation + ), + value1 = explanation[0].nodeValue, + value2 = explanation[1].nodeValue, + nodeValue = + value1 == null || value2 == null + ? null + : operatorFunction(value1, value2), + missingVariables = mergeMissing( + explanation[0].missingVariables, + explanation[1].missingVariables + ) + + return rewriteNode(node, nodeValue, explanation, missingVariables) + } + + let explanation = v.explanation.map(recurse) + + let unit = inferUnit(k, [explanation[0].unit, explanation[1].unit]) + + let jsx = (nodeValue, explanation) => ( + + + {makeJsx(explanation[0])} + {symbol || k} + + {makeJsx(explanation[1])} + + } + /> + ) + + return { + ...v, + evaluate, + jsx, + operator: symbol || k, + // is this useful ? text: rawNode, + explanation, + unit + } +} diff --git a/source/engine/parse.js b/source/engine/parse.js index 12e3e862f..a492fd82e 100644 --- a/source/engine/parse.js +++ b/source/engine/parse.js @@ -6,11 +6,11 @@ import barème from 'Engine/mecanisms/barème' import barèmeContinu from 'Engine/mecanisms/barème-continu' import barèmeLinéaire from 'Engine/mecanisms/barème-linéaire' import variations from 'Engine/mecanisms/variations' +import operation from 'Engine/mecanisms/operation' import { Parser } from 'nearley' import { add, - curry, divide, equals, gt, @@ -19,7 +19,6 @@ import { without, lt, lte, - map, multiply, propOr, subtract, @@ -29,7 +28,6 @@ import { T } from 'ramda' import React from 'react' -import { evaluateNode, makeJsx, mergeMissing, rewriteNode } from './evaluation' import Grammar from './grammar.ne' import { mecanismAllOf, @@ -46,9 +44,7 @@ import { mecanismSynchronisation, mecanismOnePossibility } from './mecanisms' -import { Node } from './mecanismViews/common' import { parseReferenceTransforms } from './parseReference' -import { inferUnit } from 'Engine/units' export let parse = (rules, rule, parsedRules) => rawNode => { let onNodeType = cond([ @@ -127,7 +123,7 @@ export let parseObject = (rules, rule, parsedRules) => rawNode => { operationDispatch = fromPairs( Object.entries(knownOperations).map(([k, [f, symbol]]) => [ k, - mecanismOperation(k, f, symbol) + operation(k, f, symbol) ]) ) @@ -173,55 +169,3 @@ export let parseObject = (rules, rule, parsedRules) => rawNode => { return action(parse(rules, rule, parsedRules), k, v) } - -let mecanismOperation = (k, operatorFunction, symbol) => (recurse, k, v) => { - let evaluate = (cache, situation, parsedRules, node) => { - let explanation = map( - curry(evaluateNode)(cache, situation, parsedRules), - node.explanation - ), - value1 = explanation[0].nodeValue, - value2 = explanation[1].nodeValue, - nodeValue = - value1 == null || value2 == null - ? null - : operatorFunction(value1, value2), - missingVariables = mergeMissing( - explanation[0].missingVariables, - explanation[1].missingVariables - ) - - return rewriteNode(node, nodeValue, explanation, missingVariables) - } - - let explanation = v.explanation.map(recurse) - - let unit = inferUnit(k, [explanation[0].unit, explanation[1].unit]) - - let jsx = (nodeValue, explanation) => ( - - - {makeJsx(explanation[0])} - {symbol || k} - - {makeJsx(explanation[1])} - - } - /> - ) - - return { - ...v, - evaluate, - jsx, - operator: symbol || k, - // is this useful ? text: rawNode, - explanation, - unit - } -}