Skip to content

Commit

Permalink
Add support for substitutions to the LogicManagerBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
homothetyhk committed Jan 10, 2022
1 parent 395cfb6 commit 73b40a6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
43 changes: 43 additions & 0 deletions RandomizerCore/Logic/LogicManagerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public enum JsonType
Locations,
LogicEdit,
MacroEdit,
LogicSubst,
}

public void DeserializeJson(JsonType type, string s)
Expand Down Expand Up @@ -173,6 +174,28 @@ public void DeserializeJson(JsonType type, JsonTextReader jtr)
LP.SetMacro(kvp.Key, new LogicClause(lcb));
}
break;

case JsonType.LogicSubst:
foreach (RawSubstDef def in JsonUtil.Deserialize<List<RawSubstDef>>(jtr) ?? Enumerable.Empty<RawSubstDef>())
{
TermToken tt = LP.GetTermToken(def.old);
LogicClause lc = LP.ParseInfixToClause(def.replacement);
if (LP.IsMacro(def.name))
{
LogicClauseBuilder lcb = new(LP.GetMacro(def.name));
lcb.Subst(tt, lc);
LP.SetMacro(def.name, new LogicClause(lcb));
}
else if (LogicLookup.TryGetValue(def.name, out LogicClause orig))
{
LogicClauseBuilder lcb = new(orig);
lcb.Subst(tt, lc);
LogicLookup[def.name] = new(lcb);
}
else throw new ArgumentException($"RawSubstDef {def} does not correspond to any known macro or logic.");
}
break;

}
}

Expand Down Expand Up @@ -247,6 +270,26 @@ public void DeserializeJson(JsonType type, JToken t)
LP.SetMacro(kvp.Key, new LogicClause(lcb));
}
break;
case JsonType.LogicSubst:
foreach (RawSubstDef def in t.ToObject<List<RawSubstDef>>() ?? Enumerable.Empty<RawSubstDef>())
{
TermToken tt = LP.GetTermToken(def.old);
LogicClause lc = LP.ParseInfixToClause(def.replacement);
if (LP.IsMacro(def.name))
{
LogicClauseBuilder lcb = new(LP.GetMacro(def.name));
lcb.Subst(tt, lc);
LP.SetMacro(def.name, new LogicClause(lcb));
}
else if (LogicLookup.TryGetValue(def.name, out LogicClause orig))
{
LogicClauseBuilder lcb = new(orig);
lcb.Subst(tt, lc);
LogicLookup[def.name] = new(lcb);
}
else throw new ArgumentException($"RawSubstDef {def} does not correspond to any known macro or logic.");
}
break;
}
}

Expand Down
6 changes: 6 additions & 0 deletions RandomizerCore/Logic/RawSubstDef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace RandomizerCore.Logic
{
public readonly record struct RawSubstDef(string name, string old, string replacement)
{
}
}
2 changes: 2 additions & 0 deletions RandomizerCore/StringLogic/LogicProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public void SetMacro(string key, LogicClause c)
if (lt is null) tokenPool[key] = new MacroToken(key, this);
}

public bool IsMacro(string name) => macros.ContainsKey(name);

public LogicClause GetMacro(string name)
{
return macros[name];
Expand Down

0 comments on commit 73b40a6

Please sign in to comment.