Parser now uses context to store details of the computation

This commit is contained in:
Stefan Zermatten
2020-09-18 10:14:53 +02:00
parent b69ad6c306
commit 06f17a6d33
12 changed files with 112 additions and 108 deletions

View File

@@ -22,7 +22,7 @@ export default class RollNode extends ParseNode {
return `${this.left.toString()}d${this.right.toString()}`;
}
}
roll(scope){
roll(scope, context){
let left = this.left.reduce(scope);
let right = this.right.reduce(scope);
if (!left.isInteger){
@@ -51,11 +51,10 @@ export default class RollNode extends ParseNode {
let roll = ~~(randomSrc.fraction() * diceSize) + 1
values.push(roll);
}
return new RollArrayNode({
values,
detail: {number, diceSize, values},
previousNodes: [this, left, right],
});
if (context){
context.storeRoll({number, diceSize, values});
}
return new RollArrayNode({values});
}
reduce(scope){
return this.roll(scope).reduce(scope);