Skip to content

Commit

Permalink
Add support for coalescing on ORIG within logic edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
homothetyhk committed Mar 2, 2024
1 parent 69e3bee commit bba2afe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions RandomizerCore/Logic/LogicManagerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ public void DoLogicEdit(RawLogicDef def)
throw new InvalidOperationException($"Logic edit \"{def.logic}\" for {def.name} is malformed.", e);
}

bool exists = LogicLookup.TryGetValue(def.name, out LogicClause orig);
lcb.PartialCoalesce(tt => tt is SimpleToken { Name: "ORIG" } ? exists : null);
if (lcb.Tokens.Any(lt => lt is SimpleToken st && st.Name == "ORIG"))
{
if (!LogicLookup.TryGetValue(def.name, out LogicClause orig))
if (!exists)
{
throw new ArgumentException($"ORIG edit requested for nonexistent logic def {def.name}: {def.logic}");
}
Expand All @@ -191,10 +193,12 @@ public void DoMacroEdit(KeyValuePair<string, string> kvp)
{
throw new InvalidOperationException($"Logic edit \"{kvp.Value}\" for macro {kvp.Key} is malformed.", e);
}


bool exists = LP.IsMacro(kvp.Key);
lcb.PartialCoalesce(tt => tt is SimpleToken { Name: "ORIG" } ? exists : null);
if (lcb.Tokens.Any(lt => lt is SimpleToken st && st.Name == "ORIG"))
{
if (!LP.IsMacro(kvp.Key))
if (!exists)
{
throw new ArgumentException($"ORIG edit requested for nonexistent macro {kvp.Key}: {kvp.Value}");
}
Expand Down Expand Up @@ -243,7 +247,6 @@ public void DoSubst(RawSubstDef def)
}
}


public void DeserializeFile(LogicFileType type, ILogicFormat logicFormat, Stream s)
{
switch(type)
Expand Down
16 changes: 16 additions & 0 deletions RandomizerCore/StringLogic/LogicClauseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ public void Coalesce(Func<TermToken, bool> tokenValidator)
}
}

/// <summary>
/// Replaces CoalescingTokens with their result as determined by the delegate. If the delegate returns null, the CoalescingToken is left in place.
/// Acts recursively on nested coalescing expressions, provided the delegate returns nonnull.
/// </summary>
public void PartialCoalesce(Func<TermToken, bool?> tokenValidator)
{
for (int i = 0; i < _tokens.Count; i++)
{
if (_tokens[i] is CoalescingToken qt && tokenValidator(qt.Left) is bool b)
{
_tokens[i] = b ? qt.Left : qt.Right;
i--;
}
}
}

/// <summary>
/// Applies the delegate to each term in the expression. If the result of the delegate is not null, replaces the term at that position with the result.
/// <br/>Returns the number of modified tokens.
Expand Down

0 comments on commit bba2afe

Please sign in to comment.