Reworked how bare symbols are handled, which should fix simplification

This commit is contained in:
Thaum Rystra
2020-05-15 14:44:08 +02:00
parent cd8a557120
commit 4478628200
4 changed files with 30 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
import math from '/imports/math.js';
import replaceBareSymbolsWithValueAccessor from '/imports/api/creature/computation/utility/replaceBareSymbolsWithValueAccessor.js';
import bareSymbolSubtitutor from '/imports/api/creature/computation/utility/bareSymbolSubtitutor.js';
export default function evaluateString(string, scope){
let errors = [];
@@ -20,7 +20,7 @@ export default function evaluateString(string, scope){
}
// Replace all bare symbols with symbol.value
let transformedCalc = calc.transform(replaceBareSymbolsWithValueAccessor);
let transformedCalc = calc.transform(bareSymbolSubtitutor(scope));
// Evaluate the expression to a number or return with substitutions
try {
@@ -29,11 +29,11 @@ export default function evaluateString(string, scope){
} catch (e1){
errors.push(e1);
try {
let result = simplifyWithAccessors(calc, scope).toHTML();
let result = simplifyWithAccessors(transformedCalc, scope).toHTML();
return {result, errors};
} catch (e2){
errors.push(e2);
return {result: calc.toHTML(), errors};
return {result: transformedCalc.toHTML(), errors};
}
}
}

View File

@@ -1,4 +1,4 @@
import replaceBareSymbolsWithValueAccessor from '/imports/api/creature/computation/utility/replaceBareSymbolsWithValueAccessor.js';
import bareSymbolSubtitutor from '/imports/api/creature/computation/utility/bareSymbolSubtitutor.js';
import computeStat from '/imports/api/creature/computation/computeStat.js';
import math from '/imports/math.js';
@@ -21,7 +21,7 @@ export default function evaluateCalculation(string, memo){
}
});
// Ensure any bare symbols are value accessors instead
let substitutedCalc = calc.transform(replaceBareSymbolsWithValueAccessor);
let substitutedCalc = calc.transform(bareSymbolSubtitutor(memo.statsByVariableName));
// Evaluate the expression to a number or return with substitutions
try {
return substitutedCalc.evaluate(memo.statsByVariableName);

View File

@@ -0,0 +1,24 @@
import math from '/imports/math.js';
export default function bareSymbolSubtitutor(scope){
return function(node, path){
if (!scope) return node;
if (node.isFunctionNode){
let fn = node.fn;
if (fn && fn.isSymbolNode){
fn.skipReplacement = true;
}
return node;
} else if (
node.isSymbolNode &&
path !== 'object' &&
node.skipReplacement !== true
) {
let stat = scope[node.name];
if (!stat) return node;
return new math.ConstantNode(stat.value);
} else {
return node;
}
}
}

View File

@@ -1,22 +0,0 @@
import math from '/imports/math.js';
export default function replaceBareSymbolsWithValueAccessor(node, path) {
if (node.isFunctionNode){
let fn = node.fn;
if (fn && fn.isSymbolNode){
fn.skipReplacement = true;
}
return node;
} else if (
node.isSymbolNode &&
path !== 'object' &&
node.skipReplacement !== true
) {
const object = new math.SymbolNode(node.name);
const address = new math.ConstantNode('value');
const index = new math.IndexNode([address]);
return new math.AccessorNode(object, index);
} else {
return node;
}
}