Skip to content

Commit

Permalink
Codetools tweaks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jul 13, 2024
1 parent 9c04cd9 commit e8d4265
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 42 deletions.
52 changes: 50 additions & 2 deletions Source/ide/codetools/simba.ide_codetools_arrayhelpers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ interface
'procedure <ArrayName>.Append(Value: <ArrayVarType>); external;' + LineEnding +
'procedure <ArrayName>.Extend(Value: <ArrayName>); external;';

HELPERS_DYNARRAY_UNTYPED =
'function <ArrayName>.Low: Integer; external;' + LineEnding +
'function <ArrayName>.High: Integer; external;' + LineEnding +
'function <ArrayName>.Contains(Value: array of <ArrayVarType>): Boolean; external;' + LineEnding +
'procedure <ArrayName>.Swap(FromIndex, ToIndex: Integer); external;' + LineEnding +
'function <ArrayName>.Unique: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.IndexOf(Value: array of <ArrayVarType>): Integer; external;' + LineEnding +
'function <ArrayName>.IndicesOf(Value: array of <ArrayVarType>): TIntegerArray; external;' + LineEnding +
'procedure <ArrayName>.Sort; external;' + LineEnding +
'procedure <ArrayName>.Sort(CompareFunc: function(constref L, R: array of <ArrayVarType>): Integer); external;' + LineEnding +
'procedure <ArrayName>.Sort(Weights: TIntegerArray; LowToHigh: Boolean); external;' + LineEnding +
'function <ArrayName>.Sorted: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Sorted(CompareFunc: function(constref L, R: array of <ArrayVarType>): Integer): array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Sorted(Weights: TIntegerArray; LowToHigh: Boolean): array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Length: Integer; external;' + LineEnding +
'function <ArrayName>.Copy: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Copy(StartIndex: Integer; Count: Integer = High(Integer)): array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.First: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Last: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.RandomValue: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Reversed: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Min: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Max: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Sum: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Mode: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Median: Double; external;' + LineEnding +
'function <ArrayName>.Mean: Double; external;' + LineEnding +
'function <ArrayName>.Variance: Double; external;' + LineEnding +
'function <ArrayName>.Stdev: Double; external;' + LineEnding +
'function <ArrayName>.Slice(Start, Stop, Step: Integer): array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Remove(Value: array of <ArrayVarType>): Boolean; external;' + LineEnding +
'function <ArrayName>.RemoveAll(Value: array of <ArrayVarType>): Integer; external;' + LineEnding +
'procedure <ArrayName>.Delete(Index: Integer; Count: Integer = High(Integer)); external;' + LineEnding +
'procedure <ArrayName>.Insert(Item: array of <ArrayVarType>; Index: Integer); external;' + LineEnding +
'procedure <ArrayName>.SetLength(NewLength: Integer); external;' + LineEnding +
'function <ArrayName>.Pop: array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.Pop(Index: Integer): array of <ArrayVarType>; external;' + LineEnding +
'function <ArrayName>.RandomValue: array of <ArrayVarType>; external;' + LineEnding +
'procedure <ArrayName>.Reverse; external;' + LineEnding +
'procedure <ArrayName>.Clear; external;' + LineEnding +
'procedure <ArrayName>.Append(Value: array of <ArrayVarType>); external;' + LineEnding +
'procedure <ArrayName>.Extend(Value: array of <ArrayVarType>); external;';

HELPERS_STATICARRAY =
'function <ArrayName>.Low: Integer; external;' + LineEnding +
'function <ArrayName>.High: Integer; external;' + LineEnding +
Expand Down Expand Up @@ -142,14 +185,19 @@ function GetArrayHelpers(Decl: TDeclaration): TDeclarationArray;
begin
ArrName := Decl.Name;
if (ArrName = '') then
ArrName := 'Array';

ArrName := 'array';
ArrType := Decl.Items.GetTextOfClass(TDeclaration_VarType);

if (ArrType <> '') then
begin
if (Decl is TDeclaration_TypeStaticArray) then
Parser := Run(HELPERS_STATICARRAY, ArrName, ArrType)
else
if (ArrName = 'array') then
Parser := Run(HELPERS_DYNARRAY_UNTYPED, ArrName, ArrType)
else
Parser := Run(HELPERS_DYNARRAY, ArrName, ArrType);
end;
end
else if (Decl is TDeclaration_TypeAlias) then
begin
Expand Down
77 changes: 44 additions & 33 deletions Source/ide/codetools/simba.ide_codetools_exprparser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface
Classes, SysUtils,
simba.ide_codetools_base, simba.ide_codetools_insight, simba.ide_codetools_parser;

function ParseExpression(Codeinsight: TCodeinsight; Expr: String): TDeclaration;
function ParseExpression(Codeinsight: TCodeinsight; Expr: String; out Members: TDeclarationArray): TDeclaration;

implementation

Expand Down Expand Up @@ -117,46 +117,40 @@ function StringToExpression(const Str: String): TExpressionItems;
end;
end;

function ParseExpression(Codeinsight: TCodeinsight; Expr: String): TDeclaration;
function ParseExpression(Codeinsight: TCodeinsight; Expr: String; out Members: TDeclarationArray): TDeclaration;

function Resolve(decls: TDeclarationArray; what: String; DoResolveType: Boolean = True): TDeclaration;
function Resolve(decls: TDeclarationArray; var Item: TExpressionItem): TDeclaration;
var
I, J: Integer;
begin
Result := nil;

for i := 0 to High(decls) do
if decls[i].IsName(what) then
for i := 0 to High(Decls) do
if Decls[i].IsName(Item.Text) then
begin
if not DoResolveType then
begin
Result := Decls[i];
// ideally return a func if there are multiple matches as that result type might be wanted
for J := I+1 to High(Decls) do
if Decls[J].IsName(what) and (Decls[J] is TDeclaration_Method) and (TDeclaration_Method(Decls[J]).ResultType <> nil) then
begin
Result := Decls[J];
Break;
end;
Exit;
end;

if decls[i] is TDeclaration_Type then
if Decls[i] is TDeclaration_Type then
begin
Result := Decls[i];
Exit;
end;

if decls[i] is TDeclaration_Method then
if TDeclaration_Method(decls[i]).ResultType <> nil then
if Decls[i] is TDeclaration_Method then
if TDeclaration_Method(Decls[i]).ResultType <> nil then
begin
Result := Codeinsight.ResolveVarType(TDeclaration_Method(decls[i]).ResultType);
// indexable property.. clear symbol.
if (Decls[i] is TDeclaration_Property) and (TDeclaration_Property(Decls[i]).ParamCount > 1) then
begin
Item.Symbols.DimCount := 0;
Item.HasSymbols := Item.Symbols.Deref or Item.Symbols.DerefDim;
end;

Result := Codeinsight.ResolveVarType(TDeclaration_Method(Decls[i]).ResultType);
Exit;
end;

if (decls[i] is TDeclaration_Var) then
if (Decls[i] is TDeclaration_Var) then
begin
Result := Codeinsight.ResolveVarType(TDeclaration_Var(decls[i]).VarType);
Result := Codeinsight.ResolveVarType(TDeclaration_Var(Decls[i]).VarType);
Exit;
end;
end;
Expand Down Expand Up @@ -213,6 +207,7 @@ function ParseExpression(Codeinsight: TCodeinsight; Expr: String): TDeclaration;

begin
Result := Decl;

if Item.HasSymbols then
begin
if Item.Symbols.Deref then Result := DoPointerDerefence(Result);
Expand All @@ -224,39 +219,55 @@ function ParseExpression(Codeinsight: TCodeinsight; Expr: String): TDeclaration;
var
Items: TExpressionItems;
Decl: TDeclaration;
Decls: TDeclarationArray;
i: Integer;
I: Integer;
Last: TExpressionItem;
begin
Result := nil;

Items := StringToExpression(Expr);
if Length(Items) = 0 then
Exit;
Decls := Codeinsight.Get(Items[0].Text);
Members := Codeinsight.Get(Items[0].Text);

for i := 0 to High(Items) - 1 do
begin
Decl := Resolve(Decls, Items[I].Text);
Decl := Resolve(Members, Items[I]);
if (Decl = nil) then
begin
CodetoolsMessage('Failed resolving ' + Items[I].Text);
Break;
end;

Decl := ResolveSymbols(Decl, Items[I]);
if (Decl = nil) then
begin
CodetoolsMessage('Failed resolving symbols ' + Items[I].Text);
Break;
end;

if Decl is TDeclaration_Type then
Decls := Codeinsight.GetTypeMembers(Decl as TDeclaration_Type)
if (Decl is TDeclaration_Type) then
Members := Codeinsight.GetTypeMembers(Decl as TDeclaration_Type)
else
Decls := Codeinsight.Get(Decl.Name);
Members := Codeinsight.Get(Decl.Name);
end;

Result := Resolve(Decls, Items[High(Items)].Text, Items[High(Items)].HasSymbols);
Result := ResolveSymbols(Result, Items[High(Items)]);
// last item is special
Last := Items[High(Items)];
for I := 0 to High(Members) do
if Members[I].IsName(Last.Text) then
begin
if (Members[I] is TDeclaration_Property) and (TDeclaration_Property(Members[I]).ParamCount > 1) then
begin
Last.Symbols.DimCount := 0;
Last.HasSymbols := Last.Symbols.Deref or Last.Symbols.DerefDim;
end;

Result := Members[I];
Break;
end;

if (Result <> nil) and Last.HasSymbols then
Result := ResolveSymbols(Result, Last);
end;

end.
Expand Down
13 changes: 11 additions & 2 deletions Source/ide/codetools/simba.ide_codetools_insight.pas
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ TCodeinsight = class(TObject)

function Get(Name: String): TDeclarationArray;
function GetTypeMembers(Decl: TDeclaration_Type; Methods: Boolean = True): TDeclarationArray;
function ParseExpr(Expr: String): TDeclaration;

function ParseExpr(Expr: String; out Members: TDeclarationArray): TDeclaration; overload;
function ParseExpr(Expr: String): TDeclaration; overload;

constructor Create;
destructor Destroy; override;
Expand Down Expand Up @@ -417,9 +419,16 @@ function TCodeinsight.GetTypeMembers(Decl: TDeclaration_Type; Methods: Boolean):
end;
end;

function TCodeinsight.ParseExpr(Expr: String; out Members: TDeclarationArray): TDeclaration;
begin
Result := ParseExpression(Self, Expr, Members);
end;

function TCodeinsight.ParseExpr(Expr: String): TDeclaration;
var
_: TDeclarationArray;
begin
Result := ParseExpression(Self, Expr);
Result := ParseExpression(Self, Expr, _);
end;

constructor TCodeinsight.Create;
Expand Down
3 changes: 2 additions & 1 deletion Source/ide/codetools/simba.ide_codetools_parser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2320,7 +2320,8 @@ function PropertyHeader(Decl: TDeclaration_Property; FullHeader: Boolean): Strin
end;
Result += ']';
end;
Result += ': ' + Params[High(Params)].Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarType);
if Length(Params) > 0 then
Result += ': ' + Params[High(Params)].Items.GetTextOfClassNoCommentsSingleLine(TDeclaration_VarType);
end;
end;
end;
Expand Down
8 changes: 4 additions & 4 deletions Source/ide/simba.ide_editor_paramhint.pas
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ procedure TSimbaParamHint.DoEditorCommand(Sender: TObject; AfterProcessing: Bool

var
Decl: TDeclaration;
Decls: TDeclarationArray;
Members, Decls: TDeclarationArray;
IsIndexing: Boolean;
P: TPoint;
begin
Expand All @@ -487,7 +487,7 @@ procedure TSimbaParamHint.DoEditorCommand(Sender: TObject; AfterProcessing: Bool
FCodeinsight.Run();

Decls := [];
Decl := FCodeinsight.ParseExpr(GetExpression(FParenthesesPoint.X -1, FParenthesesPoint.Y));
Decl := FCodeinsight.ParseExpr(GetExpression(FParenthesesPoint.X - 1, FParenthesesPoint.Y), Members);

if (Decl <> nil) then
begin
Expand All @@ -501,13 +501,13 @@ procedure TSimbaParamHint.DoEditorCommand(Sender: TObject; AfterProcessing: Bool
// properties
if (Decl is TDeclaration_Property) then
begin
Decls := FCodeinsight.SymbolTable.Get(TDeclaration_MethodOfType(Decl).ObjectName).GetByClassAndName(Decl.Name, TDeclaration_Property);
Decls := Members.GetByClassAndName(Decl.Name, TDeclaration_Property);
Decls := FilterProperties(Decls, IsIndexing);
end else
// method of type
if (Decl is TDeclaration_MethodOfType) then
begin
Decls := FCodeinsight.SymbolTable.Get(TDeclaration_MethodOfType(Decl).ObjectName).GetByClassAndName(Decl.Name, TDeclaration_MethodOfType);
Decls := Members.GetByClassAndName(Decl.Name, TDeclaration_MethodOfType);
end else
// regular methods
if (Decl is TDeclaration_Method) then
Expand Down

0 comments on commit e8d4265

Please sign in to comment.