Skip to content

Commit

Permalink
Fix StringLogic bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
homothetyhk committed Jul 3, 2022
1 parent b39c7a6 commit 817f1e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion RandomizerCore/StringLogic/Infix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
{
public static class Infix
{
private static readonly LogicProcessor _sharedTokenSource = new();

public static List<LogicToken> Tokenize(string infix)
{
return Tokenize(infix, null);
return Tokenize(infix, _sharedTokenSource);
}

public static List<LogicToken> Tokenize(string infix, ITokenSource tokenSource)
Expand Down
22 changes: 11 additions & 11 deletions RandomizerCore/StringLogic/RPN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static int GetBoundOperator(IReadOnlyList<LogicToken> logic, int startInd
else if (logic[index] is TermToken) operands++;
else throw new ArgumentException("Unknown token found in logic.", nameof(logic));

if (operands - operators == 1 && logic[index] is OperatorToken) break;
if (operands - operators <= 1 && logic[index] is OperatorToken) break;
}

if (index == logic.Count) throw new InvalidOperationException("Malformed logic.");
Expand Down Expand Up @@ -74,34 +74,34 @@ internal static Range GetClauseRangeFromEnd(IReadOnlyList<LogicToken> logic, int
/// Returns the disjunctive normal form of the expression. That is,
/// <br/>ORing together the results of ANDing the terms in each list results in an expression equivalent to the input.
/// </summary>
public static List<List<TermToken>> GetDNF(IReadOnlyList<LogicToken> logic)
public static List<HashSet<TermToken>> GetDNF(IReadOnlyList<LogicToken> logic)
{
Stack<List<List<TermToken>>> stack = new();
Stack<List<HashSet<TermToken>>> stack = new();
foreach (LogicToken lt in logic)
{
if (lt is OperatorToken ot)
{
switch (ot.OperatorType)
{
case OperatorType.AND:
List<List<TermToken>> andRight = stack.Pop();
List<List<TermToken>> andLeft = stack.Pop();
List<List<TermToken>> and = new(andRight.Count * andLeft.Count);
List<HashSet<TermToken>> andRight = stack.Pop();
List<HashSet<TermToken>> andLeft = stack.Pop();
List<HashSet<TermToken>> and = new(andRight.Count * andLeft.Count);
for (int i = 0; i < andRight.Count; i++)
{
for (int j = 0; j < andLeft.Count; j++)
{
List<TermToken> c = new(andRight[i].Count + andLeft[j].Count);
c.AddRange(andLeft[j]);
c.AddRange(andRight[i]);
HashSet<TermToken> c = new(andRight[i].Count + andLeft[j].Count);
c.UnionWith(andLeft[j]);
c.UnionWith(andRight[i]);
and.Add(c);
}
}
stack.Push(and);
break;
case OperatorType.OR:
List<List<TermToken>> orRight = stack.Pop();
List<List<TermToken>> orLeft = stack.Pop();
List<HashSet<TermToken>> orRight = stack.Pop();
List<HashSet<TermToken>> orLeft = stack.Pop();
orLeft.AddRange(orRight);
stack.Push(orLeft);
break;
Expand Down

0 comments on commit 817f1e4

Please sign in to comment.