Expression Tree¶
This page documents the structure of the Expression object as returned by roll(...).expr
. If you’re looking
for the Expression object returned by parse(...)
, check out Abstract Syntax Tree.

class
d20.
Number
(kept=True, annotation=None)[source]¶ Bases:
abc.ABC
,d20.diceast.ChildMixin
The base class for all expression objects.
Note that Numbers implement all the methods of a
ChildMixin
.
kept
: bool¶ Whether this Number was kept or dropped in the final calculation.

annotation
: Optional[str]¶ The annotation on this Number, if any.

property
children
¶ (readonly) The children of this Number, usually used for traversing the expression tree.

property
left
¶ The leftmost child of this Number, usually used for traversing the expression tree.
 Return type

property
right
¶ The rightmost child of this Number, usually used for traversing the expression tree.
 Return type

property
keptset
¶ Returns the set representation of this object, but only including children whose values were not dropped.


class
d20.
Expression
(roll, comment, **kwargs)[source]¶ Bases:
d20.expression.Number
Expressions are usually the root of all Number trees.

roll
: d20.Number¶ The roll of this expression.

comment
: Optional[str]¶ The comment of this expression.


class
d20.
Literal
(value, **kwargs)[source]¶ Bases:
d20.expression.Number
A literal integer or float.

values
: list[Union[int, float]]¶ The history of numbers that this literal has been.


class
d20.
UnOp
(op, value, **kwargs)[source]¶ Bases:
d20.expression.Number
Represents a unary operation.

op
: str¶ The unary operation.

value
: d20.Number¶ The subtree that the operation operates on.


class
d20.
BinOp
(left, op, right, **kwargs)[source]¶ Bases:
d20.expression.Number
Represents a binary operation.

op
: str¶ The binary operation.

left
: d20.Number¶ The left subtree that the operation operates on.

right
: d20.Number¶ The right subtree that the operation operates on.


class
d20.
Parenthetical
(value, operations=None, **kwargs)[source]¶ Bases:
d20.expression.Number
Represents a value inside parentheses.

value
: d20.Number¶ The subtree inside the parentheses.


class
d20.
Set
(values, operations=None, **kwargs)[source]¶ Bases:
d20.expression.Number
Represents a set of values.

values
: list[d20.Number]¶ The elements of the set.

operations
: list[d20.SetOperation]¶ The operations to run on the set.


class
d20.
Dice
(num, size, values, operations=None, context=None, **kwargs)[source]¶ Bases:
d20.expression.Set
A set of Die.

values
: list[d20.Die]¶ The elements of the set.

operations
: list[d20.SetOperation]¶ The operations to run on the set.


class
d20.
Die
(size, values, context=None)[source]¶ Bases:
d20.expression.Number
Represents a single die.

size
: int¶ How many sides this Die has.

values
: list[d20.Literal]¶ The history of values this die has rolled.


class
d20.
SetOperator
(op, sels)[source]¶ Represents an operation on a set.

op
: str¶ The operation to run on the selected elements of the set.

sels
: list[d20.SetSelector]¶ The selectors that describe how to select operands.
