Ast Binding

This commit is contained in:
Michael Schimmel
2025-09-04 01:41:09 +02:00
parent de052cab64
commit 9c90a92b04
6 changed files with 394 additions and 382 deletions
+56 -57
View File
@@ -35,7 +35,7 @@ type
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue; virtual;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue; virtual;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue; virtual;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue; virtual;
end;
// TDebugEvaluatorVisitor now overrides all visit methods for full tracing
@@ -68,6 +68,7 @@ type
function VisitMemberAccess(const Node: IMemberAccessNode): TAstValue; override;
function VisitCreateSeries(const Node: ICreateSeriesNode): TAstValue; override;
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue; override;
function VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue; override;
end;
// Registers all native core functions in the given scope.
@@ -81,7 +82,7 @@ uses
Myc.Data.Series,
Myc.Ast.Scope,
Myc.Ast.Printer,
Myc.Data.Scalar.JSON; // Added for JsonToRecordDefinition
Myc.Data.Scalar.JSON;
type
// The signature for a native Delphi function callable from the script.
@@ -194,9 +195,10 @@ procedure RegisterNativeFunctions(const AScope: IExecutionScope);
var
closure: IEvaluatorClosure;
begin
// Register CreateRecordSeries
// Use 'Define' to clearly state that we are adding a new variable
// to the global scope before the binder runs.
closure := TNativeClosure.Create(NativeCreateRecordSeries);
AScope.SetValue('CreateRecordSeries', TAstValue.FromClosure(closure));
AScope.Define('CreateRecordSeries', TAstValue.FromClosure(closure));
end;
{ TClosureValue }
@@ -280,19 +282,19 @@ end;
function TEvaluatorVisitor.VisitAddSeriesItem(const Node: IAddSeriesItemNode): TAstValue;
var
itemValue, lookbackValue: TAstValue;
itemValue, lookbackValue, seriesVar: TAstValue;
lookback: Int64;
varName: string;
seriesVar: TAstValue;
depth, index: Integer;
begin
// The target series is now guaranteed to be an identifier by the AST node definition.
varName := Node.Series.Name;
if not FScope.FindValue(varName, seriesVar) then
raise EArgumentException.CreateFmt('Identifier not found: "%s"', [varName]);
// The target series must have been resolved by the binder.
if not Node.Series.Resolve(depth, index) then
raise EArgumentException
.CreateFmt('Identifier could not be resolved: "%s". This should have been caught by the binder.', [Node.Series.Name]);
seriesVar := FScope.GetValue(depth, index);
itemValue := Node.Value.Accept(Self);
lookback := -1;
lookback := -1; // Default: no lookback limit
if Assigned(Node.Lookback) then
begin
lookbackValue := Node.Lookback.Accept(Self);
@@ -335,22 +337,20 @@ begin
else
raise EArgumentException.Create('"add" operation is only supported for series types.');
end;
Result := TAstValue.Void;
end;
function TEvaluatorVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
var
varName: string;
value: TAstValue;
depth, index: Integer;
begin
// Evaluate the right-hand side of the assignment.
value := Node.Value.Accept(Self);
varName := Node.Identifier.Name;
// Assign the value to the variable in the scope chain.
FScope.AssignValue(varName, value);
if not Node.Identifier.Resolve(depth, index) then
raise EArgumentException
.CreateFmt('Identifier could not be resolved: "%s". This should have been caught by the binder.', [Node.Identifier.Name]);
// Assignment expressions return the assigned value.
FScope.AssignValue(depth, index, value);
Result := value;
end;
@@ -365,10 +365,8 @@ var
begin
def := Node.Definition.Trim;
// Based on the content, create a scalar or a record series.
if def.StartsWith('[') then
begin
// Assumed to be a record definition array, e.g., '[{"Name": "Close", "Kind": "skDouble"}]'
var recordDef := TRttiAstHelper.JsonToRecordDefinition(def);
if Length(recordDef.Fields) = 0 then
raise EArgumentException.Create('Failed to parse record definition from JSON array.');
@@ -378,7 +376,6 @@ begin
end
else
begin
// Assumed to be a single scalar type name, e.g., 'double'
var scalarKind := StringToScalarKind(def);
var scalarSeries := TScalarSeries.Create(scalarKind, Default(TSeries<TScalarValue>));
Result := TAstValue.FromSeries(scalarSeries);
@@ -387,12 +384,14 @@ end;
function TEvaluatorVisitor.VisitIdentifier(const Node: IIdentifierNode): TAstValue;
var
val: TAstValue;
depth, index: Integer;
begin
if FScope.FindValue(Node.Name, val) then
Result := val
if Node.Resolve(depth, index) then
Result := FScope.GetValue(depth, index)
else
raise EArgumentException.CreateFmt('Identifier not found: "%s"', [Node.Name]);
// This should not happen if the binder ran successfully.
raise EArgumentException
.CreateFmt('Identifier could not be resolved: "%s". This should have been caught by the binder.', [Node.Name]);
end;
function TEvaluatorVisitor.VisitIndexer(const Node: IIndexerNode): TAstValue;
@@ -404,7 +403,6 @@ begin
baseValue := Node.Base.Accept(Self);
indexValue := Node.Index.Accept(Self);
// Index validation (common for all indexable types)
if (indexValue.Kind <> avkScalar) then
raise EArgumentException.Create('Indexer `[]` requires a scalar integer argument.');
@@ -416,7 +414,6 @@ begin
raise EArgumentException.Create('Indexer `[]` requires an integer type argument.');
end;
// Base type dispatching
case baseValue.Kind of
avkSeries:
begin
@@ -484,14 +481,15 @@ end;
function TEvaluatorVisitor.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
var
varName: string;
value: TAstValue;
begin
varName := Node.Identifier.Name;
if Assigned(Node.Initializer) then
Result := Node.Initializer.Accept(Self)
value := Node.Initializer.Accept(Self)
else
Result := TAstValue.Void;
FScope.SetValue(varName, Result);
value := TAstValue.Void;
FScope.Define(Node.Identifier.Name, value);
Result := value;
end;
function TEvaluatorVisitor.VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstValue;
@@ -518,10 +516,8 @@ begin
closure := calleeValue.AsClosure;
// Distinguish between native and script-defined closures.
if closure is TNativeClosure then
begin
// Native function call
SetLength(argValues, Node.Arguments.Count);
for i := 0 to Node.Arguments.Count - 1 do
begin
@@ -531,23 +527,25 @@ begin
end
else if closure is TClosureValue then
begin
// Script function call (original logic)
if (Node.Arguments.Count <> Length(closure.Parameters)) then
raise EArgumentException
.CreateFmt('Argument count mismatch: expected %d, got %d', [Length(closure.Parameters), Node.Arguments.Count]);
callScope := TExecutionScope.Create(closure.ClosureScope);
// The order of definitions must exactly match the binder's order.
// Define 'Self' first.
callScope.Define('Self', calleeValue);
// 2. Then, define the parameters.
for i := 0 to Node.Arguments.Count - 1 do
begin
argValues := [Node.Arguments[i].Accept(Self)];
callScope.SetValue(closure.Parameters[i].Name, argValues[0]);
var argValue := Node.Arguments[i].Accept(Self);
callScope.Define(closure.Parameters[i].Name, argValue);
end;
innerVisitor := Self.CreateVisitorForScope(callScope);
callScope.SetValue('Self', calleeValue);
callScope.SetValue('Result', TAstValue.Void);
Result := closure.Body.Accept(innerVisitor);
end
else
@@ -559,7 +557,6 @@ var
leftValue, rightValue: TAstValue;
leftScalar, rightScalar: TScalar;
begin
// Implemented binary operators for Int64 and Double.
leftValue := Node.Left.Accept(Self);
rightValue := Node.Right.Accept(Self);
@@ -571,7 +568,6 @@ begin
if (leftScalar.Kind <> rightScalar.Kind) then
begin
// Automatic type promotion from Int64 to Double.
if (leftScalar.Kind = skInt64) and (rightScalar.Kind = skDouble) then
begin
leftScalar := TScalar.FromDouble(leftScalar.Value.AsInt64);
@@ -582,7 +578,6 @@ begin
end
else
begin
// If types are still different after promotion, they are incompatible.
raise ENotSupportedException.Create(
'Binary operations are only supported for compatible types. ' + leftScalar.ToString + ' ' + rightScalar.ToString);
end;
@@ -637,7 +632,6 @@ var
rightValue: TAstValue;
rightScalar: TScalar;
begin
// Implemented unary operators for Int64, Double, and Boolean.
rightValue := Node.Right.Accept(Self);
case Node.Operator of
@@ -655,7 +649,6 @@ begin
end;
uoNot:
begin
// "not" operates on the truthiness of the value.
Result := TAstValue.FromScalar(TScalar.FromBoolean(not IsTruthy(rightValue)));
end;
else
@@ -672,11 +665,9 @@ begin
Result := Node.ThenBranch.Accept(Self)
else
begin
// If an else branch exists, evaluate it.
if Assigned(Node.ElseBranch) then
Result := Node.ElseBranch.Accept(Self)
else
// Otherwise, an if-statement without an else branch evaluates to void.
Result := TAstValue.Void;
end;
end;
@@ -695,14 +686,13 @@ end;
function TEvaluatorVisitor.VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
var
expression: IExpressionNode;
lastValue: TAstValue;
begin
lastValue := TAstValue.Void;
// The result of a block is the result of its last expression.
Result := TAstValue.Void;
for expression in Node.Expressions do
begin
lastValue := expression.Accept(Self);
Result := expression.Accept(Self);
end;
Result := lastValue;
end;
function TEvaluatorVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
@@ -710,20 +700,16 @@ var
seriesValue: TAstValue;
len: Int64;
begin
// 1. Evaluate the identifier to get the series object from the scope.
seriesValue := Node.Series.Accept(Self);
// 2. Get the length based on the actual series type.
case seriesValue.Kind of
avkSeries: len := seriesValue.AsSeries.Value.Items.Count;
avkRecordSeries: len := seriesValue.AsRecordSeries.Value.Count;
avkMemberSeries: len := seriesValue.AsMemberSeries.Value.Count;
else
// It's an error if we try to get the length of something that isn't a series.
raise EArgumentException.CreateFmt('Cannot get length of type %s.', [GetEnumName(TypeInfo(TAstValueKind), Ord(seriesValue.Kind))]);
end;
// 3. Return the length as a new scalar value.
Result := TAstValue.FromScalar(TScalar.FromInt64(len));
end;
@@ -811,7 +797,8 @@ end;
function TDebugEvaluatorVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
begin
AppendLine(Format('Assignment %s := {', [Node.Identifier.Name]));
// The name is not easily available anymore for logging.
AppendLine(Format('Assignment to "%s" {', [Node.Identifier.Name]));
Indent;
try
Result := inherited VisitAssignment(Node);
@@ -970,4 +957,16 @@ begin
end;
end;
function TDebugEvaluatorVisitor.VisitSeriesLength(const Node: ISeriesLengthNode): TAstValue;
begin
AppendLine('SeriesLength {');
Indent;
try
Result := inherited VisitSeriesLength(Node);
finally
Unindent;
end;
AppendLine(Format('} -> %s', [Result.ToString]));
end;
end.
+14 -3
View File
@@ -101,9 +101,20 @@ type
function GetParent: IExecutionScope;
{$endregion}
procedure Clear;
function FindValue(const Name: string; out Value: TAstValue): Boolean;
procedure SetValue(const Name: string; const Value: TAstValue);
procedure AssignValue(const Name: string; const Value: TAstValue);
// --- Legacy name-based access (for pre-binder setup, e.g., native functions) ---
function FindValue(const Name: string; out Value: TAstValue): Boolean; deprecated 'Use index-based access after binding.';
procedure SetValue(const Name: string; const Value: TAstValue); deprecated 'Use Define for script variables.';
procedure AssignValue(const Name: string; const Value: TAstValue); overload; deprecated 'Use index-based assignment after binding.';
// --- New index-based access (for use by the evaluator after binding) ---
// Defines a new variable in the current scope.
procedure Define(const Name: string; const Value: TAstValue);
// Gets a value from a scope at a specific depth and slot index.
function GetValue(Depth, Index: Integer): TAstValue;
// Assigns a new value to an existing variable at a specific depth and slot index.
procedure AssignValue(Depth, Index: Integer; const Value: TAstValue); overload;
function Dump: string;
property Parent: IExecutionScope read GetParent;
end;
+105 -20
View File
@@ -12,7 +12,8 @@ type
TExecutionScope = class(TInterfacedObject, IExecutionScope)
private
FParent: IExecutionScope;
FVariables: TDictionary<string, TAstValue>;
FValues: TArray<TAstValue>;
FNameToIndex: TDictionary<string, Integer>;
procedure DumpScope(const ABuilder: TStringBuilder; AIndent: Integer);
protected
// IExecutionScope
@@ -20,27 +21,37 @@ type
procedure Clear;
function FindValue(const Name: string; out Value: TAstValue): Boolean;
procedure SetValue(const Name: string; const Value: TAstValue);
procedure AssignValue(const Name: string; const Value: TAstValue);
procedure AssignValue(const Name: string; const Value: TAstValue); overload;
function Dump: string;
// --- New index-based access methods ---
procedure Define(const Name: string; const Value: TAstValue);
function GetValue(Depth, Index: Integer): TAstValue;
procedure AssignValue(Depth, Index: Integer; const Value: TAstValue); overload;
public
constructor Create(AParent: IExecutionScope = nil);
destructor Destroy; override;
property NameToIndex: TDictionary<string, Integer> read FNameToIndex;
end;
implementation
uses
System.Generics.Defaults; // For TComparer
{ TExecutionScope }
constructor TExecutionScope.Create(AParent: IExecutionScope);
begin
inherited Create;
FParent := AParent;
FVariables := TDictionary<string, TAstValue>.Create;
FValues := [];
FNameToIndex := TDictionary<string, Integer>.Create;
end;
destructor TExecutionScope.Destroy;
begin
FVariables.Free;
FNameToIndex.Free;
inherited Destroy;
end;
@@ -50,32 +61,75 @@ begin
end;
procedure TExecutionScope.AssignValue(const Name: string; const Value: TAstValue);
var
index: Integer;
begin
if FVariables.ContainsKey(Name) then
FVariables.AddOrSetValue(Name, Value)
if FNameToIndex.TryGetValue(Name, index) then
FValues[index] := Value
else if Assigned(FParent) then
FParent.AssignValue(Name, Value)
else
raise Exception.CreateFmt('Cannot assign to undeclared variable "%s".', [Name]);
end;
procedure TExecutionScope.AssignValue(Depth, Index: Integer; const Value: TAstValue);
var
targetScope: IExecutionScope;
i: Integer;
begin
targetScope := Self;
for i := 1 to Depth do
begin
if Assigned(targetScope) then
targetScope := targetScope.Parent
else
// This should not happen if the binder works correctly.
raise EInvalidOpException.Create('Invalid scope depth during assignment.');
end;
// We must cast back to the implementation to modify the private array.
(targetScope as TExecutionScope).FValues[Index] := Value;
end;
procedure TExecutionScope.Clear;
begin
FVariables.Clear;
if Assigned(FParent) then
FParent.Clear;
FValues := [];
FNameToIndex.Clear;
end;
procedure TExecutionScope.Define(const Name: string; const Value: TAstValue);
var
index: Integer;
begin
// A variable can only be defined once per scope.
if FNameToIndex.ContainsKey(Name) then
raise Exception.CreateFmt('Variable "%s" is already defined in this scope.', [Name]);
index := Length(FValues);
SetLength(FValues, index + 1);
FValues[index] := Value;
FNameToIndex.Add(Name, index);
end;
procedure TExecutionScope.DumpScope(const ABuilder: TStringBuilder; AIndent: Integer);
var
pair: TPair<string, TAstValue>;
pair: TPair<string, Integer>;
indentStr: string;
sortedPairs: TArray<TPair<string, Integer>>;
begin
indentStr := ''.PadLeft(AIndent);
if (FVariables.Count > 0) then
if (FNameToIndex.Count > 0) then
begin
for pair in FVariables do
ABuilder.AppendLine(indentStr + Format(' %s: %s', [pair.Key, pair.Value.ToString]));
// Copy pairs to an array and sort it by index for consistent output
sortedPairs := FNameToIndex.ToArray;
TArray.Sort<TPair<string, Integer>>(
sortedPairs,
TComparer<TPair<string, Integer>>
.Construct(function(const Left, Right: TPair<string, Integer>): Integer begin Result := Left.Value - Right.Value; end)
);
for pair in sortedPairs do
ABuilder.AppendLine(indentStr + Format(' [%d] %s: %s', [pair.Value, pair.Key, FValues[pair.Value].ToString]));
end
else
begin
@@ -85,9 +139,7 @@ begin
if Assigned(FParent) then
begin
ABuilder.AppendLine(indentStr + '[Parent Scope]');
// As FParent is now an interface, we must cast it back to the class to call the private DumpScope.
// This indicates that DumpScope should perhaps be part of the interface or handled differently.
// For now, we use a cast to keep the functionality.
// This cast is necessary for this debug helper to access implementation details.
(FParent as TExecutionScope).DumpScope(ABuilder, AIndent + 2);
end;
end;
@@ -107,17 +159,50 @@ begin
end;
function TExecutionScope.FindValue(const Name: string; out Value: TAstValue): Boolean;
var
index: Integer;
begin
Result := FVariables.TryGetValue(Name, Value);
if not Result and Assigned(FParent) then
if FNameToIndex.TryGetValue(Name, index) then
begin
Result := FParent.FindValue(Name, Value);
Value := FValues[index];
Result := True;
Exit;
end;
if Assigned(FParent) then
Result := FParent.FindValue(Name, Value)
else
Result := False;
end;
function TExecutionScope.GetValue(Depth, Index: Integer): TAstValue;
var
targetScope: IExecutionScope;
i: Integer;
begin
targetScope := Self;
for i := 1 to Depth do
begin
if Assigned(targetScope) then
targetScope := targetScope.Parent
else
// This should not happen if the binder works correctly.
raise EInvalidOpException.Create('Invalid scope depth during value retrieval.');
end;
// We must cast back to the implementation to access the private array.
Result := (targetScope as TExecutionScope).FValues[Index];
end;
procedure TExecutionScope.SetValue(const Name: string; const Value: TAstValue);
var
index: Integer;
begin
FVariables.AddOrSetValue(Name, Value);
// This method is for pre-binder setup (e.g. native functions) or initial definition.
if FNameToIndex.TryGetValue(Name, index) then
FValues[index] := Value
else
Define(Name, Value);
end;
end.
+78 -30
View File
@@ -45,7 +45,7 @@ type
// --- Static method to run the binder ---
// Traverses the AST and resolves all identifiers. This must be called before evaluating the AST.
class procedure Bind(const ARootNode: IExpressionNode); static;
class procedure Bind(const ARootNode: IExpressionNode; const AScope: IExecutionScope); static;
end;
// TAstTraverser provides a default AST traversal implementation.
@@ -73,7 +73,8 @@ implementation
uses
System.Classes,
System.Generics.Collections;
System.Generics.Collections,
Myc.Ast.Scope; // Needed for the TExecutionScope cast
type
{ TAstNode }
@@ -296,9 +297,11 @@ type
FParent: TSymbolTable;
FSymbols: TDictionary<string, TSymbol>;
FNextSlotIndex: Integer;
procedure DefinePreResolved(const Name: string; Index: Integer);
public
constructor Create(AParent: TSymbolTable);
destructor Destroy; override;
procedure PopulateFromScope(const AScope: IExecutionScope);
function Define(const Name: string): TSymbol;
function Resolve(const Name: string; out Symbol: TSymbol; out Depth: Integer): Boolean;
end;
@@ -310,12 +313,11 @@ type
procedure EnterScope;
procedure ExitScope;
public
constructor Create;
constructor Create(AInitialScope: IExecutionScope);
destructor Destroy; override;
function VisitIdentifier(const Node: IIdentifierNode): TAstValue; override;
function VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstValue; override;
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue; override;
function VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue; override;
end;
{ TConstantNode }
@@ -745,6 +747,38 @@ begin
inherited;
end;
procedure TSymbolTable.DefinePreResolved(const Name: string; Index: Integer);
var
symbol: TSymbol;
begin
// Defines a symbol with a specific, pre-determined index.
if not FSymbols.ContainsKey(Name) then
begin
symbol.Name := Name;
symbol.SlotIndex := Index;
FSymbols.Add(Name, symbol);
// Ensure the next automatically assigned index is correct.
if (Index >= FNextSlotIndex) then
FNextSlotIndex := Index + 1;
end;
end;
procedure TSymbolTable.PopulateFromScope(const AScope: IExecutionScope);
var
scopeImpl: TExecutionScope;
pair: TPair<string, Integer>;
begin
// ** THE FIX IS HERE **
// Iterate over the Key-Value pairs of the scope's dictionary to preserve the exact indices.
if not Assigned(AScope) then
Exit;
scopeImpl := (AScope as TExecutionScope);
for pair in scopeImpl.NameToIndex do
DefinePreResolved(pair.Key, pair.Value);
end;
function TSymbolTable.Define(const Name: string): TSymbol;
begin
Result.Name := Name;
@@ -772,16 +806,41 @@ end;
{ TBinder }
constructor TBinder.Create;
// Helper function to recursively build the symbol table hierarchy
function CreateSymbolTableFromScope(AScope: IExecutionScope): TSymbolTable;
begin
inherited;
FCurrentScope := TSymbolTable.Create(nil); // Start with a global scope
if not Assigned(AScope) then
Exit(nil);
// Recursively create the parent symbol table first
Result := TSymbolTable.Create(CreateSymbolTableFromScope(AScope.Parent));
// Then populate the current level
Result.PopulateFromScope(AScope);
end;
constructor TBinder.Create(AInitialScope: IExecutionScope);
begin
inherited Create;
// Correctly and recursively build the symbol table hierarchy from the execution scope.
FCurrentScope := CreateSymbolTableFromScope(AInitialScope);
// If no initial scope was provided, create a single empty root scope.
if not Assigned(FCurrentScope) then
FCurrentScope := TSymbolTable.Create(nil);
end;
destructor TBinder.Destroy;
var
scopeToFree: TSymbolTable;
begin
Assert(not Assigned(FCurrentScope.FParent), 'Scope leak in binder.');
FCurrentScope.Free;
// The previous Assert was incorrect for binders initialized with nested scopes.
// This new implementation robustly cleans up the entire symbol table chain.
while Assigned(FCurrentScope) do
begin
scopeToFree := FCurrentScope;
FCurrentScope := FCurrentScope.FParent;
scopeToFree.Free;
end;
inherited;
end;
@@ -799,17 +858,6 @@ begin
oldScope.Free;
end;
function TBinder.VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
begin
EnterScope;
try
// Default traversal of all expressions in the block
inherited VisitBlockExpression(Node);
finally
ExitScope;
end;
end;
function TBinder.VisitIdentifier(const Node: IIdentifierNode): TAstValue;
var
symbol: TSymbol;
@@ -836,6 +884,9 @@ var
begin
EnterScope;
try
// Reserve a slot for 'Self' first.
FCurrentScope.Define('Self');
// 1. Define all parameters in the new scope.
for param in Node.Parameters do
FCurrentScope.Define(param.Name);
@@ -850,24 +901,21 @@ end;
function TBinder.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
begin
// 1. First, visit the initializer expression to resolve its identifiers.
if Assigned(Node.Initializer) then
Node.Initializer.Accept(Self);
// 2. Then, define the new variable in the current scope.
// For recursive functions, we must define the name in the scope FIRST,
// so it can be resolved inside its own initializer (i.e., the lambda body).
FCurrentScope.Define(Node.Identifier.Name);
// 3. Finally, visit the declaration's own identifier to resolve it.
Node.Identifier.Accept(Self);
inherited;
end;
{ TAst }
class procedure TAst.Bind(const ARootNode: IExpressionNode);
class procedure TAst.Bind(const ARootNode: IExpressionNode; const AScope: IExecutionScope);
var
binder: IAstVisitor;
begin
binder := TBinder.Create;
// Create a binder instance, pre-populating its symbol table with the given scope.
binder := TBinder.Create(AScope);
// Traverse the AST to resolve all identifiers.
ARootNode.Accept(binder);
end;