diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index 564603c..a82e0a2 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -33,12 +33,8 @@ public List Types { get; } = []; // Symbol sub-types - public List Labels { get; } = []; - public List Equates { get; } = []; - - // Value lookup structures - public Dictionary> Addresses { get; } = []; - public Dictionary> Constants { get; } = []; + public IEnumerable Labels => this.SelectSymbolsByType("lab"); + public IEnumerable Equates => this.SelectSymbolsByType("equ"); // Scope clarification public List AddressableScopes { get; } = []; @@ -54,33 +50,15 @@ private static IEnumerable SelectIdMatching(int id, IEnumerable items) where T : IdentifiableSection => from item in items where item.ID == id select item; + private IEnumerable SelectSymbolsByType(string type) => from symbol in this.Symbols where symbol.Type == type select symbol; + #region Label lookup - public void AddLabel(Symbol symbol) - { - ArgumentOutOfRangeException.ThrowIfNotEqual("lab", symbol.Type, nameof(symbol)); - this.Labels.Add(symbol); - this.AddAddress(symbol); - } - - private void AddAddress(Symbol symbol) - { - var value = symbol.Value; - if (this.Addresses.TryGetValue(value, out var symbols)) - { - symbols.Add(symbol); - } - else - { - this.Addresses.Add(value, [symbol]); - } - } - - private List LookupLabelsByAddress(int address) => this.Addresses.TryGetValue(address, out var symbols) ? symbols : []; + private IEnumerable LookupLabelsByAddress(int address) => from label in this.Labels where label.Value == address select label; public Symbol? LookupLabelByAddress(int address) { - var labels = this.LookupLabelsByAddress(address); + var labels = this.LookupLabelsByAddress(address).ToList(); return labels.Count > 0 ? labels[0] : null; } @@ -104,31 +82,11 @@ #region Constant lookup - public void AddEquate(Symbol symbol) - { - ArgumentOutOfRangeException.ThrowIfNotEqual("equ", symbol.Type, nameof(symbol)); - this.Equates.Add(symbol); - this.AddConstant(symbol); - } - - private void AddConstant(Symbol symbol) - { - var value = symbol.Value; - if (this.Constants.TryGetValue(value, out var symbols)) - { - symbols.Add(symbol); - } - else - { - this.Constants.Add(value, [symbol]); - } - } - - private List LookupEquatesByValue(int constant) => this.Constants.TryGetValue(constant, out var symbols) ? symbols : []; + private IEnumerable LookupEquatesByValue(int constant) => from equate in this.Equates where equate.Value == constant select equate; public Symbol? LookupEquateByValue(int constant) { - var equates = this.LookupEquatesByValue(constant); + var equates = this.LookupEquatesByValue(constant).ToList(); return equates.Count > 0 ? equates[0] : null; } diff --git a/M6502/M6502.Symbols/Symbol.cs b/M6502/M6502.Symbols/Symbol.cs index 16fc173..bcf8a37 100644 --- a/M6502/M6502.Symbols/Symbol.cs +++ b/M6502/M6502.Symbols/Symbol.cs @@ -32,24 +32,6 @@ [SectionEnumeration("type")] public string Type => this.TakeString("type"); - - public override void Parse(IDictionary entries) - { - base.Parse(entries); - if (this.Type is "lab") - { - this._container?.AddLabel(this); - - } - else if (this.Type is "equ") - { - this._container?.AddEquate(this); - } - else - { - throw new InvalidOperationException($"Unknown symbol type: {this.Type}"); - } - } } } }