Reworked how bare symbols are handled, which should fix simplification
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import math from '/imports/math.js';
|
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){
|
export default function evaluateString(string, scope){
|
||||||
let errors = [];
|
let errors = [];
|
||||||
@@ -20,7 +20,7 @@ export default function evaluateString(string, scope){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Replace all bare symbols with symbol.value
|
// 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
|
// Evaluate the expression to a number or return with substitutions
|
||||||
try {
|
try {
|
||||||
@@ -29,11 +29,11 @@ export default function evaluateString(string, scope){
|
|||||||
} catch (e1){
|
} catch (e1){
|
||||||
errors.push(e1);
|
errors.push(e1);
|
||||||
try {
|
try {
|
||||||
let result = simplifyWithAccessors(calc, scope).toHTML();
|
let result = simplifyWithAccessors(transformedCalc, scope).toHTML();
|
||||||
return {result, errors};
|
return {result, errors};
|
||||||
} catch (e2){
|
} catch (e2){
|
||||||
errors.push(e2);
|
errors.push(e2);
|
||||||
return {result: calc.toHTML(), errors};
|
return {result: transformedCalc.toHTML(), errors};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 computeStat from '/imports/api/creature/computation/computeStat.js';
|
||||||
import math from '/imports/math.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
|
// 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
|
// Evaluate the expression to a number or return with substitutions
|
||||||
try {
|
try {
|
||||||
return substitutedCalc.evaluate(memo.statsByVariableName);
|
return substitutedCalc.evaluate(memo.statsByVariableName);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user