Keywords as basic scalar type

Script enhancements
This commit is contained in:
Michael Schimmel
2025-11-01 00:56:59 +01:00
parent 689dede600
commit 957171f089
7 changed files with 498 additions and 78 deletions
+10 -9
View File
@@ -34,7 +34,7 @@ type
class var
FLock: TSpinLock;
class var
FRegistry: TDictionary<string, IKeyword>;
FRegistry: TDictionary<string, Integer>;
class var
FReverseMap: TList<IKeyword>; // Use TList
class constructor Create;
@@ -116,7 +116,7 @@ end;
class constructor TKeywordRegistry.Create;
begin
FLock := TSpinLock.Create(false);
FRegistry := TDictionary<string, IKeyword>.Create;
FRegistry := TDictionary<string, Integer>.Create;
FReverseMap := TList<IKeyword>.Create;
Intern('false');
@@ -135,24 +135,25 @@ begin
try
// Check bounds
if (AIdx >= 0) and (AIdx < FReverseMap.Count) then
Result := FReverseMap[AIdx].Name
else
Result := ''; // Return empty for safety
Result := FReverseMap[AIdx].Name;
finally
FLock.Exit;
end;
end;
class function TKeywordRegistry.Intern(const AName: string): IKeyword;
var
idx: Integer;
begin
FLock.Enter;
try
if not FRegistry.TryGetValue(AName, Result) then
if not FRegistry.TryGetValue(AName, idx) then
begin
Result := TKeyword.Create(AName, FRegistry.Count);
FReverseMap.Add(Result); // Add to reverse map (Idx -> Interface)
FRegistry.Add(AName, Result); // Add to forward map (Name -> Interface)
end;
FRegistry.Add(AName, FReverseMap.Add(Result));
end
else
Result := FReverseMap[idx];
finally
FLock.Exit;
end;