diff --git a/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs b/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs index 69f1d4d6..9bd6735d 100644 --- a/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs +++ b/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs @@ -48,6 +48,14 @@ public static (string expr, int min, int max)[] DiceExpressions = ("3*2d6", 6, 36), // Single dice ("1d6", 1, 6), + // Single die shorthand + ("d6", 1, 6), + // Single die shorthand with multiply and add + ("d12*2+3", 5, 27), + // Parenthesized expression as dice count + ("(3+2)d10", 5, 50), + // Whitespace before dice operator + ("2 d6", 2, 12), // Single dice with add ("1d6+3", 4, 9), // Single dice with add and multiply diff --git a/GoRogue/DiceNotation/Parser.cs b/GoRogue/DiceNotation/Parser.cs index c311f90b..d8e0146b 100644 --- a/GoRogue/DiceNotation/Parser.cs +++ b/GoRogue/DiceNotation/Parser.cs @@ -133,10 +133,10 @@ private static IEnumerable ToPostfix(string infix) } else // Separate so we can increment charIndex differently { - lastWasOperator = true; switch (infix[charIndex]) { case '(': + lastWasOperator = true; operators.Push(infix[charIndex]); break; case ')': @@ -148,17 +148,22 @@ private static IEnumerable ToPostfix(string infix) op = operators.Pop(); } + lastWasOperator = false; break; } default: { if (s_operatorPrecedence.ContainsKey(infix[charIndex])) { + if (infix[charIndex] == 'd' && lastWasOperator) + output.Add("1"); + while (operators.Count > 0 && s_operatorPrecedence[operators.Peek()] >= s_operatorPrecedence[infix[charIndex]]) output.Add(operators.Pop().ToString()); operators.Push(infix[charIndex]); + lastWasOperator = true; } break;