From 494b1ab8aca7742623ed321e3a5b004a5f6e4b0f Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Fri, 7 Jun 2024 18:53:47 +0100 Subject: [PATCH] Let's try some LINQ --- M6502/M6502.Symbols/Parser.cs | 38 +++++++++-------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index 1e249c4..d8a8498 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -44,6 +44,8 @@ #region Lookups + public static IEnumerable SelectNameMatching(string name, IEnumerable items) where T : NamedSection => from item in items where item.Name == name select item; + #region Label lookup public void AddLabel(Symbol symbol) @@ -77,22 +79,11 @@ return labels.Count > 0 ? labels[0] : null; } - public List LookupLabels(string name) - { - var returned = new List(); - foreach (var label in this.Labels) - { - if (label.Name == name) - { - returned.Add(label); - } - } - return returned; - } + public IEnumerable LookupLabels(string name) => SelectNameMatching(name, this.Labels); public Symbol? LookupLabel(string name) { - var labels = this.LookupLabels(name); + var labels = this.LookupLabels(name).ToList(); return labels.Count > 0 ? labels[0] : null; } @@ -135,16 +126,12 @@ #region Scope lookup + public IEnumerable LookupScopes(string name) => SelectNameMatching(name, this.Scopes); + public Scope? LookupScope(string name) { - foreach (var scope in this.Scopes) - { - if (scope.Name == name) - { - return scope; - } - } - return null; + var scopes = this.LookupScopes(name).ToList(); + return scopes.Count > 0 ? scopes[0] : null; } private int LocateScope(int address) @@ -370,13 +357,8 @@ { throw new InvalidOperationException("Fully parsed scopes are unavailable"); } - foreach (var scope in this.Scopes) - { - if (scope.Symbol != null) - { - this.AddressableScopes.Add(scope); - } - } + var scopes = from scope in this.Scopes where scope.Symbol != null select scope; + this.AddressableScopes.AddRange(scopes); } private void ParseLine(string[] elements)