Type System

This commit is contained in:
Michael Schimmel
2025-08-26 11:06:35 +02:00
parent e4681e2bf7
commit 5adbe67d0b
15 changed files with 1426 additions and 612 deletions
+35 -35
View File
@@ -9,7 +9,7 @@ uses
type
// Implements the array data type.
TDataArrayType = class(TInterfacedObject, IDataType, IDataArrayType)
TImplDataArrayType = class(TInterfacedObject, IDataType, IDataArrayType)
private
FElementType: IDataType;
FEmptyValue: IDataArrayValue; // Cached empty array value for this type
@@ -29,7 +29,7 @@ uses
type
// Implements the array data value.
TDataArrayValue = class(TInterfacedObject, IDataValue, IDataArrayValue)
TImplDataArrayValue = class(TInterfacedObject, IDataValue, IDataArrayValue)
private
FArrayType: IDataArrayType;
FItems: TArray<IDataValue>;
@@ -43,7 +43,7 @@ type
end;
// Optimized implementation for an array with 0 elements.
TDataArrayValue0 = class(TInterfacedObject, IDataValue, IDataArrayValue)
TImplDataArrayValue0 = class(TInterfacedObject, IDataValue, IDataArrayValue)
strict private
class var
FEmptyTValue: TValue; // Singleton TValue for an empty array
@@ -60,7 +60,7 @@ type
end;
// Optimized implementation for an array with 1 element.
TDataArrayValue1 = class(TInterfacedObject, IDataValue, IDataArrayValue)
TImplDataArrayValue1 = class(TInterfacedObject, IDataValue, IDataArrayValue)
private
FArrayType: IDataArrayType;
FItem0: IDataValue;
@@ -73,9 +73,9 @@ type
constructor Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
end;
{ TDataArrayValue (generic implementation for N > 1 elements) }
{ TImplDataArrayValue (generic implementation for N > 1 elements) }
constructor TDataArrayValue.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
constructor TImplDataArrayValue.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
var
i: Integer;
begin
@@ -86,12 +86,12 @@ begin
FItems[i] := AItems[i];
end;
function TDataArrayValue.GetDataType: IDataType;
function TImplDataArrayValue.GetDataType: IDataType;
begin
Result := FArrayType;
end;
function TDataArrayValue.GetAsString: string;
function TImplDataArrayValue.GetAsString: string;
var
sb: TStringBuilder;
i: Integer;
@@ -112,7 +112,7 @@ begin
end;
end;
function TDataArrayValue.AsTValue: TValue;
function TImplDataArrayValue.AsTValue: TValue;
var
tvalueArray: TArray<TValue>;
i: Integer;
@@ -123,70 +123,70 @@ begin
Result := TValue.From<TArray<TValue>>(tvalueArray);
end;
function TDataArrayValue.GetElementCount: Integer;
function TImplDataArrayValue.GetElementCount: Integer;
begin
Result := Length(FItems);
end;
function TDataArrayValue.GetItem(Idx: Integer): IDataValue;
function TImplDataArrayValue.GetItem(Idx: Integer): IDataValue;
begin
Result := FItems[Idx];
end;
{ TDataArrayValue0 }
{ TImplDataArrayValue0 }
class constructor TDataArrayValue0.CreateClass;
class constructor TImplDataArrayValue0.CreateClass;
begin
// Create a singleton TValue representing an empty dynamic array.
FEmptyTValue := TValue.From<TArray<TValue>>([]);
end;
constructor TDataArrayValue0.Create(const AArrayType: IDataArrayType);
constructor TImplDataArrayValue0.Create(const AArrayType: IDataArrayType);
begin
inherited Create;
FArrayType := AArrayType;
end;
function TDataArrayValue0.GetAsString: string;
function TImplDataArrayValue0.GetAsString: string;
begin
Result := '[]';
end;
function TDataArrayValue0.AsTValue: TValue;
function TImplDataArrayValue0.AsTValue: TValue;
begin
Result := FEmptyTValue;
end;
function TDataArrayValue0.GetDataType: IDataType;
function TImplDataArrayValue0.GetDataType: IDataType;
begin
Result := FArrayType;
end;
function TDataArrayValue0.GetElementCount: Integer;
function TImplDataArrayValue0.GetElementCount: Integer;
begin
Result := 0;
end;
function TDataArrayValue0.GetItem(Idx: Integer): IDataValue;
function TImplDataArrayValue0.GetItem(Idx: Integer): IDataValue;
begin
raise EArgumentException.Create('Index out of bounds');
end;
{ TDataArrayValue1 }
{ TImplDataArrayValue1 }
constructor TDataArrayValue1.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
constructor TImplDataArrayValue1.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue);
begin
inherited Create;
FArrayType := AArrayType;
FItem0 := AItems[0];
end;
function TDataArrayValue1.GetAsString: string;
function TImplDataArrayValue1.GetAsString: string;
begin
Result := '[' + FItem0.AsString + ']';
end;
function TDataArrayValue1.AsTValue: TValue;
function TImplDataArrayValue1.AsTValue: TValue;
var
tvalueArray: TArray<TValue>;
begin
@@ -195,17 +195,17 @@ begin
Result := TValue.From<TArray<TValue>>(tvalueArray);
end;
function TDataArrayValue1.GetDataType: IDataType;
function TImplDataArrayValue1.GetDataType: IDataType;
begin
Result := FArrayType;
end;
function TDataArrayValue1.GetElementCount: Integer;
function TImplDataArrayValue1.GetElementCount: Integer;
begin
Result := 1;
end;
function TDataArrayValue1.GetItem(Idx: Integer): IDataValue;
function TImplDataArrayValue1.GetItem(Idx: Integer): IDataValue;
begin
case Idx of
0: Result := FItem0;
@@ -214,16 +214,16 @@ begin
end;
end;
{ TDataArrayType }
{ TImplDataArrayType }
constructor TDataArrayType.Create(const AElementType: IDataType);
constructor TImplDataArrayType.Create(const AElementType: IDataType);
begin
inherited Create;
FElementType := AElementType;
FEmptyValue := TDataArrayValue0.Create(Self);
FEmptyValue := TImplDataArrayValue0.Create(Self);
end;
function TDataArrayType.CreateValue(const AItems: array of IDataValue): IDataArrayValue;
function TImplDataArrayType.CreateValue(const AItems: array of IDataValue): IDataArrayValue;
var
item: IDataValue;
i: Integer;
@@ -242,24 +242,24 @@ begin
// Use optimized implementations for arrays with 0 or 1 elements.
case Length(AItems) of
0: Result := FEmptyValue;
1: Result := TDataArrayValue1.Create(Self, AItems);
1: Result := TImplDataArrayValue1.Create(Self, AItems);
else
// Use the generic implementation for arrays with more than 1 element.
Result := TDataArrayValue.Create(Self, AItems);
Result := TImplDataArrayValue.Create(Self, AItems);
end;
end;
function TDataArrayType.GetElementType: IDataType;
function TImplDataArrayType.GetElementType: IDataType;
begin
Result := FElementType;
end;
function TDataArrayType.GetName: String;
function TImplDataArrayType.GetName: String;
begin
Result := Format('Array<%s>', [FElementType.Name]);
end;
function TDataArrayType.GetKind: TDataKind;
function TImplDataArrayType.GetKind: TDataKind;
begin
Result := dkArray;
end;