This commit is contained in:
Michael Schimmel
2025-09-04 21:17:23 +02:00
parent bb74d408da
commit 4a8075fecf
5 changed files with 76 additions and 155 deletions
+30 -37
View File
@@ -44,36 +44,30 @@ type
ICreateSeriesNode = interface;
IAddSeriesItemNode = interface;
ISeriesLengthNode = interface;
IEvaluatorClosure = interface;
IExecutionScope = interface;
IScopeDescriptor = interface;
// --- Concrete Type Definitions ---
TResolvedAddress = record
ScopeDepth: Integer;
SlotIndex: Integer;
end;
IEvaluatorClosure = interface
{$region 'private'}
function GetBody: IExpressionNode;
function GetClosureScope: IExecutionScope;
function GetParameters: TArray<IIdentifierNode>;
{$endregion}
property Body: IExpressionNode read GetBody;
property ClosureScope: IExecutionScope read GetClosureScope;
property Parameters: TArray<IIdentifierNode> read GetParameters;
end;
TAstValue = record
private
type
IClosure = interface
{$region 'private'}
function GetBody: IExpressionNode;
function GetClosureScope: IExecutionScope;
function GetParameters: TArray<IIdentifierNode>;
{$endregion}
property Body: IExpressionNode read GetBody;
property ClosureScope: IExecutionScope read GetClosureScope;
property Parameters: TArray<IIdentifierNode> read GetParameters;
end;
TVal<T> = class(TInterfacedObject)
Value: T;
constructor Create(const AValue: T);
end;
private
var
FKind: TAstValueKind;
FScalar: TScalar;
@@ -84,14 +78,14 @@ type
class operator Initialize(out Dest: TAstValue);
class function Void: TAstValue; inline; static;
class function FromScalar(const AValue: TScalar): TAstValue; inline; static;
class function FromClosure(const AValue: IEvaluatorClosure): TAstValue; inline; static;
class function FromClosure(const AValue: TAstValue.IClosure): TAstValue; inline; static;
class function FromText(const AValue: String): TAstValue; inline; static;
class function FromSeries(const [ref] AValue: TScalarSeries): TAstValue; static; inline;
class function FromRecordSeries(const [ref] AValue: TScalarRecordSeries): TAstValue; static; inline;
class function FromRecord(const [ref] AValue: TScalarRecord): TAstValue; static; inline;
class function FromMemberSeries(const [ref] AValue: TScalarMemberSeries): TAstValue; static; inline;
function AsScalar: TScalar; inline;
function AsClosure: IEvaluatorClosure; inline;
function AsClosure: TAstValue.IClosure; inline;
function AsText: String; inline;
function AsRecordSeries: TVal<TScalarRecordSeries>; inline;
function AsRecord: TScalarRecord; inline;
@@ -102,25 +96,24 @@ type
property Kind: TAstValueKind read GetKind;
end;
TResolvedAddress = record
ScopeDepth: Integer;
SlotIndex: Integer;
end;
IExecutionScope = interface
{$region 'private'}
function GetParent: IExecutionScope;
function GetValues(const Address: TResolvedAddress): TAstValue;
procedure SetValues(const Address: TResolvedAddress; const Value: TAstValue);
{$endregion}
procedure Define(const Name: string; const Value: TAstValue);
function Dump: string;
procedure Clear;
// --- Legacy name-based access ---
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); overload;
// --- New index-based access ---
procedure Define(const Name: string; const Value: TAstValue);
function Dump: string;
property Values[const Address: TResolvedAddress]: TAstValue read GetValues write SetValues; default;
property Parent: IExecutionScope read GetParent;
end;
@@ -130,8 +123,7 @@ type
function GetSlotCount: Integer;
function GetSymbols: TDictionary<string, Integer>;
{$endregion}
function Instantiate(const AParent: IExecutionScope): IExecutionScope;
procedure PopulateFromScope(const AScope: IExecutionScope);
function CreateScope(const AParent: IExecutionScope): IExecutionScope;
property Parent: IScopeDescriptor read GetParent;
property SlotCount: Integer read GetSlotCount;
property Symbols: TDictionary<string, Integer> read GetSymbols;
@@ -173,11 +165,12 @@ type
IIdentifierNode = interface(IExpressionNode)
{$region 'private'}
function GetIsResolved: Boolean;
function GetAddress: TResolvedAddress;
function GetName: string;
{$endregion}
function Resolve: TResolvedAddress;
property IsResolved: Boolean read GetIsResolved;
property Name: string read GetName;
property IsResolved: Boolean read GetIsResolved;
property Address: TResolvedAddress read GetAddress;
end;
IBinaryExpressionNode = interface(IExpressionNode)
@@ -337,11 +330,11 @@ begin
Dest.FInterface := nil;
end;
function TAstValue.AsClosure: IEvaluatorClosure;
function TAstValue.AsClosure: TAstValue.IClosure;
begin
if (FKind <> avkClosure) then
raise EInvalidCast.Create('Cannot read value as a Closure.');
Result := IEvaluatorClosure(FInterface);
Result := TAstValue.IClosure(FInterface);
end;
function TAstValue.AsMemberSeries: TVal<TScalarMemberSeries>;
@@ -388,7 +381,7 @@ begin
Result := (FInterface as TVal<String>).Value;
end;
class function TAstValue.FromClosure(const AValue: IEvaluatorClosure): TAstValue;
class function TAstValue.FromClosure(const AValue: TAstValue.IClosure): TAstValue;
begin
Result.FKind := avkClosure;
Result.FInterface := AValue;