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