From 98ebec0272732765b7020293cbe27b8df6f6858a Mon Sep 17 00:00:00 2001 From: lyrae-versebound Date: Thu, 13 Aug 2026 23:04:28 -0400 Subject: [PATCH 1/2] Support single-die shorthand in dice notation --- GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs | 4 ++++ GoRogue/DiceNotation/Parser.cs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs b/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs index 69f1d4d6..623347f4 100644 --- a/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs +++ b/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs @@ -48,6 +48,10 @@ 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), // 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..c489824e 100644 --- a/GoRogue/DiceNotation/Parser.cs +++ b/GoRogue/DiceNotation/Parser.cs @@ -133,6 +133,9 @@ private static IEnumerable ToPostfix(string infix) } else // Separate so we can increment charIndex differently { + if (infix[charIndex] == 'd' && lastWasOperator) + output.Add("1"); + lastWasOperator = true; switch (infix[charIndex]) { From c594256eec21449d7424102b5ba2f5e613036b38 Mon Sep 17 00:00:00 2001 From: lyrae-versebound Date: Thu, 13 Aug 2026 23:47:39 -0400 Subject: [PATCH 2/2] Preserve dice operand state for shorthand --- GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs | 4 ++++ GoRogue/DiceNotation/Parser.cs | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs b/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs index 623347f4..9bd6735d 100644 --- a/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs +++ b/GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs @@ -52,6 +52,10 @@ public static (string expr, int min, int max)[] DiceExpressions = ("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 c489824e..d8e0146b 100644 --- a/GoRogue/DiceNotation/Parser.cs +++ b/GoRogue/DiceNotation/Parser.cs @@ -133,13 +133,10 @@ private static IEnumerable ToPostfix(string infix) } else // Separate so we can increment charIndex differently { - if (infix[charIndex] == 'd' && lastWasOperator) - output.Add("1"); - - lastWasOperator = true; switch (infix[charIndex]) { case '(': + lastWasOperator = true; operators.Push(infix[charIndex]); break; case ')': @@ -151,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;