-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic generic map/stringmap/heap functionality
- Loading branch information
Showing
20 changed files
with
1,832 additions
and
939 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
{ | ||
Author: Raymond van Venetië and Merlijn Wajer | ||
Project: Simba (https://github.com/MerlijnWajer/Simba) | ||
License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0) | ||
} | ||
unit simba.ide_codetools_generics; | ||
|
||
{$i simba.inc} | ||
|
||
interface | ||
|
||
uses | ||
Classes, SysUtils, | ||
simba.base, simba.ide_codetools_parser; | ||
|
||
const | ||
MAP_METHODS = | ||
'function <MapName>.Exists(Key: <KeyType>): Boolean; external;' + LineEnding + | ||
'property <MapName>.Value(Key: <KeyType>): <ValueType>; external;' + LineEnding + | ||
'property <MapName>.Value(Key: <KeyType>; Value: <ValueType>); external;' + LineEnding + | ||
'property <MapName>.Count: Integer; external;' + LineEnding + | ||
'procedure <MapName>.Clear; external;' + LineEnding + | ||
'function <MapName>.IndexOf(Key: <KeyType>): Integer; external;' + LineEnding + | ||
'procedure <MapName>.Delete(Key: <KeyType>); external;' + LineEnding + | ||
'procedure <MapName>.DeleteIndex(Index: Integer); external;' + LineEnding + | ||
'function <MapName>.ToString: String; external;' + LineEnding + | ||
'property <MapName>.ValueFromIndex(Index: Integer): <ValueType>; external;' + LineEnding + | ||
'property <MapName>.ValueFromIndex(Index: Integer; Value: <ValueType>); external;' + LineEnding + | ||
'property <MapName>.KeyFromIndex(Index: Integer): <KeyType>; external;' + LineEnding + | ||
'property <MapName>.KeyFromIndex(Index: Integer; Key: <KeyType>); external;' + LineEnding + | ||
'property <MapName>.Values: array of <ValueType>; external;' + LineEnding + | ||
'property <MapName>.Keys: array of <KeyType>; external;' + LineEnding + | ||
'property <MapName>.InvalidVal: <ValueType>; external;' + LineEnding + | ||
'property <MapName>.InvalidVal(Value: <ValueType>); external;' + LineEnding; | ||
|
||
STRINGMAP_METHODS = MAP_METHODS + | ||
'procedure <MapName>.Load(FileName: String; Sep: String; StrToValue: function(Str: String): <ValueType>); external;' + LineEnding + | ||
'procedure <MapName>.Save(FileName: String; Sep: String; ValueToStr: function(Value: <ValueType>): String); external;' + LineEnding + | ||
'property <MapName>.CaseSens: Boolean; external;' + LineEnding + | ||
'property <MapName>.CaseSens(Value: Boolean); external;' + LineEnding; | ||
|
||
HEAP_METHODS = | ||
'procedure <HeapName>.Push(Value: <ValueType>; Index: Integer); external;' + LineEnding + | ||
'property <HeapName>.Pop: record Value: <ValueType>; Index: Integer; end; external;' + LineEnding + | ||
'property <HeapName>.Peek: record Value: <ValueType>; Index: Integer; end; external;' + LineEnding + | ||
'property <HeapName>.Items: array of record Value: <ValueType>; Index: Integer; end; external;' + LineEnding + | ||
'property <HeapName>.Count: Integer; external;' + LineEnding + | ||
'procedure <HeapName>.Clear; external;' + LineEnding + | ||
'function <HeapName>.ToString: String; external;' + LineEnding; | ||
|
||
function GetGeneric(Decl: TDeclaration): TDeclarationArray; | ||
|
||
implementation | ||
|
||
var | ||
GenericParsers: TCodeParserList; | ||
|
||
function GetGeneric(Decl: TDeclaration): TDeclarationArray; | ||
|
||
function RunStrMap(Name, Key, Value: String): TCodeParser; | ||
var | ||
I: Integer; | ||
Methods, FileName: String; | ||
begin | ||
FileName := '!GenericStringMap::' + Name + '::' + Value; | ||
for I := 0 to GenericParsers.Count - 1 do | ||
if (GenericParsers[I].Lexer.FileName = FileName) then | ||
Exit(GenericParsers[I]); | ||
|
||
Methods := STRINGMAP_METHODS; | ||
Methods := Methods.Replace('<MapName>', Name); | ||
Methods := Methods.Replace('<KeyType>', Key); | ||
Methods := Methods.Replace('<ValueType>', Value); | ||
|
||
Result := TCodeParser.Create(); | ||
Result.SetScript(Methods); | ||
Result.Run(); | ||
|
||
GenericParsers.Add(Result); | ||
end; | ||
|
||
function RunMap(Name, Key, Value: String): TCodeParser; | ||
var | ||
I: Integer; | ||
Methods, FileName: String; | ||
begin | ||
FileName := '!GenericMap::' + Name + '::' + Key + ', ' + Value; | ||
for I := 0 to GenericParsers.Count - 1 do | ||
if (GenericParsers[I].Lexer.FileName = FileName) then | ||
Exit(GenericParsers[I]); | ||
|
||
Methods := MAP_METHODS; | ||
Methods := Methods.Replace('<MapName>', Name); | ||
Methods := Methods.Replace('<KeyType>', Key); | ||
Methods := Methods.Replace('<ValueType>', Value); | ||
|
||
Result := TCodeParser.Create(); | ||
Result.SetScript(Methods); | ||
Result.Run(); | ||
|
||
GenericParsers.Add(Result); | ||
end; | ||
|
||
function RunHeap(Name, Value: String): TCodeParser; | ||
var | ||
I: Integer; | ||
Methods, FileName: String; | ||
begin | ||
FileName := '!GenericHeap::' + Name + '::' + Value; | ||
for I := 0 to GenericParsers.Count - 1 do | ||
if (GenericParsers[I].Lexer.FileName = FileName) then | ||
Exit(GenericParsers[I]); | ||
|
||
Methods := HEAP_METHODS; | ||
Methods := Methods.Replace('<HeapName>', Name); | ||
Methods := Methods.Replace('<ValueType>', Value); | ||
|
||
Result := TCodeParser.Create(); | ||
Result.SetScript(Methods); | ||
Result.Run(); | ||
|
||
GenericParsers.Add(Result); | ||
end; | ||
|
||
var | ||
Parser: TCodeParser; | ||
Params: TDeclarationArray; | ||
Name, Kind: String; | ||
begin | ||
Parser := nil; | ||
|
||
if (Decl is TDeclaration_TypeFakeGeneric) then | ||
begin | ||
Kind := Decl.Items.GetTextOfClass(TDeclaration_Identifier); | ||
Name := Decl.Name; | ||
if (Name = '') then | ||
Name := Kind; | ||
|
||
case LowerCase(Kind) of | ||
'stringmap': | ||
begin | ||
Params := Decl.Items.GetByClass(TDeclaration_Parameter, True, True); | ||
if Length(Params) = 1 then | ||
Parser := RunStrMap(Name, 'String', Params[0].Name); | ||
end; | ||
|
||
'map': | ||
begin | ||
Params := Decl.Items.GetByClass(TDeclaration_Parameter, True, True); | ||
if Length(Params) = 2 then | ||
Parser := RunMap(Name, Params[0].Name, Params[1].Name); | ||
end; | ||
|
||
'heap': | ||
begin | ||
Params := Decl.Items.GetByClass(TDeclaration_Parameter, True, True); | ||
if Length(Params) = 1 then | ||
Parser := RunHeap(Name, Params[0].Name); | ||
end; | ||
end; | ||
end; | ||
|
||
if (Parser <> nil) then | ||
Result := Parser.Items.ToArray | ||
else | ||
Result := []; | ||
end; | ||
|
||
initialization | ||
GenericParsers := TCodeParserList.Create(True); | ||
|
||
finalization | ||
FreeAndNil(GenericParsers); | ||
|
||
end. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.