unit Myc.Ast.Scope; interface uses System.SysUtils, System.Generics.Collections, System.Classes, Myc.Data.Value, Myc.Ast.Nodes; type TExecutionScope = class(TInterfacedObject, IExecutionScope) private FParent: IExecutionScope; FValues: TArray; // Stores references to the value cells captured by a closure. FCapturedUpvalues: TArray; FNameToIndex: TDictionary; procedure DumpScope(const ABuilder: TStringBuilder; AIndent: Integer); function GetParent: IExecutionScope; function GetCell(const Address: TResolvedAddress): IValueCell; public constructor Create( AParent: IExecutionScope = nil; const ADescriptor: IScopeDescriptor = nil; const ACapturedUpvalues: TArray = nil ); destructor Destroy; override; procedure Clear; function Dump: string; procedure Define(const Name: string; const Value: TDataValue); property NameToIndex: TDictionary read FNameToIndex; property Parent: IExecutionScope read FParent; property Values: TArray read FValues; end; implementation uses System.Generics.Defaults; type TValueCell = class(TInterfacedObject, IValueCell) private FValue: TDataValue; // <-- Changed from TAstValue function GetValue: TDataValue; // <-- Changed from TAstValue procedure SetValue(const AValue: TDataValue); // <-- Changed from TAstValue end; { TValueCell } function TValueCell.GetValue: TDataValue; begin Result := FValue; end; procedure TValueCell.SetValue(const AValue: TDataValue); begin FValue := AValue; end; { TExecutionScope } constructor TExecutionScope.Create( AParent: IExecutionScope; const ADescriptor: IScopeDescriptor; const ACapturedUpvalues: TArray ); var slotCount: Integer; i: Integer; begin inherited Create; FParent := AParent; FValues := []; FNameToIndex := TDictionary.Create; FCapturedUpvalues := ACapturedUpvalues; // Store upvalues if ADescriptor <> nil then begin for var item in ADescriptor.Symbols do FNameToIndex.AddOrSetValue(item.Key, item.Value); slotCount := ADescriptor.SlotCount; SetLength(FValues, slotCount); for i := 0 to slotCount - 1 do FValues[i] := TValueCell.Create as IValueCell; end; end; destructor TExecutionScope.Destroy; begin FNameToIndex.Free; inherited Destroy; end; function TExecutionScope.GetCell(const Address: TResolvedAddress): IValueCell; begin case Address.Kind of akUpvalue: begin Assert(Assigned(FCapturedUpvalues), 'Attempt to access an upvalue in a scope with no closure context.'); Assert( (Address.SlotIndex >= 0) and (Address.SlotIndex < Length(FCapturedUpvalues)), 'Invalid upvalue index during value retrieval.' ); Result := FCapturedUpvalues[Address.SlotIndex]; end; akLocalOrParent: begin var targetScope := Self; for var i := 1 to Address.ScopeDepth do begin targetScope := targetScope.Parent as TExecutionScope; Assert(Assigned(targetScope), 'Invalid scope depth during value retrieval.'); end; Assert( (Address.SlotIndex >= 0) and (Address.SlotIndex < Length(targetScope.Values)), 'Invalid scope index during value retrieval.' ); Result := targetScope.Values[Address.SlotIndex]; end; else raise EInvalidOpException.Create('Cannot get value for an unresolved address.'); end; end; function TExecutionScope.GetParent: IExecutionScope; begin Result := FParent; end; procedure TExecutionScope.Clear; begin FValues := []; FNameToIndex.Clear; end; procedure TExecutionScope.Define(const Name: string; const Value: TDataValue); // <-- Changed from TAstValue var index: Integer; begin 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] := TValueCell.Create as IValueCell; FValues[index].Value := Value; FNameToIndex.Add(Name, index); end; procedure TExecutionScope.DumpScope(const ABuilder: TStringBuilder; AIndent: Integer); var pair: TPair; indentStr: string; sortedPairs: TArray>; begin indentStr := ''.PadLeft(AIndent); if (FNameToIndex.Count > 0) then begin sortedPairs := FNameToIndex.ToArray; TArray.Sort>( sortedPairs, TComparer> .Construct(function(const Left, Right: TPair): Integer begin Result := Left.Value - Right.Value; end) ); for pair in sortedPairs do // Access the value inside the cell for printing. ABuilder.AppendLine(indentStr + Format(' [%d] %s: %s', [pair.Value, pair.Key, FValues[pair.Value].Value.ToString])); end else begin ABuilder.AppendLine(indentStr + ' (empty)'); end; if Assigned(FParent) then begin ABuilder.AppendLine(indentStr + '[Parent Scope]'); (FParent as TExecutionScope).DumpScope(ABuilder, AIndent + 2); end; end; function TExecutionScope.Dump: string; var builder: TStringBuilder; begin builder := TStringBuilder.Create; try builder.AppendLine('[Current Scope]'); DumpScope(builder, 0); Result := builder.ToString.TrimRight; finally builder.Free; end; end; end.