Skip to content

Commit

Permalink
Add ReferenceToken. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
homothetyhk authored Jun 5, 2022
1 parent 5491313 commit 2da7ea7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
18 changes: 17 additions & 1 deletion RandomizerCore/Logic/LogicManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public class LogicManager : ILogicManager
private readonly Dictionary<string, int> _variableIndices;
private readonly Dictionary<string, LogicItem> _items;
private readonly Dictionary<string, LogicTransition> _transitions;
private readonly LogicManagerBuilder _source; // set to null on exiting constructor

public VariableResolver VariableResolver { get; }

public const int intVariableOffset = -100;

public LogicManager(LogicManagerBuilder source)
{
_source = source;
LP = source.LP;
VariableResolver = source.VariableResolver;

Expand Down Expand Up @@ -76,6 +79,8 @@ public LogicManager(LogicManagerBuilder source)
_items[kvp.Key] = kvp.Value;
}
ItemLookup = new(_items);

_source = null;
}

public OptimizedLogicDef GetLogicDef(string name)
Expand Down Expand Up @@ -219,7 +224,18 @@ private void ApplyToken(List<int> logic, LogicToken lt)
}
else if (lt is MacroToken mt)
{
foreach (var tt in mt.Value) ApplyToken(logic, tt);
foreach (LogicToken tt in mt.Value) ApplyToken(logic, tt);
}
else if (lt is ReferenceToken rt)
{
if (_logicDefs.TryGetValue(rt.Target, out OptimizedLogicDef o))
{
OptimizedLogicDef.Concat(logic, o);
}
else if (_source != null && _source.LogicLookup.TryGetValue(rt.Target, out LogicClause lc))
{
foreach (LogicToken tt in lc) ApplyToken(logic, tt);
}
}
else if (lt is SimpleToken st)
{
Expand Down
5 changes: 5 additions & 0 deletions RandomizerCore/Logic/OptimizedLogicDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ private void GetComparisonStrings(ref int i, out string left, out string right)
right = logic[i] >= 0 ? lm.GetTerm(logic[i]).Name : lm.GetVariable(logic[i]).Name;
}

internal static void Concat(List<int> ts, OptimizedLogicDef o)
{
ts.AddRange(o.logic);
}

// cursed hacks below to make polymorphic deserialization work
[JsonConstructor]
private OptimizedLogicDef(string Name, string Logic) : this(Json.LogicDefConverter.Instance.LM.FromString(new(Name, Logic)))
Expand Down
4 changes: 4 additions & 0 deletions RandomizerCore/StringLogic/DictPM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public override bool Evaluate(TermToken token)
{
return Evaluate(mt.Value);
}
else if (token is ReferenceToken rt)
{
throw new NotSupportedException($"Unable to evaluate token: {rt}. DictPM does not store named logic references.");
}
throw new ArgumentException($"Unable to evaluate TermToken: {token}");
}
}
Expand Down
7 changes: 7 additions & 0 deletions RandomizerCore/StringLogic/LogicProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public TermToken GetTermToken(string name)
if (globalTokens.TryGetValue(name, out LogicToken lt) || tokenPool.TryGetValue(name, out lt)) return (TermToken)lt;
else
{
if (name[0] == '*')
{
ReferenceToken rt = new(name[1..]);
tokenPool.Add(name, rt);
return rt;
}

TermToken tt = new SimpleToken(name);
tokenPool.Add(name, tt);
return tt;
Expand Down
8 changes: 8 additions & 0 deletions RandomizerCore/StringLogic/LogicToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public record MacroToken(string Name, IMacroSource Source) : TermToken
public LogicClause Value => Source.GetMacro(Name);
}

/// <summary>
/// TermToken which represents a nested LogicClause by name.
/// </summary>
public record ReferenceToken(string Target) : TermToken
{
public override string Write() => $"*{Target}";
}

/// <summary>
/// TermToken which represents a constant bool.
/// </summary>
Expand Down

0 comments on commit 2da7ea7

Please sign in to comment.