diff --git a/RandomizerCore/Logic/LogicManagerBuilder.cs b/RandomizerCore/Logic/LogicManagerBuilder.cs index dc8b389..923e34e 100644 --- a/RandomizerCore/Logic/LogicManagerBuilder.cs +++ b/RandomizerCore/Logic/LogicManagerBuilder.cs @@ -86,6 +86,7 @@ public enum JsonType Locations, LogicEdit, MacroEdit, + LogicSubst, } public void DeserializeJson(JsonType type, string s) @@ -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>(jtr) ?? Enumerable.Empty()) + { + 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; + } } @@ -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>() ?? Enumerable.Empty()) + { + 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; } } diff --git a/RandomizerCore/Logic/RawSubstDef.cs b/RandomizerCore/Logic/RawSubstDef.cs new file mode 100644 index 0000000..78ec44a --- /dev/null +++ b/RandomizerCore/Logic/RawSubstDef.cs @@ -0,0 +1,6 @@ +namespace RandomizerCore.Logic +{ + public readonly record struct RawSubstDef(string name, string old, string replacement) + { + } +} diff --git a/RandomizerCore/StringLogic/LogicProcessor.cs b/RandomizerCore/StringLogic/LogicProcessor.cs index 79e12ca..7a1ce46 100644 --- a/RandomizerCore/StringLogic/LogicProcessor.cs +++ b/RandomizerCore/StringLogic/LogicProcessor.cs @@ -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];