From 0cb6f6e85e0e61c094d57c3c5e1fbb96a42be3aa Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Wed, 17 Sep 2025 14:25:23 +0200 Subject: [PATCH] Refactoring Binder --- Src/AST/Myc.Ast.Binding.pas | 33 +++++++++++++++++---------------- Src/AST/Myc.Ast.Nodes.pas | 1 + 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Src/AST/Myc.Ast.Binding.pas b/Src/AST/Myc.Ast.Binding.pas index f351573..a26ebcd 100644 --- a/Src/AST/Myc.Ast.Binding.pas +++ b/Src/AST/Myc.Ast.Binding.pas @@ -75,12 +75,12 @@ type function GetSlotCount: Integer; function GetSymbols: TDictionary; 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 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; diff --git a/Src/AST/Myc.Ast.Nodes.pas b/Src/AST/Myc.Ast.Nodes.pas index 7dd5e9c..a29bdee 100644 --- a/Src/AST/Myc.Ast.Nodes.pas +++ b/Src/AST/Myc.Ast.Nodes.pas @@ -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 read GetSymbols;