Skip to content

Commit

Permalink
fix + unary priority and rename to add to list (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
twonirwana authored Jan 23, 2024
1 parent fab9397 commit c882b2c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ All operators are case insensitiv.
|Equal Filter |`<list> == <element>` |`3d6==3` |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 |`<list> k <numberToKept>` |`3d6k2` |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 |`<list> l <numberToKept>` |`3d6l2` |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
|Appending |`<left> + <right>` |`2d6 + 2` or `+3` |Combines the rolls of both sides to a single list. If used as unary operator, it will be ignored e.g. `+5` will process to `5` |25 (max for unary) |left for binary and right for unary |none or more elements |one or more elements
|Negative Appending |`<left> - <right>` |`2 - 1` or `-d6` |Combines the rolls of both sides to a single list. The right side is multiplied by -1. |26 |left for binary and right for unary |none or more elements |one or more numbers
|Add to List |`<left> + <right>` |`2d6 + 2` or `+3` |Combines the rolls of both sides to a single list. If used as unary operator, it will be ignored e.g. `+5` will process to `5` |25 |left for binary and right for unary |none or more elements |one or more elements
|Negative add to List |`<left> - <right>` |`2 - 1` or `-d6` |Combines the rolls of both sides to a single list. The right side is multiplied by -1. |26 |left for binary and right for unary |none or more elements |one or more numbers
|Reroll |`<expression>rr<rerollIfIn>` |`10d6rr1` | Reroll the whole `<expression>` once if any of the elements of `<expression>` are in the elements of `<rerollIfIn>` |27 |left|one or more elements|one or more elements
|Tag |`<expression>tag<text>` |`d6 tag 'special'` | 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 |`<expression>col<text>` |`d6 col 'red'` | 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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/janno/evaluator/dice/DiceEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public DiceEvaluator(@NonNull NumberSupplier numberSupplier, int maxNumberOfDice
.add(new RegularDice(numberSupplier, maxNumberOfDice))
.add(new ExplodingDice(numberSupplier, maxNumberOfDice))
.add(new ExplodingAddDice(numberSupplier, maxNumberOfDice))
.add(new Appending())
.add(new AddToList())
.add(new Concat())
.add(new Color())
.add(new Tag())
.add(new Sum())
.add(new Repeat())
.add(new RepeatList())
.add(new NegateOrNegativAppending())
.add(new NegateOrNegativAddToList())
.add(new IntegerDivide())
.add(new DecimalDivide())
.add(new Multiply())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public final class OperatorOrder {
.add(KeepHighest.class)
.add(KeepLowest.class)
//dice should be first
.add(Appending.class)
.add(NegateOrNegativAppending.class)
.add(AddToList.class)
.add(NegateOrNegativAddToList.class)
.add(Reroll.class)
.add(Tag.class)
.add(Color.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import static de.janno.evaluator.dice.RollBuilder.extendAllBuilder;
import static de.janno.evaluator.dice.ValidatorUtil.checkRollSize;

public final class Appending extends Operator {
public Appending() {
super("+", Operator.Associativity.RIGHT, Integer.MAX_VALUE, Operator.Associativity.LEFT, OperatorOrder.getOderNumberOf(Appending.class));
public final class AddToList extends Operator {
public AddToList() {
super("+", Operator.Associativity.RIGHT, OperatorOrder.getOderNumberOf(AddToList.class), Operator.Associativity.LEFT, OperatorOrder.getOderNumberOf(AddToList.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import static de.janno.evaluator.dice.ValidatorUtil.checkContainsOnlyDecimal;
import static de.janno.evaluator.dice.ValidatorUtil.checkRollSize;

public final class NegateOrNegativAppending extends Operator {
public final class NegateOrNegativAddToList extends Operator {
private final static BigDecimal MINUS_ONE = BigDecimal.valueOf(-1);

public NegateOrNegativAppending() {
super("-", Operator.Associativity.RIGHT, OperatorOrder.getOderNumberOf(NegateOrNegativAppending.class), Operator.Associativity.LEFT, OperatorOrder.getOderNumberOf(NegateOrNegativAppending.class));
public NegateOrNegativAddToList() {
super("-", Operator.Associativity.RIGHT, OperatorOrder.getOderNumberOf(NegateOrNegativAddToList.class), Operator.Associativity.LEFT, OperatorOrder.getOderNumberOf(NegateOrNegativAddToList.class));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
| Keep Highest | k | 3d6k2 |
| Keep Lowest | l | 3d6l2 |
| Sum | = | 3d6= |
| Appending | + | 3d6+2d12 |
| Add to List | + | 3d6+2d12 |
| Concatenate | _ | 3d6 _ 'dmg' |
| Negative Appending | - | 3d6-2d12 or -2d6 |
| Negative add to List | - | 3d6-2d12 or -2d6 |
| Decimal Divide | // | 2/6 |
| Divide | / | 12/6 |
| Multiply | * | 12*d6 |
Expand Down
59 changes: 53 additions & 6 deletions src/test/java/de/janno/evaluator/dice/DiceEvaluatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private static Stream<Arguments> generateData() {
Arguments.of("double(9+10+1,10)", List.of(), List.of(9, 10, 10, 1)),
Arguments.of("double(9+10+1,10+9)", List.of(), List.of(9, 9, 10, 10, 1)),
Arguments.of("double(9+10+1,10+9+9)", List.of(), List.of(9, 9, 10, 10, 1)),
Arguments.of("1d+6", List.of(2), List.of(2)),
Arguments.of("+6+6", List.of(), List.of(6, 6)),
Arguments.of("+6*6", List.of(), List.of(36)),
Arguments.of("+6*+6", List.of(), List.of(36)),
Expand Down Expand Up @@ -165,23 +164,66 @@ private static List<String> values(List<Roll> in) {

private static Stream<Arguments> generateStringDiceData() {
return Stream.of(
//custom dice
Arguments.of("d[head/ torso/ left arm/ right arm/ left leg/ right leg]", List.of(3), List.of("left arm")),
Arguments.of("d[head]", List.of(1), List.of("head")),
Arguments.of("3d[head/ torso/ left arm/ right arm/ left leg/ right leg]", List.of(3, 2, 1), List.of("left arm", "torso", "head")),
Arguments.of("3d[head/ torso/ left arm/ right arm/ left leg/ right leg] + 2d6", List.of(3, 2, 1, 4, 5), List.of("left arm", "torso", "head", "4", "5")),
Arguments.of("1d6 + 'a' + 1", List.of(3), List.of("3", "a", "1")),
Arguments.of("1d6 + [x/y] + 1", List.of(3), List.of("3", "x", "y", "1")),
Arguments.of("1d6 + [1d6] + 1", List.of(3), List.of("3", "1d6", "1")),
Arguments.of("1d6 + [1D6] + 1", List.of(3), List.of("3", "1D6", "1")),
Arguments.of("3d[10/20/30] + 2d6", List.of(3, 2, 1, 4, 5), List.of("30", "20", "10", "4", "5")),
Arguments.of("d('head'+'torso'+'left arm'+'right arm'+'left leg'+'right leg')", List.of(3), List.of("left arm")),
Arguments.of("d('head')", List.of(1), List.of("head")),
Arguments.of("3d('head'+'torso'+'left arm'+'right arm'+'left leg'+'right leg')", List.of(3, 2, 1), List.of("left arm", "torso", "head")),
Arguments.of("3d('head'+'torso'+'left arm'+'right arm'+'left leg'+'right leg') + 2d6", List.of(3, 2, 1, 4, 5), List.of("left arm", "torso", "head", "4", "5")),
Arguments.of("3d(10+20+30) + 2d6", List.of(3, 2, 1, 4, 5), List.of("30", "20", "10", "4", "5")),
Arguments.of("+d[a/b/c]", List.of(2), List.of("b")),
Arguments.of("+1d[a/b/c]", List.of(2), List.of("b")),
Arguments.of("1d[a/b/c]", List.of(2), List.of("b")),

//Add to List
Arguments.of("1+1", List.of(), List.of("1","1")),
Arguments.of("1++1", List.of(), List.of("1","1")),
Arguments.of("++1", List.of(), List.of("1")),
Arguments.of("+d6", List.of(1), List.of("1")),
Arguments.of("++d6", List.of(1), List.of("1")),
Arguments.of("1+d6", List.of(2), List.of("1","2")),
Arguments.of("d6+d6", List.of(1,2), List.of("1","2")),
Arguments.of("d6++d6", List.of(1,2), List.of("1","2")),
Arguments.of("1+2d6", List.of(2,3), List.of("1","2","3")),
Arguments.of("d6+1", List.of(2), List.of("2","1")),
Arguments.of("2d6+1", List.of(2,3), List.of("2","3","1")),
Arguments.of("+1+d6", List.of(2), List.of("1","2")),
Arguments.of("+1+2d6", List.of(2,3), List.of("1","2","3")),
Arguments.of("+d6+1", List.of(2), List.of("2","1")),
Arguments.of("+2d6+1", List.of(2,3), List.of("2","3","1")),
Arguments.of("1+d6k1", List.of(2), List.of("2")),
Arguments.of("1+d6k+1", List.of(2), List.of("2")),

//Add negative to List
Arguments.of("1-1", List.of(), List.of("1","-1")),
Arguments.of("1--1", List.of(), List.of("1","1")),
Arguments.of("--1", List.of(), List.of("1")),
Arguments.of("-d6", List.of(1), List.of("-1")),
Arguments.of("--d6", List.of(1), List.of("1")),
Arguments.of("1-d6", List.of(2), List.of("1","-2")),
Arguments.of("d6-d6", List.of(1,2), List.of("1","-2")),
Arguments.of("d6--d6", List.of(1,2), List.of("1","2")),
Arguments.of("1-2d6", List.of(2,3), List.of("1","-2","-3")),
Arguments.of("d6-1", List.of(2), List.of("2","-1")),
Arguments.of("2d6-1", List.of(2,3), List.of("2","3","-1")),
Arguments.of("-1-d6", List.of(2), List.of("-1","-2")),
Arguments.of("-1-2d6", List.of(2,3), List.of("-1","-2","-3")),
Arguments.of("-d6-1", List.of(2), List.of("-2","-1")),
Arguments.of("-2d6-1", List.of(2,3), List.of("-2","-3","-1")),
Arguments.of("1-d6k1", List.of(2), List.of("1")),
Arguments.of("1-d6l1", List.of(2), List.of("-2")),

Arguments.of("1d6 + 'a' + 1", List.of(3), List.of("3", "a", "1")),
Arguments.of("1d6 + [x/y] + 1", List.of(3), List.of("3", "x", "y", "1")),
Arguments.of("1d6 + [1d6] + 1", List.of(3), List.of("3", "1d6", "1")),
Arguments.of("1d6 + [1D6] + 1", List.of(3), List.of("3", "1D6", "1")),
Arguments.of("1d6 + ('a'+'y') + 1", List.of(3), List.of("3", "a", "y", "1")),
Arguments.of("1d6 + '1d6' + 1", List.of(3), List.of("3", "1d6", "1")),
Arguments.of("1d6 + '1D6' + 1", List.of(3), List.of("3", "1D6", "1")),
Arguments.of("3d(10+20+30) + 2d6", List.of(3, 2, 1, 4, 5), List.of("30", "20", "10", "4", "5")),
Arguments.of("ifE(1d6,3,'+3','!3')", List.of(3), List.of("+3")),
Arguments.of("ifE(1d6,3,'+3')", List.of(2), List.of("2")),
Arguments.of("ifE(1d6,3,'+3',4,'+4')", List.of(2), List.of("2")),
Expand Down Expand Up @@ -255,6 +297,10 @@ private static Stream<Arguments> generateStringDiceData() {
Arguments.of("3x(val('$1',1d6)+'$1')", List.of(1, 2, 3), List.of("1", "2", "3")),
Arguments.of("min(3x1d6)", List.of(1, 2, 3), List.of("1")),
Arguments.of("1d6 rr 1", List.of(1, 2), List.of("2")),
Arguments.of("1d6 rr 2+1", List.of(1, 2), List.of("1","1")),
Arguments.of("1d6 rr 2+1d6", List.of(1, 2), List.of("1","2")),
Arguments.of("1d6 rr 2+d6", List.of(1, 2), List.of("1","2")),
Arguments.of("1d6 rr 1+d6", List.of(1, 2,3), List.of("2","3")),
Arguments.of("1d6 rr 3", List.of(1, 2), List.of("1")),
Arguments.of("1d6rr1", List.of(1, 1), List.of("1")),
Arguments.of("2d6 rr [1/5]", List.of(3, 5, 3, 4), List.of("3", "4")),
Expand Down Expand Up @@ -483,6 +529,7 @@ private static Stream<Arguments> generateErrorData() {
Arguments.of("if('false', 'false', [], 'true')", "'if' requires as position 3 input a single boolean but was '[]'"),
Arguments.of("if('false', 'false', , 'true')", "A separator can't be followed by another separator or open bracket"),
Arguments.of(" if('false', 'false', val('$v',1d6) , 'true')", "'if' requires a non-empty input as 3 argument"),
Arguments.of("1d+6", "Not enough values, d needs 2"),

Arguments.of("d", "Operator d has right associativity but the right value was: empty")

Expand Down
Loading

0 comments on commit c882b2c

Please sign in to comment.