diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index dce0ef6..0d0455f 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -32,25 +32,41 @@ public List Labels { get; } = []; public List Equates { get; } = []; + // Value lookup structures + public Dictionary> Addresses { get; } = []; + public Dictionary> Constants { get; } = []; + #endregion #region Lookups #region Label lookup - public List LookupLabels(int address) + public void AddLabel(Symbol symbol) { - var returned = new List(); - foreach (var label in this.Labels) + if (symbol.Type != "lab") { - if (label.Value == address) - { - returned.Add(label); - } + throw new ArgumentOutOfRangeException(nameof(symbol), "Not a label"); } - return returned; + 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]); + } + } + + public List LookupLabels(int address) => this.Addresses.TryGetValue(address, out var symbols) ? symbols : []; + public Symbol? LookupLabel(int address) { var labels = this.LookupLabels(address); @@ -80,19 +96,31 @@ #region Constant lookup - public List LookupEquates(int constant) + public void AddEquate(Symbol symbol) { - var returned = new List(); - foreach (var equate in this.Equates) + if (symbol.Type != "equ") { - if (equate.Value == constant) - { - returned.Add(equate); - } + throw new ArgumentOutOfRangeException(nameof(symbol), "Not an equate"); } - return returned; + 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]); + } + } + + public List LookupEquates(int constant) => this.Constants.TryGetValue(constant, out var symbols) ? symbols : []; + public Symbol? LookupEquate(int constant) { var equates = this.LookupEquates(constant); diff --git a/M6502/M6502.Symbols/Symbol.cs b/M6502/M6502.Symbols/Symbol.cs index b1ccc80..d390378 100644 --- a/M6502/M6502.Symbols/Symbol.cs +++ b/M6502/M6502.Symbols/Symbol.cs @@ -42,11 +42,12 @@ base.Parse(parent, entries); if (this.Type is "lab") { - this._parent.Labels.Add(this); + this._parent?.AddLabel(this); + } else if (this.Type is "equ") { - this._parent.Equates.Add(this); + this._parent?.AddEquate(this); } else {