From 591290c3f5466d1273194cfe4caff561913e24a1 Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Sun, 6 Oct 2024 15:55:04 +0100 Subject: [PATCH] Refactor section entry extraction for clarity --- M6502/M6502.Symbols/IdentifiableSection.cs | 4 +-- M6502/M6502.Symbols/Parser.cs | 35 ++++++++++++++----- .../ReflectedSectionProperties.cs | 20 +++-------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/M6502/M6502.Symbols/IdentifiableSection.cs b/M6502/M6502.Symbols/IdentifiableSection.cs index 0dad750..c52edf3 100644 --- a/M6502/M6502.Symbols/IdentifiableSection.cs +++ b/M6502/M6502.Symbols/IdentifiableSection.cs @@ -78,7 +78,7 @@ var hasID = this.MaybeExtractIntegerFromParsed(entryName, out var id); if (hasID) { - var (name, type, _) = connection; + var (name, type) = connection; this.ExtractReferenceProperty(id, name, type); } } @@ -117,7 +117,7 @@ var hasIDs = this.MaybeExtractCompoundInteger(entryName, out var ids); if (hasIDs) { - var (name, type, _) = connection; + var (name, type) = connection; this.ExtractReferencesProperty(ids, name, type); } } diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index dcf1c76..d7c22d1 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -12,11 +12,10 @@ { #region Variables, properties etc. - // Section -> Unique ID list of dictionary entries - private Version? _version; private Information? _information; + // Section -> Unique ID list of dictionary entries public Dictionary>> Parsed { get; } = []; public List
SectionEntries { get; } = []; @@ -253,22 +252,37 @@ { throw new ArgumentOutOfRangeException(nameof(key), key, "Debugging section is unavailable"); } + this.ExtractIdentifiableSection(key, into, parsed); + } + private void ExtractIdentifiableSection(string key, List into, List> parsed) where T : IdentifiableSection + { into.Capacity = parsed.Count; for (var id = 0; id < parsed.Count; ++id) { - Debug.Assert(into.Count == id); - var entry = (T?)Activator.CreateInstance(typeof(T), this); - Debug.Assert(entry != null); - var information = parsed[id]; - entry.Parse(information); - into.Add(entry); + this.ExtractIdentifiableEntry(id, parsed, into); } Debug.Assert(into.Count == parsed.Count); - this.VerifyInformationCount(key, into.Count); } + private void ExtractIdentifiableEntry(int id, List> parsed, List into) where T : IdentifiableSection + { + Debug.Assert(into.Count == id); + var information = parsed[id]; + var entry = this.ExtractEntry(information); + Debug.Assert(into.Capacity > id); + into.Add(entry); + } + + private T ExtractEntry(Dictionary information) where T : Section + { + var entry = (T?)Activator.CreateInstance(typeof(T), this); + Debug.Assert(entry != null); + entry.Parse(information); + return entry; + } + private void VerifyInformationCount(string key, int actual) { Debug.Assert(this._information != null); @@ -310,6 +324,9 @@ this.ExtractSections(); this.ExtractReferences(); + + this.Parsed.Clear(); + this.BuildAddressableScopes(); } diff --git a/M6502/M6502.Symbols/ReflectedSectionProperties.cs b/M6502/M6502.Symbols/ReflectedSectionProperties.cs index 7f5dcb3..60bfa44 100644 --- a/M6502/M6502.Symbols/ReflectedSectionProperties.cs +++ b/M6502/M6502.Symbols/ReflectedSectionProperties.cs @@ -24,15 +24,9 @@ internal SectionAttribute? ClassAttribute { get; private set; } - internal Dictionary> Attributes { get; } = []; + internal Dictionary> ReferenceAttributes { get; } = []; - internal Dictionary> ReferenceAttributes { get; } = []; - - internal Dictionary> ReferencesAttributes { get; } = []; - - //public ReflectedSectionProperties() - //{ - //} + internal Dictionary> ReferencesAttributes { get; } = []; public void Build(System.Type type) { @@ -159,18 +153,14 @@ private void ProcessSectionPropertyAttribute(System.Type originalType, string name, SectionPropertyAttribute attribute) { var key = attribute.Key; - this._entriesToProperties.Add(key, name); - this.Attributes.Add(key, new Tuple(name, originalType, attribute)); - Debug.Assert(this.Attributes.Count == this.Properties.Count); - - if (attribute is SectionReferenceAttribute referenceAttribute) + if (attribute is SectionReferenceAttribute) { - this.ReferenceAttributes.Add(key, new Tuple(name, originalType, referenceAttribute)); + this.ReferenceAttributes.Add(key, new Tuple(name, originalType)); } else if (attribute is SectionReferencesAttribute referencesAttribute) { - this.ReferencesAttributes.Add(key, new Tuple(name, originalType, referencesAttribute)); + this.ReferencesAttributes.Add(key, new Tuple(name, originalType)); } var multiples = attribute.Many;