Began implementing dice rolls in the maths parser

This commit is contained in:
Stefan Zermatten
2020-06-30 14:40:20 +02:00
parent 56f9e82326
commit 7be4280508
5 changed files with 116 additions and 7 deletions

View File

@@ -0,0 +1,20 @@
import math from '/imports/math.js';
const diceRegex = /d\d+/;
export default function substituteRollsWithFunctions(node){
// TODO also replace dx as 1dx
if (
node.isOperatorNode &&
node.fn === 'multiply' &&
node.implicit &&
node.args[1].isSymbolNode &&
diceRegex.test(node.args[1].name)
){
let diceSize = node.args[1].name.slice(1);
let diceSizeNode = new math.ConstantNode(diceSize);
return new math.FunctionNode('roll', [node.args[0], diceSizeNode]);
} else {
return node;
}
}