Refactoring Binder

This commit is contained in:
Michael Schimmel
2025-09-17 14:25:23 +02:00
parent ea5879520a
commit 0cb6f6e85e
2 changed files with 18 additions and 16 deletions
+17 -16
View File
@@ -75,12 +75,12 @@ type
function GetSlotCount: Integer;
function GetSymbols: TDictionary<string, Integer>;
public
constructor Create(AParent: IScopeDescriptor);
constructor Create(const AParent: IScopeDescriptor);
destructor Destroy; override;
function Define(const Name: string): Integer;
function FindSymbol(const Name: string; out Depth, Index: Integer): Boolean;
function CreateScope(const AParent: IExecutionScope): IExecutionScope;
procedure PopulateFromScope(const AScope: IExecutionScope);
function CreateScope(const Parent: IExecutionScope): IExecutionScope;
procedure PopulateFromScope(Scope: TExecutionScope);
property Symbols: TDictionary<string, Integer> read FSymbols;
end;
@@ -140,8 +140,9 @@ class function TAstBinder.CreateDescriptor(const Scope: IExecutionScope): IScope
begin
if Scope is TExecutionScope then
begin
Result := TScopeDescriptor.Create(CreateDescriptor(Scope.Parent));
(Result as TScopeDescriptor).PopulateFromScope(Scope);
var res := TScopeDescriptor.Create(CreateDescriptor(Scope.Parent));
res.PopulateFromScope(Scope as TExecutionScope);
Result := res;
end
else
Result := TScopeDescriptor.Create(nil);
@@ -216,7 +217,7 @@ begin
if identNode.Address.Kind <> akUnresolved then
Exit;
if (FCurrentDescriptor as TScopeDescriptor).FindSymbol(identNode.Name, depth, idx) then
if FCurrentDescriptor.FindSymbol(identNode.Name, depth, idx) then
begin
if (depth > 0) and (FUpvalueStack.Count > 0) then
begin
@@ -346,7 +347,7 @@ end;
{ TScopeDescriptor }
constructor TScopeDescriptor.Create(AParent: IScopeDescriptor);
constructor TScopeDescriptor.Create(const AParent: IScopeDescriptor);
begin
inherited Create;
FParent := AParent;
@@ -367,16 +368,16 @@ end;
function TScopeDescriptor.FindSymbol(const Name: string; out Depth, Index: Integer): Boolean;
var
currentDescriptor: IScopeDescriptor;
currentDescriptor: TScopeDescriptor;
begin
Depth := 0;
currentDescriptor := Self;
while Assigned(currentDescriptor) do
while currentDescriptor <> nil do
begin
if (currentDescriptor as TScopeDescriptor).FSymbols.TryGetValue(Name, Index) then
Exit(True);
if currentDescriptor.FSymbols.TryGetValue(Name, Index) then
exit(true);
inc(Depth);
currentDescriptor := currentDescriptor.Parent;
currentDescriptor := currentDescriptor.FParent as TScopeDescriptor;
end;
Result := False;
end;
@@ -396,14 +397,14 @@ begin
Result := FSymbols;
end;
function TScopeDescriptor.CreateScope(const AParent: IExecutionScope): IExecutionScope;
function TScopeDescriptor.CreateScope(const Parent: IExecutionScope): IExecutionScope;
begin
Result := TExecutionScope.Create(AParent, Self, nil);
Result := TExecutionScope.Create(Parent, Self, nil);
end;
procedure TScopeDescriptor.PopulateFromScope(const AScope: IExecutionScope);
procedure TScopeDescriptor.PopulateFromScope(Scope: TExecutionScope);
begin
for var pair in (AScope as TExecutionScope).NameToIndex do
for var pair in Scope.NameToIndex do
if not FSymbols.ContainsKey(pair.Key) then
FSymbols.Add(pair.Key, pair.Value);
end;
+1
View File
@@ -78,6 +78,7 @@ type
{$endregion}
function CreateScope(const AParent: IExecutionScope): IExecutionScope;
function Define(const Name: string): Integer;
function FindSymbol(const Name: string; out Depth, Index: Integer): Boolean;
property Parent: IScopeDescriptor read GetParent;
property SlotCount: Integer read GetSlotCount;
property Symbols: TDictionary<string, Integer> read GetSymbols;