Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions GoRogue.UnitTests/DiceNotation/DiceNotationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion GoRogue/DiceNotation/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ private static IEnumerable<string> 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 ')':
Expand All @@ -148,17 +148,22 @@ private static IEnumerable<string> 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;
Expand Down