Dice infix notation (aka calculator notation) expression evaluator, using the shunting yard algorithm. The dice can have numbers or text/symbols on them. Some operators work only on numbers. It is also possible to tag elements and provide a color. This implementation was inspired by Javaluator. The dice evaluator works on lists, summing the rolls together is optional.
-
Always get the dice rolls in the answer, even for complex expressions
-
Clear defined operator precedence and correct bracket handling
-
Support for custom dice sides with text or symbols instant of numbers
-
Don’t always give the sum of the dice as answer but working with lists
-
Operators can handle, when possible and intuitive, lists of elements/numbers
-
Custom exception for errors in the expression evaluation
-
Usage of as few symbols as possible
-
No infinity loops possible and configurable limits for expensive operations
-
Make testing easy be providing the options to provide a custom, non-random, number generator
-
It should be possible to get the resul of each die (for e.g. pool systems) and to add the dice results together. Therefor the default must be only append elements together and a special operator
=
must be used to sum the elements. -
Exception over unintuitive behavior → It is better that the user knows what is not working then that something is working, but they don’t know what and why
-
not every operator can handel lists → to many operators are not intuitive:
-
is max on list the count or the sum of the elements
-
how to multiply and divide lists
-
The evaluator processes dice expression and returns a list of rolls, each containing a list of elements.
Elements have a value (a number or a text) and can have a color.
For example 2d6
rolls two six-sided dice and returns a list with two elements, each with a value between 1 and 6. To get the sum of the roll, simple add a =
at the end, for example in this case 2d6=
.
The same applies to numbers 3 + 5
has as result a list with the elements 3 and 5, only if written as 3 + 5=
the result is 8.
All non-functional text must be surrounded (escaped) by '
. For example 1d('head' + 'tail')
will flip a coin.
List can be included into the expression by using square brackets.
An empty list []
or empty literal ''
are non value.
For example 1d[2,2,4,4,6,6]
will a die which has two sides with 2, two sides with 4 and two sides with 6. The roll will be a list with one element, which has a value of 2, 4 or 6. Lists also escape characters, so 1d[head,tail]
will also flip a coin.
Multiple expression can be separated by ,
.
For example 3d6, 4d8
will roll two six-sided dice and return a list with two rolls, the first one containing the roll elements of the 3d6
and the second one the roll of the 4d8
.
Operators have a precedent, which is defined by the order of the operators in the table below.
Operators with a higher precedence are evaluated first.
Brackets can be used to change the order of evaluation.
For example 1d4+3d6
is the appending of roll of 1d4 and 3d6, but (1d4+3=)d6)
gets first the sum of the roll of 1d4 and 3 and then rolls this number of d6.
The number of dice is limited to 1000 and every number approve 9 digits or with more than 9 digit after the decimal dot result in an error.
Boolean values will be represented by 'true'
(or 1
) and 'false'
(or 0
) .
It is possible to set tags and color. Colors have no direct effect and will be also set to all random elements of the expression. Tags on the other hand wil change the interaction of with other operators, in most cases operators will work only on elements with the same tag.
All operators are case insensitiv.
Name | Notation | Example | Description | Precedent | Associativity | Left parameter | Right parameter |
---|---|---|---|---|---|---|---|
Repeat |
|
|
Repeats the expression separately a number of times given in <number>. This should be used outside other expressions and will not work inside most expressions |
0 |
left |
a single integer number between 1-10 |
a expression |
List Repeat |
|
|
Repeats the expression a number of times given in <number> and combines the results in one list. |
1 |
left |
a single integer number between 0-20 |
a expression |
Concat |
|
|
Combines the result of both expression into on single element |
2 |
left |
one or more elements |
one or more elements |
Or |
|
|
Boolean or operation of the two boolean values |
3 |
left |
boolean value |
boolean value |
And |
|
|
Boolean and operation of the two boolean values |
4 |
left |
boolean value |
boolean value |
Not |
|
|
Negates the boolean value right from it |
5 |
right |
boolean value |
|
Equal |
|
|
Compare the left and the right and returns true if equal and false otherwise |
6 |
left |
one or more elements |
one or more elements |
Lesser |
|
|
Compare the left and the right and returns true if |
7 |
left |
a single number |
a single number |
Lesser Equal |
|
|
Compare the left and the right and returns true if |
8 |
left |
a single number |
a single number |
Greater |
|
|
Compare the left and the right and returns true if |
9 |
left |
a single number |
a single number |
Greater Equal |
|
|
Compare the left and the right and returns true if |
10 |
left |
a single number |
a single number |
In |
|
|
Returns true if every element in left is contained in right otherwise false |
11 |
left |
a one or more elements |
one or more elements |
Sum |
|
|
Sums the list of on the left side of the symbol. An empty list has the sum of 0 |
12 |
left |
a list of numbers |
- |
Modulo |
|
|
returns the remainder of the division |
13 |
left |
a single integer number |
a single non zero integer number |
Multiply |
|
|
Multiplies the right number with the left number |
14 |
left |
a single number |
a single number |
Divide |
|
|
Divides the right number with the left number and rounds down to the next full number |
15 |
left |
a single integer number |
a single integer number |
Decimal Divide |
|
|
Divides the right number with the left number and provides a decimal number with up to 5 decimal digital |
16 |
left |
a single number |
a single number |
Count |
|
|
Counts the number of elements in a list |
17 |
left |
a list |
- |
Greater Then Filter |
|
|
Keeps only the elements of the left list that are bigger as the right number. Applies only to elements with the same tag. |
18 |
left |
one or more numbers |
a single number |
Lesser Then Filter |
|
|
Keeps only the elements of the left list that are lesser as the right number. Applies only to elements with the same tag. |
19 |
left |
one or more numbers |
a single number |
Greater Equal Then Filter |
|
|
Keeps only the elements of the left list that are bigger or equal as the right number. Applies only to elements with the same tag. |
20 |
left |
one or more numbers |
a single number |
Lesser Equal Then Filter |
|
|
Keeps only the elements of the left list that are lesser or equal as the right number. Applies only to elements with the same tag. |
21 |
left |
one or more numbers |
a single number |
Equal Filter |
|
|
Keeps only the elements of the left list that are equal to the element. Applies only to elements with the same tag. |
22 |
left |
one or more elements |
a single elements |
Keep Highest |
|
|
keeps the highest values out a list, like the roll of multiple dice. Applies only to elements with the same tag. |
23 |
left |
one or more elements |
a single number |
Keep Lowest |
|
|
keeps the lowest values out a list, like the roll of multiple dice. Applies only to elements with the same tag. |
24 |
left |
one or more elements |
a single number |
Add to List |
|
|
Combines the rolls of both sides to a single list. If used as unary operator, it will be ignored e.g. |
25 |
left for binary and right for unary |
none or more elements |
one or more elements |
Remove or Negative add to List |
|
|
Combines the rolls of both sides to a single list. If the element exists on both sides, it will be removed. If the element only exists on the right side and is a number then it will be multiplied with -1 and added |
26 |
left for binary and right for unary |
none or more elements |
numbers or elements that are also elements of the left side |
Reroll |
|
|
Reroll the whole |
27 |
left |
one or more elements |
one or more elements |
Tag |
|
|
Set a tag to all elements of an expression, most operator work on elements with the same tag. The tag will be appended to the name but a number remains a number, even with a text tag. |
28 |
left |
one or more elements |
a single text |
Color |
|
|
Set a color to all elements, and all in it involved random elements, of an expression. The color will not directly given in the result and has no effect on other operations |
29 |
left |
one or more elements |
a single text |
Exploding Add Dice |
|
|
Throws dice and any time the max value of a die is rolled, that die is re-rolled and added to the die previous resul total. A roll of the reroll the sum of the value. |
30 |
left for binary and right for unary |
none or a single positiv integer number (max 1000) |
a single integer number |
Exploding Dice |
|
|
Throws dice and any time the max value of a die is rolled, that die is re-rolled and added to the dice set total. A reroll will be represented as two dice roll elements |
31 |
left for binary and right for unary |
none or a single integer number (max 1000) |
a single positiv integer number |
Regular Dice |
|
|
Throws a number of dice given by the left number. The number sides are given by the right number. If the right side a list, an element of the list is randomly picked. The roll is a list with the dice throw |
32 |
left for binary and right for unary |
none or a single integer number (max 1000) |
a single positiv number or multiple elements |
All functions are case insensitiv.
Name | Notation | Example | Description |
---|---|---|---|
min |
|
|
returns the smallest elements (multiple if the smallest is not unique) of one or more inner expressions. Text is compared alphabetically |
max |
|
|
returns the largest elements (multiple if the largest is not unique) of one or more inner expressions. Text is compared alphabetically |
sort asc |
|
|
sorts all elements ascending of one or more inner expressions. Text is compared alphabetically |
sort desc |
|
|
sorts all elements descending of one or more inner expressions. Text is compared alphabetically |
cancel |
|
|
the elements of listA and listB (can also be single elements) cancel each other and remove each other from the result. |
replace |
|
|
each element in |
color on |
|
|
each element in |
explode |
|
|
Rerolls the |
if |
|
|
if |
group count |
|
|
counts all elements of with the same value and provides the results as list in the format of |
concatenate |
|
|
Joining all expressions together to a single result. |
value |
|
|
Defining a value (that get evaluated once) that can be used in multiple times in the same expression. The value name must be surrounded by two |