Skip to content

Commit

Permalink
WI TypeCobolTeam#1301 GetFromTable don't create multiple list but use…
Browse files Browse the repository at this point in the history
… result list passed as arguments

This avoid to create multiple list especially when there is no
symbol found in the current SymbolTable
  • Loading branch information
osmedile committed Mar 10, 2019
1 parent cc6ad9d commit c296c7b
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions TypeCobol/Compiler/CodeModel/SymbolTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,55 @@ public SymbolTable(SymbolTable enclosing, Scope current)
throw new InvalidOperationException("Only Table of INTRINSIC symbols don't have any enclosing scope.");
}

/// <summary>
/// GetFromTableAndEnclosing 2
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <param name="getTableFunction"></param>
/// <param name="maxScope"></param>
/// <returns></returns>
[NotNull]
private List<T> GetFromTableAndEnclosing<T>(string head,
Func<SymbolTable, IDictionary<string, List<T>>> getTableFunction, Scope maxScope = Scope.Intrinsic) where T : Node
{
System.Diagnostics.Debug.Assert(head != null);
var result = new List<T>();
this.GetFromTableAndEnclosing2(head, getTableFunction, result, maxScope);

return result;
}

private void GetFromTableAndEnclosing2<T>([NotNull] string head,
Func<SymbolTable, IDictionary<string, List<T>>> getTableFunction, List<T> result, Scope maxScope = Scope.Intrinsic) where T : Node
{
var table = getTableFunction.Invoke(this);
var values = GetFromTable(head, table);
GetFromTable(head, table, result);
if (EnclosingScope != null && EnclosingScope.CurrentScope >= maxScope)
{
values.AddRange(EnclosingScope.GetFromTableAndEnclosing(head, getTableFunction, maxScope));
EnclosingScope.GetFromTableAndEnclosing2(head, getTableFunction, result, maxScope);
}
return values;
}



[NotNull]
private List<T> GetFromTable<T>(string head, IDictionary<string, List<T>> table) where T : Node
{
if (head != null)
{
List<T> values;
table.TryGetValue(head, out values);
table.TryGetValue(head, out List<T> values);
if (values != null) return values.ToList();
}
return new List<T>();
}

private void GetFromTable<T>(string head, IDictionary<string, List<T>> table, List<T> result) where T : Node
{
table.TryGetValue(head, out List<T> values);
if (values != null) result.AddRange(values);
}

#region DATA SYMBOLS

/// <summary>
Expand Down

0 comments on commit c296c7b

Please sign in to comment.