Skip to content

Commit

Permalink
Tweak codetools
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Jan 10, 2024
1 parent 65d3234 commit 3bf8229
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
35 changes: 19 additions & 16 deletions Source/codetools/simba.ide_codetools_insight.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface
type
{$SCOPEDENUMS ON}
EParseExpressionFlag = (
WantVarType, // if found decl is a variable, return the variables type
WantVarType, // if found decl is a variable, return the variables type
WantMethodResult // if found decl is a function, return the result type
);
EParseExpressionFlags = set of EParseExpressionFlag;
Expand Down Expand Up @@ -405,14 +405,14 @@ function TCodeinsight.DoPointerDeref(Decl: TDeclaration): TDeclaration;

function TCodeinsight.ParseExpression(Expr: TExpressionItems; Flags: EParseExpressionFlags): TDeclaration;

function FindMember(Decl: TDeclaration; Expr: TExpressionItem): TDeclaration;
function FindMember(Decl: TDeclaration; Expr: TExpressionItem; isLast: Boolean): TDeclaration;
begin
Result := nil;

for Decl in GetMembersOfType(Decl) do
if Decl.IsName(Expr.Text) then
begin
if Expr.IsLastItem then
if isLast and (not Expr.HasSymbols) then // end of the tree, result will change depending on `Flags`
Result := Decl
else
if (Decl is TDeclaration_Var) and (TDeclaration_Var(Decl).VarType <> nil) then
Expand All @@ -428,13 +428,13 @@ function TCodeinsight.ParseExpression(Expr: TExpressionItems; Flags: EParseExpre
function DoSymbols(Decl: TDeclaration; Expr: TExpressionItem): TDeclaration;
begin
Result := Decl;
if Expr.DerefText then
if Expr.Symbols.Deref then
Result := DoPointerDeref(Result);

if (Expr.Dimensions > 0) then
if (Expr.Symbols.DimCount > 0) then
begin
Result := DoArrayIndex(Result, Expr.Dimensions);
if Expr.DerefDimension then
Result := DoArrayIndex(Result, Expr.Symbols.DimCount);
if Expr.Symbols.DerefDim then
Result := DoPointerDeref(Result);
end;
end;
Expand All @@ -448,19 +448,20 @@ function TCodeinsight.ParseExpression(Expr: TExpressionItems; Flags: EParseExpre
Exit;

Decl := GetByName(Expr[0].Text);
if (Length(Expr) = 1) and (Flags * [EParseExpressionFlag.WantMethodResult, EParseExpressionFlag.WantVarType] = []) then
if (Length(Expr) = 1) and (Flags = []) then
begin
Result := Decl;
Exit;
end;

if (Decl is TDeclaration_Method) and (not Expr[0].IsLastItem) then
Decl := EnsureTypeDeclaration(TDeclaration_Method(Decl).ResultType)
else
if (Decl is TDeclaration_Var) then
Decl := EnsureTypeDeclaration(TDeclaration_Var(Decl).VarType)
else
Decl := EnsureTypeDeclaration(Decl);
if (Length(Expr) > 1) or Expr[0].HasSymbols then
if (Decl is TDeclaration_Method) then
Decl := EnsureTypeDeclaration(TDeclaration_Method(Decl).ResultType)
else
if (Decl is TDeclaration_Var) then
Decl := EnsureTypeDeclaration(TDeclaration_Var(Decl).VarType)
else
Decl := EnsureTypeDeclaration(Decl);

if (Decl = nil) then
begin
Expand All @@ -477,12 +478,13 @@ function TCodeinsight.ParseExpression(Expr: TExpressionItems; Flags: EParseExpre

for I := 1 to High(Expr) do
begin
Decl := FindMember(Decl, Expr[I]);
Decl := FindMember(Decl, Expr[I], I=High(Expr));
if (Decl = nil) then
begin
DebugLn('TCodeinsight.ParseExpression: Failed on member "' + Expr[I].Text + '"');
Exit;
end;

Decl := DoSymbols(Decl, Expr[I]);
if (Decl = nil) then
begin
Expand All @@ -492,6 +494,7 @@ function TCodeinsight.ParseExpression(Expr: TExpressionItems; Flags: EParseExpre
end;

Result := Decl;

if (EParseExpressionFlag.WantVarType in Flags) and (Result is TDeclaration_Var) then
Result := EnsureTypeDeclaration(TDeclaration_Var(Result).VarType)
else
Expand Down
29 changes: 15 additions & 14 deletions Source/codetools/simba.ide_codetools_utils.pas
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ interface

type
TExpressionItem = record
IsLastItem: Boolean;

Text: String;
Dimensions: Integer;

DerefDimension: Boolean;
DerefText: Boolean;
HasSymbols: Boolean;
Symbols: record
DimCount: Integer; // [0,0] or [0][0] is `DimCount=2`
Deref: Boolean; // arr^
DerefDim: Boolean; // arr[0]^
end;
end;
TExpressionItems = array of TExpressionItem;

Expand Down Expand Up @@ -67,8 +67,9 @@ function StringToExpression(const Str: String): TExpressionItems;
Lex: TmwPasLex;
InSquare, InRound: Integer;
LastToken: TptTokenKind;
I: Integer;
begin
Result := nil;
Result := [];

InSquare := 0;
InRound := 0;
Expand Down Expand Up @@ -98,14 +99,14 @@ function StringToExpression(const Str: String): TExpressionItems;
tokSquareClose:
begin
if (InSquare > 0) and (InRound = 0) and (Length(Result) > 0) then
Inc(Result[High(Result)].Dimensions);
Inc(Result[High(Result)].Symbols.DimCount);

Dec(InSquare);
end;
tokComma:
begin
if (InSquare > 0) and (InRound = 0) and (Length(Result) > 0) then
Inc(Result[High(Result)].Dimensions);
Inc(Result[High(Result)].Symbols.DimCount);
end;
tokRoundOpen:
begin
Expand All @@ -120,9 +121,9 @@ function StringToExpression(const Str: String): TExpressionItems;
if (InRound = 0) and (InSquare = 0) and (Length(Result) > 0) then
begin
if (LastToken = tokSquareClose) then
Result[High(Result)].DerefDimension := True
Result[High(Result)].Symbols.DerefDim := True
else
Result[High(Result)].DerefText := True;
Result[High(Result)].Symbols.Deref := True;
end;
end;
tokStringConst:
Expand All @@ -138,12 +139,12 @@ function StringToExpression(const Str: String): TExpressionItems;
LastToken := Lex.TokenID;
Lex.NextNoJunk();
end;

for I := 0 to High(Result) do
Result[I].HasSymbols := (Result[I].Symbols.DimCount > 0) or Result[I].Symbols.Deref or Result[I].Symbols.DerefDim;
finally
Lex.Free();
end;

if (Length(Result) > 0) then
Result[High(Result)].IsLastItem := True;
end;

end.
2 changes: 1 addition & 1 deletion Source/editor/simba.editor.pas
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ function TSimbaEditor.GetExpression(X, Y: Integer): String;

else
begin
if (Line[X] in ['A'..'Z', 'a'..'z', '0'..'9', '_', '.']) then
if (Line[X] in ['A'..'Z', 'a'..'z', '0'..'9', '_', '.', '^']) then
// in identifier
else
if (InRound <= 0) and (InSquare <= 0) then
Expand Down

0 comments on commit 3bf8229

Please sign in to comment.