Skip to content

@dice-roller/rpg-dice-roller / Evaluator

Class: Evaluator ​

Defined in: src/evaluator/Evaluator.ts:87

Walks the AST and builds the result tree.

Evaluation uses the supplied context's roll mode, generator and limits. The same AST can be evaluated in random mode for a roll and in min / max mode for its corresponding totals.

Constructors ​

Constructor ​

new Evaluator(registries): Evaluator

Defined in: src/evaluator/Evaluator.ts:89

Creates an evaluator using the given die and modifier registries.

Parameters ​

registries ​

EvaluatorRegistries

Returns ​

Evaluator

Properties ​

registries ​

readonly registries: EvaluatorRegistries

Defined in: src/evaluator/Evaluator.ts:89

Methods ​

applyModifiers() ​

protected applyModifiers<T>(items, modifiers, target, input): T[]

Defined in: src/evaluator/Evaluator.ts:433

Applies the modifiers in execution order (ModifierRegistry.inExecutionOrder()).

Type Parameters ​

T ​

T extends ScoredItem

Parameters ​

items ​

readonly T[]

modifiers ​

readonly ModifierNode[]

target ​

ModifierTarget

input ​

Omit<ModifierInput, "modifier">

Returns ​

T[]

Throws ​

a handler doesn't apply to the target (e.g. explode on a group)


applyToFlattenedDice() ​

protected applyToFlattenedDice(result, apply): ExpressionResult

Defined in: src/evaluator/Evaluator.ts:262

Applies apply to the flattened dice of every pool in the result, then writes the dice back into their own pools, in the order apply returned them, and recalculates the result.

Each die is tagged with its pool, so the handler may copy, reorder or remove dice.

Parameters ​

result ​

ExpressionResult

apply ​

(dice) => TaggedDieResult[]

Returns ​

ExpressionResult

Throws ​

the handler returned a die that didn't come from a pool


createDie() ​

protected createDie(node, args): ResolvedDie

Defined in: src/evaluator/Evaluator.ts:177

Creates and resolves the die of a pool node, with already evaluated arguments.

Parameters ​

node ​

PoolNode

args ​

readonly number[]

Returns ​

ResolvedDie


evaluate() ​

evaluate(node, ctx): ExpressionResult

Defined in: src/evaluator/Evaluator.ts:96

Evaluates an expression tree into a result tree, rolling dice according to the context.

Parameters ​

node ​

ExpressionNode

ctx ​

RollContext

Returns ​

ExpressionResult

Throws ​

if a node, quantity, operation or modifier result is invalid


evaluateBinary() ​

protected evaluateBinary(node, ctx): BinaryResult

Defined in: src/evaluator/Evaluator.ts:373

Uses operators from math.ts.

Parameters ​

node ​

BinaryNode

ctx ​

RollContext

Returns ​

BinaryResult


evaluateFunction() ​

protected evaluateFunction(node, ctx): FunctionResult

Defined in: src/evaluator/Evaluator.ts:404

Uses functions from math.ts.

Parameters ​

node ​

FunctionNode

ctx ​

RollContext

Returns ​

FunctionResult


evaluateGroup() ​

protected evaluateGroup(node, ctx): GroupResult

Defined in: src/evaluator/Evaluator.ts:191

Evaluates each expression, then applies the modifiers in execution order.

Each handler gets the members, unless its groupHandling says otherwise:

  • flatten: as in v5, a group with a single expression ({4d6+2d8}k2) gives the handler the dice of every pool inside it. The die results are flattened, then the result tree is walked again to write them back into their pools and recalculate everything above them.
  • each-pool: the handler gets each nested group's members and each pool's dice.

Parameters ​

node ​

GroupNode

ctx ​

RollContext

Returns ​

GroupResult


evaluateNumber() ​

protected evaluateNumber(node, _ctx): NumberResult

Defined in: src/evaluator/Evaluator.ts:120

Parameters ​

node ​

NumberNode

_ctx ​

RollContext

Returns ​

NumberResult


evaluateParen() ​

protected evaluateParen(node, ctx): ParenResult

Defined in: src/evaluator/Evaluator.ts:397

Parameters ​

node ​

ParenNode

ctx ​

RollContext

Returns ​

ParenResult


evaluatePool() ​

protected evaluatePool(node, ctx): PoolResult

Defined in: src/evaluator/Evaluator.ts:135

Evaluates qty and the die's args, validates qty against ctx.limits.maxQty, creates the die with registries.dice.create(node.die.name, ...args), resolves it with resolveDie(), rolls it qty times with rollDie(), then applies the modifiers.

Parameters ​

node ​

PoolNode

ctx ​

RollContext

Returns ​

PoolResult


evaluateUnary() ​

protected evaluateUnary(node, ctx): UnaryResult

Defined in: src/evaluator/Evaluator.ts:391

Uses negate from math.ts.

Parameters ​

node ​

UnaryNode

ctx ​

RollContext

Returns ​

UnaryResult


handlerFor() ​

protected handlerFor(modifier, target): ModifierHandler

Defined in: src/evaluator/Evaluator.ts:454

The handler of the modifier.

Parameters ​

modifier ​

ModifierNode

target ​

ModifierTarget

Returns ​

ModifierHandler

Throws ​

no handler is registered, or it doesn't apply to the target


rebuildPools() ​

protected rebuildPools(result, replace, transformMembers?): ExpressionResult

Defined in: src/evaluator/Evaluator.ts:308

Walks the result tree, replacing each pool with replace(pool, index) and recalculating the values above it. Pools are visited in notation order.

Pools in a pool's quantity or die arguments aren't visited (they've already been used), nor are pools in nested groups unless transformMembers is supplied.

Parameters ​

result ​

ExpressionResult

replace ​

(pool, index) => PoolResult

transformMembers? ​

(members) => GroupMemberResult[]

Returns ​

ExpressionResult


runHandler() ​

protected runHandler<T>(handler, items, input): T[]

Defined in: src/evaluator/Evaluator.ts:470

Type Parameters ​

T ​

T extends ScoredItem

Parameters ​

handler ​

ModifierHandler

items ​

readonly T[]

input ​

ModifierInput

Returns ​

T[]

Throws ​

the handler doesn't return an array

MIT Licensed · Credits